001package org.cpsolver.exam.criteria.additional; 002 003import java.util.Collection; 004import java.util.HashSet; 005import java.util.Set; 006 007import org.cpsolver.exam.criteria.ExamCriterion; 008import org.cpsolver.exam.model.Exam; 009import org.cpsolver.exam.model.ExamDistributionConstraint; 010import org.cpsolver.exam.model.ExamModel; 011import org.cpsolver.exam.model.ExamPlacement; 012import org.cpsolver.ifs.assignment.Assignment; 013 014 015/** 016 * Experimental criterion counting violations of hard distribution constraints. 017 * <br><br> 018 * To enable breaking of hard distribution constraints, set parameter Exam.SoftDistributions to 019 * a weight that should be inferred by a hard distribution constraint being broken. 020 * 021 * <br> 022 * 023 * @author Tomáš Müller 024 * @version ExamTT 1.3 (Examination Timetabling)<br> 025 * Copyright (C) 2008 - 2014 Tomáš Müller<br> 026 * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br> 027 * <a href="http://muller.unitime.org">http://muller.unitime.org</a><br> 028 * <br> 029 * This library is free software; you can redistribute it and/or modify 030 * it under the terms of the GNU Lesser General Public License as 031 * published by the Free Software Foundation; either version 3 of the 032 * License, or (at your option) any later version. <br> 033 * <br> 034 * This library is distributed in the hope that it will be useful, but 035 * WITHOUT ANY WARRANTY; without even the implied warranty of 036 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 037 * Lesser General Public License for more details. <br> 038 * <br> 039 * You should have received a copy of the GNU Lesser General Public 040 * License along with this library; if not see 041 * <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>. 042 */ 043public class DistributionViolation extends ExamCriterion { 044 045 public DistributionViolation() { 046 super(); 047 setValueUpdateType(ValueUpdateType.NoUpdate); 048 } 049 050 @Override 051 public String getWeightName() { 052 return "Exam.SoftDistributions"; 053 } 054 055 @Override 056 public String getXmlWeightName() { 057 return "softDistributions"; 058 } 059 060 @Override 061 public double getValue(Assignment<Exam, ExamPlacement> assignment, ExamPlacement value, Set<ExamPlacement> conflicts) { 062 int penalty = 0; 063 ExamPlacement original = assignment.getValue(value.variable()); 064 for (ExamDistributionConstraint dc : value.variable().getDistributionConstraints()) { 065 if (dc.isHard() || getWeight() != dc.getWeight()) 066 continue; 067 penalty += dc.countViolations(assignment, value); 068 if (original != null) penalty -= dc.countViolations(assignment, original); 069 } 070 return penalty; 071 } 072 073 @Override 074 protected double[] computeBounds(Assignment<Exam, ExamPlacement> assignment) { 075 double[] bounds = new double[] { 0.0, 0.0 }; 076 for (ExamDistributionConstraint dc : ((ExamModel)getModel()).getDistributionConstraints()) { 077 if (!dc.isHard() && getWeight() == dc.getWeight()) 078 bounds[1] += dc.variables().size() * (dc.variables().size() - 1) / 2; 079 } 080 return bounds; 081 } 082 083 @Override 084 public boolean isRoomCriterion() { return true; } 085 086 @Override 087 public double getRoomValue(Assignment<Exam, ExamPlacement> assignment, ExamPlacement value) { 088 int penalty = 0; 089 ExamPlacement original = assignment.getValue(value.variable()); 090 for (ExamDistributionConstraint dc : value.variable().getDistributionConstraints()) { 091 if (dc.isHard() || getWeight() != dc.getWeight() || !dc.isRoomRelated()) 092 continue; 093 penalty += dc.countViolations(assignment, value); 094 if (original != null) penalty -= dc.countViolations(assignment, original); 095 } 096 return penalty; 097 } 098 099 @Override 100 public boolean isPeriodCriterion() { return true; } 101 102 @Override 103 public double getPeriodValue(Assignment<Exam, ExamPlacement> assignment, ExamPlacement value) { 104 int penalty = 0; 105 ExamPlacement original = assignment.getValue(value.variable()); 106 for (ExamDistributionConstraint dc : value.variable().getDistributionConstraints()) { 107 if (dc.isHard() || getWeight() != dc.getWeight() || !dc.isPeriodRelated()) 108 continue; 109 penalty += dc.countViolations(assignment, value); 110 if (original != null) penalty -= dc.countViolations(assignment, original); 111 } 112 return penalty; 113 } 114 115 @Override 116 public double getValue(Assignment<Exam, ExamPlacement> assignment, Collection<Exam> variables) { 117 int penalty = 0; 118 Set<ExamDistributionConstraint> added = new HashSet<ExamDistributionConstraint>(); 119 for (Exam exam: variables) { 120 for (ExamDistributionConstraint dc : exam.getDistributionConstraints()) { 121 if (added.add(dc)) { 122 if (dc.isHard() || getWeight() != dc.getWeight()) 123 continue; 124 penalty += dc.countViolations(assignment); 125 } 126 } 127 } 128 return penalty; 129 } 130 131 @Override 132 public String toString(Assignment<Exam, ExamPlacement> assignment) { 133 return (getValue(assignment) <= 0.0 ? "" : "!D:" + sDoubleFormat.format(getValue(assignment))); 134 } 135}