001package org.cpsolver.coursett.criteria;
002
003import java.util.Collection;
004import java.util.HashSet;
005import java.util.Set;
006
007import org.cpsolver.coursett.constraint.DepartmentSpreadConstraint;
008import org.cpsolver.coursett.model.Lecture;
009import org.cpsolver.coursett.model.Placement;
010import org.cpsolver.ifs.assignment.Assignment;
011import org.cpsolver.ifs.util.DataProperties;
012
013
014/**
015 * Department balancing penalty. This criterion tries to balance classes of each
016 * department evenly. This means that for instance each department gets a fair
017 * amount of unpopular times like early morning or late afternoon. This criterion
018 * is counted by {@link DepartmentSpreadConstraint}. 
019 * <br>
020 * 
021 * @version CourseTT 1.3 (University Course Timetabling)<br>
022 *          Copyright (C) 2006 - 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 DepartmentBalancingPenalty extends SameSubpartBalancingPenalty {
041
042    @Override
043    public double getWeightDefault(DataProperties config) {
044        return 12.0 * config.getPropertyDouble("Comparator.DeptSpreadPenaltyWeight", 1.0);
045    }
046    
047    @Override
048    public String getPlacementSelectionWeightName() {
049        return "Placement.DeptSpreadPenaltyWeight";
050    }
051
052    @Override
053    public double getValue(Assignment<Lecture, Placement> assignment, Placement value, Set<Placement> conflicts) {
054        return (value.variable().getDeptSpreadConstraint() == null ? 0.0 : value.variable().getDeptSpreadConstraint().getPenalty(assignment, value)) / 12.0;
055    }
056    
057    @Override
058    public double getValue(Assignment<Lecture, Placement> assignment, Collection<Lecture> variables) {
059        double ret = 0;
060        Set<DepartmentSpreadConstraint> constraints = new HashSet<DepartmentSpreadConstraint>();
061        for (Lecture lect: variables)
062            if (lect.getDeptSpreadConstraint() != null && constraints.add(lect.getDeptSpreadConstraint()))
063                ret += lect.getDeptSpreadConstraint().getPenalty(assignment);
064        return ret / 12.0;
065    }
066}