use of org.cpsolver.instructor.model.TeachingAssignment in project cpsolver by UniTime.
the class SameCourse method getValue.
@Override
public double getValue(Assignment<TeachingRequest.Variable, TeachingAssignment> assignment, TeachingAssignment value, Set<TeachingAssignment> conflicts) {
Instructor.Context context = value.getInstructor().getContext(assignment);
double penalty = 0.0;
int pairs = 0;
for (TeachingAssignment ta : context.getAssignments()) {
if (ta.variable().equals(value.variable()))
continue;
penalty += value.variable().getRequest().getSameCoursePenalty(ta.variable().getRequest());
pairs++;
}
return (pairs == 0 ? 0.0 : penalty / pairs);
}
use of org.cpsolver.instructor.model.TeachingAssignment in project cpsolver by UniTime.
the class SameCommon method getValue.
@Override
public double getValue(Assignment<TeachingRequest.Variable, TeachingAssignment> assignment, TeachingAssignment value, Set<TeachingAssignment> conflicts) {
Instructor.Context context = value.getInstructor().getContext(assignment);
double penalty = 0.0;
int pairs = 0;
for (TeachingAssignment ta : context.getAssignments()) {
if (ta.variable().equals(value.variable()))
continue;
penalty += value.variable().getRequest().getSameCommonPenalty(ta.variable().getRequest());
pairs++;
}
return (pairs == 0 ? 0.0 : penalty / pairs);
}
use of org.cpsolver.instructor.model.TeachingAssignment in project cpsolver by UniTime.
the class ChmTest method generateReports.
@Override
protected void generateReports(File dir, Assignment<TeachingRequest.Variable, TeachingAssignment> assignment) throws IOException {
PrintWriter out = new PrintWriter(new File(dir, "solution-assignments.csv"));
out.println("Course,Sections,Time,Room,Skill,Qualification,Performance,Load,Student,Name,Not Available,Max Load,Skill,Qualification,Performance,Requested");
for (TeachingRequest.Variable request : variables()) {
out.print(request.getCourse().getCourseName());
String sect = "", time = "", room = "";
if (request.getId() < 0) {
out.print(",\"SUPER\",,");
} else {
for (Iterator<Section> i = request.getSections().iterator(); i.hasNext(); ) {
Section section = i.next();
// if (section.isCommon() && section.isAllowOverlap()) continue;
if (!sect.isEmpty()) {
sect += ", ";
time += ", ";
room += ", ";
}
sect += (section.isCommon() ? "(" : "") + section.getSectionType() + " " + section.getExternalId() + (section.isCommon() ? ")" : "");
time += (section.getTime() == null ? "-" : section.getTime().getDayHeader() + " " + section.getTime().getStartTimeHeader(true) + "-" + section.getTime().getEndTimeHeader(true));
room += (section.getRoom() == null ? "-" : section.getRoom());
}
out.print(",\"" + sect + "\",\"" + time + "\",\"" + room + "\"");
}
out.print(",\"" + getAttributes(request.getRequest(), "Skill") + "\"");
out.print(",\"" + getAttributes(request.getRequest(), "Qualification") + "\"");
out.print(",\"" + getAttributes(request.getRequest(), "Performance Level") + "\"");
out.print("," + new DecimalFormat("0.0").format(request.getRequest().getLoad()));
TeachingAssignment ta = assignment.getValue(request);
if (ta != null) {
Instructor instructor = ta.getInstructor();
out.print("," + instructor.getExternalId());
out.print(",\"" + instructor.getName() + "\"");
out.print(",\"" + instructor.getAvailable() + "\"");
out.print("," + new DecimalFormat("0.0").format(instructor.getMaxLoad()));
out.print(",\"" + getAttributes(request.getRequest(), instructor, "Skill") + "\"");
out.print(",\"" + getAttributes(request.getRequest(), instructor, "Qualification") + "\"");
out.print(",\"" + getAttributes(request.getRequest(), instructor, "Performance Level") + "\"");
out.print(",\"" + getCoursePreference(request.getRequest(), instructor) + "\"");
}
out.println();
}
out.flush();
out.close();
out = new PrintWriter(new File(dir, "solution-students.csv"));
out.println("Student,Name,Not Available,Skill,Qualification,Performance,Requests,Max Load,Assigned Load,1st Assignment,2nd Assignment,Skill,Qualification,Performance,Requested");
for (Instructor instructor : getInstructors()) {
out.print(instructor.getExternalId());
out.print(",\"" + instructor.getName() + "\"");
out.print(",\"" + instructor.getAvailable() + "\"");
out.print(",\"" + getAttributes(instructor, "Skill") + "\"");
out.print(",\"" + getAttributes(instructor, "Qualification") + "\"");
out.print(",\"" + getAttributes(instructor, "Performance Level") + "\"");
out.print(",\"" + getCoursePrefs(instructor) + "\"");
out.print("," + new DecimalFormat("0.0").format(instructor.getMaxLoad()));
Instructor.Context context = instructor.getContext(assignment);
out.print("," + new DecimalFormat("0.0").format(context.getLoad()));
/*
out.print("," + (context.countBackToBackPercentage() == 0.0 ? "" : new DecimalFormat("0.0").format(100.0 * context.countBackToBackPercentage())));
out.print("," + (context.countDifferentLectures() == 0.0 ? "" : new DecimalFormat("0.0").format(100.0 * context.countDifferentLectures())));
int share = 0;
for (TeachingAssignment ta : context.getAssignments()) {
for (Preference<TimeLocation> p: instructor.getTimePreferences()) {
if (!p.isProhibited())
share += ta.variable().getRequest().share(p.getTarget());
}
}
out.print("," + (share == 0 ? "" : new DecimalFormat("0.#").format(share / 12.0)));
*/
TeachingRequest req = null;
for (TeachingAssignment ta : context.getAssignments()) {
String sect = "";
if (req == null || req.getRequestId() < 0)
req = ta.variable().getRequest();
if (ta.variable().getId() < 0) {
sect = "SUPER";
} else {
for (Iterator<Section> i = ta.variable().getSections().iterator(); i.hasNext(); ) {
Section section = i.next();
if (section.isCommon() && section.isAllowOverlap())
continue;
sect += (sect.isEmpty() ? "" : ", ") + (section.isCommon() ? "(" : "") + section.getSectionType() + " " + section.getExternalId() + (section.getTime() == null ? "" : " " + section.getTime().getDayHeader() + " " + section.getTime().getStartTimeHeader(true) + "-" + section.getTime().getEndTimeHeader(true)) + (section.isCommon() ? ")" : "");
}
}
out.print(",\"" + ta.variable().getCourse() + " " + sect + "\"");
}
if (req != null) {
for (int i = context.getAssignments().size(); i < 2; i++) out.print(",");
out.print(",\"" + getAttributes(req, instructor, "Skill") + "\"");
out.print(",\"" + getAttributes(req, instructor, "Qualification") + "\"");
out.print(",\"" + getAttributes(req, instructor, "Performance Level") + "\"");
out.print(",\"" + getCoursePreference(req, instructor) + "\"");
}
out.println();
}
out.flush();
out.close();
}
use of org.cpsolver.instructor.model.TeachingAssignment in project cpsolver by UniTime.
the class Test method execute.
/**
* Run the problem
*/
public void execute() {
int nrSolvers = getProperties().getPropertyInt("Parallel.NrSolvers", 1);
Solver<TeachingRequest.Variable, TeachingAssignment> solver = (nrSolvers == 1 ? new Solver<TeachingRequest.Variable, TeachingAssignment>(getProperties()) : new ParallelSolver<TeachingRequest.Variable, TeachingAssignment>(getProperties()));
Assignment<TeachingRequest.Variable, TeachingAssignment> assignment = (nrSolvers <= 1 ? new DefaultSingleAssignment<TeachingRequest.Variable, TeachingAssignment>() : new DefaultParallelAssignment<TeachingRequest.Variable, TeachingAssignment>());
if (!load(new File(getProperties().getProperty("input", "input/solution.xml")), assignment))
return;
solver.setInitalSolution(new Solution<TeachingRequest.Variable, TeachingAssignment>(this, assignment));
solver.currentSolution().addSolutionListener(new SolutionListener<TeachingRequest.Variable, TeachingAssignment>() {
@Override
public void solutionUpdated(Solution<TeachingRequest.Variable, TeachingAssignment> solution) {
}
@Override
public void getInfo(Solution<TeachingRequest.Variable, TeachingAssignment> solution, Map<String, String> info) {
}
@Override
public void getInfo(Solution<TeachingRequest.Variable, TeachingAssignment> solution, Map<String, String> info, Collection<TeachingRequest.Variable> variables) {
}
@Override
public void bestCleared(Solution<TeachingRequest.Variable, TeachingAssignment> solution) {
}
@Override
public void bestSaved(Solution<TeachingRequest.Variable, TeachingAssignment> solution) {
Model<TeachingRequest.Variable, TeachingAssignment> m = solution.getModel();
Assignment<TeachingRequest.Variable, TeachingAssignment> a = solution.getAssignment();
System.out.println("**BEST[" + solution.getIteration() + "]** " + m.toString(a));
}
@Override
public void bestRestored(Solution<TeachingRequest.Variable, TeachingAssignment> solution) {
}
});
solver.start();
try {
solver.getSolverThread().join();
} catch (InterruptedException e) {
}
Solution<TeachingRequest.Variable, TeachingAssignment> solution = solver.lastSolution();
solution.restoreBest();
sLog.info("Best solution found after " + solution.getBestTime() + " seconds (" + solution.getBestIteration() + " iterations).");
sLog.info("Number of assigned variables is " + solution.getModel().assignedVariables(solution.getAssignment()).size());
sLog.info("Total value of the solution is " + solution.getModel().getTotalValue(solution.getAssignment()));
sLog.info("Info: " + ToolBox.dict2string(solution.getExtendedInfo(), 2));
File outDir = new File(getProperties().getProperty("output", "output"));
outDir.mkdirs();
save(outDir, solution.getAssignment());
try {
generateReports(outDir, assignment);
} catch (IOException e) {
sLog.error("Failed to write reports: " + e.getMessage(), e);
}
ConflictStatistics<TeachingRequest.Variable, TeachingAssignment> cbs = null;
for (Extension<TeachingRequest.Variable, TeachingAssignment> extension : solver.getExtensions()) {
if (ConflictStatistics.class.isInstance(extension)) {
cbs = (ConflictStatistics<TeachingRequest.Variable, TeachingAssignment>) extension;
}
}
if (cbs != null) {
PrintWriter out = null;
try {
out = new PrintWriter(new FileWriter(new File(outDir, "cbs.txt")));
out.println(cbs.toString());
out.flush();
out.close();
} catch (IOException e) {
sLog.error("Failed to write CBS: " + e.getMessage(), e);
} finally {
if (out != null)
out.close();
}
}
}
use of org.cpsolver.instructor.model.TeachingAssignment in project cpsolver by UniTime.
the class SameInstructorConstraint method getCurrentPreference.
/**
* Current constraint preference (if soft)
* @param assignment current assignment
* @param value proposed change
* @return change in the current preference value of this constraint
*/
public int getCurrentPreference(Assignment<TeachingRequest.Variable, TeachingAssignment> assignment, TeachingAssignment value) {
// no preference
if (isHard())
return 0;
// not enough variables
if (countAssignedVariables(assignment) + (assignment.getValue(value.variable()) == null ? 1 : 0) < 2)
return 0;
int nrViolatedPairsAfter = 0;
int nrViolatedPairsBefore = 0;
for (TeachingRequest.Variable v1 : variables()) {
for (TeachingRequest.Variable v2 : variables()) {
if (v1.getId() >= v2.getId())
continue;
TeachingAssignment p1 = (v1.equals(value.variable()) ? null : assignment.getValue(v1));
TeachingAssignment p2 = (v2.equals(value.variable()) ? null : assignment.getValue(v2));
if (p1 != null && p2 != null && !isSatisfiedPair(assignment, p1, p2))
nrViolatedPairsBefore++;
if (v1.equals(value.variable()))
p1 = value;
if (v2.equals(value.variable()))
p2 = value;
if (p1 != null && p2 != null && !isSatisfiedPair(assignment, p1, p2))
nrViolatedPairsAfter++;
}
}
return (nrViolatedPairsAfter > 0 ? Math.abs(iPreference) * nrViolatedPairsAfter : 0) - (nrViolatedPairsBefore > 0 ? Math.abs(iPreference) * nrViolatedPairsBefore : 0);
}
Aggregations