001package org.cpsolver.coursett.criteria.placement;
002
003import java.util.HashMap;
004import java.util.Map;
005import java.util.Set;
006
007import org.cpsolver.coursett.model.Lecture;
008import org.cpsolver.coursett.model.Placement;
009import org.cpsolver.ifs.assignment.Assignment;
010import org.cpsolver.ifs.util.Counter;
011
012
013/**
014 * Count number of past assignments of a value. Avoid repetition by penalizing
015 * values that have been assigned too many times already. 
016 * <br>
017 * 
018 * @version CourseTT 1.3 (University Course Timetabling)<br>
019 *          Copyright (C) 2006 - 2014 Tomáš Müller<br>
020 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
021 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
022 * <br>
023 *          This library is free software; you can redistribute it and/or modify
024 *          it under the terms of the GNU Lesser General Public License as
025 *          published by the Free Software Foundation; either version 3 of the
026 *          License, or (at your option) any later version. <br>
027 * <br>
028 *          This library is distributed in the hope that it will be useful, but
029 *          WITHOUT ANY WARRANTY; without even the implied warranty of
030 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
031 *          Lesser General Public License for more details. <br>
032 * <br>
033 *          You should have received a copy of the GNU Lesser General Public
034 *          License along with this library; if not see
035 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
036 */
037@Deprecated
038public class AssignmentCount extends PlacementSelectionCriterion {
039    
040    public AssignmentCount() {
041        setValueUpdateType(ValueUpdateType.BeforeUnassignedAfterAssigned);
042    }
043    
044    @Override
045    public String getPlacementSelectionWeightName() {
046        return "Placement.NrAssignmentsWeight";
047    }
048
049    @Override
050    public double getValue(Assignment<Lecture, Placement> assignment, Placement value, Set<Placement> conflicts) {
051        return ((AssignmentCountContext)getContext(assignment)).countAssignments(value);
052    }
053    
054    @Override
055    public ValueContext createAssignmentContext(Assignment<Lecture, Placement> assignment) {
056        return new AssignmentCountContext(assignment);
057    }
058
059    public class AssignmentCountContext extends ValueContext {
060        Map<Placement, Counter> iCounter = new HashMap<Placement, Counter>();
061
062        protected AssignmentCountContext(Assignment<Lecture, Placement> assignment) {
063            super(assignment);
064        }
065        
066        @Override
067        protected void assigned(Assignment<Lecture, Placement> assignment, Placement value) {
068            Counter c = iCounter.get(value);
069            if (c == null) {
070                c = new Counter();
071                iCounter.put(value, c);
072            }
073            c.inc(1);
074        }
075        
076        @Override
077        protected void unassigned(Assignment<Lecture, Placement> assignment, Placement value) {
078        }
079        
080        public long countAssignments(Placement value) {
081            Counter c = iCounter.get(value);
082            return c == null ? 0 : c.get();
083        }
084    }
085}