Search in sources :

Example 36 with Placement

use of org.cpsolver.coursett.model.Placement in project cpsolver by UniTime.

the class RoomSwap method selectNeighbour.

@Override
public Neighbour<Lecture, Placement> selectNeighbour(Solution<Lecture, Placement> solution) {
    TimetableModel model = (TimetableModel) solution.getModel();
    Assignment<Lecture, Placement> assignment = solution.getAssignment();
    double total = model.getTotalValue(assignment);
    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 || old.getNrRooms() != 1)
            continue;
        List<RoomLocation> values = lecture.roomLocations();
        if (values.isEmpty())
            continue;
        Lock lock = solution.getLock().writeLock();
        lock.lock();
        try {
            int attempts = 0;
            int valIdx = ToolBox.random(values.size());
            long startTime = JProf.currentTimeMillis();
            for (int j = 0; j < values.size(); j++) {
                RoomLocation room = values.get((j + valIdx) % values.size());
                if (room.getPreference() > 50)
                    continue;
                if (room.equals(old.getRoomLocation()))
                    continue;
                Placement placement = new Placement(lecture, old.getTimeLocation(), room);
                if (!placement.isValid())
                    continue;
                Set<Placement> conflicts = model.conflictValues(assignment, placement);
                if (conflicts.contains(placement))
                    continue;
                if (conflicts.isEmpty()) {
                    SimpleNeighbour<Lecture, Placement> n = new SimpleNeighbour<Lecture, Placement>(lecture, placement);
                    if (!iHC || n.value(assignment) <= 0)
                        return n;
                    else
                        continue;
                }
                Map<Lecture, Placement> assignments = new HashMap<Lecture, Placement>();
                assignments.put(lecture, placement);
                for (Placement conflict : conflicts) assignment.unassign(solution.getIteration(), conflict.variable());
                assignment.assign(solution.getIteration(), placement);
                Double v = resolve(solution, total, startTime, assignments, new ArrayList<Placement>(conflicts), 0);
                if (!conflicts.isEmpty())
                    attempts++;
                assignment.unassign(solution.getIteration(), lecture);
                for (Placement conflict : conflicts) assignment.assign(solution.getIteration(), conflict);
                assignment.assign(solution.getIteration(), old);
                if (v != null)
                    return new SwapNeighbour(assignments.values(), v);
                if (attempts >= iMaxAttempts)
                    break;
            }
        } finally {
            lock.unlock();
        }
    }
    return null;
}
Also used : Lecture(org.cpsolver.coursett.model.Lecture) RoomLocation(org.cpsolver.coursett.model.RoomLocation) HashMap(java.util.HashMap) Lock(java.util.concurrent.locks.Lock) Placement(org.cpsolver.coursett.model.Placement) SimpleNeighbour(org.cpsolver.ifs.model.SimpleNeighbour) TimetableModel(org.cpsolver.coursett.model.TimetableModel)

Example 37 with Placement

use of org.cpsolver.coursett.model.Placement in project cpsolver by UniTime.

the class TimeSwap method selectNeighbour.

@Override
public Neighbour<Lecture, Placement> selectNeighbour(Solution<Lecture, Placement> solution) {
    TimetableModel model = (TimetableModel) solution.getModel();
    Assignment<Lecture, Placement> assignment = solution.getAssignment();
    double total = model.getTotalValue(assignment);
    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 = lecture.getAssignment(assignment);
        if (old == null)
            continue;
        List<TimeLocation> values = lecture.timeLocations();
        if (values.isEmpty())
            continue;
        Lock lock = solution.getLock().writeLock();
        lock.lock();
        try {
            int attempts = 0;
            long startTime = JProf.currentTimeMillis();
            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;
                if (time.equals(old.getTimeLocation()))
                    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())
                    continue;
                Set<Placement> conflicts = model.conflictValues(assignment, placement);
                if (conflicts.contains(placement))
                    continue;
                if (conflicts.isEmpty()) {
                    SimpleNeighbour<Lecture, Placement> n = new SimpleNeighbour<Lecture, Placement>(lecture, placement);
                    if (!iHC || n.value(assignment) <= 0)
                        return n;
                    else
                        continue;
                }
                Map<Lecture, Placement> assignments = new HashMap<Lecture, Placement>();
                assignments.put(lecture, placement);
                for (Placement conflict : conflicts) assignment.unassign(solution.getIteration(), conflict.variable());
                assignment.assign(solution.getIteration(), placement);
                Double v = resolve(solution, total, startTime, assignments, new ArrayList<Placement>(conflicts), 0);
                if (!conflicts.isEmpty())
                    attempts++;
                assignment.unassign(solution.getIteration(), lecture);
                for (Placement conflict : conflicts) assignment.assign(solution.getIteration(), conflict);
                assignment.assign(solution.getIteration(), old);
                if (v != null)
                    return new SwapNeighbour(assignments.values(), v);
                if (attempts >= iMaxAttempts)
                    break;
            }
        } finally {
            lock.unlock();
        }
    }
    return null;
}
Also used : Lecture(org.cpsolver.coursett.model.Lecture) TimeLocation(org.cpsolver.coursett.model.TimeLocation) HashMap(java.util.HashMap) Lock(java.util.concurrent.locks.Lock) Placement(org.cpsolver.coursett.model.Placement) SimpleNeighbour(org.cpsolver.ifs.model.SimpleNeighbour) TimetableModel(org.cpsolver.coursett.model.TimetableModel)

