use of org.cpsolver.coursett.model.Lecture in project cpsolver by UniTime.
the class StudentSwap method assign.
@Override
public void assign(Assignment<Lecture, Placement> assignment, long iteration) {
for (Lecture l1 : iFirstLectures) {
for (Lecture l2 : iFirstStudent.getLectures()) {
if (l1.equals(l2))
continue;
if (iFirstLectures.contains(l2) && l1.getClassId().compareTo(l2.getClassId()) >= 0)
continue;
decJenrl(assignment, iFirstStudent, l1, l2);
}
}
if (iSecondStudent != null) {
for (Lecture l1 : iSecondLectures) {
for (Lecture l2 : iSecondStudent.getLectures()) {
if (l1.equals(l2))
continue;
if (iSecondLectures.contains(l2) && l1.getClassId().compareTo(l2.getClassId()) >= 0)
continue;
decJenrl(assignment, iSecondStudent, l1, l2);
}
}
}
for (Lecture l1 : iFirstLectures) {
l1.removeStudent(assignment, iFirstStudent);
iFirstStudent.removeLecture(l1);
if (iSecondStudent != null) {
l1.addStudent(assignment, iSecondStudent);
iSecondStudent.addLecture(l1);
}
}
for (Lecture l1 : iSecondLectures) {
if (iSecondStudent != null) {
l1.removeStudent(assignment, iSecondStudent);
iSecondStudent.removeLecture(l1);
}
l1.addStudent(assignment, iFirstStudent);
iFirstStudent.addLecture(l1);
}
if (iSecondStudent != null) {
for (Lecture l1 : iFirstLectures) {
for (Lecture l2 : iSecondStudent.getLectures()) {
if (l1.equals(l2))
continue;
if (iFirstLectures.contains(l2) && l1.getClassId().compareTo(l2.getClassId()) >= 0)
continue;
incJenrl(assignment, iSecondStudent, l1, l2);
}
}
}
for (Lecture l1 : iSecondLectures) {
for (Lecture l2 : iFirstStudent.getLectures()) {
if (l1.equals(l2))
continue;
if (iSecondLectures.contains(l2) && l1.getClassId().compareTo(l2.getClassId()) >= 0)
continue;
incJenrl(assignment, iFirstStudent, l1, l2);
}
}
if (!iFirstConfig.equals(iSecondConfig)) {
iFirstStudent.removeConfiguration(iFirstConfig);
iFirstStudent.addConfiguration(iSecondConfig);
if (iSecondStudent != null) {
iSecondStudent.removeConfiguration(iSecondConfig);
iSecondStudent.addConfiguration(iFirstConfig);
}
}
}
use of org.cpsolver.coursett.model.Lecture in project cpsolver by UniTime.
the class StudentSwap method value.
@Override
public double value(List<StudentConflict> criteria, Assignment<Lecture, Placement> assignment) {
double delta = 0;
for (Lecture l1 : iFirstLectures) {
Placement p1 = assignment.getValue(l1);
if (p1 == null)
continue;
for (Lecture l2 : iFirstStudent.getLectures()) {
Placement p2 = assignment.getValue(l2);
if (l1.equals(l2) || p2 == null)
continue;
if (iFirstLectures.contains(l2) && l1.getClassId().compareTo(l2.getClassId()) >= 0)
continue;
delta -= getJenrConflictWeight(criteria, iFirstStudent, p1, p2);
}
if (iSecondStudent != null) {
for (Lecture l2 : iSecondStudent.getLectures()) {
if (iSecondLectures.contains(l2))
continue;
Placement p2 = assignment.getValue(l2);
if (l1.equals(l2) || p2 == null)
continue;
delta += getJenrConflictWeight(criteria, iSecondStudent, p1, p2);
}
for (Lecture l2 : iFirstLectures) {
Placement p2 = assignment.getValue(l2);
if (l1.equals(l2) || p2 == null)
continue;
if (l1.getClassId().compareTo(l2.getClassId()) >= 0)
continue;
delta += getJenrConflictWeight(criteria, iSecondStudent, p1, p2);
}
}
}
for (Lecture l1 : iSecondLectures) {
Placement p1 = assignment.getValue(l1);
if (p1 == null)
continue;
if (iSecondStudent != null) {
for (Lecture l2 : iSecondStudent.getLectures()) {
Placement p2 = assignment.getValue(l2);
if (l1.equals(l2) || p2 == null)
continue;
if (iSecondLectures.contains(l2) && l1.getClassId().compareTo(l2.getClassId()) >= 0)
continue;
delta -= getJenrConflictWeight(criteria, iSecondStudent, p1, p2);
}
}
for (Lecture l2 : iFirstStudent.getLectures()) {
if (iFirstLectures.contains(l2))
continue;
Placement p2 = assignment.getValue(l2);
if (l1.equals(l2) || p2 == null)
continue;
delta += getJenrConflictWeight(criteria, iFirstStudent, p1, p2);
}
for (Lecture l2 : iSecondLectures) {
Placement p2 = assignment.getValue(l2);
if (l1.equals(l2) || p2 == null)
continue;
if (l1.getClassId().compareTo(l2.getClassId()) >= 0)
continue;
delta += getJenrConflictWeight(criteria, iFirstStudent, p1, p2);
}
}
return delta;
}
use of org.cpsolver.coursett.model.Lecture in project cpsolver by UniTime.
the class TimeChange method selectNeighbour.
@Override
public Neighbour<Lecture, Placement> selectNeighbour(Solution<Lecture, Placement> solution) {
TimetableModel model = (TimetableModel) solution.getModel();
Assignment<Lecture, Placement> assignment = solution.getAssignment();
int varIdx = ToolBox.random(model.variables().size());
for (int i = 0; i < model.variables().size(); i++) {
Lecture lecture = model.variables().get((i + varIdx) % model.variables().size());
Placement old = assignment.getValue(lecture);
if (old == null)
continue;
List<TimeLocation> values = lecture.timeLocations();
if (values.isEmpty())
continue;
int valIdx = ToolBox.random(values.size());
for (int j = 0; j < values.size(); j++) {
TimeLocation time = values.get((j + valIdx) % values.size());
if (time.getPreference() > 50)
continue;
Placement placement = null;
if (lecture.getNrRooms() == 0)
placement = new Placement(lecture, time, (RoomLocation) null);
else if (lecture.getNrRooms() == 1)
placement = new Placement(lecture, time, old.getRoomLocation());
else
placement = new Placement(lecture, time, old.getRoomLocations());
if (placement.isValid() && !model.inConflict(assignment, placement)) {
SimpleNeighbour<Lecture, Placement> n = new SimpleNeighbour<Lecture, Placement>(lecture, placement);
if (!iHC || n.value(assignment) <= 0)
return n;
}
}
}
return null;
}
use of org.cpsolver.coursett.model.Lecture in project cpsolver by UniTime.
the class NeighbourSelectionWithSuggestions method selectNeighbourWithSuggestions.
public Neighbour<Lecture, Placement> selectNeighbourWithSuggestions(Solution<Lecture, Placement> solution, Lecture lecture, int depth) {
if (lecture == null)
return null;
NeighbourSelectionWithSuggestionsContext context = new NeighbourSelectionWithSuggestionsContext(solution);
Lock lock = solution.getLock().writeLock();
lock.lock();
try {
// System.out.println("BEFORE BT ("+lecture.getName()+"): nrAssigned="+iSolution.getModel().assignedVariables().size()+", value="+iCmp.currentValue(iSolution));
List<Lecture> initialLectures = new ArrayList<Lecture>(1);
initialLectures.add(lecture);
backtrack(context, initialLectures, new HashMap<Lecture, Placement>(), new HashMap<Lecture, Placement>(), depth);
// System.out.println("AFTER BT ("+lecture.getName()+"): nrAssigned="+iSolution.getModel().assignedVariables().size()+", value="+iCmp.currentValue(iSolution));
} finally {
lock.unlock();
}
return context.getSuggestionNeighbour();
}
use of org.cpsolver.coursett.model.Lecture in project cpsolver by UniTime.
the class PlacementSelection method selectValue.
@Override
public Placement selectValue(Solution<Lecture, Placement> solution, Lecture var) {
if (var == null)
return null;
Lecture selectedVariable = var;
TimetableModel model = (TimetableModel) solution.getModel();
Assignment<Lecture, Placement> assignment = solution.getAssignment();
if (selectedVariable.getInitialAssignment() != null) {
if (iMPPLimit >= 0 && model.perturbVariables(assignment).size() >= iMPPLimit) {
if (!containsItselfSingletonOrCommited(model, model.conflictValues(assignment, selectedVariable.getInitialAssignment()), selectedVariable.getInitialAssignment()))
return selectedVariable.getInitialAssignment();
} else if (iMPPPenaltyLimit >= 0.0 && solution.getPerturbationsCounter() != null && solution.getPerturbationsCounter().getPerturbationPenalty(assignment, model) > iMPPPenaltyLimit) {
if (!containsItselfSingletonOrCommited(model, model.conflictValues(assignment, selectedVariable.getInitialAssignment()), selectedVariable.getInitialAssignment()))
return selectedVariable.getInitialAssignment();
} else if (selectedVariable.getInitialAssignment() != null && ToolBox.random() <= iInitialSelectionProb) {
if (!containsItselfSingletonOrCommited(model, model.conflictValues(assignment, selectedVariable.getInitialAssignment()), selectedVariable.getInitialAssignment()))
return selectedVariable.getInitialAssignment();
}
}
List<Placement> values = selectedVariable.values(solution.getAssignment());
if (iRW && ToolBox.random() <= iRandomWalkProb) {
for (int i = 0; i < 5; i++) {
Placement ret = ToolBox.random(values);
if (!containsItselfSingletonOrCommited(model, model.conflictValues(assignment, ret), ret))
return ret;
}
}
Placement current = assignment.getValue(selectedVariable);
if (iProp != null && current == null && ToolBox.random() <= iGoodSelectionProb) {
Collection<Placement> goodValues = iProp.goodValues(assignment, selectedVariable);
if (!goodValues.isEmpty())
values = new ArrayList<Placement>(goodValues);
}
if (values.size() == 1) {
Placement ret = values.get(0);
if (!containsItselfSingletonOrCommited(model, model.conflictValues(assignment, ret), ret))
return ret;
}
long[] bestCost = new long[NR_LEVELS];
List<Placement> selectionValues = null;
HeuristicSelector<Placement> selector = (iUseThreshold ? new HeuristicSelector<Placement>(iThresholdKoef) : null);
for (Placement value : values) {
if (iTabu != null && iTabu.contains(value))
continue;
if (current != null && current.equals(value))
continue;
Set<Placement> conflicts = value.variable().getModel().conflictValues(assignment, value);
if (containsItselfSingletonOrCommited(model, conflicts, value))
continue;
if (iUseThreshold) {
Double flt = selector.firstLevelThreshold();
double[] costs = new double[NR_LEVELS];
for (int level = 0; level < NR_LEVELS; level++) {
costs[level] = getCost(assignment, level, value, conflicts);
if (level == 0 && flt != null && costs[0] > flt.doubleValue()) {
break;
}
}
if (flt != null && costs[0] > flt.doubleValue())
continue;
selector.add(costs, value);
} else {
boolean fail = false;
boolean best = false;
for (int level = 0; !fail && level < 1; level++) {
double val = getCost(assignment, level, value, conflicts);
long cost = Math.round(PRECISION * val);
if (selectionValues != null && !best) {
if (cost > bestCost[level]) {
fail = true;
}
if (cost < bestCost[level]) {
bestCost[level] = cost;
selectionValues.clear();
best = true;
}
} else {
bestCost[level] = cost;
}
}
if (selectionValues == null)
selectionValues = new ArrayList<Placement>(values.size());
if (!fail)
selectionValues.add(value);
}
}
// ToolBox.print("Best "+selectionValues.size()+" locations for variable "+selectedVariable.getId()+" have "+bestConflicts+" conflicts ("+bestRemovals+" weighted) and "+bestStudentConflicts+" ("+bestOriginalStudentConflicts+" * "+bestKoef+" + "+bestPenalty+") preference.");
Placement selectedValue = null;
if (iUseThreshold) {
List<HeuristicSelector<Placement>.Element<Placement>> selectionElements = selector.selection();
if (selectedVariable.getInitialAssignment() != null) {
for (HeuristicSelector<Placement>.Element<Placement> element : selectionElements) {
Placement value = element.getObject();
if (value.equals(selectedVariable.getInitialAssignment())) {
selectedValue = value;
break;
}
}
// &&
// selectionValues.contains(selectedVariable.getInitialAssignment()))
// return selectedVariable.getInitialAssignment();
}
if (selectedValue == null) {
HeuristicSelector<Placement>.Element<Placement> selection = ToolBox.random(selectionElements);
selectedValue = (selection == null ? null : selection.getObject());
}
} else {
if (selectedVariable.getInitialAssignment() != null && selectionValues.contains(selectedVariable.getInitialAssignment()))
return selectedVariable.getInitialAssignment();
selectedValue = ToolBox.random(selectionValues);
}
if (selectedValue != null && iTabu != null) {
if (iTabu.size() == iTabuPos)
iTabu.add(selectedValue);
else
iTabu.set(iTabuPos, selectedValue);
iTabuPos = (iTabuPos + 1) % iTabuSize;
}
return selectedValue;
}
Aggregations