use of org.cpsolver.ifs.model.Neighbour in project cpsolver by UniTime.
the class ExamColoringConstruction method selectNeighbour.
@Override
public Neighbour<Exam, ExamPlacement> selectNeighbour(final Solution<Exam, ExamPlacement> solution) {
ExamModel model = (ExamModel) solution.getModel();
// if (!model.assignedVariables().isEmpty()) return null;
final HashMap<Exam, Vertex> vertices = new HashMap<Exam, Vertex>();
for (Exam x : model.variables()) {
vertices.put(x, new Vertex(x));
}
for (ExamStudent s : model.getStudents()) {
for (Exam x : s.variables()) {
for (Exam y : s.variables()) {
if (!x.equals(y)) {
vertices.get(x).neighbors().add(vertices.get(y));
vertices.get(y).neighbors().add(vertices.get(x));
}
}
}
}
for (ExamInstructor i : model.getInstructors()) {
for (Exam x : i.variables()) {
for (Exam y : i.variables()) {
if (!x.equals(y)) {
vertices.get(x).neighbors().add(vertices.get(y));
vertices.get(y).neighbors().add(vertices.get(x));
}
}
}
}
iProgress.setPhase("Graph coloring-based construction", vertices.size());
iProgress.info("Looking for a conflict-free assignment using " + model.getPeriods().size() + " periods.");
iT0 = JProf.currentTimeSec();
iTimeLimitReached = false;
if (iMode.isGreedy()) {
iProgress.info("Using greedy heuristics only (no backtracking)...");
} else if (backTrack(solution, 0, (iMode.isRedundant() ? null : new HashSet<Integer>()), vertices.values())) {
iProgress.info("Success!");
} else if (iTimeLimitReached) {
iProgress.info("There was no conflict-free schedule found during the given time.");
} else if (iSolver.isStop()) {
iProgress.info("Solver was stopped.");
} else {
if (iMode.isRedundant())
iProgress.info("There is no conflict-free schedule!");
else
iProgress.info("Conflict-free schedule not found.");
}
if (iMode.isConstraintCheck())
solution.restoreBest();
HashSet<Vertex> remaning = new HashSet<Vertex>();
for (Vertex v : vertices.values()) if (v.color() < 0)
remaning.add(v);
remaining: while (!remaning.isEmpty()) {
Vertex vertex = null;
for (Vertex v : remaning) if (vertex == null || v.compareTo(vertex) < 0)
vertex = v;
remaning.remove(vertex);
for (int color : vertex.domain()) if (vertex.colorize(solution.getAssignment(), color))
continue remaining;
}
if (!iMode.isConstraintCheck()) {
return new Neighbour<Exam, ExamPlacement>() {
@Override
public void assign(Assignment<Exam, ExamPlacement> assignment, long iteration) {
iProgress.info("Using graph coloring solution ...");
for (Vertex vertex : new TreeSet<Vertex>(vertices.values())) {
ExamPeriodPlacement period = vertex.period();
if (period == null || !vertex.exam().checkDistributionConstraints(assignment, period))
continue;
Set<ExamRoomPlacement> rooms = findRooms(assignment, vertex.exam(), period);
if (rooms == null)
continue;
assignment.assign(iteration, new ExamPlacement(vertex.exam(), period, rooms));
}
HashSet<Vertex> unassigned = new HashSet<Vertex>();
for (Vertex vertex : vertices.values()) {
if (assignment.getValue(vertex.exam()) == null) {
unassigned.add(vertex);
vertex.colorize(assignment, -1);
}
}
solution.saveBest();
iProgress.info("Extending ...");
unassigned: while (!unassigned.isEmpty()) {
Vertex vertex = null;
for (Vertex v : unassigned) if (vertex == null || v.compareTo(vertex) < 0)
vertex = v;
unassigned.remove(vertex);
for (int color : vertex.domain()) {
if (!vertex.colorize(assignment, color))
continue;
ExamPeriodPlacement period = vertex.period(color);
if (period == null || !vertex.exam().checkDistributionConstraints(assignment, period))
continue;
Set<ExamRoomPlacement> rooms = findRooms(assignment, vertex.exam(), period);
if (rooms == null)
continue;
assignment.assign(iteration, new ExamPlacement(vertex.exam(), period, rooms));
continue unassigned;
}
vertex.colorize(assignment, -1);
}
solution.saveBest();
iProgress.info("Construction done.");
}
@Override
public double value(Assignment<Exam, ExamPlacement> assignment) {
return 0;
}
@Override
public Map<Exam, ExamPlacement> assignments() {
throw new UnsupportedOperationException();
}
};
}
return null;
}
Aggregations