Example 38 with Placement

use of org.cpsolver.coursett.model.Placement in project cpsolver by UniTime.

the class EqualStudentWeights method main.

/**
     * Test case -- run to see the weights for a few courses
     * @param args program arguments
     */
public static void main(String[] args) {
    EqualStudentWeights pw = new EqualStudentWeights(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(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 sections:");
    pw.iMPP = true;
    for (Request r : s.getRequests()) {
        CourseRequest cr = (CourseRequest) r;
        double[] w = new double[] { 0.0, 0.0, 0.0 };
        double dif = 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);
            cr.setInitialAssignment(new Enrollment(cr, i, cfg, sections, assignment));
            w[i] = pw.getWeight(assignment, e, null, null);
            dif = pw.getDifference(e);
        }
        System.out.println(cr + ": " + df.format(w[0]) + "  " + df.format(w[1]) + "  " + df.format(w[2]) + " (" + df.format(dif) + ")");
    }
    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 };
        double dif = 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);
            dif = pw.getDifference(e);
        }
        System.out.println(cr + ": " + df.format(w[0]) + "  " + df.format(w[1]) + "  " + df.format(w[2]) + " (" + df.format(dif) + ")");
    }
    System.out.println("Same time sections:");
    for (Request r : s.getRequests()) {
        CourseRequest cr = (CourseRequest) r;
        double dif = 0;
        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(1, null, "Josef Novak", null)));
            cr.setInitialAssignment(new Enrollment(cr, i, cfg, other, assignment));
            w[i] = pw.getWeight(assignment, e, null, null);
            dif = pw.getDifference(e);
        }
        System.out.println(cr + ": " + df.format(w[0]) + "  " + df.format(w[1]) + "  " + df.format(w[2]) + " (" + df.format(dif) + ")");
    }
    System.out.println("Same configuration sections:");
    for (Request r : s.getRequests()) {
        CourseRequest cr = (CourseRequest) r;
        double[] w = new double[] { 0.0, 0.0, 0.0 };
        double dif = 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);
            cr.getSelectedChoices().add(new Choice(cfg));
            cr.setInitialAssignment(null);
            w[i] = pw.getWeight(assignment, e, null, null);
            dif = pw.getDifference(e);
        }
        System.out.println(cr + ": " + df.format(w[0]) + "  " + df.format(w[1]) + "  " + df.format(w[2]) + " (" + df.format(dif) + ")");
    }
    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 };
        double dif = 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);
            dif = pw.getDifference(e);
        }
        System.out.println(cr + ": " + df.format(w[0]) + "  " + df.format(w[1]) + "  " + df.format(w[2]) + " (" + df.format(dif) + ")");
    }
    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 };
        double dif = 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(1, null, "Josef Novak", null)));
            cr.setInitialAssignment(new Enrollment(cr, i, cfg, other, assignment));
            w[i] = pw.getWeight(assignment, e, null, null);
            dif = pw.getDifference(e);
        }
        System.out.println(cr + ": " + df.format(w[0]) + "  " + df.format(w[1]) + "  " + df.format(w[2]) + " (" + df.format(dif) + ")");
    }
}
Also used : Choice(org.cpsolver.studentsct.model.Choice) TimeLocation(org.cpsolver.coursett.model.TimeLocation) Config(org.cpsolver.studentsct.model.Config) DecimalFormat(java.text.DecimalFormat) DistanceConflict(org.cpsolver.studentsct.extension.DistanceConflict) Instructor(org.cpsolver.studentsct.model.Instructor) DataProperties(org.cpsolver.ifs.util.DataProperties) Placement(org.cpsolver.coursett.model.Placement) Enrollment(org.cpsolver.studentsct.model.Enrollment) Course(org.cpsolver.studentsct.model.Course) HashSet(java.util.HashSet) RoomLocation(org.cpsolver.coursett.model.RoomLocation) Request(org.cpsolver.studentsct.model.Request) CourseRequest(org.cpsolver.studentsct.model.CourseRequest) BitSet(java.util.BitSet) Student(org.cpsolver.studentsct.model.Student) Offering(org.cpsolver.studentsct.model.Offering) Section(org.cpsolver.studentsct.model.Section) TimeOverlapsCounter(org.cpsolver.studentsct.extension.TimeOverlapsCounter) CourseRequest(org.cpsolver.studentsct.model.CourseRequest) DistanceConflict(org.cpsolver.studentsct.extension.DistanceConflict) Subpart(org.cpsolver.studentsct.model.Subpart) DefaultSingleAssignment(org.cpsolver.ifs.assignment.DefaultSingleAssignment) SctAssignment(org.cpsolver.studentsct.model.SctAssignment)

