use of org.cpsolver.exam.model.ExamModel in project cpsolver by UniTime.
the class MistaTables method main.
public static void main(String[] args) {
try {
ToolBox.configureLogging();
DataProperties config = new DataProperties();
Table[] tables = new Table[] { new Problems(), new Rooms() };
for (int i = 0; i < args.length; i++) {
File file = new File(args[i]);
sLog.info("Loading " + file);
ExamModel model = new ExamModel(config);
Assignment<Exam, ExamPlacement> assignment = new DefaultSingleAssignment<Exam, ExamPlacement>();
model.load(new SAXReader().read(file), assignment);
String name = file.getName();
if (name.contains("."))
name = name.substring(0, name.indexOf('.'));
for (Table table : tables) table.add(name, model);
Progress.removeInstance(model);
}
sLog.info("Saving tables...");
File output = new File("tables");
output.mkdir();
for (Table table : tables) table.save(output);
sLog.info("All done.");
} catch (Exception e) {
sLog.error(e.getMessage(), e);
}
}
use of org.cpsolver.exam.model.ExamModel in project cpsolver by UniTime.
the class ExamTabuSearch method selectValue.
/**
* Value selection
*/
@Override
public ExamPlacement selectValue(Solution<Exam, ExamPlacement> solution, Exam exam) {
if (iFirstIteration < 0)
iFirstIteration = solution.getIteration();
TabuList tabu = getContext(solution.getAssignment());
long idle = solution.getIteration() - Math.max(iFirstIteration, solution.getBestIteration());
if (idle > iMaxIdleIterations) {
sLog.debug(" [tabu] max idle iterations reached");
iFirstIteration = -1;
if (tabu.size() > 0)
tabu.clear();
return null;
}
if (tabu.size() > 0 && iTabuMaxSize > iTabuMinSize) {
if (idle == 0) {
tabu.resize(iTabuMinSize);
} else if (idle % (iMaxIdleIterations / (iTabuMaxSize - iTabuMinSize)) == 0) {
tabu.resize(Math.min(iTabuMaxSize, tabu.size() + 1));
}
}
ExamModel model = (ExamModel) solution.getModel();
Assignment<Exam, ExamPlacement> assignment = solution.getAssignment();
double bestEval = 0.0;
List<ExamPlacement> best = null;
ExamPlacement assigned = assignment.getValue(exam);
// double assignedVal =
// (assigned==null?-iConflictWeight:iValueWeight*assigned.toDouble());
double assignedVal = (assigned == null ? iConflictWeight : iValueWeight * assigned.toDouble(assignment));
for (ExamPeriodPlacement period : exam.getPeriodPlacements()) {
Set<ExamRoomPlacement> rooms = exam.findBestAvailableRooms(assignment, period);
if (rooms == null)
rooms = exam.findRoomsRandom(assignment, period, false);
if (rooms == null) {
sLog.info("Exam " + exam.getName() + " has no rooms for period " + period);
continue;
}
ExamPlacement value = new ExamPlacement(exam, period, rooms);
if (value.equals(assigned))
continue;
Set<ExamPlacement> conflicts = model.conflictValues(assignment, value);
double eval = iValueWeight * value.toDouble(assignment) - assignedVal;
for (ExamPlacement conflict : conflicts) {
eval -= iValueWeight * conflict.toDouble(assignment);
eval += iConflictWeight * (1.0 + (iStat == null ? 0.0 : iStat.countRemovals(solution.getIteration(), conflict, value)));
}
if (tabu.size() > 0 && tabu.contains(exam.getId() + ":" + value.getPeriod().getIndex())) {
int un = model.variables().size() - assignment.nrAssignedVariables() - (assigned == null ? 0 : 1);
if (un > model.getBestUnassignedVariables())
continue;
if (un == model.getBestUnassignedVariables() && model.getTotalValue(assignment) + eval >= solution.getBestValue())
continue;
}
if (best == null || bestEval > eval) {
if (best == null)
best = new ArrayList<ExamPlacement>();
else
best.clear();
best.add(value);
bestEval = eval;
} else if (bestEval == eval) {
best.add(value);
}
}
if (best == null)
return null;
ExamPlacement bestVal = ToolBox.random(best);
if (sLog.isDebugEnabled()) {
Set<ExamPlacement> conflicts = model.conflictValues(assignment, bestVal);
double wconf = (iStat == null ? 0.0 : iStat.countRemovals(solution.getIteration(), conflicts, bestVal));
sLog.debug(" [tabu] " + bestVal + " (" + (assignment.getValue(bestVal.variable()) == null ? "" : "was=" + assignment.getValue(bestVal.variable()) + ", ") + "val=" + bestEval + (conflicts.isEmpty() ? "" : ", conf=" + (wconf + conflicts.size()) + "/" + conflicts) + ")");
}
if (tabu.size() > 0)
tabu.add(exam.getId() + ":" + bestVal.getPeriod().getIndex());
return bestVal;
}
use of org.cpsolver.exam.model.ExamModel in project cpsolver by UniTime.
the class ExamUnassignedVariableSelection method selectVariable.
/** Variable selection */
@Override
public Exam selectVariable(Solution<Exam, ExamPlacement> solution) {
ExamModel model = (ExamModel) solution.getModel();
Assignment<Exam, ExamPlacement> assignment = solution.getAssignment();
if (model.variables().size() == assignment.nrAssignedVariables())
return null;
if (iRandomSelection) {
int idx = ToolBox.random(model.variables().size() - assignment.nrAssignedVariables());
for (Exam v : model.variables()) {
if (assignment.getValue(v) != null)
continue;
if (idx == 0)
return v;
idx--;
}
}
Exam variable = null;
for (Exam v : model.variables()) {
if (assignment.getValue(v) != null)
continue;
if (variable == null || v.compareTo(variable) < 0)
variable = v;
}
return variable;
}
use of org.cpsolver.exam.model.ExamModel in project cpsolver by UniTime.
the class ExamPeriodSwapMove method selectNeighbour.
/**
* Select an exam randomly,
* select an available period randomly (if it is not assigned),
* use rooms if possible, select rooms using {@link Exam#findBestAvailableRooms(Assignment, ExamPeriodPlacement)} if not (exam is unassigned, a room is not available or used).
*/
@Override
public Neighbour<Exam, ExamPlacement> selectNeighbour(Solution<Exam, ExamPlacement> solution) {
ExamModel model = (ExamModel) solution.getModel();
Assignment<Exam, ExamPlacement> assignment = solution.getAssignment();
Exam x1 = ToolBox.random(model.variables());
ExamPlacement v1 = assignment.getValue(x1);
if (v1 == null)
return null;
int x = ToolBox.random(model.variables().size());
for (int v = 0; v < model.variables().size(); v++) {
Exam x2 = model.variables().get((v + x) % (model.variables().size()));
ExamPlacement v2 = assignment.getValue(x2);
if (x1.equals(x2) || v2 == null)
continue;
ExamPeriodPlacement p1 = x1.getPeriodPlacement(v2.getPeriod());
ExamPeriodPlacement p2 = x2.getPeriodPlacement(v1.getPeriod());
if (p1 == null || p2 == null)
continue;
if (iCheckStudentConflicts && (x1.countStudentConflicts(assignment, p1) > 0 || x2.countStudentConflicts(assignment, p2) > 0))
continue;
if (iCheckDistributionConstraints) {
Map<Exam, ExamPlacement> placements = new HashMap<Exam, ExamPlacement>();
placements.put(x1, new ExamPlacement(x1, p1, new HashSet<ExamRoomPlacement>()));
placements.put(x2, new ExamPlacement(x2, p2, new HashSet<ExamRoomPlacement>()));
if (!checkDistributionConstraints(assignment, x1, p1, placements) || !checkDistributionConstraints(assignment, x2, p2, placements))
continue;
}
Set<ExamPlacement> conflicts = new HashSet<ExamPlacement>();
conflicts.add(v1);
conflicts.add(v2);
Map<Exam, ExamPlacement> placements = new HashMap<Exam, ExamPlacement>();
Set<ExamRoomPlacement> r1 = findBestAvailableRooms(assignment, x1, p1, conflicts, placements);
if (r1 == null)
continue;
placements.put(x1, new ExamPlacement(x1, p1, r1));
Set<ExamRoomPlacement> r2 = findBestAvailableRooms(assignment, x2, p2, conflicts, placements);
if (r2 == null)
continue;
return new LazySwap<Exam, ExamPlacement>(new ExamPlacement(x1, p1, r1), new ExamPlacement(x2, p2, r2));
}
return null;
}
use of org.cpsolver.exam.model.ExamModel in project cpsolver by UniTime.
the class ExamTimeMove method selectNeighbour.
/**
* Select an exam randomly,
* select an available period randomly (if it is not assigned),
* use rooms if possible, select rooms using {@link Exam#findBestAvailableRooms(Assignment, ExamPeriodPlacement)} if not (exam is unassigned, a room is not available or used).
*/
@Override
public Neighbour<Exam, ExamPlacement> selectNeighbour(Solution<Exam, ExamPlacement> solution) {
ExamModel model = (ExamModel) solution.getModel();
Assignment<Exam, ExamPlacement> assignment = solution.getAssignment();
ExamRoomSharing sharing = model.getRoomSharing();
Exam exam = ToolBox.random(model.variables());
ExamPlacement placement = assignment.getValue(exam);
int px = ToolBox.random(exam.getPeriodPlacements().size());
for (int p = 0; p < exam.getPeriodPlacements().size(); p++) {
ExamPeriodPlacement period = exam.getPeriodPlacements().get((p + px) % exam.getPeriodPlacements().size());
if (placement != null && placement.getPeriod().equals(period))
continue;
if (iCheckStudentConflicts && exam.countStudentConflicts(assignment, period) > 0)
continue;
if (iCheckDistributionConstraints && !exam.checkDistributionConstraints(assignment, period))
continue;
if (placement != null) {
boolean ok = true;
if (sharing != null && placement.getRoomPlacements().size() == 1) {
ExamRoomPlacement room = placement.getRoomPlacements().iterator().next();
ok = room.isAvailable(period.getPeriod()) && !sharing.inConflict(exam, room.getRoom().getPlacements(assignment, period.getPeriod()), room.getRoom());
} else {
for (Iterator<ExamRoomPlacement> i = placement.getRoomPlacements().iterator(); i.hasNext(); ) {
ExamRoomPlacement room = i.next();
if (!room.isAvailable(period.getPeriod()) || !room.getRoom().getPlacements(assignment, period.getPeriod()).isEmpty()) {
ok = false;
break;
}
}
}
if (ok)
return new ExamSimpleNeighbour(assignment, new ExamPlacement(exam, period, placement.getRoomPlacements()));
}
Set<ExamRoomPlacement> rooms = exam.findBestAvailableRooms(assignment, period);
if (rooms == null)
continue;
return new ExamSimpleNeighbour(assignment, new ExamPlacement(exam, period, rooms));
}
return null;
}
Aggregations