001 package net.sf.cpsolver.ifs.example.tt; 002 003 import java.util.*; 004 005 import net.sf.cpsolver.ifs.model.*; 006 007 /** 008 * Resource constraint 009 * 010 * @version 011 * IFS 1.1 (Iterative Forward Search)<br> 012 * Copyright (C) 2006 Tomáš Müller<br> 013 * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br> 014 * Lazenska 391, 76314 Zlin, Czech Republic<br> 015 * <br> 016 * This library is free software; you can redistribute it and/or 017 * modify it under the terms of the GNU Lesser General Public 018 * License as published by the Free Software Foundation; either 019 * version 2.1 of the License, or (at your option) any later version. 020 * <br><br> 021 * This library is distributed in the hope that it will be useful, 022 * but WITHOUT ANY WARRANTY; without even the implied warranty of 023 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 024 * Lesser General Public License for more details. 025 * <br><br> 026 * You should have received a copy of the GNU Lesser General Public 027 * License along with this library; if not, write to the Free Software 028 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 029 */ 030 public class Resource extends Constraint { 031 private String iName = null; 032 private String iResourceId = null; 033 private Activity[] iResource; 034 private Set iProhibitedSlots = new HashSet(); 035 private Set iDiscouragedSlots = new HashSet(); 036 private int iType = TYPE_OTHER; 037 038 public static final int TYPE_ROOM = 0; 039 public static final int TYPE_INSTRUCTOR = 1; 040 public static final int TYPE_CLASS = 2; 041 public static final int TYPE_OTHER = 3; 042 043 public Resource(String id, int type, String name) { 044 super(); 045 iResourceId = id; 046 iName = name; 047 iType = type; 048 } 049 050 public void setModel(Model model) { 051 super.setModel(model); 052 iResource = new Activity[((TimetableModel)model).getNrDays()*((TimetableModel)model).getNrHours()]; 053 for (int i=0;i<iResource.length;i++) 054 iResource[i] = null; 055 } 056 057 public String getResourceId() { return iResourceId; } 058 public String getName() { return iName; } 059 public int getType() { return iType; } 060 public Set getProhibitedSlots() { return iProhibitedSlots; } 061 public Set getDiscouragedSlots() { return iDiscouragedSlots; } 062 public void addProhibitedSlot(int day, int hour) { 063 iProhibitedSlots.add(new Integer(((TimetableModel)getModel()).getNrHours()*day+hour)); 064 } 065 public void addDiscouragedSlot(int day, int hour) { 066 iDiscouragedSlots.add(new Integer(((TimetableModel)getModel()).getNrHours()*day+hour)); 067 } 068 public boolean isProhibitedSlot(int day, int hour) { 069 return iProhibitedSlots.contains(new Integer(((TimetableModel)getModel()).getNrHours()*day+hour)); 070 } 071 public boolean isDiscouragedSlot(int day, int hour) { 072 return iDiscouragedSlots.contains(new Integer(((TimetableModel)getModel()).getNrHours()*day+hour)); 073 } 074 public void addProhibitedSlot(int slot) { 075 iProhibitedSlots.add(new Integer(slot)); 076 } 077 public void addDiscouragedSlot(int slot) { 078 iDiscouragedSlots.add(new Integer(slot)); 079 } 080 public boolean isProhibitedSlot(int slot) { 081 return iProhibitedSlots.contains(new Integer(slot)); 082 } 083 public boolean isDiscouragedSlot(int slot) { 084 return iDiscouragedSlots.contains(new Integer(slot)); 085 } 086 public boolean isProhibited(int day, int hour, int length) { 087 int slot = ((TimetableModel)getModel()).getNrHours()*day+hour; 088 for (int i=0;i<length;i++) 089 if (iProhibitedSlots.contains(new Integer(slot+i))) return true; 090 return false; 091 } 092 093 public void computeConflicts(Value value, Set conflicts) { 094 Activity activity = (Activity) value.variable(); 095 Location location = (Location) value; 096 if (!location.containResource(this)) return; 097 for (int i=location.getSlot(); i<location.getSlot()+activity.getLength(); i++) { 098 Activity conf = iResource[i]; 099 if (conf!=null && !activity.equals(conf)) 100 conflicts.add(conf.getAssignment()); 101 } 102 } 103 104 public boolean inConflict(Value value) { 105 Activity activity = (Activity) value.variable(); 106 Location location = (Location) value; 107 if (!location.containResource(this)) return false; 108 for (int i=location.getSlot(); i<location.getSlot()+activity.getLength(); i++) { 109 if (iResource[i]!=null) return true; 110 } 111 return false; 112 } 113 114 public boolean isConsistent(Value value1, Value value2) { 115 Location l1 = (Location) value1; 116 Location l2 = (Location) value2; 117 return !l1.containResource(this) || !l2.containResource(this) || !l1.hasIntersection(l2); 118 } 119 120 public void assigned(long iteration, Value value) { 121 super.assigned(iteration, value); 122 Activity activity = (Activity) value.variable(); 123 Location location = (Location) value; 124 if (!location.containResource(this)) return; 125 for (int i=location.getSlot(); i<location.getSlot()+activity.getLength(); i++) { 126 iResource[i] = activity; 127 } 128 } 129 public void unassigned(long iteration, Value value) { 130 super.unassigned(iteration, value); 131 Activity activity = (Activity) value.variable(); 132 Location location = (Location) value; 133 if (!location.containResource(this)) return; 134 for (int i=location.getSlot(); i<location.getSlot()+activity.getLength(); i++) { 135 iResource[i] = null; 136 } 137 } 138 }