001package org.cpsolver.coursett.criteria.additional;
002
003import java.util.Collection;
004import java.util.Set;
005
006import org.cpsolver.coursett.criteria.StudentConflict;
007import org.cpsolver.coursett.model.Lecture;
008import org.cpsolver.coursett.model.Placement;
009import org.cpsolver.coursett.model.Student;
010import org.cpsolver.ifs.assignment.Assignment;
011import org.cpsolver.ifs.util.DataProperties;
012
013
014/**
015 * Ignored committed student conflicts. This criterion counts committed student conflicts (both overlapping and distance) between classes
016 * which are connected by a {@link IgnoredStudentConflict} constraint. This criterion was created mostly for debugging
017 * as these student conflicts are to be ignored.
018 * <br>
019 * 
020 * @version CourseTT 1.3 (University Course Timetabling)<br>
021 *          Copyright (C) 2013 - 2014 Tomáš Müller<br>
022 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
023 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
024 * <br>
025 *          This library is free software; you can redistribute it and/or modify
026 *          it under the terms of the GNU Lesser General Public License as
027 *          published by the Free Software Foundation; either version 3 of the
028 *          License, or (at your option) any later version. <br>
029 * <br>
030 *          This library is distributed in the hope that it will be useful, but
031 *          WITHOUT ANY WARRANTY; without even the implied warranty of
032 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
033 *          Lesser General Public License for more details. <br>
034 * <br>
035 *          You should have received a copy of the GNU Lesser General Public
036 *          License along with this library; if not see
037 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
038 */
039public class IgnoredCommittedStudentConflict extends StudentConflict {
040
041    @Override
042    public double getWeightDefault(DataProperties config) {
043        return config.getPropertyDouble("Comparator.IgnoredCommitedStudentConflictWeight", 0.0);
044    }
045
046    @Override
047    public String getPlacementSelectionWeightName() {
048        return "Placement.NrIgnoredCommitedStudConfsWeight";
049    }
050    
051    @Override
052    public boolean isApplicable(Lecture l1, Lecture l2) {
053        return l1 != null && l2 != null && ignore(l1, l2) && committed(l1, l2);
054    }
055
056    public int countCommittedConflicts(Student student, Placement placement) {
057        if (student.getCommitedPlacements() == null) return 0;
058        int conflicts = 0;
059        Lecture lecture = placement.variable();
060        for (Placement commitedPlacement : student.getCommitedPlacements()) {
061            Lecture commitedLecture = commitedPlacement.variable();
062            if (lecture.getSchedulingSubpartId() != null && lecture.getSchedulingSubpartId().equals(commitedLecture.getSchedulingSubpartId())) continue;
063            if (ignore(lecture, commitedLecture) && (overlaps(placement, commitedPlacement) || distance(getMetrics(), placement, commitedPlacement)))
064                conflicts ++;
065        }
066        if (conflicts == 0) return 0;
067        double w = student.getOfferingWeight((placement.variable()).getConfiguration());
068        return (int) Math.round(student.avg(w, 1.0) * conflicts);
069    }
070    
071    public double countCommittedConflicts(Placement placement) {
072        double ret = 0;
073        Lecture lecture = placement.variable();
074        for (Student student : lecture.students()) {
075            ret += countCommittedConflicts(student, placement);
076        }
077        return ret;
078    }
079        
080    @Override
081    public double[] getBounds(Assignment<Lecture, Placement> assignment, Collection<Lecture> variables) {
082        double[] bounds = super.getBounds(assignment, variables);
083        for (Lecture lecture: variables) {
084            Double max = null;
085            for (Placement placement: lecture.values(assignment)) {
086                if (max == null) { max = new Double(countCommittedConflicts(placement)); continue; }
087                max = Math.max(max, countCommittedConflicts(placement));
088            }
089            if (max != null) bounds[0] += max;
090        }
091        return bounds;
092    }
093    
094    @Override
095    public double getValue(Assignment<Lecture, Placement> assignment, Placement value, Set<Placement> conflicts) {
096        double ret = super.getValue(assignment, value, conflicts);
097        ret += countCommittedConflicts(value);
098        if (iIncludeConflicts && conflicts != null)
099            for (Placement conflict: conflicts)
100                ret -= countCommittedConflicts(conflict);
101        return ret;
102    }
103    
104    @Override
105    public double getValue(Assignment<Lecture, Placement> assignment, Collection<Lecture> variables) {
106        double ret = super.getValue(assignment, variables);
107        for (Lecture lect: variables) {
108            Placement plac = assignment.getValue(lect);
109            if (plac != null)
110                ret += countCommittedConflicts(plac);
111        }
112        return Math.round(ret);
113    }
114}