    001package org.cpsolver.coursett.criteria.placement;
    002
    003import java.util.Collection;
    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.extension.ConflictStatistics;
    011import org.cpsolver.ifs.extension.Extension;
    012import org.cpsolver.ifs.solution.Solution;
    013import org.cpsolver.ifs.solution.SolutionListener;
    014import org.cpsolver.ifs.solver.Solver;
    015
    016
    017/**
    018 * Hard conflicts weighted by the conflict-based statistics (past occurrences).
    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 WeightedHardConflicts extends PlacementSelectionCriterion implements SolutionListener<Lecture, Placement> {
    041    protected ConflictStatistics<Lecture, Placement> iStat = null;
    042
    043    @Override
    044    public boolean init(Solver<Lecture, Placement> solver) {
    045        super.init(solver);
    046        for (Extension<Lecture, Placement> extension : solver.getExtensions()) {
    047            if (ConflictStatistics.class.isInstance(extension))
    048                iStat = (ConflictStatistics<Lecture, Placement>) extension;
    049        }
    050        solver.currentSolution().addSolutionListener(this);
    051        return true;
    052    }
    053
    054    @Override
    055    public String getPlacementSelectionWeightName() {
    056        return "Placement.NrConflictsWeight";
    057    }
    058
    059    @Override
    060    public double getValue(Assignment<Lecture, Placement> assignment, Placement value, Set<Placement> conflicts) {
    061        if (iStat != null && conflicts != null && !conflicts.isEmpty()) {
    062            return iStat.countRemovals(((IterationContext)getContext(assignment)).getIteration(), conflicts, value);
    063        } else {
    064            return 0.0;
    065        }
    066    }
    067
    068    @Override
    069    public double getPlacementSelectionWeightDefault(int level) {
    070        return (level == 0 ? 3.0 : 0.0);
    071    }
    072
    073    @Override
    074    public void solutionUpdated(Solution<Lecture, Placement> solution) {
    075        ((IterationContext)getContext(solution.getAssignment())).setIteration(solution.getIteration());
    076    }
    077
    078    @Override
    079    public void getInfo(Solution<Lecture, Placement> solution, Map<String, String> info) {
    080    }
    081
    082    @Override
    083    public void getInfo(Solution<Lecture, Placement> solution, Map<String, String> info, Collection<Lecture> variables) {
    084    }
    085
    086    @Override
    087    public void bestCleared(Solution<Lecture, Placement> solution) {
    088        ((IterationContext)getContext(solution.getAssignment())).setIteration(solution.getIteration());
    089    }
    090
    091    @Override
    092    public void bestSaved(Solution<Lecture, Placement> solution) {
    093        ((IterationContext)getContext(solution.getAssignment())).setIteration(solution.getIteration());
    094    }
    095
    096    @Override
    097    public void bestRestored(Solution<Lecture, Placement> solution) {
    098        ((IterationContext)getContext(solution.getAssignment())).setIteration(solution.getIteration());
    099    }
    100
    101    @Override
    102    public ValueContext createAssignmentContext(Assignment<Lecture, Placement> assignment) {
    103        return new IterationContext(assignment);
    104    }
    105
    106    public class IterationContext extends ValueContext {
    107        protected long iIteration = 0;
    108
    109        protected IterationContext(Assignment<Lecture, Placement> assignment) {
    110            super(assignment);
    111        }
    112
    113        public void setIteration(long iteration) {
    114            iIteration = iteration;
    115        }
    116
    117        public long getIteration() {
    118            return iIteration;
    119        }
    120    }
    121}