001package org.cpsolver.coursett.criteria; 002 003import org.cpsolver.coursett.heuristics.PlacementSelection; 004import org.cpsolver.coursett.model.Lecture; 005import org.cpsolver.coursett.model.Placement; 006import org.cpsolver.ifs.criteria.AbstractCriterion; 007import org.cpsolver.ifs.solver.Solver; 008 009/** 010 * Abstract class for all timetabling criteria. On top of the {@link AbstractCriterion}, it provides 011 * weights for the {@link PlacementSelection} heuristics. 012 * <br> 013 * 014 * @version CourseTT 1.3 (University Course Timetabling)<br> 015 * Copyright (C) 2006 - 2014 Tomas Muller<br> 016 * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br> 017 * <a href="http://muller.unitime.org">http://muller.unitime.org</a><br> 018 * <br> 019 * This library is free software; you can redistribute it and/or modify 020 * it under the terms of the GNU Lesser General Public License as 021 * published by the Free Software Foundation; either version 3 of the 022 * License, or (at your option) any later version. <br> 023 * <br> 024 * This library is distributed in the hope that it will be useful, but 025 * WITHOUT ANY WARRANTY; without even the implied warranty of 026 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 027 * Lesser General Public License for more details. <br> 028 * <br> 029 * You should have received a copy of the GNU Lesser General Public 030 * License along with this library; if not see 031 * <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>. 032 */ 033public abstract class TimetablingCriterion extends AbstractCriterion<Lecture, Placement> { 034 private double[] iPlacementSelectionWeight = null; 035 private Double[][] iPlacementSelectionAdjusts = null; 036 037 @Override 038 public boolean init(Solver<Lecture, Placement> solver) { 039 super.init(solver); 040 if (getPlacementSelectionWeightName() != null) { 041 iPlacementSelectionWeight = new double[] { 0.0, 0.0, 0.0 }; 042 for (int i = 0; i < 3; i++) 043 iPlacementSelectionWeight[i] = solver.getProperties().getPropertyDouble(getPlacementSelectionWeightName() + (1 + i), getPlacementSelectionWeightDefault(i)); 044 iPlacementSelectionAdjusts = new Double[][] { null, null, null}; 045 for (int i = 0; i < 3; i++) { 046 iPlacementSelectionAdjusts[i] = solver.getProperties().getPropertyDoubleArry(getPlacementSelectionAdjustmentsName() + (1 + i), 047 solver.getProperties().getPropertyDoubleArry(getPlacementSelectionAdjustmentsName(), null)); 048 } 049 } 050 return true; 051 } 052 053 public String getPlacementSelectionWeightName() { 054 return "Placement." + getClass().getName().substring(1 + getClass().getName().lastIndexOf('.')) + "Weight"; 055 } 056 057 public String getPlacementSelectionAdjustmentsName() { 058 return getPlacementSelectionWeightName() + "Adjustments"; 059 } 060 061 public double getPlacementSelectionWeight(int level, int idx) { 062 double w = (iPlacementSelectionWeight == null ? 0.0 : iPlacementSelectionWeight[level]); 063 if (idx < 0 || iPlacementSelectionAdjusts == null || iPlacementSelectionAdjusts[level] == null || idx >= iPlacementSelectionAdjusts[level].length || iPlacementSelectionAdjusts[level][idx] == null) return w; 064 return w * iPlacementSelectionAdjusts[level][idx]; 065 } 066 067 public double getPlacementSelectionWeightDefault(int level) { 068 return (level <= 1 ? getWeight() : 0.0); 069 } 070}