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