Example 39 with Placement

use of org.cpsolver.coursett.model.Placement in project cpsolver by UniTime.

the class NeighbourSelectionWithSuggestions method selectNeighbourWithSuggestions.

public Neighbour<Lecture, Placement> selectNeighbourWithSuggestions(Solution<Lecture, Placement> solution, Lecture lecture, int depth) {
    if (lecture == null)
        return null;
    NeighbourSelectionWithSuggestionsContext context = new NeighbourSelectionWithSuggestionsContext(solution);
    Lock lock = solution.getLock().writeLock();
    lock.lock();
    try {
        // System.out.println("BEFORE BT ("+lecture.getName()+"): nrAssigned="+iSolution.getModel().assignedVariables().size()+",  value="+iCmp.currentValue(iSolution));
        List<Lecture> initialLectures = new ArrayList<Lecture>(1);
        initialLectures.add(lecture);
        backtrack(context, initialLectures, new HashMap<Lecture, Placement>(), new HashMap<Lecture, Placement>(), depth);
    // System.out.println("AFTER  BT ("+lecture.getName()+"): nrAssigned="+iSolution.getModel().assignedVariables().size()+",  value="+iCmp.currentValue(iSolution));
    } finally {
        lock.unlock();
    }
    return context.getSuggestionNeighbour();
}
Also used : Lecture(org.cpsolver.coursett.model.Lecture) Placement(org.cpsolver.coursett.model.Placement) ArrayList(java.util.ArrayList) Lock(java.util.concurrent.locks.Lock)

Example 40 with Placement

use of org.cpsolver.coursett.model.Placement in project cpsolver by UniTime.

the class PlacementSelection method selectValue.

