001package net.sf.cpsolver.coursett.criteria.placement;
002
003import java.util.Collection;
004import java.util.Map;
005import java.util.Set;
006
007import net.sf.cpsolver.coursett.model.Lecture;
008import net.sf.cpsolver.coursett.model.Placement;
009import net.sf.cpsolver.ifs.extension.ConflictStatistics;
010import net.sf.cpsolver.ifs.extension.Extension;
011import net.sf.cpsolver.ifs.solution.Solution;
012import net.sf.cpsolver.ifs.solution.SolutionListener;
013import net.sf.cpsolver.ifs.solver.Solver;
014
015/**
016 * Hard conflicts weighted by the conflict-based statistics (past occurrences).
017 * <br>
018 * 
019 * @version CourseTT 1.2 (University Course Timetabling)<br>
020 *          Copyright (C) 2006 - 2011 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 */
038public class WeightedHardConflicts extends PlacementSelectionCriterion implements SolutionListener<Lecture, Placement> {
039    protected ConflictStatistics<Lecture, Placement> iStat = null;
040    protected long iIteration = 0;
041    
042    @Override
043    public boolean init(Solver<Lecture, Placement> solver) {
044        super.init(solver);
045        for (Extension<Lecture, Placement> extension : solver.getExtensions()) {
046            if (ConflictStatistics.class.isInstance(extension))
047                iStat = (ConflictStatistics<Lecture, Placement>) extension;
048        }
049        solver.currentSolution().addSolutionListener(this);
050        return true;
051    }
052    
053    @Override
054    public String getPlacementSelectionWeightName() {
055        return "Placement.NrConflictsWeight";
056    }
057
058    @Override
059    public double getValue(Placement value, Set<Placement> conflicts) {
060        return (iStat == null || conflicts == null ? 0.0 : iStat.countRemovals(iIteration, conflicts, value));
061    }
062
063    @Override
064    public double getPlacementSelectionWeightDefault(int level) {
065        return (level == 0 ? 3.0 : 0.0);
066    }
067
068    @Override
069    public void solutionUpdated(Solution<Lecture, Placement> solution) {
070        iIteration = solution.getIteration();
071    }
072
073    @Override
074    public void getInfo(Solution<Lecture, Placement> solution, Map<String, String> info) {
075    }
076
077    @Override
078    public void getInfo(Solution<Lecture, Placement> solution, Map<String, String> info, Collection<Lecture> variables) {
079    }
080
081    @Override
082    public void bestCleared(Solution<Lecture, Placement> solution) {
083        iIteration = solution.getIteration();        
084    }
085
086    @Override
087    public void bestSaved(Solution<Lecture, Placement> solution) {
088        iIteration = solution.getIteration();
089    }
090
091    @Override
092    public void bestRestored(Solution<Lecture, Placement> solution) {
093        iIteration = solution.getIteration();
094    }
095
096}