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 * @author Tomáš Müller 022 * @version CourseTT 1.3 (University Course Timetabling)<br> 023 * Copyright (C) 2006 - 2014 Tomáš Müller<br> 024 * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br> 025 * <a href="http://muller.unitime.org">http://muller.unitime.org</a><br> 026 * <br> 027 * This library is free software; you can redistribute it and/or modify 028 * it under the terms of the GNU Lesser General Public License as 029 * published by the Free Software Foundation; either version 3 of the 030 * License, or (at your option) any later version. <br> 031 * <br> 032 * This library is distributed in the hope that it will be useful, but 033 * WITHOUT ANY WARRANTY; without even the implied warranty of 034 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 035 * Lesser General Public License for more details. <br> 036 * <br> 037 * You should have received a copy of the GNU Lesser General Public 038 * License along with this library; if not see 039 * <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>. 040 */ 041public class WeightedHardConflicts extends PlacementSelectionCriterion implements SolutionListener<Lecture, Placement> { 042 protected ConflictStatistics<Lecture, Placement> iStat = null; 043 044 @Override 045 public boolean init(Solver<Lecture, Placement> solver) { 046 super.init(solver); 047 for (Extension<Lecture, Placement> extension : solver.getExtensions()) { 048 if (ConflictStatistics.class.isInstance(extension)) 049 iStat = (ConflictStatistics<Lecture, Placement>) extension; 050 } 051 solver.currentSolution().addSolutionListener(this); 052 return true; 053 } 054 055 @Override 056 public String getPlacementSelectionWeightName() { 057 return "Placement.NrConflictsWeight"; 058 } 059 060 @Override 061 public double getValue(Assignment<Lecture, Placement> assignment, Placement value, Set<Placement> conflicts) { 062 if (iStat != null && conflicts != null && !conflicts.isEmpty()) { 063 return iStat.countRemovals(((IterationContext)getContext(assignment)).getIteration(), conflicts, value); 064 } else { 065 return 0.0; 066 } 067 } 068 069 @Override 070 public double getPlacementSelectionWeightDefault(int level) { 071 return (level == 0 ? 3.0 : 0.0); 072 } 073 074 @Override 075 public void solutionUpdated(Solution<Lecture, Placement> solution) { 076 ((IterationContext)getContext(solution.getAssignment())).setIteration(solution.getIteration()); 077 } 078 079 @Override 080 public void getInfo(Solution<Lecture, Placement> solution, Map<String, String> info) { 081 } 082 083 @Override 084 public void getInfo(Solution<Lecture, Placement> solution, Map<String, String> info, Collection<Lecture> variables) { 085 } 086 087 @Override 088 public void bestCleared(Solution<Lecture, Placement> solution) { 089 ((IterationContext)getContext(solution.getAssignment())).setIteration(solution.getIteration()); 090 } 091 092 @Override 093 public void bestSaved(Solution<Lecture, Placement> solution) { 094 ((IterationContext)getContext(solution.getAssignment())).setIteration(solution.getIteration()); 095 } 096 097 @Override 098 public void bestRestored(Solution<Lecture, Placement> solution) { 099 ((IterationContext)getContext(solution.getAssignment())).setIteration(solution.getIteration()); 100 } 101 102 @Override 103 public ValueContext createAssignmentContext(Assignment<Lecture, Placement> assignment) { 104 return new IterationContext(assignment); 105 } 106 107 public class IterationContext extends ValueContext { 108 protected long iIteration = 0; 109 110 protected IterationContext(Assignment<Lecture, Placement> assignment) { 111 super(assignment); 112 } 113 114 public void setIteration(long iteration) { 115 iIteration = iteration; 116 } 117 118 public long getIteration() { 119 return iIteration; 120 } 121 } 122}