use of org.cpsolver.coursett.model.Lecture in project cpsolver by UniTime.
the class RoomSwap method selectNeighbour.
@Override
public Neighbour<Lecture, Placement> selectNeighbour(Solution<Lecture, Placement> solution) {
TimetableModel model = (TimetableModel) solution.getModel();
Assignment<Lecture, Placement> assignment = solution.getAssignment();
double total = model.getTotalValue(assignment);
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 || old.getNrRooms() != 1)
continue;
List<RoomLocation> values = lecture.roomLocations();
if (values.isEmpty())
continue;
Lock lock = solution.getLock().writeLock();
lock.lock();
try {
int attempts = 0;
int valIdx = ToolBox.random(values.size());
long startTime = JProf.currentTimeMillis();
for (int j = 0; j < values.size(); j++) {
RoomLocation room = values.get((j + valIdx) % values.size());
if (room.getPreference() > 50)
continue;
if (room.equals(old.getRoomLocation()))
continue;
Placement placement = new Placement(lecture, old.getTimeLocation(), room);
if (!placement.isValid())
continue;
Set<Placement> conflicts = model.conflictValues(assignment, placement);
if (conflicts.contains(placement))
continue;
if (conflicts.isEmpty()) {
SimpleNeighbour<Lecture, Placement> n = new SimpleNeighbour<Lecture, Placement>(lecture, placement);
if (!iHC || n.value(assignment) <= 0)
return n;
else
continue;
}
Map<Lecture, Placement> assignments = new HashMap<Lecture, Placement>();
assignments.put(lecture, placement);
for (Placement conflict : conflicts) assignment.unassign(solution.getIteration(), conflict.variable());
assignment.assign(solution.getIteration(), placement);
Double v = resolve(solution, total, startTime, assignments, new ArrayList<Placement>(conflicts), 0);
if (!conflicts.isEmpty())
attempts++;
assignment.unassign(solution.getIteration(), lecture);
for (Placement conflict : conflicts) assignment.assign(solution.getIteration(), conflict);
assignment.assign(solution.getIteration(), old);
if (v != null)
return new SwapNeighbour(assignments.values(), v);
if (attempts >= iMaxAttempts)
break;
}
} finally {
lock.unlock();
}
}
return null;
}
use of org.cpsolver.coursett.model.Lecture in project cpsolver by UniTime.
the class SctModel method unassign.
/**
* Unassign all previous enrollments into the given offering
*/
public void unassign() {
for (SctStudent student : iStudents) {
Configuration configuration = null;
for (Lecture lecture : student.getCurrentEnrollment(false).getLectures()) {
if (configuration == null)
configuration = lecture.getConfiguration();
for (Lecture other : student.getStudent().getLectures()) decJenrl(getAssignment(), student.getStudent(), lecture, other);
lecture.removeStudent(getAssignment(), student.getStudent());
student.getStudent().removeLecture(lecture);
}
if (configuration != null)
student.getStudent().removeConfiguration(configuration);
}
}
use of org.cpsolver.coursett.model.Lecture in project cpsolver by UniTime.
the class SctSectioning method switchStudents.
@Override
public void switchStudents(Solution<Lecture, Placement> solution, TerminationCondition<Lecture, Placement> termination) {
getProgress().setStatus("Student Sectioning...");
getProgress().info("Student Conflicts: " + sDF2.format(value(solution)) + " (group: " + sDF2.format(StudentSwapSectioning.gp(solution)) + "%)");
for (int i = 1; i <= iNrRounds; i++) {
getProgress().setPhase("Swapping students [" + i + "]...", iModel.variables().size());
Set<Long> offeringIds = new HashSet<Long>();
for (Lecture lecture : iModel.variables()) {
getProgress().incProgress();
if (lecture.students().isEmpty() || lecture.isSingleSection())
continue;
if (termination != null && !termination.canContinue(solution))
return;
if (offeringIds.add(lecture.getConfiguration().getOfferingId())) {
SctModel model = new SctModel(iModel, solution.getAssignment());
model.setConfiguration(lecture.getConfiguration());
SctSolution s1 = model.currentSolution();
SctSolution s2 = model.computeSolution();
if (model.isTimeOutReached())
getProgress().info("Timeout reached for " + lecture.getName());
if (s2.isBetter(s1)) {
model.unassign();
model.assign(s2);
getProgress().info("Student Conflicts: " + sDF2.format(value(solution)) + " (group: " + sDF2.format(StudentSwapSectioning.gp(solution)) + "%)");
}
}
}
getProgress().info("Student Conflicts: " + sDF2.format(value(solution)) + " (group: " + sDF2.format(StudentSwapSectioning.gp(solution)) + "%)");
}
}
use of org.cpsolver.coursett.model.Lecture in project cpsolver by UniTime.
the class TimeSwap method selectNeighbour.
@Override
public Neighbour<Lecture, Placement> selectNeighbour(Solution<Lecture, Placement> solution) {
TimetableModel model = (TimetableModel) solution.getModel();
Assignment<Lecture, Placement> assignment = solution.getAssignment();
double total = model.getTotalValue(assignment);
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 = lecture.getAssignment(assignment);
if (old == null)
continue;
List<TimeLocation> values = lecture.timeLocations();
if (values.isEmpty())
continue;
Lock lock = solution.getLock().writeLock();
lock.lock();
try {
int attempts = 0;
long startTime = JProf.currentTimeMillis();
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;
if (time.equals(old.getTimeLocation()))
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())
continue;
Set<Placement> conflicts = model.conflictValues(assignment, placement);
if (conflicts.contains(placement))
continue;
if (conflicts.isEmpty()) {
SimpleNeighbour<Lecture, Placement> n = new SimpleNeighbour<Lecture, Placement>(lecture, placement);
if (!iHC || n.value(assignment) <= 0)
return n;
else
continue;
}
Map<Lecture, Placement> assignments = new HashMap<Lecture, Placement>();
assignments.put(lecture, placement);
for (Placement conflict : conflicts) assignment.unassign(solution.getIteration(), conflict.variable());
assignment.assign(solution.getIteration(), placement);
Double v = resolve(solution, total, startTime, assignments, new ArrayList<Placement>(conflicts), 0);
if (!conflicts.isEmpty())
attempts++;
assignment.unassign(solution.getIteration(), lecture);
for (Placement conflict : conflicts) assignment.assign(solution.getIteration(), conflict);
assignment.assign(solution.getIteration(), old);
if (v != null)
return new SwapNeighbour(assignments.values(), v);
if (attempts >= iMaxAttempts)
break;
}
} finally {
lock.unlock();
}
}
return null;
}
use of org.cpsolver.coursett.model.Lecture in project cpsolver by UniTime.
the class StudentSwapGenerator method generateSwap.
public Neighbour<Lecture, Placement> generateSwap(TimetableModel model, Assignment<Lecture, Placement> assignment, Student student, Configuration config) {
if (student == null || config == null)
return null;
if (!config.getAltConfigurations().isEmpty()) {
int idx = ToolBox.random(config.getAltConfigurations().size() + 2);
if (idx > 1)
config = config.getAltConfigurations().get(idx - 2);
}
Long subpartId = ToolBox.random(config.getTopSubpartIds());
int nrAttempts = ToolBox.random(10);
for (int i = 0; i < nrAttempts; i++) {
Lecture lecture = ToolBox.random(config.getTopLectures(subpartId));
Student other = ToolBox.random(lecture.students());
if (other != null && !student.equals(other)) {
StudentSwap swap = new StudentSwap(model, assignment, student, other, config.getOfferingId());
if (swap.isAllowed())
return swap;
}
}
List<Lecture> lectures = new ArrayList<Lecture>();
Queue<Collection<Lecture>> queue = new LinkedList<Collection<Lecture>>(config.getTopLectures().values());
while (!queue.isEmpty()) {
List<Lecture> adepts = new ArrayList<Lecture>();
for (Lecture adept : queue.poll()) if (student.canEnroll(adept))
adepts.add(adept);
if (adepts.isEmpty())
return null;
Lecture lect = ToolBox.random(adepts);
lectures.add(lect);
if (lect.hasAnyChildren())
queue.addAll(lect.getChildren().values());
}
StudentSwap swap = new StudentSwap(model, assignment, student, lectures);
return (swap.isAllowed() ? swap : null);
}
Aggregations