001package org.cpsolver.studentsct.heuristics.selection; 002 003import java.text.DecimalFormat; 004import java.util.ArrayList; 005import java.util.Collection; 006import java.util.Collections; 007import java.util.Comparator; 008import java.util.ConcurrentModificationException; 009import java.util.HashMap; 010import java.util.Iterator; 011import java.util.LinkedList; 012import java.util.List; 013import java.util.ListIterator; 014import java.util.Map; 015import java.util.concurrent.locks.Lock; 016 017import org.cpsolver.ifs.assignment.Assignment; 018import org.cpsolver.ifs.heuristics.BacktrackNeighbourSelection; 019import org.cpsolver.ifs.heuristics.NeighbourSelection; 020import org.cpsolver.ifs.model.InfoProvider; 021import org.cpsolver.ifs.model.Neighbour; 022import org.cpsolver.ifs.solution.Solution; 023import org.cpsolver.ifs.solver.Solver; 024import org.cpsolver.ifs.solver.SolverListener; 025import org.cpsolver.ifs.util.DataProperties; 026import org.cpsolver.ifs.util.Progress; 027import org.cpsolver.studentsct.StudentSectioningModel; 028import org.cpsolver.studentsct.heuristics.selection.BacktrackSelection.RequestComparator; 029import org.cpsolver.studentsct.model.CourseRequest; 030import org.cpsolver.studentsct.model.Enrollment; 031import org.cpsolver.studentsct.model.Request; 032import org.cpsolver.studentsct.model.Request.RequestPriority; 033import org.cpsolver.studentsct.model.Student.StudentPriority; 034 035/** 036 * Swap enrollments of different students of a course. This is to improve 037 * the enrollment alternativity {@link Enrollment#getPriority()} as well as 038 * selection preferences {@link Enrollment#percentSelected()}. 039 * 040 * <br> 041 * <br> 042 * 043 * @version StudentSct 1.3 (Student Sectioning)<br> 044 * Copyright (C) 2007 - 2018 Tomáš Müller<br> 045 * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br> 046 * <a href="http://muller.unitime.org">http://muller.unitime.org</a><br> 047 * <br> 048 * This library is free software; you can redistribute it and/or modify 049 * it under the terms of the GNU Lesser General Public License as 050 * published by the Free Software Foundation; either version 3 of the 051 * License, or (at your option) any later version. <br> 052 * <br> 053 * This library is distributed in the hope that it will be useful, but 054 * WITHOUT ANY WARRANTY; without even the implied warranty of 055 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 056 * Lesser General Public License for more details. <br> 057 * <br> 058 * You should have received a copy of the GNU Lesser General Public 059 * License along with this library; if not see 060 * <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>. 061 */ 062 063public class StudentEnrollmentSwapSelection implements NeighbourSelection<Request, Enrollment>, InfoProvider<Request, Enrollment>, SolverListener<Request, Enrollment> { 064 private static DecimalFormat sDF = new DecimalFormat("0.00"); 065 private Selection iSelection = null; 066 protected LinkedList<Request> iRequests = null; 067 068 protected long iNbrIterations = 0; 069 protected long iTotalTime = 0; 070 protected long iNbrTimeoutReached = 0; 071 protected long iNbrNoSolution = 0; 072 protected RequestComparator iRequestComparator; 073 074 public StudentEnrollmentSwapSelection(DataProperties properties) { 075 iRequestComparator = new RequestComparator(properties); 076 } 077 078 public void init(Solver<Request, Enrollment> solver, String name) { 079 List<Request> variables = new ArrayList<Request>(solver.currentSolution().getModel().assignedVariables(solver.currentSolution().getAssignment())); 080 Collections.shuffle(variables); 081 Collections.sort(variables, iRequestComparator); 082 iRequests = new LinkedList<Request>(variables); 083 if (iSelection == null) { 084 try { 085 iSelection = new Selection(solver.getProperties()); 086 iSelection.init(solver); 087 } catch (Exception e) { 088 throw new RuntimeException(e.getMessage(), e); 089 } 090 } 091 Progress.getInstance(solver.currentSolution().getModel()).setPhase(name, variables.size()); 092 iNbrIterations = 0; 093 iNbrTimeoutReached = 0; 094 iNbrNoSolution = 0; 095 iTotalTime = 0; 096 } 097 098 @Override 099 public void init(Solver<Request, Enrollment> solver) { 100 init(solver, "Enrollment swaps..."); 101 } 102 103 protected synchronized Request nextRequest() { 104 return iRequests.poll(); 105 } 106 107 public synchronized void addRequest(Request request) { 108 if (iRequests != null && request != null && !request.getStudent().isDummy()) { 109 if (request.getStudent().getPriority().ordinal() < StudentPriority.Normal.ordinal() || request.getRequestPriority().ordinal() < RequestPriority.Normal.ordinal()) { 110 for (ListIterator<Request> i = iRequests.listIterator(); i.hasNext();) { 111 Request r = i.next(); 112 if (iRequestComparator.compare(r, request) > 0) { 113 i.previous(); // go one back 114 i.add(request); 115 return; 116 } 117 } 118 } 119 iRequests.add(request); 120 } 121 } 122 123 @Override 124 public Neighbour<Request, Enrollment> selectNeighbour(Solution<Request, Enrollment> solution) { 125 Request request = null; 126 while ((request = nextRequest()) != null) { 127 Progress p = Progress.getInstance(solution.getModel()); 128 p.incProgress(); 129 if (p.getProgress() > 2.0 * p.getProgressMax()) return null; 130 if (request instanceof CourseRequest) { 131 try { 132 Enrollment e = request.getAssignment(solution.getAssignment()); 133 if (e == null || e.getPriority() > 0 || !((CourseRequest)request).getSelectedChoices().isEmpty()) { 134 Neighbour<Request, Enrollment> n = iSelection.selectNeighbour(solution, request); 135 if (iSelection.getContext() != null) { 136 iNbrIterations ++; 137 iTotalTime += iSelection.getContext().getTime(); 138 if (iSelection.getContext().isTimeoutReached()) iNbrTimeoutReached ++; 139 if (n == null) iNbrNoSolution ++; 140 } 141 if (n != null && n.value(solution.getAssignment()) <= 0.0) 142 return n; 143 } 144 } catch (ConcurrentModificationException e) { 145 addRequest(request); 146 } 147 } 148 } 149 return null; 150 } 151 152 class Selection extends BacktrackNeighbourSelection<Request, Enrollment> { 153 private int iMaxValues = 1000; 154 155 Selection(DataProperties properties) throws Exception { 156 super(properties); 157 setTimeout(properties.getPropertyInt("Neighbour.EnrollmentSwapTimeout", 500)); 158 iMaxValues = properties.getPropertyInt("Neighbour.EnrollmentSwapMaxValues", iMaxValues); 159 } 160 161 @Override 162 protected Iterator<Enrollment> values(BacktrackNeighbourSelection<Request, Enrollment>.BacktrackNeighbourSelectionContext context, Request variable) { 163 if (variable instanceof CourseRequest) { 164 final CourseRequest request = (CourseRequest)variable; 165 final StudentSectioningModel model = (StudentSectioningModel)context.getModel(); 166 final Assignment<Request, Enrollment> assignment = context.getAssignment(); 167 List<Enrollment> values = null; 168 Enrollment current = request.getAssignment(context.getAssignment()); 169 if (!request.getSelectedChoices().isEmpty() && current != null && current.getPriority() == 0) { 170 values = request.getSelectedEnrollments(assignment, false); 171 } else { 172 values = (iMaxValues > 0 ? request.computeRandomEnrollments(assignment, iMaxValues) : request.computeEnrollments(assignment)); 173 } 174 Collections.sort(values, new Comparator<Enrollment>() { 175 private HashMap<Enrollment, Double> iValues = new HashMap<Enrollment, Double>(); 176 private Double value(Enrollment e) { 177 Double value = iValues.get(e); 178 if (value == null) { 179 if (model.getStudentQuality() != null) 180 value = model.getStudentWeights().getWeight(assignment, e, model.getStudentQuality().conflicts(e)); 181 else 182 value = model.getStudentWeights().getWeight(assignment, e, 183 (model.getDistanceConflict() == null ? null : model.getDistanceConflict().conflicts(e)), 184 (model.getTimeOverlaps() == null ? null : model.getTimeOverlaps().conflicts(e))); 185 iValues.put(e, value); 186 } 187 return value; 188 } 189 @Override 190 public int compare(Enrollment e1, Enrollment e2) { 191 if (e1.equals(assignment.getValue(request))) return -1; 192 if (e2.equals(assignment.getValue(request))) return 1; 193 Double v1 = value(e1), v2 = value(e2); 194 return v1.equals(v2) ? e1.compareTo(assignment, e2) : v2.compareTo(v1); 195 } 196 }); 197 return values.iterator(); 198 } else { 199 return variable.computeEnrollments(context.getAssignment()).iterator(); 200 } 201 } 202 203 @Override 204 protected void selectNeighbour(Solution<Request, Enrollment> solution, Request variable, BacktrackNeighbourSelectionContext context) { 205 iContext = context; 206 Lock lock = solution.getLock().writeLock(); 207 lock.lock(); 208 try { 209 exploreEnrollmentSwaps(context, variable); 210 } finally { 211 lock.unlock(); 212 } 213 } 214 215 protected void exploreEnrollmentSwaps(BacktrackNeighbourSelectionContext context, Request variable) { 216 Enrollment current = context.getAssignment().getValue(variable); 217 double currentValue = (current == null ? 0.0 : current.toDouble(context.getAssignment())); 218 for (Iterator<Enrollment> e = values(context, variable); canContinueEvaluation(context) && e.hasNext();) { 219 Enrollment value = e.next(); 220 if (value.equals(current)) continue; 221 if (current != null && currentValue <= value.toDouble(context.getAssignment())) continue; 222 if (context.isTimeoutReached() || context.isMaxItersReached()) break; 223 context.incIteration(); 224 if (context.getModel().inConflict(context.getAssignment(), value)) { 225 for (Enrollment other: new ArrayList<Enrollment>(value.getCourse().getContext(context.getAssignment()).getEnrollments())) { 226 if (other.getStudent().equals(value.getStudent()) || !other.getSections().equals(value.getSections())) continue; 227 context.getAssignment().unassign(0, other.variable()); 228 if (!context.getModel().inConflict(context.getAssignment(), value)) { 229 if (current != null) 230 context.getAssignment().unassign(0, current.variable()); 231 context.getAssignment().assign(0, value); 232 for (Iterator<Enrollment> f = values(context, other.variable()); canContinueEvaluation(context) && f.hasNext();) { 233 Enrollment fix = f.next(); 234 if (!context.getModel().inConflict(context.getAssignment(), fix)) { 235 context.getAssignment().assign(0, fix); 236 context.saveBest(variable, other.variable()); 237 context.getAssignment().unassign(0, fix.variable()); 238 } 239 } 240 if (current == null) 241 context.getAssignment().unassign(0, variable); 242 else 243 context.getAssignment().assign(0, current); 244 } 245 context.getAssignment().assign(0, other); 246 } 247 } else { 248 if (current != null) 249 context.getAssignment().unassign(0, current.variable()); 250 context.getAssignment().assign(0, value); 251 context.saveBest(variable); 252 if (current == null) 253 context.getAssignment().unassign(0, variable); 254 else 255 context.getAssignment().assign(0, current); 256 } 257 } 258 } 259 } 260 261 @Override 262 public void getInfo(Assignment<Request, Enrollment> assignment, Map<String, String> info) { 263 if (iNbrIterations > 0) 264 info.put("Timing of " + getClass().getSimpleName(), sDF.format(((double)iTotalTime) / iNbrIterations) + " ms/it (" + 265 iNbrIterations + " iterations, " + 266 (iNbrNoSolution == 0 ? "" : sDF.format(100.0 * iNbrNoSolution / iNbrIterations) + "% no solution, ") + 267 sDF.format(100.0 * iNbrTimeoutReached / iNbrIterations) + "% time limit of " + sDF.format(iSelection.getTimeout() / 1000.0) + " seconds reached)"); 268 } 269 270 @Override 271 public void getInfo(Assignment<Request, Enrollment> assignment, Map<String, String> info, Collection<Request> variables) { 272 } 273 274 @Override 275 public boolean variableSelected(Assignment<Request, Enrollment> assignment, long iteration, Request variable) { 276 return false; 277 } 278 @Override 279 public boolean valueSelected(Assignment<Request, Enrollment> assignment, long iteration, Request variable, Enrollment value) { 280 return false; 281 } 282 @Override 283 public boolean neighbourSelected(Assignment<Request, Enrollment> assignment, long iteration, Neighbour<Request, Enrollment> neighbour) { 284 return false; 285 } 286 @Override 287 public void neighbourFailed(Assignment<Request, Enrollment> assignment, long iteration, Neighbour<Request, Enrollment> neighbour) { 288 if (neighbour instanceof BacktrackNeighbourSelection.BackTrackNeighbour) 289 addRequest(((BacktrackNeighbourSelection<Request, Enrollment>.BackTrackNeighbour)neighbour).getAssignments().get(0).getRequest()); 290 } 291}