Search in sources :

Example 1 with FlexibleConstraint

use of org.cpsolver.coursett.constraint.FlexibleConstraint in project cpsolver by UniTime.

the class TimetableXMLSaver method doSave.

protected void doSave(Element root) {
    root.addAttribute("version", "2.5");
    root.addAttribute("initiative", getModel().getProperties().getProperty("Data.Initiative"));
    root.addAttribute("term", getModel().getProperties().getProperty("Data.Term"));
    root.addAttribute("year", String.valueOf(getModel().getYear()));
    root.addAttribute("created", String.valueOf(new Date()));
    root.addAttribute("nrDays", String.valueOf(Constants.DAY_CODES.length));
    root.addAttribute("slotsPerDay", String.valueOf(Constants.SLOTS_PER_DAY));
    if (!iConvertIds && getModel().getProperties().getProperty("General.SessionId") != null)
        root.addAttribute("session", getModel().getProperties().getProperty("General.SessionId"));
    if (iShowNames && !iConvertIds && getModel().getProperties().getProperty("General.SolverGroupId") != null)
        root.addAttribute("solverGroup", getId("solverGroup", getModel().getProperties().getProperty("General.SolverGroupId")));
    HashMap<String, Element> roomElements = new HashMap<String, Element>();
    Element roomsEl = root.addElement("rooms");
    for (RoomConstraint roomConstraint : getModel().getRoomConstraints()) {
        Element roomEl = roomsEl.addElement("room").addAttribute("id", getId("room", roomConstraint.getResourceId()));
        roomEl.addAttribute("constraint", "true");
        if (roomConstraint instanceof DiscouragedRoomConstraint)
            roomEl.addAttribute("discouraged", "true");
        if (iShowNames) {
            roomEl.addAttribute("name", roomConstraint.getRoomName());
        }
        if (!iConvertIds && roomConstraint.getBuildingId() != null)
            roomEl.addAttribute("building", getId("bldg", roomConstraint.getBuildingId()));
        roomElements.put(getId("room", roomConstraint.getResourceId()), roomEl);
        roomEl.addAttribute("capacity", String.valueOf(roomConstraint.getCapacity()));
        if (roomConstraint.getPosX() != null && roomConstraint.getPosY() != null)
            roomEl.addAttribute("location", roomConstraint.getPosX() + "," + roomConstraint.getPosY());
        if (roomConstraint.getIgnoreTooFar())
            roomEl.addAttribute("ignoreTooFar", "true");
        if (!roomConstraint.getConstraint())
            roomEl.addAttribute("fake", "true");
        if (roomConstraint.getSharingModel() != null) {
            RoomSharingModel sharingModel = roomConstraint.getSharingModel();
            Element sharingEl = roomEl.addElement("sharing");
            sharingEl.addElement("pattern").addAttribute("unit", String.valueOf(sharingModel.getStep())).setText(sharingModel.getPreferences());
            sharingEl.addElement("freeForAll").addAttribute("value", String.valueOf(sharingModel.getFreeForAllPrefChar()));
            sharingEl.addElement("notAvailable").addAttribute("value", String.valueOf(sharingModel.getNotAvailablePrefChar()));
            for (Long id : sharingModel.getDepartmentIds()) {
                sharingEl.addElement("department").addAttribute("value", String.valueOf(sharingModel.getCharacter(id))).addAttribute("id", getId("dept", id));
            }
        }
        if (roomConstraint.getType() != null && iShowNames)
            roomEl.addAttribute("type", roomConstraint.getType().toString());
        Map<Long, Integer> travelTimes = getModel().getDistanceMetric().getTravelTimes().get(roomConstraint.getResourceId());
        if (travelTimes != null)
            for (Map.Entry<Long, Integer> time : travelTimes.entrySet()) roomEl.addElement("travel-time").addAttribute("id", getId("room", time.getKey())).addAttribute("minutes", time.getValue().toString());
    }
    Element instructorsEl = root.addElement("instructors");
    Element departmentsEl = root.addElement("departments");
    HashMap<Long, String> depts = new HashMap<Long, String>();
    Element configsEl = (iShowNames ? root.addElement("configurations") : null);
    HashSet<Configuration> configs = new HashSet<Configuration>();
    Element classesEl = root.addElement("classes");
    HashMap<Long, Element> classElements = new HashMap<Long, Element>();
    List<Lecture> vars = new ArrayList<Lecture>(getModel().variables());
    if (getModel().hasConstantVariables())
        vars.addAll(getModel().constantVariables());
    for (Lecture lecture : vars) {
        Placement placement = getAssignment().getValue(lecture);
        if (lecture.isCommitted() && placement == null)
            placement = lecture.getInitialAssignment();
        Placement initialPlacement = lecture.getInitialAssignment();
        // if (initialPlacement==null) initialPlacement =
        // (Placement)lecture.getAssignment();
        Placement bestPlacement = lecture.getBestAssignment();
        Element classEl = classesEl.addElement("class").addAttribute("id", getId("class", lecture.getClassId()));
        classElements.put(lecture.getClassId(), classEl);
        if (iShowNames && lecture.getNote() != null)
            classEl.addAttribute("note", lecture.getNote());
        if (iShowNames && !lecture.isCommitted())
            classEl.addAttribute("ord", String.valueOf(lecture.getOrd()));
        if (lecture.getWeight() != 1.0)
            classEl.addAttribute("weight", String.valueOf(lecture.getWeight()));
        if (iShowNames && lecture.getSolverGroupId() != null)
            classEl.addAttribute("solverGroup", getId("solverGroup", lecture.getSolverGroupId()));
        if (lecture.getParent() == null && lecture.getConfiguration() != null) {
            if (!iShowNames)
                classEl.addAttribute("offering", getId("offering", lecture.getConfiguration().getOfferingId().toString()));
            classEl.addAttribute("config", getId("config", lecture.getConfiguration().getConfigId().toString()));
            if (iShowNames && configs.add(lecture.getConfiguration())) {
                configsEl.addElement("config").addAttribute("id", getId("config", lecture.getConfiguration().getConfigId().toString())).addAttribute("limit", String.valueOf(lecture.getConfiguration().getLimit())).addAttribute("offering", getId("offering", lecture.getConfiguration().getOfferingId().toString()));
            }
        }
        classEl.addAttribute("committed", (lecture.isCommitted() ? "true" : "false"));
        if (lecture.getParent() != null)
            classEl.addAttribute("parent", getId("class", lecture.getParent().getClassId()));
        if (lecture.getSchedulingSubpartId() != null)
            classEl.addAttribute("subpart", getId("subpart", lecture.getSchedulingSubpartId()));
        if (iShowNames && lecture.isCommitted() && placement != null && placement.getAssignmentId() != null) {
            classEl.addAttribute("assignment", getId("assignment", placement.getAssignmentId()));
        }
        if (!lecture.isCommitted()) {
            if (lecture.minClassLimit() == lecture.maxClassLimit()) {
                classEl.addAttribute("classLimit", String.valueOf(lecture.maxClassLimit()));
            } else {
                classEl.addAttribute("minClassLimit", String.valueOf(lecture.minClassLimit()));
                classEl.addAttribute("maxClassLimit", String.valueOf(lecture.maxClassLimit()));
            }
            if (lecture.roomToLimitRatio() != 1.0f)
                classEl.addAttribute("roomToLimitRatio", sStudentWeightFormat.format(lecture.roomToLimitRatio()));
        }
        if (lecture.getNrRooms() != 1)
            classEl.addAttribute("nrRooms", String.valueOf(lecture.getNrRooms()));
        if (lecture.getNrRooms() > 1 && lecture.getMaxRoomCombinations() > 0)
            classEl.addAttribute("maxRoomCombinations", String.valueOf(lecture.getMaxRoomCombinations()));
        if (iShowNames)
            classEl.addAttribute("name", lecture.getName());
        if (lecture.getDeptSpreadConstraint() != null) {
            classEl.addAttribute("department", getId("dept", lecture.getDeptSpreadConstraint().getDepartmentId()));
            depts.put(lecture.getDeptSpreadConstraint().getDepartmentId(), lecture.getDeptSpreadConstraint().getName());
        }
        if (lecture.getScheduler() != null)
            classEl.addAttribute("scheduler", getId("dept", lecture.getScheduler()));
        for (InstructorConstraint ic : lecture.getInstructorConstraints()) {
            Element instrEl = classEl.addElement("instructor").addAttribute("id", getId("inst", ic.getResourceId()));
            if ((lecture.isCommitted() || iSaveCurrent) && placement != null)
                instrEl.addAttribute("solution", "true");
            if (iSaveInitial && initialPlacement != null)
                instrEl.addAttribute("initial", "true");
            if (iSaveBest && bestPlacement != null && !bestPlacement.equals(placement))
                instrEl.addAttribute("best", "true");
        }
        for (RoomLocation rl : lecture.roomLocations()) {
            Element roomLocationEl = classEl.addElement("room");
            roomLocationEl.addAttribute("id", getId("room", rl.getId()));
            roomLocationEl.addAttribute("pref", String.valueOf(rl.getPreference()));
            if ((lecture.isCommitted() || iSaveCurrent) && placement != null && placement.hasRoomLocation(rl.getId()))
                roomLocationEl.addAttribute("solution", "true");
            if (iSaveInitial && initialPlacement != null && initialPlacement.hasRoomLocation(rl.getId()))
                roomLocationEl.addAttribute("initial", "true");
            if (iSaveBest && bestPlacement != null && !bestPlacement.equals(placement) && bestPlacement.hasRoomLocation(rl.getId()))
                roomLocationEl.addAttribute("best", "true");
            if (!roomElements.containsKey(getId("room", rl.getId()))) {
                // room location without room constraint
                Element roomEl = roomsEl.addElement("room").addAttribute("id", getId("room", rl.getId()));
                roomEl.addAttribute("constraint", "false");
                if (!iConvertIds && rl.getBuildingId() != null)
                    roomEl.addAttribute("building", getId("bldg", rl.getBuildingId()));
                if (iShowNames) {
                    roomEl.addAttribute("name", rl.getName());
                }
                roomElements.put(getId("room", rl.getId()), roomEl);
                roomEl.addAttribute("capacity", String.valueOf(rl.getRoomSize()));
                if (rl.getPosX() != null && rl.getPosY() != null)
                    roomEl.addAttribute("location", rl.getPosX() + "," + rl.getPosY());
                if (rl.getIgnoreTooFar())
                    roomEl.addAttribute("ignoreTooFar", "true");
            }
        }
        boolean first = true;
        Set<Long> dp = new HashSet<Long>();
        for (TimeLocation tl : lecture.timeLocations()) {
            Element timeLocationEl = classEl.addElement("time");
            timeLocationEl.addAttribute("days", sDF[7].format(Long.parseLong(Integer.toBinaryString(tl.getDayCode()))));
            timeLocationEl.addAttribute("start", String.valueOf(tl.getStartSlot()));
            timeLocationEl.addAttribute("length", String.valueOf(tl.getLength()));
            timeLocationEl.addAttribute("breakTime", String.valueOf(tl.getBreakTime()));
            if (iShowNames) {
                timeLocationEl.addAttribute("pref", String.valueOf(tl.getPreference()));
                timeLocationEl.addAttribute("npref", String.valueOf(tl.getNormalizedPreference()));
            } else {
                timeLocationEl.addAttribute("pref", String.valueOf(tl.getNormalizedPreference()));
            }
            if (!iConvertIds && tl.getTimePatternId() != null)
                timeLocationEl.addAttribute("pattern", getId("pat", tl.getTimePatternId()));
            if (tl.getDatePatternId() != null && dp.add(tl.getDatePatternId())) {
                Element dateEl = classEl.addElement("date");
                dateEl.addAttribute("id", getId("dpat", String.valueOf(tl.getDatePatternId())));
                if (iShowNames)
                    dateEl.addAttribute("name", tl.getDatePatternName());
                dateEl.addAttribute("pattern", bitset2string(tl.getWeekCode()));
            }
            if (tl.getDatePatternPreference() != 0)
                timeLocationEl.addAttribute("datePref", String.valueOf(tl.getDatePatternPreference()));
            if (tl.getTimePatternId() == null && first) {
                if (iShowNames)
                    classEl.addAttribute("datePatternName", tl.getDatePatternName());
                classEl.addAttribute("dates", bitset2string(tl.getWeekCode()));
                first = false;
            }
            if (tl.getDatePatternId() != null) {
                timeLocationEl.addAttribute("date", getId("dpat", String.valueOf(tl.getDatePatternId())));
            }
            if ((lecture.isCommitted() || iSaveCurrent) && placement != null && placement.getTimeLocation().equals(tl))
                timeLocationEl.addAttribute("solution", "true");
            if (iSaveInitial && initialPlacement != null && initialPlacement.getTimeLocation().equals(tl))
                timeLocationEl.addAttribute("initial", "true");
            if (iSaveBest && bestPlacement != null && !bestPlacement.equals(placement) && bestPlacement.getTimeLocation().equals(tl))
                timeLocationEl.addAttribute("best", "true");
        }
    }
    for (InstructorConstraint ic : getModel().getInstructorConstraints()) {
        if (iShowNames || ic.isIgnoreDistances() || ic instanceof SoftInstructorConstraint) {
            Element instrEl = instructorsEl.addElement("instructor").addAttribute("id", getId("inst", ic.getResourceId()));
            if (iShowNames) {
                if (ic.getPuid() != null && ic.getPuid().length() > 0)
                    instrEl.addAttribute("puid", ic.getPuid());
                instrEl.addAttribute("name", ic.getName());
                if (ic.getType() != null && iShowNames)
                    instrEl.addAttribute("type", ic.getType().toString());
            }
            if (ic.isIgnoreDistances()) {
                instrEl.addAttribute("ignDist", "true");
            }
            if (ic instanceof SoftInstructorConstraint)
                instrEl.addAttribute("soft", "true");
        }
        if (ic.getUnavailabilities() != null) {
            for (Placement placement : ic.getUnavailabilities()) {
                Lecture lecture = placement.variable();
                Element classEl = classElements.get(lecture.getClassId());
                classEl.addElement("instructor").addAttribute("id", getId("inst", ic.getResourceId())).addAttribute("solution", "true");
            }
        }
    }
    if (instructorsEl.elements().isEmpty())
        root.remove(instructorsEl);
    Element grConstraintsEl = root.addElement("groupConstraints");
    for (GroupConstraint gc : getModel().getGroupConstraints()) {
        Element grEl = grConstraintsEl.addElement("constraint").addAttribute("id", getId("gr", String.valueOf(gc.getId())));
        grEl.addAttribute("type", gc.getType().reference());
        grEl.addAttribute("pref", gc.getPrologPreference());
        for (Lecture l : gc.variables()) {
            grEl.addElement("class").addAttribute("id", getId("class", l.getClassId()));
        }
    }
    for (SpreadConstraint spread : getModel().getSpreadConstraints()) {
        Element grEl = grConstraintsEl.addElement("constraint").addAttribute("id", getId("gr", String.valueOf(spread.getId())));
        grEl.addAttribute("type", "SPREAD");
        grEl.addAttribute("pref", Constants.sPreferenceRequired);
        if (iShowNames)
            grEl.addAttribute("name", spread.getName());
        for (Lecture l : spread.variables()) {
            grEl.addElement("class").addAttribute("id", getId("class", l.getClassId()));
        }
    }
    for (Constraint<Lecture, Placement> c : getModel().constraints()) {
        if (c instanceof MinimizeNumberOfUsedRoomsConstraint) {
            Element grEl = grConstraintsEl.addElement("constraint").addAttribute("id", getId("gr", String.valueOf(c.getId())));
            grEl.addAttribute("type", "MIN_ROOM_USE");
            grEl.addAttribute("pref", Constants.sPreferenceRequired);
            for (Lecture l : c.variables()) {
                grEl.addElement("class").addAttribute("id", getId("class", l.getClassId()));
            }
        }
        if (c instanceof MinimizeNumberOfUsedGroupsOfTime) {
            Element grEl = grConstraintsEl.addElement("constraint").addAttribute("id", getId("gr", String.valueOf(c.getId())));
            grEl.addAttribute("type", ((MinimizeNumberOfUsedGroupsOfTime) c).getConstraintName());
            grEl.addAttribute("pref", Constants.sPreferenceRequired);
            for (Lecture l : c.variables()) {
                grEl.addElement("class").addAttribute("id", getId("class", l.getClassId()));
            }
        }
        if (c instanceof IgnoreStudentConflictsConstraint) {
            Element grEl = grConstraintsEl.addElement("constraint").addAttribute("id", getId("gr", String.valueOf(c.getId())));
            grEl.addAttribute("type", IgnoreStudentConflictsConstraint.REFERENCE);
            grEl.addAttribute("pref", Constants.sPreferenceRequired);
            for (Lecture l : c.variables()) {
                grEl.addElement("class").addAttribute("id", getId("class", l.getClassId()));
            }
        }
    }
    for (ClassLimitConstraint clc : getModel().getClassLimitConstraints()) {
        Element grEl = grConstraintsEl.addElement("constraint").addAttribute("id", getId("gr", String.valueOf(clc.getId())));
        grEl.addAttribute("type", "CLASS_LIMIT");
        grEl.addAttribute("pref", Constants.sPreferenceRequired);
        if (clc.getParentLecture() != null) {
            grEl.addElement("parentClass").addAttribute("id", getId("class", clc.getParentLecture().getClassId()));
        } else
            grEl.addAttribute("courseLimit", String.valueOf(clc.classLimit() - clc.getClassLimitDelta()));
        if (clc.getClassLimitDelta() != 0)
            grEl.addAttribute("delta", String.valueOf(clc.getClassLimitDelta()));
        if (iShowNames)
            grEl.addAttribute("name", clc.getName());
        for (Lecture l : clc.variables()) {
            grEl.addElement("class").addAttribute("id", getId("class", l.getClassId()));
        }
    }
    for (FlexibleConstraint gc : getModel().getFlexibleConstraints()) {
        Element flEl = grConstraintsEl.addElement("constraint").addAttribute("id", getId("gr", String.valueOf(gc.getId())));
        flEl.addAttribute("reference", gc.getReference());
        flEl.addAttribute("owner", gc.getOwner());
        flEl.addAttribute("pref", gc.getPrologPreference());
        flEl.addAttribute("type", gc.getType().toString());
        for (Lecture l : gc.variables()) {
            flEl.addElement("class").addAttribute("id", getId("class", l.getClassId()));
        }
    }
    HashMap<Student, List<String>> students = new HashMap<Student, List<String>>();
    for (Lecture lecture : vars) {
        for (Student student : lecture.students()) {
            List<String> enrls = students.get(student);
            if (enrls == null) {
                enrls = new ArrayList<String>();
                students.put(student, enrls);
            }
            enrls.add(getId("class", lecture.getClassId()));
        }
    }
    Element studentsEl = root.addElement("students");
    Element groupsEl = root.addElement("groups");
    Map<StudentGroup, Element> groups = new HashMap<StudentGroup, Element>();
    for (Student student : new TreeSet<Student>(students.keySet())) {
        Element stEl = studentsEl.addElement("student").addAttribute("id", getId("student", student.getId()));
        if (iShowNames) {
            if (student.getAcademicArea() != null)
                stEl.addAttribute("area", student.getAcademicArea());
            if (student.getAcademicClassification() != null)
                stEl.addAttribute("classification", student.getAcademicClassification());
            if (student.getMajor() != null)
                stEl.addAttribute("major", student.getMajor());
            if (student.getCurriculum() != null)
                stEl.addAttribute("curriculum", student.getCurriculum());
        }
        for (Map.Entry<Long, Double> entry : student.getOfferingsMap().entrySet()) {
            Long offeringId = entry.getKey();
            Double weight = entry.getValue();
            Element offEl = stEl.addElement("offering").addAttribute("id", getId("offering", offeringId.toString()));
            if (weight.doubleValue() != 1.0)
                offEl.addAttribute("weight", sStudentWeightFormat.format(weight));
            Double priority = student.getPriority(offeringId);
            if (priority != null)
                offEl.addAttribute("priority", priority.toString());
            Long altId = student.getAlternative(offeringId);
            if (altId != null)
                offEl.addAttribute("alternative", altId.toString());
        }
        if (iExportStudentSectioning || getModel().nrUnassignedVariables(getAssignment()) == 0 || student.getOfferingsMap().isEmpty()) {
            List<String> lectures = students.get(student);
            Collections.sort(lectures);
            for (String classId : lectures) {
                stEl.addElement("class").addAttribute("id", classId);
            }
        }
        Map<Long, Set<Lecture>> canNotEnroll = student.canNotEnrollSections();
        if (canNotEnroll != null) {
            for (Set<Lecture> canNotEnrollLects : canNotEnroll.values()) {
                for (Iterator<Lecture> i3 = canNotEnrollLects.iterator(); i3.hasNext(); ) {
                    stEl.addElement("prohibited-class").addAttribute("id", getId("class", (i3.next()).getClassId()));
                }
            }
        }
        if (student.getCommitedPlacements() != null) {
            for (Placement placement : student.getCommitedPlacements()) {
                stEl.addElement("class").addAttribute("id", getId("class", placement.variable().getClassId()));
            }
        }
        if (student.getInstructor() != null)
            stEl.addAttribute("instructor", getId("inst", student.getInstructor().getResourceId()));
        for (StudentGroup group : student.getGroups()) {
            Element groupEl = groups.get(group);
            if (groupEl == null) {
                groupEl = groupsEl.addElement("group");
                groupEl.addAttribute("id", getId("group", group.getId()));
                if (group.getWeight() != 1.0)
                    groupEl.addAttribute("weight", String.valueOf(group.getWeight()));
                if (iShowNames && group.getName() != null)
                    groupEl.addAttribute("name", group.getName());
                groups.put(group, groupEl);
            }
            groupEl.addElement("student").addAttribute("id", getId("student", student.getId()));
        }
    }
    if (getModel().getProperties().getPropertyInt("MPP.GenTimePert", 0) > 0) {
        Element perturbationsEl = root.addElement("perturbations");
        int nrChanges = getModel().getProperties().getPropertyInt("MPP.GenTimePert", 0);
        List<Lecture> lectures = new ArrayList<Lecture>();
        while (lectures.size() < nrChanges) {
            Lecture lecture = ToolBox.random(getAssignment().assignedVariables());
            if (lecture.isCommitted() || lecture.timeLocations().size() <= 1 || lectures.contains(lecture))
                continue;
            Placement placement = getAssignment().getValue(lecture);
            TimeLocation tl = placement.getTimeLocation();
            perturbationsEl.addElement("class").addAttribute("id", getId("class", lecture.getClassId())).addAttribute("days", sDF[7].format(Long.parseLong(Integer.toBinaryString(tl.getDayCode())))).addAttribute("start", String.valueOf(tl.getStartSlot())).addAttribute("length", String.valueOf(tl.getLength()));
            lectures.add(lecture);
        }
    }
    for (Map.Entry<Long, String> entry : depts.entrySet()) {
        Long id = entry.getKey();
        String name = entry.getValue();
        if (iShowNames) {
            departmentsEl.addElement("department").addAttribute("id", getId("dept", id.toString())).addAttribute("name", name);
        }
    }
    if (departmentsEl.elements().isEmpty())
        root.remove(departmentsEl);
}
Also used : DiscouragedRoomConstraint(org.cpsolver.coursett.constraint.DiscouragedRoomConstraint) RoomConstraint(org.cpsolver.coursett.constraint.RoomConstraint) Lecture(org.cpsolver.coursett.model.Lecture) Configuration(org.cpsolver.coursett.model.Configuration) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SpreadConstraint(org.cpsolver.coursett.constraint.SpreadConstraint) FlexibleConstraint(org.cpsolver.coursett.constraint.FlexibleConstraint) Placement(org.cpsolver.coursett.model.Placement) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) List(java.util.List) IgnoreStudentConflictsConstraint(org.cpsolver.coursett.constraint.IgnoreStudentConflictsConstraint) HashSet(java.util.HashSet) Student(org.cpsolver.coursett.model.Student) ClassLimitConstraint(org.cpsolver.coursett.constraint.ClassLimitConstraint) GroupConstraint(org.cpsolver.coursett.constraint.GroupConstraint) HashMap(java.util.HashMap) Map(java.util.Map) MinimizeNumberOfUsedGroupsOfTime(org.cpsolver.coursett.constraint.MinimizeNumberOfUsedGroupsOfTime) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Set(java.util.Set) BitSet(java.util.BitSet) TimeLocation(org.cpsolver.coursett.model.TimeLocation) Element(org.dom4j.Element) DiscouragedRoomConstraint(org.cpsolver.coursett.constraint.DiscouragedRoomConstraint) RoomSharingModel(org.cpsolver.coursett.model.RoomSharingModel) MinimizeNumberOfUsedRoomsConstraint(org.cpsolver.coursett.constraint.MinimizeNumberOfUsedRoomsConstraint) RoomLocation(org.cpsolver.coursett.model.RoomLocation) SoftInstructorConstraint(org.cpsolver.coursett.constraint.SoftInstructorConstraint) Date(java.util.Date) InstructorConstraint(org.cpsolver.coursett.constraint.InstructorConstraint) SoftInstructorConstraint(org.cpsolver.coursett.constraint.SoftInstructorConstraint) DiscouragedRoomConstraint(org.cpsolver.coursett.constraint.DiscouragedRoomConstraint) ClassLimitConstraint(org.cpsolver.coursett.constraint.ClassLimitConstraint) IgnoreStudentConflictsConstraint(org.cpsolver.coursett.constraint.IgnoreStudentConflictsConstraint) InstructorConstraint(org.cpsolver.coursett.constraint.InstructorConstraint) SoftInstructorConstraint(org.cpsolver.coursett.constraint.SoftInstructorConstraint) MinimizeNumberOfUsedRoomsConstraint(org.cpsolver.coursett.constraint.MinimizeNumberOfUsedRoomsConstraint) SpreadConstraint(org.cpsolver.coursett.constraint.SpreadConstraint) GroupConstraint(org.cpsolver.coursett.constraint.GroupConstraint) Constraint(org.cpsolver.ifs.model.Constraint) FlexibleConstraint(org.cpsolver.coursett.constraint.FlexibleConstraint) RoomConstraint(org.cpsolver.coursett.constraint.RoomConstraint) StudentGroup(org.cpsolver.coursett.model.StudentGroup)

