use of org.cpsolver.coursett.model.TimeLocation in project cpsolver by UniTime.
the class PriorityStudentWeights method main.
/**
* Test case -- run to see the weights for a few courses
* @param args program arguments
*/
public static void main(String[] args) {
PriorityStudentWeights pw = new PriorityStudentWeights(new DataProperties());
DecimalFormat df = new DecimalFormat("0.0000");
Student s = new Student(0l);
new CourseRequest(1l, 0, false, s, ToolBox.toList(new Course(1, "A", "1", new Offering(0, "A")), new Course(1, "A", "2", new Offering(0, "A")), new Course(1, "A", "3", new Offering(0, "A"))), false, null);
new CourseRequest(2l, 1, false, s, ToolBox.toList(new Course(1, "B", "1", new Offering(0, "B")), new Course(1, "B", "2", new Offering(0, "B")), new Course(1, "B", "3", new Offering(0, "B"))), false, null);
new CourseRequest(3l, 2, false, s, ToolBox.toList(new Course(1, "C", "1", new Offering(0, "C")), new Course(1, "C", "2", new Offering(0, "C")), new Course(1, "C", "3", new Offering(0, "C"))), false, null);
new CourseRequest(4l, 3, false, s, ToolBox.toList(new Course(1, "D", "1", new Offering(0, "D")), new Course(1, "D", "2", new Offering(0, "D")), new Course(1, "D", "3", new Offering(0, "D"))), false, null);
new CourseRequest(5l, 4, false, s, ToolBox.toList(new Course(1, "E", "1", new Offering(0, "E")), new Course(1, "E", "2", new Offering(0, "E")), new Course(1, "E", "3", new Offering(0, "E"))), false, null);
new CourseRequest(6l, 5, true, s, ToolBox.toList(new Course(1, "F", "1", new Offering(0, "F")), new Course(1, "F", "2", new Offering(0, "F")), new Course(1, "F", "3", new Offering(0, "F"))), false, null);
new CourseRequest(7l, 6, true, s, ToolBox.toList(new Course(1, "G", "1", new Offering(0, "G")), new Course(1, "G", "2", new Offering(0, "G")), new Course(1, "G", "3", new Offering(0, "G"))), false, null);
Assignment<Request, Enrollment> assignment = new DefaultSingleAssignment<Request, Enrollment>();
Placement p = new Placement(null, new TimeLocation(1, 90, 12, 0, 0, null, null, new BitSet(), 10), new ArrayList<RoomLocation>());
for (Request r : s.getRequests()) {
CourseRequest cr = (CourseRequest) r;
double[] w = new double[] { 0.0, 0.0, 0.0 };
for (int i = 0; i < cr.getCourses().size(); i++) {
Config cfg = new Config(0l, -1, "", cr.getCourses().get(i).getOffering());
Set<SctAssignment> sections = new HashSet<SctAssignment>();
sections.add(new Section(0, 1, "x", new Subpart(0, "Lec", "Lec", cfg, null), p, null));
Enrollment e = new Enrollment(cr, i, cfg, sections, assignment);
w[i] = pw.getWeight(assignment, e, null, null);
}
System.out.println(cr + ": " + df.format(w[0]) + " " + df.format(w[1]) + " " + df.format(w[2]));
}
System.out.println("With one distance conflict:");
for (Request r : s.getRequests()) {
CourseRequest cr = (CourseRequest) r;
double[] w = new double[] { 0.0, 0.0, 0.0 };
for (int i = 0; i < cr.getCourses().size(); i++) {
Config cfg = new Config(0l, -1, "", cr.getCourses().get(i).getOffering());
Set<SctAssignment> sections = new HashSet<SctAssignment>();
sections.add(new Section(0, 1, "x", new Subpart(0, "Lec", "Lec", cfg, null), p, null));
Enrollment e = new Enrollment(cr, i, cfg, sections, assignment);
Set<DistanceConflict.Conflict> dc = new HashSet<DistanceConflict.Conflict>();
dc.add(new DistanceConflict.Conflict(s, e, (Section) sections.iterator().next(), e, (Section) sections.iterator().next()));
w[i] = pw.getWeight(assignment, e, dc, null);
}
System.out.println(cr + ": " + df.format(w[0]) + " " + df.format(w[1]) + " " + df.format(w[2]));
}
System.out.println("With two distance conflicts:");
for (Request r : s.getRequests()) {
CourseRequest cr = (CourseRequest) r;
double[] w = new double[] { 0.0, 0.0, 0.0 };
for (int i = 0; i < cr.getCourses().size(); i++) {
Config cfg = new Config(0l, -1, "", cr.getCourses().get(i).getOffering());
Set<SctAssignment> sections = new HashSet<SctAssignment>();
sections.add(new Section(0, 1, "x", new Subpart(0, "Lec", "Lec", cfg, null), p, null));
Enrollment e = new Enrollment(cr, i, cfg, sections, assignment);
Set<DistanceConflict.Conflict> dc = new HashSet<DistanceConflict.Conflict>();
dc.add(new DistanceConflict.Conflict(s, e, (Section) sections.iterator().next(), e, (Section) sections.iterator().next()));
dc.add(new DistanceConflict.Conflict(s, e, (Section) sections.iterator().next(), e, new Section(1, 1, "x", new Subpart(0, "Lec", "Lec", cfg, null), p, null)));
w[i] = pw.getWeight(assignment, e, dc, null);
}
System.out.println(cr + ": " + df.format(w[0]) + " " + df.format(w[1]) + " " + df.format(w[2]));
}
System.out.println("With 25% time overlapping conflict:");
for (Request r : s.getRequests()) {
CourseRequest cr = (CourseRequest) r;
double[] w = new double[] { 0.0, 0.0, 0.0 };
for (int i = 0; i < cr.getCourses().size(); i++) {
Config cfg = new Config(0l, -1, "", cr.getCourses().get(i).getOffering());
Set<SctAssignment> sections = new HashSet<SctAssignment>();
sections.add(new Section(0, 1, "x", new Subpart(0, "Lec", "Lec", cfg, null), p, null));
Enrollment e = new Enrollment(cr, i, cfg, sections, assignment);
Set<TimeOverlapsCounter.Conflict> toc = new HashSet<TimeOverlapsCounter.Conflict>();
toc.add(new TimeOverlapsCounter.Conflict(s, 3, e, sections.iterator().next(), e, sections.iterator().next()));
w[i] = pw.getWeight(assignment, e, null, toc);
}
System.out.println(cr + ": " + df.format(w[0]) + " " + df.format(w[1]) + " " + df.format(w[2]));
}
System.out.println("Disbalanced sections (by 2 / 10 students):");
for (Request r : s.getRequests()) {
CourseRequest cr = (CourseRequest) r;
double[] w = new double[] { 0.0, 0.0, 0.0 };
for (int i = 0; i < cr.getCourses().size(); i++) {
Config cfg = new Config(0l, -1, "", cr.getCourses().get(i).getOffering());
Set<SctAssignment> sections = new HashSet<SctAssignment>();
Subpart x = new Subpart(0, "Lec", "Lec", cfg, null);
Section a = new Section(0, 10, "x", x, p, null);
new Section(1, 10, "y", x, p, null);
sections.add(a);
a.assigned(assignment, new Enrollment(s.getRequests().get(0), i, cfg, sections, assignment));
a.assigned(assignment, new Enrollment(s.getRequests().get(0), i, cfg, sections, assignment));
cfg.getContext(assignment).assigned(assignment, new Enrollment(s.getRequests().get(0), i, cfg, sections, assignment));
cfg.getContext(assignment).assigned(assignment, new Enrollment(s.getRequests().get(0), i, cfg, sections, assignment));
Enrollment e = new Enrollment(cr, i, cfg, sections, assignment);
w[i] = pw.getWeight(assignment, e, null, null);
}
System.out.println(cr + ": " + df.format(w[0]) + " " + df.format(w[1]) + " " + df.format(w[2]));
}
System.out.println("Same choice sections:");
pw.iMPP = true;
for (Request r : s.getRequests()) {
CourseRequest cr = (CourseRequest) r;
double[] w = new double[] { 0.0, 0.0, 0.0 };
for (int i = 0; i < cr.getCourses().size(); i++) {
Config cfg = new Config(0l, -1, "", cr.getCourses().get(i).getOffering());
Set<SctAssignment> sections = new HashSet<SctAssignment>();
sections.add(new Section(0, 1, "x", new Subpart(0, "Lec", "Lec", cfg, null), p, null));
Enrollment e = new Enrollment(cr, i, cfg, sections, assignment);
Set<SctAssignment> other = new HashSet<SctAssignment>();
other.add(new Section(1, 1, "x", new Subpart(0, "Lec", "Lec", cfg, null), p, null));
cr.setInitialAssignment(new Enrollment(cr, i, cfg, other, assignment));
w[i] = pw.getWeight(assignment, e, null, null);
}
System.out.println(cr + ": " + df.format(w[0]) + " " + df.format(w[1]) + " " + df.format(w[2]));
}
System.out.println("Same time sections:");
for (Request r : s.getRequests()) {
CourseRequest cr = (CourseRequest) r;
double[] w = new double[] { 0.0, 0.0, 0.0 };
for (int i = 0; i < cr.getCourses().size(); i++) {
Config cfg = new Config(0l, -1, "", cr.getCourses().get(i).getOffering());
Set<SctAssignment> sections = new HashSet<SctAssignment>();
sections.add(new Section(0, 1, "x", new Subpart(0, "Lec", "Lec", cfg, null), p, null));
Enrollment e = new Enrollment(cr, i, cfg, sections, assignment);
Set<SctAssignment> other = new HashSet<SctAssignment>();
other.add(new Section(1, 1, "x", new Subpart(0, "Lec", "Lec", cfg, null), p, null, new Instructor(1l, null, "Josef Novak", null)));
cr.setInitialAssignment(new Enrollment(cr, i, cfg, other, assignment));
w[i] = pw.getWeight(assignment, e, null, null);
}
System.out.println(cr + ": " + df.format(w[0]) + " " + df.format(w[1]) + " " + df.format(w[2]));
}
System.out.println("Different time sections:");
Placement q = new Placement(null, new TimeLocation(1, 102, 12, 0, 0, null, null, new BitSet(), 10), new ArrayList<RoomLocation>());
for (Request r : s.getRequests()) {
CourseRequest cr = (CourseRequest) r;
double[] w = new double[] { 0.0, 0.0, 0.0 };
for (int i = 0; i < cr.getCourses().size(); i++) {
Config cfg = new Config(0l, -1, "", cr.getCourses().get(i).getOffering());
Set<SctAssignment> sections = new HashSet<SctAssignment>();
sections.add(new Section(0, 1, "x", new Subpart(0, "Lec", "Lec", cfg, null), p, null));
Enrollment e = new Enrollment(cr, i, cfg, sections, assignment);
Set<SctAssignment> other = new HashSet<SctAssignment>();
other.add(new Section(1, 1, "x", new Subpart(0, "Lec", "Lec", cfg, null), q, null));
cr.setInitialAssignment(new Enrollment(cr, i, cfg, other, assignment));
w[i] = pw.getWeight(assignment, e, null, null);
}
System.out.println(cr + ": " + df.format(w[0]) + " " + df.format(w[1]) + " " + df.format(w[2]));
}
System.out.println("Two sections, one same choice, one same time:");
for (Request r : s.getRequests()) {
CourseRequest cr = (CourseRequest) r;
double[] w = new double[] { 0.0, 0.0, 0.0 };
for (int i = 0; i < cr.getCourses().size(); i++) {
Config cfg = new Config(0l, -1, "", cr.getCourses().get(i).getOffering());
Set<SctAssignment> sections = new HashSet<SctAssignment>();
sections.add(new Section(0, 1, "x", new Subpart(0, "Lec", "Lec", cfg, null), p, null));
sections.add(new Section(1, 1, "y", new Subpart(1, "Rec", "Rec", cfg, null), p, null));
Enrollment e = new Enrollment(cr, i, cfg, sections, assignment);
Set<SctAssignment> other = new HashSet<SctAssignment>();
other.add(new Section(2, 1, "x", new Subpart(0, "Lec", "Lec", cfg, null), p, null));
other.add(new Section(3, 1, "y", new Subpart(1, "Rec", "Rec", cfg, null), p, null, new Instructor(1l, null, "Josef Novak", null)));
cr.setInitialAssignment(new Enrollment(cr, i, cfg, other, assignment));
w[i] = pw.getWeight(assignment, e, null, null);
}
System.out.println(cr + ": " + df.format(w[0]) + " " + df.format(w[1]) + " " + df.format(w[2]));
}
}
use of org.cpsolver.coursett.model.TimeLocation in project cpsolver by UniTime.
the class BrokenTimePatterns method countUselessSlotsBrokenTimePatterns.
/** Number of broken time patterns for this room
* @param rc room constraint
* @param placement placement that is being considered
* @return number of broken time patterns caused by the given placement
**/
protected static int countUselessSlotsBrokenTimePatterns(RoomConstraintContext rc, Placement placement) {
int ret = 0;
TimeLocation time = placement.getTimeLocation();
int slot = time.getStartSlot() % Constants.SLOTS_PER_DAY;
int days = time.getDayCode();
if ((days & sDaysMWF) != 0 && (days & sDaysMWF) != sDaysMWF) {
for (int s = slot; s < slot + time.getLength(); s++) {
int d = (days & sDaysMWF);
if (d == Constants.DAY_CODES[0] && isEmpty(rc, s, 0, placement)) {
if (isEmpty(rc, s, 2, placement) != isEmpty(rc, s, 4, placement))
ret++;
if (!isEmpty(rc, s, 2, placement) && !isEmpty(rc, s, 4, placement))
ret--;
} else if (d == Constants.DAY_CODES[2] && isEmpty(rc, s, 2, placement)) {
if (isEmpty(rc, s, 0, placement) != isEmpty(rc, s, 4, placement))
ret++;
if (!isEmpty(rc, s, 0, placement) && !isEmpty(rc, s, 4, placement))
ret--;
} else if (d == Constants.DAY_CODES[4] && isEmpty(rc, s, 4, placement)) {
if (isEmpty(rc, s, 0, placement) != isEmpty(rc, s, 2, placement))
ret++;
if (!isEmpty(rc, s, 0, placement) && !isEmpty(rc, s, 2, placement))
ret--;
} else if (d == (Constants.DAY_CODES[0] | Constants.DAY_CODES[2]) && isEmpty(rc, s, 0, placement) && isEmpty(rc, s, 2, placement)) {
if (isEmpty(rc, s, 4, placement))
ret++;
} else if (d == (Constants.DAY_CODES[2] | Constants.DAY_CODES[4]) && isEmpty(rc, s, 2, placement) && isEmpty(rc, s, 4, placement)) {
if (isEmpty(rc, s, 0, placement))
ret++;
} else if (d == (Constants.DAY_CODES[0] | Constants.DAY_CODES[4]) && isEmpty(rc, s, 0, placement) && isEmpty(rc, s, 4, placement)) {
if (isEmpty(rc, s, 2, placement))
ret++;
}
}
}
if ((days & sDaysTTh) != 0 && (days & sDaysTTh) != sDaysTTh) {
for (int s = slot; s < slot + time.getLength(); s++) {
if (isEmpty(rc, s, 1, placement) && isEmpty(rc, s, 3, placement))
ret++;
int d = (days & sDaysTTh);
if (d == Constants.DAY_CODES[1] && isEmpty(rc, s, 1, placement) && !isEmpty(rc, s, 3, placement))
ret--;
if (d == Constants.DAY_CODES[3] && isEmpty(rc, s, 3, placement) && !isEmpty(rc, s, 1, placement))
ret--;
}
}
return ret;
}
use of org.cpsolver.coursett.model.TimeLocation in project cpsolver by UniTime.
the class MinimizeNumberOfUsedGroupsOfTime method estimateLimit.
public int estimateLimit() {
int nrSlotsUsed = 0;
int minSlotsUsed = 0;
boolean firstLecture = true;
for (Lecture lecture : variables()) {
boolean first = true;
int minSlotsUsedThisLecture = 0;
for (TimeLocation time : lecture.timeLocations()) {
int min = 0;
for (int i = 0; i < iGroupsOfTime.length; i++) {
if (iGroupsOfTime[i].overlap(time))
min++;
}
if (first) {
nrSlotsUsed += time.getLength() * time.getNrMeetings();
minSlotsUsedThisLecture = min;
first = false;
} else {
minSlotsUsedThisLecture = Math.min(minSlotsUsedThisLecture, min);
}
}
if (firstLecture) {
minSlotsUsed = minSlotsUsedThisLecture;
firstLecture = false;
} else {
minSlotsUsed = Math.min(minSlotsUsed, minSlotsUsedThisLecture);
}
}
return Math.max(Math.max(1, (int) Math.ceil(((double) nrSlotsUsed) / iGroupsOfTime[0].size())), minSlotsUsed);
}
use of org.cpsolver.coursett.model.TimeLocation in project cpsolver by UniTime.
the class TimeSwap method resolve.
@Override
public Double resolve(Solution<Lecture, Placement> solution, double total, long startTime, Map<Lecture, Placement> assignments, List<Placement> conflicts, int index) {
Assignment<Lecture, Placement> assignment = solution.getAssignment();
if (index == conflicts.size())
return solution.getModel().getTotalValue(assignment) - total;
Placement conflict = conflicts.get(index);
Lecture variable = conflict.variable();
List<TimeLocation> values = variable.timeLocations();
if (values.isEmpty())
return null;
int valIdx = ToolBox.random(values.size());
int attempts = 0;
for (int i = 0; i < values.size(); i++) {
TimeLocation time = values.get((i + valIdx) % values.size());
if (time.getPreference() > 50)
continue;
if (time.equals(conflict.getTimeLocation()))
continue;
Placement value = null;
if (variable.getNrRooms() == 0)
value = new Placement(variable, time, (RoomLocation) null);
else if (variable.getNrRooms() == 1)
value = new Placement(variable, time, conflict.getRoomLocation());
else
value = new Placement(variable, time, conflict.getRoomLocations());
if (!value.isValid() || solution.getModel().inConflict(assignment, value))
continue;
assignment.assign(solution.getIteration(), value);
Double v = resolve(solution, total, startTime, assignments, conflicts, 1 + index);
assignment.unassign(solution.getIteration(), variable);
attempts++;
if (v != null && (!iHC || v <= 0)) {
assignments.put(variable, value);
return v;
}
if (attempts >= iMaxAttempts || isTimeLimitReached(startTime))
break;
}
return null;
}
use of org.cpsolver.coursett.model.TimeLocation in project cpsolver by UniTime.
the class TimeChange method selectNeighbour.
@Override
public Neighbour<Lecture, Placement> selectNeighbour(Solution<Lecture, Placement> solution) {
TimetableModel model = (TimetableModel) solution.getModel();
Assignment<Lecture, Placement> assignment = solution.getAssignment();
int varIdx = ToolBox.random(model.variables().size());
for (int i = 0; i < model.variables().size(); i++) {
Lecture lecture = model.variables().get((i + varIdx) % model.variables().size());
Placement old = assignment.getValue(lecture);
if (old == null)
continue;
List<TimeLocation> values = lecture.timeLocations();
if (values.isEmpty())
continue;
int valIdx = ToolBox.random(values.size());
for (int j = 0; j < values.size(); j++) {
TimeLocation time = values.get((j + valIdx) % values.size());
if (time.getPreference() > 50)
continue;
Placement placement = null;
if (lecture.getNrRooms() == 0)
placement = new Placement(lecture, time, (RoomLocation) null);
else if (lecture.getNrRooms() == 1)
placement = new Placement(lecture, time, old.getRoomLocation());
else
placement = new Placement(lecture, time, old.getRoomLocations());
if (placement.isValid() && !model.inConflict(assignment, placement)) {
SimpleNeighbour<Lecture, Placement> n = new SimpleNeighbour<Lecture, Placement>(lecture, placement);
if (!iHC || n.value(assignment) <= 0)
return n;
}
}
}
return null;
}
Aggregations