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