Example 2 with FlexibleConstraint

use of org.cpsolver.coursett.constraint.FlexibleConstraint in project cpsolver by UniTime.

the class FlexibleConstraintCriterion method getValue.

@Override
public double getValue(Assignment<Lecture, Placement> assignment, Placement value, Set<Placement> conflicts) {
    HashMap<Lecture, Placement> assignments = new HashMap<Lecture, Placement>();
    assignments.put(value.variable(), value);
    double ret = 0.0;
    for (FlexibleConstraint gc : value.variable().getFlexibleGroupConstraints()) ret += gc.getCurrentPreference(assignment, conflicts, assignments);
    assignments.put(value.variable(), null);
    for (FlexibleConstraint gc : value.variable().getFlexibleGroupConstraints()) ret -= gc.getCurrentPreference(assignment, conflicts, assignments);
    return ret;
}
Also used : FlexibleConstraint(org.cpsolver.coursett.constraint.FlexibleConstraint) Lecture(org.cpsolver.coursett.model.Lecture) Placement(org.cpsolver.coursett.model.Placement) HashMap(java.util.HashMap)

Example 3 with FlexibleConstraint

use of org.cpsolver.coursett.constraint.FlexibleConstraint in project cpsolver by UniTime.

the class FlexibleConstraintCriterion method getInfo.

