use of org.cpsolver.studentsct.model.Request in project cpsolver by UniTime.
the class StandardSelection method selectNeighbour.
/**
* Employ the provided {@link VariableSelection} and {@link ValueSelection}
* and return the selected value as {@link SimpleNeighbour}. The selection
* is stopped (null is returned) after the number of iterations equal to the
* number of variables in the problem or when a complete solution is found.
*/
@Override
public Neighbour<Request, Enrollment> selectNeighbour(Solution<Request, Enrollment> solution) {
if (iNrIterations < 0) {
iNrIterations = solution.getModel().nrUnassignedVariables(solution.getAssignment());
Progress.getInstance(solution.getModel()).setPhase("Ifs...", iNrIterations);
}
if (solution.getModel().unassignedVariables(solution.getAssignment()).isEmpty() || ++iIteration >= iNrIterations)
return null;
Progress.getInstance(solution.getModel()).incProgress();
for (int i = 0; i < 10; i++) {
Request request = iVariableSelection.selectVariable(solution);
if (request == null)
continue;
Enrollment enrollment = iValueSelection.selectValue(solution, request);
if (enrollment == null)
continue;
Set<Enrollment> conflicts = enrollment.variable().getModel().conflictValues(solution.getAssignment(), enrollment);
if (!conflicts.contains(enrollment))
return new SimpleNeighbour<Request, Enrollment>(request, enrollment, conflicts);
}
return null;
}
use of org.cpsolver.studentsct.model.Request in project cpsolver by UniTime.
the class SwapStudentSelection method selectNeighbour.
/**
* For each student that does not have a complete schedule, try to find a
* request and a student that can be moved out of an enrollment so that the
* selected student can be assigned to the selected request.
*/
@Override
public Neighbour<Request, Enrollment> selectNeighbour(Solution<Request, Enrollment> solution) {
Student student = null;
while ((student = nextStudent()) != null) {
Progress.getInstance(solution.getModel()).incProgress();
if (student.isComplete(solution.getAssignment()) || student.nrAssignedRequests(solution.getAssignment()) == 0)
continue;
Selection selection = getSelection(solution.getAssignment(), student);
Neighbour<Request, Enrollment> neighbour = selection.select();
if (neighbour != null) {
addStudent(student);
return neighbour;
} else
iProblemStudents.addAll(selection.getProblemStudents());
}
return null;
}
use of org.cpsolver.studentsct.model.Request in project cpsolver by UniTime.
the class SuggestionsBranchAndBound method backtrack.
/**
* Main branch and bound rutine
* @param requests2resolve remaining requests to assign
* @param altRequests2resolve alternative requests to assign
* @param idx current depth
* @param depth remaining depth
* @param alt can leave a request unassigned
*/
protected void backtrack(ArrayList<Request> requests2resolve, TreeSet<Request> altRequests2resolve, int idx, int depth, boolean alt) {
if (!iTimeoutReached && iTimeout > 0 && System.currentTimeMillis() - iT0 > iTimeout)
iTimeoutReached = true;
int nrUnassigned = requests2resolve.size() - idx;
if (nrUnassigned == 0) {
List<FreeTimeRequest> okFreeTimes = new ArrayList<FreeTimeRequest>();
double sectionsWithPenalty = 0;
for (Request r : iStudent.getRequests()) {
Enrollment e = iAssignment.getValue(r);
if (iMaxSectionsWithPenalty >= 0 && e != null && r instanceof CourseRequest) {
for (Section s : e.getSections()) sectionsWithPenalty += iModel.getOverExpected(iAssignment, s, r);
}
if (e == null && r instanceof FreeTimeRequest) {
FreeTimeRequest ft = (FreeTimeRequest) r;
Enrollment enrollment = ft.createEnrollment();
if (iModel.conflictValues(iAssignment, enrollment).isEmpty()) {
iAssignment.assign(0, enrollment);
okFreeTimes.add(ft);
}
}
}
if (iMaxSectionsWithPenalty >= 0 && sectionsWithPenalty > iMaxSectionsWithPenalty)
return;
Suggestion s = new Suggestion(requests2resolve);
if (iSuggestions.size() >= iMaxSuggestions && iSuggestions.last().compareTo(s) <= 0)
return;
if (iMatched != 1) {
for (Iterator<Suggestion> i = iSuggestions.iterator(); i.hasNext(); ) {
Suggestion x = i.next();
if (x.sameSelectedSection()) {
if (x.compareTo(s) <= 0)
return;
i.remove();
}
}
}
s.init();
iSuggestions.add(s);
if (iSuggestions.size() > iMaxSuggestions)
iSuggestions.remove(iSuggestions.last());
for (FreeTimeRequest ft : okFreeTimes) iAssignment.unassign(0, ft);
return;
}
if (!canContinue(requests2resolve, idx, depth))
return;
Request request = requests2resolve.get(idx);
for (Enrollment enrollment : values(request)) {
if (!canContinueEvaluation())
break;
if (!isAllowed(enrollment))
continue;
if (enrollment.equals(iAssignment.getValue(request)))
continue;
if (enrollment.getAssignments().isEmpty() && alt)
continue;
Set<Enrollment> conflicts = iModel.conflictValues(iAssignment, enrollment);
if (!checkBound(requests2resolve, idx, depth, enrollment, conflicts))
continue;
Enrollment current = iAssignment.getValue(request);
ArrayList<Request> newVariables2resolve = new ArrayList<Request>(requests2resolve);
for (Iterator<Enrollment> i = conflicts.iterator(); i.hasNext(); ) {
Enrollment conflict = i.next();
iAssignment.unassign(0, conflict.variable());
if (!newVariables2resolve.contains(conflict.variable()))
newVariables2resolve.add(conflict.variable());
}
if (current != null)
iAssignment.unassign(0, current.variable());
iAssignment.assign(0, enrollment);
if (enrollment.getAssignments().isEmpty()) {
if (altRequests2resolve != null && !altRequests2resolve.isEmpty()) {
Suggestion lastBefore = (iSuggestions.isEmpty() ? null : iSuggestions.last());
int sizeBefore = iSuggestions.size();
for (Request r : altRequests2resolve) {
newVariables2resolve.add(r);
backtrack(newVariables2resolve, null, idx + 1, depth, true);
newVariables2resolve.remove(r);
}
Suggestion lastAfter = (iSuggestions.isEmpty() ? null : iSuggestions.last());
int sizeAfter = iSuggestions.size();
// did not succeeded with an alternative -> try without it
if (sizeBefore == sizeAfter && (sizeAfter < iMaxSuggestions || sizeAfter == 0 || lastAfter.compareTo(lastBefore) == 0))
backtrack(newVariables2resolve, altRequests2resolve, idx + 1, depth - 1, alt);
} else {
backtrack(newVariables2resolve, altRequests2resolve, idx + 1, depth - 1, alt);
}
} else {
backtrack(newVariables2resolve, altRequests2resolve, idx + 1, depth - 1, alt);
}
if (current == null)
iAssignment.unassign(0, request);
else
iAssignment.assign(0, current);
for (Iterator<Enrollment> i = conflicts.iterator(); i.hasNext(); ) {
Enrollment conflict = i.next();
iAssignment.assign(0, conflict);
}
}
}
use of org.cpsolver.studentsct.model.Request in project cpsolver by UniTime.
the class StudentChoiceOrder method avgNrChoices.
/** Average number of choices for each student
* @param student given student
* @return average number of choices of the given student
**/
public double avgNrChoices(Student student) {
int nrRequests = 0;
int nrChoices = 0;
for (Request request : student.getRequests()) {
if (request instanceof CourseRequest) {
CourseRequest cr = (CourseRequest) request;
for (Course course : cr.getCourses()) {
for (Config config : course.getOffering().getConfigs()) {
Integer nrChoicesThisCfg = iCache.get(config);
if (nrChoicesThisCfg == null) {
nrChoicesThisCfg = new Integer(nrChoices(config, 0, new HashSet<Section>()));
iCache.put(config, nrChoicesThisCfg);
}
nrChoices += nrChoicesThisCfg.intValue();
}
}
nrRequests++;
}
}
return (nrRequests == 0 ? 0.0 : ((double) nrChoices) / nrRequests);
}
use of org.cpsolver.studentsct.model.Request in project cpsolver by UniTime.
the class Test method main.
public static void main(String[] args) {
try {
System.setProperty("jprof", "cpu");
BasicConfigurator.configure();
DataProperties cfg = new DataProperties();
cfg.setProperty("Neighbour.BranchAndBoundTimeout", "5000");
cfg.setProperty("Suggestions.Timeout", "1000");
cfg.setProperty("Extensions.Classes", DistanceConflict.class.getName() + ";" + TimeOverlapsCounter.class.getName());
cfg.setProperty("StudentWeights.Class", StudentSchedulingAssistantWeights.class.getName());
cfg.setProperty("StudentWeights.PriorityWeighting", "true");
cfg.setProperty("StudentWeights.LeftoverSpread", "true");
cfg.setProperty("StudentWeights.BalancingFactor", "0.0");
cfg.setProperty("Reservation.CanAssignOverTheLimit", "true");
cfg.setProperty("Distances.Ellipsoid", DistanceMetric.Ellipsoid.WGS84.name());
cfg.setProperty("StudentWeights.MultiCriteria", "true");
cfg.setProperty("CourseRequest.SameTimePrecise", "true");
cfg.setProperty("log4j.rootLogger", "INFO, A1");
cfg.setProperty("log4j.appender.A1", "org.apache.log4j.ConsoleAppender");
cfg.setProperty("log4j.appender.A1.layout", "org.apache.log4j.PatternLayout");
cfg.setProperty("log4j.appender.A1.layout.ConversionPattern", "%-5p %c{2}: %m%n");
cfg.setProperty("log4j.logger.org.hibernate", "INFO");
cfg.setProperty("log4j.logger.org.hibernate.cfg", "WARN");
cfg.setProperty("log4j.logger.org.hibernate.cache.EhCacheProvider", "ERROR");
cfg.setProperty("log4j.logger.org.unitime.commons.hibernate", "INFO");
cfg.setProperty("log4j.logger.net", "INFO");
cfg.setProperty("Xml.LoadBest", "false");
cfg.setProperty("Xml.LoadCurrent", "false");
cfg.putAll(System.getProperties());
PropertyConfigurator.configure(cfg);
final Test test = new Test(cfg);
final File input = new File(args[0]);
StudentSectioningXMLLoader loader = new StudentSectioningXMLLoader(test.model(), test.assignment());
loader.setInputFile(input);
loader.load();
test.run();
Solver<Request, Enrollment> s = new Solver<Request, Enrollment>(cfg);
s.setInitalSolution(test.model());
StudentSectioningXMLSaver saver = new StudentSectioningXMLSaver(s);
File output = new File(input.getParentFile(), input.getName().substring(0, input.getName().lastIndexOf('.')) + "-" + cfg.getProperty("run", "r0") + ".xml");
saver.save(output);
test.stats(input);
} catch (Exception e) {
sLog.error("Test failed: " + e.getMessage(), e);
}
}
Aggregations