001package org.cpsolver.ifs.example.tt;
002
003import java.util.ArrayList;
004import java.util.Collection;
005import java.util.HashSet;
006import java.util.List;
007import java.util.Set;
008
009import org.cpsolver.ifs.model.Variable;
010
011
012/**
013 * Activity (variable). It encodes a name, length
014 * 
015 * @author  Tomáš Müller
016 * @version IFS 1.3 (Iterative Forward Search)<br>
017 *          Copyright (C) 2006 - 2014 Tomáš Müller<br>
018 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
019 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
020 * <br>
021 *          This library is free software; you can redistribute it and/or modify
022 *          it under the terms of the GNU Lesser General Public License as
023 *          published by the Free Software Foundation; either version 3 of the
024 *          License, or (at your option) any later version. <br>
025 * <br>
026 *          This library is distributed in the hope that it will be useful, but
027 *          WITHOUT ANY WARRANTY; without even the implied warranty of
028 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
029 *          Lesser General Public License for more details. <br>
030 * <br>
031 *          You should have received a copy of the GNU Lesser General Public
032 *          License along with this library; if not see
033 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
034 */
035public class Activity extends Variable<Activity, Location> {
036    private int iLength = 1;
037    private String iActivityId = null;
038    private String iName = null;
039    private ArrayList<List<Resource>> iResorces = new ArrayList<List<Resource>>();
040    private Set<Integer> iProhibitedSlots = new HashSet<Integer>();
041    private Set<Integer> iDiscouragedSlots = new HashSet<Integer>();
042
043    public Activity(int length, String id, String name) {
044        super(null);
045        iLength = length;
046        iActivityId = id;
047        iName = name;
048    }
049
050    @Override
051    public String getName() {
052        return iName;
053    }
054
055    public String getActivityId() {
056        return iActivityId;
057    }
058
059    public int getLength() {
060        return iLength;
061    }
062
063    public void addResourceGroup(List<Resource> resources) {
064        for (Resource r : resources)
065            r.addVariable(this);
066        iResorces.add(resources);
067    }
068
069    public void addResourceGroup(Resource[] resources) {
070        ArrayList<Resource> rg = new ArrayList<Resource>(resources.length);
071        for (int i = 0; i < resources.length; i++) {
072            rg.add(resources[i]);
073            resources[i].addVariable(this);
074        }
075        iResorces.add(rg);
076    }
077
078    public void addResourceGroup(Resource resource) {
079        ArrayList<Resource> rg = new ArrayList<Resource>(1);
080        rg.add(resource);
081        iResorces.add(rg);
082        resource.addVariable(this);
083    }
084
085    public List<Resource> getResourceGroup(int idx) {
086        return iResorces.get(idx);
087    }
088
089    public List<List<Resource>> getResourceGroups() {
090        return iResorces;
091    }
092
093    public Set<Integer> getProhibitedSlots() {
094        return iProhibitedSlots;
095    }
096
097    public Set<Integer> getDiscouragedSlots() {
098        return iDiscouragedSlots;
099    }
100
101    public void addProhibitedSlot(int day, int hour) {
102        iProhibitedSlots.add(((TimetableModel) getModel()).getNrHours() * day + hour);
103    }
104
105    public void addDiscouragedSlot(int day, int hour) {
106        iDiscouragedSlots.add(((TimetableModel) getModel()).getNrHours() * day + hour);
107    }
108
109    public boolean isProhibitedSlot(int day, int hour) {
110        return iProhibitedSlots.contains(((TimetableModel) getModel()).getNrHours() * day + hour);
111    }
112
113    public boolean isDiscouragedSlot(int day, int hour) {
114        return iDiscouragedSlots.contains(((TimetableModel) getModel()).getNrHours() * day + hour);
115    }
116
117    public void addProhibitedSlot(int slot) {
118        iProhibitedSlots.add(slot);
119    }
120
121    public void addDiscouragedSlot(int slot) {
122        iDiscouragedSlots.add(slot);
123    }
124
125    public boolean isProhibitedSlot(int slot) {
126        return iProhibitedSlots.contains(slot);
127    }
128
129    public boolean isDiscouragedSlot(int slot) {
130        return iDiscouragedSlots.contains(slot);
131    }
132
133    public boolean isProhibited(int day, int hour, int length) {
134        int slot = ((TimetableModel) getModel()).getNrHours() * day + hour;
135        for (int i = 0; i < length; i++)
136            if (iProhibitedSlots.contains(slot + i))
137                return true;
138        return false;
139    }
140
141    public void init() {
142        setValues(computeValues());
143    }
144
145    private void addValues(Collection<Location> values, int day, int hour, int level, Resource[] resources) {
146        if (level == getResourceGroups().size()) {
147            values.add(new Location(this, day, hour, resources.clone()));
148            return;
149        }
150        Collection<Resource> rg = getResourceGroups().get(level);
151        for (Resource r : rg) {
152            if (r.isProhibited(day, hour, getLength()))
153                continue;
154            resources[level] = r;
155            addValues(values, day, hour, level + 1, resources);
156        }
157    }
158
159    public List<Location> computeValues() {
160        List<Location> values = new ArrayList<Location>();
161        Resource[] res = new Resource[getResourceGroups().size()];
162        for (int day = 0; day < ((TimetableModel) getModel()).getNrDays(); day++)
163            for (int hour = 0; hour <= ((TimetableModel) getModel()).getNrHours() - getLength(); hour++) {
164                if (isProhibited(day, hour, getLength()))
165                    continue;
166                addValues(values, day, hour, 0, res);
167            }
168        return values;
169    }
170}