@Override
public void getInfo(Assignment<Lecture, Placement> assignment, Map<String, String> info, Collection<Lecture> variables) {
    for (FlexibleConstraintType type : FlexibleConstraintType.values()) {
        Set<FlexibleConstraint> constraints = new HashSet<FlexibleConstraint>();
        for (Lecture lecture : variables) {
            for (FlexibleConstraint c : lecture.getFlexibleGroupConstraints()) {
                if (type.equals(c.getType()))
                    constraints.add(c);
            }
        }
        if (!constraints.isEmpty()) {
            int violated = 0;
            double penalty = 0.0;
            StringBuilder debug = null;
            for (FlexibleConstraint c : constraints) {
                if (c.getContext(assignment).getPreference() > 0) {
                    violated++;
                    penalty += c.getContext(assignment).getPreference();
                    if (iDebug) {
                        if (debug == null)
                            debug = new StringBuilder(c.getOwner());
                        else
                            debug.append("; " + c.getOwner());
                    }
                }
            }
            info.put(type.getName() + " Constraints", getPerc(violated, 0, constraints.size()) + "%" + (violated == 0 ? "" : " (" + (constraints.size() - violated) + " of " + constraints.size() + ", average penalty:" + sDoubleFormat.format(penalty / violated) + ")"));
            if (iDebug && violated > 0)
                info.put(type.getName() + " Violations", debug.toString());
        }
    }
}
Also used : FlexibleConstraint(org.cpsolver.coursett.constraint.FlexibleConstraint) Lecture(org.cpsolver.coursett.model.Lecture) FlexibleConstraintType(org.cpsolver.coursett.constraint.FlexibleConstraint.FlexibleConstraintType) FlexibleConstraint(org.cpsolver.coursett.constraint.FlexibleConstraint) HashSet(java.util.HashSet)

