use of org.cpsolver.instructor.model.TeachingAssignment in project cpsolver by UniTime.
the class InstructorConstraint method computeConflicts.
@Override
public void computeConflicts(Assignment<TeachingRequest.Variable, TeachingAssignment> assignment, TeachingAssignment value, Set<TeachingAssignment> conflicts) {
Context context = value.getInstructor().getContext(assignment);
// Check availability
if (context.getInstructor().getTimePreference(value.variable().getRequest()).isProhibited()) {
conflicts.add(value);
return;
}
// Check for overlaps
for (TeachingAssignment ta : context.getAssignments()) {
if (ta.variable().equals(value.variable()) || conflicts.contains(ta))
continue;
if (ta.variable().getRequest().overlaps(value.variable().getRequest()))
conflicts.add(ta);
}
// Same course and/or common
for (TeachingAssignment ta : context.getAssignments()) {
if (ta.variable().equals(value.variable()) || conflicts.contains(ta))
continue;
if (ta.variable().getRequest().isSameCourseViolated(value.variable().getRequest()) || ta.variable().getRequest().isSameCommonViolated(value.variable().getRequest()))
conflicts.add(ta);
}
// Check load
float load = value.variable().getRequest().getLoad();
List<TeachingAssignment> adepts = new ArrayList<TeachingAssignment>();
for (TeachingAssignment ta : context.getAssignments()) {
if (ta.variable().equals(value.variable()) || conflicts.contains(ta))
continue;
adepts.add(ta);
load += ta.variable().getRequest().getLoad();
}
while (load > context.getInstructor().getMaxLoad()) {
if (adepts.isEmpty()) {
conflicts.add(value);
break;
}
TeachingAssignment conflict = ToolBox.random(adepts);
load -= conflict.variable().getRequest().getLoad();
adepts.remove(conflict);
conflicts.add(conflict);
}
}
use of org.cpsolver.instructor.model.TeachingAssignment in project cpsolver by UniTime.
the class SameLinkConstraint method getCurrentPreference.
/**
* Current constraint preference (if soft)
* @param assignment current assignment
* @return that is number of requests that are not of this link assigned to the instructors that have at least one request of this link if preferred;
* number of additional requests of this link given to the same instructor if discouraged
*/
public int getCurrentPreference(Assignment<TeachingRequest.Variable, TeachingAssignment> assignment) {
// no preference
if (isHard())
return 0;
if (getPreference() < 0) {
// preferred
int ret = 0;
Set<Instructor> checked = new HashSet<Instructor>();
for (TeachingRequest.Variable tr : variables()) {
TeachingAssignment ta = assignment.getValue(tr);
if (ta == null || !checked.add(ta.getInstructor()))
continue;
Instructor.Context context = ta.getInstructor().getContext(assignment);
for (TeachingAssignment other : context.getAssignments()) {
if (!variables().contains(other.variable())) {
ret++;
}
}
}
return ret;
} else if (getPreference() > 0) {
int ret = 0;
Set<Instructor> checked = new HashSet<Instructor>();
for (TeachingRequest.Variable tr : variables()) {
TeachingAssignment ta = assignment.getValue(tr);
if (ta == null || !checked.add(ta.getInstructor()))
continue;
Instructor.Context context = ta.getInstructor().getContext(assignment);
for (TeachingAssignment other : context.getAssignments()) {
if (!variables().equals(tr) && variables().contains(other.variable())) {
ret++;
}
}
}
return ret;
} else {
return 0;
}
}
use of org.cpsolver.instructor.model.TeachingAssignment in project cpsolver by UniTime.
the class Test method generateReports.
/**
* Generate a few reports
* @param outputDir output directory
* @param assignment current assignments
* @throws IOException
*/
protected void generateReports(File outputDir, Assignment<TeachingRequest.Variable, TeachingAssignment> assignment) throws IOException {
PrintWriter out = new PrintWriter(new File(outputDir, "solution-assignments.csv"));
out.println("Course,Section,Time,Room,Load,Student,Name,Instructor Pref,Course Pref,Attribute Pref,Time Pref,Back-To-Back,Same-Days,Same-Room,Different Lecture,Overlap [h]");
double diffRoomWeight = getProperties().getPropertyDouble("BackToBack.DifferentRoomWeight", 0.8);
double diffTypeWeight = getProperties().getPropertyDouble("BackToBack.DifferentTypeWeight", 0.5);
double diffRoomWeightSD = getProperties().getPropertyDouble("SameDays.DifferentRoomWeight", 0.8);
double diffTypeWeightSD = getProperties().getPropertyDouble("SameDays.DifferentTypeWeight", 0.5);
double diffTypeWeightSR = getProperties().getPropertyDouble("SameRoom.DifferentTypeWeight", 0.5);
for (TeachingRequest.Variable request : variables()) {
out.print(request.getCourse().getCourseName());
String sect = "", time = "", room = "";
for (Iterator<Section> i = request.getSections().iterator(); i.hasNext(); ) {
Section section = i.next();
sect += section.getSectionName();
time += (section.getTime() == null ? "-" : section.getTime().getDayHeader() + " " + section.getTime().getStartTimeHeader(true));
room += (section.getRoom() == null ? "-" : section.getRoom());
if (i.hasNext()) {
sect += ", ";
time += ", ";
room += ", ";
}
}
out.print(",\"" + sect + "\",\"" + time + "\",\"" + room + "\"");
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("," + (ta.getInstructorPreference() == 0 ? "" : ta.getInstructorPreference()));
out.print("," + (ta.getCoursePreference() == 0 ? "" : ta.getCoursePreference()));
out.print("," + (ta.getAttributePreference() == 0 ? "" : ta.getAttributePreference()));
out.print("," + (ta.getTimePreference() == 0 ? "" : ta.getTimePreference()));
double b2b = instructor.countBackToBacks(assignment, ta, diffRoomWeight, diffTypeWeight);
out.print("," + (b2b == 0.0 ? "" : new DecimalFormat("0.0").format(b2b)));
double sd = instructor.countSameDays(assignment, ta, diffRoomWeightSD, diffTypeWeightSD);
out.print("," + (sd == 0.0 ? "" : new DecimalFormat("0.0").format(sd)));
double sr = instructor.countSameRooms(assignment, ta, diffTypeWeightSR);
out.print("," + (sr == 0.0 ? "" : new DecimalFormat("0.0").format(sr)));
double dl = instructor.differentLectures(assignment, ta);
out.print("," + (dl == 0.0 ? "" : new DecimalFormat("0.0").format(dl)));
double sh = instructor.share(assignment, ta);
out.print("," + (sh == 0 ? "" : new DecimalFormat("0.0").format(sh / 12.0)));
}
out.println();
}
out.flush();
out.close();
out = new PrintWriter(new File(outputDir, "solution-students.csv"));
out.println("Student,Name,Preference,Not Available,Time Pref,Course Pref,Back-to-Back,Same-Days,Same-Room,Max Load,Assigned Load,Back-To-Back,Same-Days,Same-Room,Different Lecture,Overlap [h],1st Assignment,2nd Assignment, 3rd Assignment");
for (Instructor instructor : getInstructors()) {
out.print(instructor.getExternalId());
out.print(",\"" + instructor.getName() + "\"");
out.print("," + (instructor.getPreference() == 0 ? "" : instructor.getPreference()));
out.print(",\"" + instructor.getAvailable() + "\"");
String timePref = "";
for (Preference<TimeLocation> p : instructor.getTimePreferences()) {
if (!p.isProhibited()) {
if (!timePref.isEmpty())
timePref += ", ";
timePref += p.getTarget().getLongName(true).trim() + ": " + (p.isRequired() ? "R" : p.isProhibited() ? "P" : p.getPreference());
}
}
out.print(",\"" + timePref + "\"");
String coursePref = "";
for (Preference<Course> p : instructor.getCoursePreferences()) {
if (!coursePref.isEmpty())
coursePref += ", ";
coursePref += p.getTarget().getCourseName() + ": " + (p.isRequired() ? "R" : p.isProhibited() ? "P" : p.getPreference());
}
out.print(",\"" + coursePref + "\"");
out.print("," + (instructor.getBackToBackPreference() == 0 ? "" : instructor.getBackToBackPreference()));
out.print("," + (instructor.getSameDaysPreference() == 0 ? "" : instructor.getSameDaysPreference()));
out.print("," + (instructor.getSameRoomPreference() == 0 ? "" : instructor.getSameRoomPreference()));
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.countSameDaysPercentage() == 0.0 ? "" : new DecimalFormat("0.0").format(100.0 * context.countSameDaysPercentage())));
out.print("," + (context.countSameRoomPercentage() == 0.0 ? "" : new DecimalFormat("0.0").format(100.0 * context.countSameRoomPercentage())));
out.print("," + (context.countDifferentLectures() == 0.0 ? "" : new DecimalFormat("0.0").format(100.0 * context.countDifferentLectures())));
out.print("," + (context.countTimeOverlaps() == 0.0 ? "" : new DecimalFormat("0.0").format(context.countTimeOverlaps() / 12.0)));
for (TeachingAssignment ta : context.getAssignments()) {
String sect = "";
for (Iterator<Section> i = ta.variable().getSections().iterator(); i.hasNext(); ) {
Section section = i.next();
sect += section.getSectionName() + (section.getTime() == null ? "" : " " + section.getTime().getDayHeader() + " " + section.getTime().getStartTimeHeader(true));
if (i.hasNext())
sect += ", ";
}
out.print(",\"" + ta.variable().getCourse() + " " + sect + "\"");
}
out.println();
}
out.flush();
out.close();
}
use of org.cpsolver.instructor.model.TeachingAssignment in project cpsolver by UniTime.
the class UnusedInstructorLoad method getValue.
@Override
public double getValue(Assignment<TeachingRequest.Variable, TeachingAssignment> assignment, Collection<TeachingRequest.Variable> variables) {
double unused = 0.0;
Set<Instructor> instructors = new HashSet<Instructor>();
for (TeachingRequest.Variable req : variables) {
TeachingAssignment ta = assignment.getValue(req);
if (ta != null && instructors.add(ta.getInstructor())) {
unused += ta.getInstructor().getMaxLoad() - ta.getInstructor().getContext(assignment).getLoad();
}
}
return unused;
}
use of org.cpsolver.instructor.model.TeachingAssignment in project cpsolver by UniTime.
the class MathTest 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("Assignment Id,Course,Section,Time,Room,Link,Level,Load,Student,Availability,1st Preference,2nd Preference,3rd Preference,Graduate,Back-To-Back,Max Load,Level,Level,Preference");
for (TeachingRequest.Variable request : variables()) {
Long assId = getAssignmentId(request);
out.print(assId == null ? "" : assId);
out.print("," + request.getCourse().getCourseName());
Section section = request.getSections().get(0);
out.print("," + section.getSectionType());
out.print("," + section.getTimeName(true));
out.print(",\"" + (section.hasRoom() ? section.getRoom() : "") + "\"");
String link = getLink(request);
out.print("," + (link == null ? "" : link));
Map<String, Integer> levels = new HashMap<String, Integer>();
for (Preference<Attribute> p : request.getRequest().getAttributePreferences()) if (p.getTarget().getType().getTypeName().equals("Level"))
levels.put(p.getTarget().getAttributeName(), -p.getPreference());
out.print(",\"" + levels + "\"");
out.print("," + new DecimalFormat("0.0").format(request.getRequest().getLoad()));
TeachingAssignment value = assignment.getValue(request);
if (value != null) {
out.print("," + toString(value.getInstructor()));
out.print("," + (-value.getAttributePreference()));
out.print("," + (value.getCoursePreference() == -10 ? "1" : value.getCoursePreference() == -8 ? "2" : value.getCoursePreference() == -5 ? "3" : value.getCoursePreference()));
}
out.println();
}
out.flush();
out.close();
out = new PrintWriter(new File(dir, "solution-students.csv"));
out.println("Student,Availability,1st Preference,2nd Preference,3rd Preference,Graduate,Back-To-Back,Max Load,Level,Assigned Load,Avg Level,Avg Preference,Back-To-Back,Diff Links,1st Assignment,2nd Assignment, 3rd Assignment");
for (Instructor instructor : getInstructors()) {
out.print(instructor.getExternalId());
out.print(",\"" + instructor.getAvailable() + "\"");
for (int i = 0; i < 3; i++) {
Preference<Course> p = (i < instructor.getCoursePreferences().size() ? instructor.getCoursePreferences().get(i) : null);
out.print("," + (p == null ? "" : p.getTarget().getCourseName()));
}
out.print("," + (instructor.getPreference() == 0 ? "Yes" : "No"));
out.print("," + (instructor.isBackToBackPreferred() ? "1" : instructor.isBackToBackDiscouraged() ? "-1" : "0"));
out.print("," + new DecimalFormat("0.0").format(instructor.getMaxLoad()));
String level = getLevel(instructor);
out.print("," + (level == null ? "" : level));
Instructor.Context context = instructor.getContext(assignment);
out.print("," + new DecimalFormat("0.0").format(context.getLoad()));
double att = 0.0, pref = 0.0;
for (TeachingAssignment ta : context.getAssignments()) {
att += Math.abs(ta.getAttributePreference());
pref += (ta.getCoursePreference() == -10 ? 1 : ta.getCoursePreference() == -8 ? 2 : ta.getCoursePreference() == -5 ? 3 : ta.getCoursePreference());
}
int diffLinks = countDiffLinks(context.getAssignments());
out.print("," + (context.getAssignments().isEmpty() ? "" : new DecimalFormat("0.0").format(att / context.getAssignments().size())));
out.print("," + (context.getAssignments().isEmpty() || pref == 0.0 ? "" : new DecimalFormat("0.0").format(pref / context.getAssignments().size())));
out.print("," + new DecimalFormat("0.0").format(100.0 * context.countBackToBackPercentage()));
out.print("," + (diffLinks <= 0 ? "" : diffLinks));
for (TeachingAssignment ta : context.getAssignments()) {
String link = getLink(ta.variable());
out.print("," + ta.variable().getCourse() + " " + ta.variable().getSections().get(0).getSectionType() + " " + ta.variable().getSections().get(0).getTime().getName(true) + (link == null ? "" : " " + link));
}
out.println();
}
out.flush();
out.close();
out = new PrintWriter(new File(dir, "input-courses.csv"));
Set<String> levels = new TreeSet<String>();
for (TeachingRequest.Variable request : variables()) {
for (Preference<Attribute> p : request.getRequest().getAttributePreferences()) levels.add(p.getTarget().getAttributeName());
}
out.print("Course,Type,Load");
for (String level : levels) out.print("," + level);
out.println();
Set<String> courses = new HashSet<String>();
for (TeachingRequest.Variable request : variables()) {
if (courses.add(request.getCourse() + "," + request.getSections().get(0).getSectionType())) {
out.print(request.getCourse().getCourseName() + "," + request.getSections().get(0).getSectionType() + "," + request.getRequest().getLoad());
for (String level : levels) {
int pref = 0;
for (Preference<Attribute> p : request.getRequest().getAttributePreferences()) if (p.getTarget().getAttributeName().equals(level))
pref = p.getPreference();
out.print("," + (pref == 0 ? "" : -pref));
}
out.println();
}
}
out.flush();
out.close();
}
Aggregations