@Override
public Placement selectValue(Solution<Lecture, Placement> solution, Lecture var) {
    if (var == null)
        return null;
    Lecture selectedVariable = var;
    TimetableModel model = (TimetableModel) solution.getModel();
    Assignment<Lecture, Placement> assignment = solution.getAssignment();
    if (selectedVariable.getInitialAssignment() != null) {
        if (iMPPLimit >= 0 && model.perturbVariables(assignment).size() >= iMPPLimit) {
            if (!containsItselfSingletonOrCommited(model, model.conflictValues(assignment, selectedVariable.getInitialAssignment()), selectedVariable.getInitialAssignment()))
                return selectedVariable.getInitialAssignment();
        } else if (iMPPPenaltyLimit >= 0.0 && solution.getPerturbationsCounter() != null && solution.getPerturbationsCounter().getPerturbationPenalty(assignment, model) > iMPPPenaltyLimit) {
            if (!containsItselfSingletonOrCommited(model, model.conflictValues(assignment, selectedVariable.getInitialAssignment()), selectedVariable.getInitialAssignment()))
                return selectedVariable.getInitialAssignment();
        } else if (selectedVariable.getInitialAssignment() != null && ToolBox.random() <= iInitialSelectionProb) {
            if (!containsItselfSingletonOrCommited(model, model.conflictValues(assignment, selectedVariable.getInitialAssignment()), selectedVariable.getInitialAssignment()))
                return selectedVariable.getInitialAssignment();
        }
    }
    List<Placement> values = selectedVariable.values(solution.getAssignment());
    if (iRW && ToolBox.random() <= iRandomWalkProb) {
        for (int i = 0; i < 5; i++) {
            Placement ret = ToolBox.random(values);
            if (!containsItselfSingletonOrCommited(model, model.conflictValues(assignment, ret), ret))
                return ret;
        }
    }
    Placement current = assignment.getValue(selectedVariable);
    if (iProp != null && current == null && ToolBox.random() <= iGoodSelectionProb) {
        Collection<Placement> goodValues = iProp.goodValues(assignment, selectedVariable);
        if (!goodValues.isEmpty())
            values = new ArrayList<Placement>(goodValues);
    }
    if (values.size() == 1) {
        Placement ret = values.get(0);
        if (!containsItselfSingletonOrCommited(model, model.conflictValues(assignment, ret), ret))
            return ret;
    }
    long[] bestCost = new long[NR_LEVELS];
    List<Placement> selectionValues = null;
    HeuristicSelector<Placement> selector = (iUseThreshold ? new HeuristicSelector<Placement>(iThresholdKoef) : null);
    for (Placement value : values) {
        if (iTabu != null && iTabu.contains(value))
            continue;
        if (current != null && current.equals(value))
            continue;
        Set<Placement> conflicts = value.variable().getModel().conflictValues(assignment, value);
        if (containsItselfSingletonOrCommited(model, conflicts, value))
            continue;
        if (iUseThreshold) {
            Double flt = selector.firstLevelThreshold();
            double[] costs = new double[NR_LEVELS];
            for (int level = 0; level < NR_LEVELS; level++) {
                costs[level] = getCost(assignment, level, value, conflicts);
                if (level == 0 && flt != null && costs[0] > flt.doubleValue()) {
                    break;
                }
            }
            if (flt != null && costs[0] > flt.doubleValue())
                continue;
            selector.add(costs, value);
        } else {
            boolean fail = false;
            boolean best = false;
            for (int level = 0; !fail && level < 1; level++) {
                double val = getCost(assignment, level, value, conflicts);
                long cost = Math.round(PRECISION * val);
                if (selectionValues != null && !best) {
                    if (cost > bestCost[level]) {
                        fail = true;
                    }
                    if (cost < bestCost[level]) {
                        bestCost[level] = cost;
                        selectionValues.clear();
                        best = true;
                    }
                } else {
                    bestCost[level] = cost;
                }
            }
            if (selectionValues == null)
                selectionValues = new ArrayList<Placement>(values.size());
            if (!fail)
                selectionValues.add(value);
        }
    }
    // ToolBox.print("Best "+selectionValues.size()+" locations for variable "+selectedVariable.getId()+" have "+bestConflicts+" conflicts ("+bestRemovals+" weighted) and "+bestStudentConflicts+" ("+bestOriginalStudentConflicts+" * "+bestKoef+" + "+bestPenalty+") preference.");
    Placement selectedValue = null;
    if (iUseThreshold) {
        List<HeuristicSelector<Placement>.Element<Placement>> selectionElements = selector.selection();
        if (selectedVariable.getInitialAssignment() != null) {
            for (HeuristicSelector<Placement>.Element<Placement> element : selectionElements) {
                Placement value = element.getObject();
                if (value.equals(selectedVariable.getInitialAssignment())) {
                    selectedValue = value;
                    break;
                }
            }
        // &&
        // selectionValues.contains(selectedVariable.getInitialAssignment()))
        // return selectedVariable.getInitialAssignment();
        }
        if (selectedValue == null) {
            HeuristicSelector<Placement>.Element<Placement> selection = ToolBox.random(selectionElements);
            selectedValue = (selection == null ? null : selection.getObject());
        }
    } else {
        if (selectedVariable.getInitialAssignment() != null && selectionValues.contains(selectedVariable.getInitialAssignment()))
            return selectedVariable.getInitialAssignment();
        selectedValue = ToolBox.random(selectionValues);
    }
    if (selectedValue != null && iTabu != null) {
        if (iTabu.size() == iTabuPos)
            iTabu.add(selectedValue);
        else
            iTabu.set(iTabuPos, selectedValue);
        iTabuPos = (iTabuPos + 1) % iTabuSize;
    }
    return selectedValue;
}
Also used : Lecture(org.cpsolver.coursett.model.Lecture) ArrayList(java.util.ArrayList) Placement(org.cpsolver.coursett.model.Placement) TimetableModel(org.cpsolver.coursett.model.TimetableModel)

Aggregations

Placement (org.cpsolver.coursett.model.Placement)72 Lecture (org.cpsolver.coursett.model.Lecture)55 HashSet (java.util.HashSet)24 TimetableModel (org.cpsolver.coursett.model.TimetableModel)18 TimeLocation (org.cpsolver.coursett.model.TimeLocation)17 WeakeningConstraint (org.cpsolver.ifs.model.WeakeningConstraint)17 ArrayList (java.util.ArrayList)16 BitSet (java.util.BitSet)16 Constraint (org.cpsolver.ifs.model.Constraint)15 HashMap (java.util.HashMap)14 RoomLocation (org.cpsolver.coursett.model.RoomLocation)13 InstructorConstraint (org.cpsolver.coursett.constraint.InstructorConstraint)9 GlobalConstraint (org.cpsolver.ifs.model.GlobalConstraint)9 Student (org.cpsolver.coursett.model.Student)7 List (java.util.List)6 RoomConstraint (org.cpsolver.coursett.constraint.RoomConstraint)6 DataProperties (org.cpsolver.ifs.util.DataProperties)6 GroupConstraint (org.cpsolver.coursett.constraint.GroupConstraint)5 JenrlConstraint (org.cpsolver.coursett.constraint.JenrlConstraint)5 SpreadConstraint (org.cpsolver.coursett.constraint.SpreadConstraint)5