Example 4 with FlexibleConstraint

use of org.cpsolver.coursett.constraint.FlexibleConstraint in project cpsolver by UniTime.

the class FlexibleConstraintCriterion method getValue.

@Override
public double getValue(Assignment<Lecture, Placement> assignment, Collection<Lecture> variables) {
    Set<FlexibleConstraint> flexibleConstraints = new HashSet<FlexibleConstraint>();
    for (Lecture lecture : variables) {
        flexibleConstraints.addAll(lecture.getFlexibleGroupConstraints());
    }
    int ret = 0;
    for (FlexibleConstraint gc : flexibleConstraints) {
        ret += gc.getContext(assignment).getPreference();
    }
    return ret;
}
Also used : FlexibleConstraint(org.cpsolver.coursett.constraint.FlexibleConstraint) Lecture(org.cpsolver.coursett.model.Lecture) FlexibleConstraint(org.cpsolver.coursett.constraint.FlexibleConstraint) HashSet(java.util.HashSet)

Example 5 with FlexibleConstraint

use of org.cpsolver.coursett.constraint.FlexibleConstraint in project cpsolver by UniTime.

the class FlexibleConstraintCriterion method getInfo.

@Override
public void getInfo(Assignment<Lecture, Placement> assignment, Map<String, String> info) {
    TimetableModel m = (TimetableModel) getModel();
    if (m.getFlexibleConstraints().isEmpty())
        return;
    for (FlexibleConstraintType type : FlexibleConstraintType.values()) {
        StringBuilder debug = null;
        int violated = 0, constraints = 0;
        double penalty = 0.0;
        for (FlexibleConstraint c : m.getFlexibleConstraints()) {
            if (type.equals(c.getType())) {
                constraints++;
                if (c.getContext(assignment).getPreference() > 0) {
                    violated++;
                    penalty += c.getContext(assignment).getPreference();
                    if (iDebug) {
                        if (debug == null)
                            debug = new StringBuilder(c.getOwner() + " (" + sDoubleFormat.format(c.getNrViolations(assignment, new HashSet<Placement>(), null)) + ")");
                        else
                            debug.append("; " + c.getOwner() + " (" + sDoubleFormat.format(c.getNrViolations(assignment, new HashSet<Placement>(), null)) + ")");
                    }
                }
            }
        }
        if (constraints > 0) {
            info.put(type.getName() + " Constraints", getPerc(violated, 0, constraints) + "%" + (violated == 0 ? "" : " (" + (constraints - violated) + "/" + constraints + ", average penalty:" + sDoubleFormat.format(penalty / violated) + ")"));
            if (iDebug && violated > 0)
                info.put(type.getName() + " Violations", debug.toString());
        }
    }
}
Also used : FlexibleConstraint(org.cpsolver.coursett.constraint.FlexibleConstraint) Placement(org.cpsolver.coursett.model.Placement) FlexibleConstraintType(org.cpsolver.coursett.constraint.FlexibleConstraint.FlexibleConstraintType) TimetableModel(org.cpsolver.coursett.model.TimetableModel) FlexibleConstraint(org.cpsolver.coursett.constraint.FlexibleConstraint) HashSet(java.util.HashSet)

Aggregations

FlexibleConstraint (org.cpsolver.coursett.constraint.FlexibleConstraint)7 HashSet (java.util.HashSet)4 Lecture (org.cpsolver.coursett.model.Lecture)4 ClassLimitConstraint (org.cpsolver.coursett.constraint.ClassLimitConstraint)3 GroupConstraint (org.cpsolver.coursett.constraint.GroupConstraint)3 InstructorConstraint (org.cpsolver.coursett.constraint.InstructorConstraint)3 SpreadConstraint (org.cpsolver.coursett.constraint.SpreadConstraint)3 Placement (org.cpsolver.coursett.model.Placement)3 HashMap (java.util.HashMap)2 DepartmentSpreadConstraint (org.cpsolver.coursett.constraint.DepartmentSpreadConstraint)2 FlexibleConstraintType (org.cpsolver.coursett.constraint.FlexibleConstraint.FlexibleConstraintType)2 JenrlConstraint (org.cpsolver.coursett.constraint.JenrlConstraint)2 WeakeningConstraint (org.cpsolver.ifs.model.WeakeningConstraint)2 ArrayList (java.util.ArrayList)1 BitSet (java.util.BitSet)1 Date (java.util.Date)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1