1 /**
2 *
3 */
4 package org.opentrafficsim.water.demand;
5
6 import java.io.Serializable;
7 import java.util.HashMap;
8 import java.util.HashSet;
9 import java.util.Map;
10 import java.util.Set;
11
12 import org.opentrafficsim.core.dsol.OTSDEVSSimulatorInterface;
13 import org.opentrafficsim.water.role.Company;
14 import org.opentrafficsim.water.transfer.Terminal;
15
16 /**
17 * Demand the between terminals in an operating area (region) and other terminals.
18 * <p>
19 * Copyright (c) 2013-2017 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
20 * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
21 * </p>
22 * <p>
23 * Based on software from the IDVV project, which is Copyright (c) 2013 Rijkswaterstaat - Dienst Water, Verkeer en Leefomgeving
24 * and licensed without restrictions to Delft University of Technology, including the right to sub-license sources and derived
25 * products to third parties.
26 * </p>
27 * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
28 * initial version Nov 6, 2016 <br>
29 * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
30 */
31 public class TransportDemand implements Serializable
32 {
33 /** */
34 private static final long serialVersionUID = 1L;
35
36 /** the simulator to schedule on. */
37 private OTSDEVSSimulatorInterface simulator;
38
39 /** the operating area for the demand. */
40 private Region operatingArea;
41
42 /** the demand from and to terminals. */
43 private Map<Terminal, Map<Terminal, DemandCell>> demandMap = new HashMap<Terminal, Map<Terminal, DemandCell>>();
44
45 /**
46 * @param simulator the simulator for scheduling the demand
47 * @param operatingArea the region for which this demand holds
48 */
49 public TransportDemand(final OTSDEVSSimulatorInterface simulator, final Region operatingArea)
50 {
51 this.simulator = simulator;
52 this.operatingArea = operatingArea;
53 }
54
55 /**
56 * @return the operatingArea
57 */
58 public final Region getOperatingArea()
59 {
60 return this.operatingArea;
61 }
62
63 /**
64 * @return the demandCells
65 */
66 public final Map<Terminal, Map<Terminal, DemandCell>> getDemandMap()
67 {
68 return this.demandMap;
69 }
70
71 /**
72 * add a cell to the demandmap.
73 * @param demandCell the cell to add
74 */
75 public final void addDemand(final DemandCell demandCell)
76 {
77 Map<Terminal, DemandCell> partMap = this.demandMap.get(demandCell.getTerminalFrom());
78 if (partMap == null)
79 {
80 partMap = new HashMap<Terminal, DemandCell>();
81 this.demandMap.put(demandCell.getTerminalFrom(), partMap);
82 }
83 partMap.put(demandCell.getTerminalTo(), demandCell);
84 }
85
86 /**
87 * add a cell to the demandmap.
88 * @param terminalFrom origin terminal
89 * @param terminalTo destination terminal
90 * @param numberAnnual annual number of containers per year
91 * @param fraction20ft fraction of 20 ft containers (rest is 40 ft)
92 * @param fractionEmpty fraction of empty containers (rest is full)
93 * @param fractionOwners map of owners with fraction (adding up to 1.0) indicating who owns the containers
94 */
95 public final void addDemand(final Terminal terminalFrom, final Terminal terminalTo, final int numberAnnual,
96 final double fraction20ft, final double fractionEmpty, final Map<Company, Double> fractionOwners)
97 {
98 DemandCell demandCell =
99 new DemandCell(terminalFrom, terminalTo, numberAnnual, fraction20ft, fractionEmpty, fractionOwners);
100 addDemand(demandCell);
101 }
102
103 /**
104 * @param terminalFrom origin terminal
105 * @param terminalTo destination terminal
106 * @return the demand between two terminals.
107 */
108 public final DemandCell getDemand(final Terminal terminalFrom, final Terminal terminalTo)
109 {
110 if (this.demandMap.containsKey(terminalFrom))
111 {
112 return this.demandMap.get(terminalFrom).get(terminalTo);
113 }
114 return null;
115 }
116
117 /**
118 * @param terminalFrom origin terminal
119 * @return the map of demands originating at a terminal.
120 */
121 public final Map<Terminal, DemandCell> getDemandMapFrom(final Terminal terminalFrom)
122 {
123 return this.demandMap.get(terminalFrom);
124 }
125
126 /**
127 * @param terminalTo destination terminal
128 * @return the map of demands for a destination terminal.
129 */
130 public final Map<Terminal, DemandCell> getDemandMapTo(final Terminal terminalTo)
131 {
132 Map<Terminal, DemandCell> toMap = new HashMap<Terminal, DemandCell>();
133 for (Terminal from : this.demandMap.keySet())
134 {
135 for (Terminal to : this.demandMap.get(from).keySet())
136 {
137 if (terminalTo.equals(to))
138 {
139 toMap.put(from, this.demandMap.get(from).get(to));
140 }
141 }
142 }
143 return toMap;
144 }
145
146 /**
147 * @param terminalFrom origin terminal
148 * @return the demands originating at a terminal as a set.
149 */
150 public final Set<DemandCell> getDemandSetFrom(final Terminal terminalFrom)
151 {
152 return new HashSet<DemandCell>(this.demandMap.get(terminalFrom).values());
153 }
154
155 /**
156 * @param terminalTo destination terminal
157 * @return the demands for a destination terminal as a set.
158 */
159 public final Set<DemandCell> getDemandSetTo(final Terminal terminalTo)
160 {
161 Set<DemandCell> toSet = new HashSet<DemandCell>();
162 for (Terminal from : this.demandMap.keySet())
163 {
164 for (Terminal to : this.demandMap.get(from).keySet())
165 {
166 if (terminalTo.equals(to))
167 {
168 toSet.add(this.demandMap.get(from).get(to));
169 }
170 }
171 }
172 return toSet;
173 }
174
175 /**
176 * @return the simulator
177 */
178 public final OTSDEVSSimulatorInterface getSimulator()
179 {
180 return this.simulator;
181 }
182
183 /**
184 * A cell of demand from a terminal to a terminal.
185 * <p>
186 * Copyright (c) 2013-2017 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
187 * <br>
188 * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
189 * </p>
190 * <p>
191 * Based on software from the IDVV project, which is Copyright (c) 2013 Rijkswaterstaat - Dienst Water, Verkeer en
192 * Leefomgeving and licensed without restrictions to Delft University of Technology, including the right to sub-license
193 * sources and derived products to third parties.
194 * </p>
195 * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
196 * initial version Nov 6, 2016 <br>
197 * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
198 */
199 public class DemandCell
200 {
201 /** origin terminal. */
202 private Terminal terminalFrom;
203
204 /** destination terminal. */
205 private Terminal terminalTo;
206
207 /** number of containers per year. */
208 private int numberAnnual;
209
210 /** fraction of 20 ft containers of total (rest is 40 ft). */
211 private double fraction20ft;
212
213 /** fraction of empty containers (rest is full). */
214 private double fractionEmpty;
215
216 /** table with fractions (adding up to 1.0) who owns the containers. */
217 private Map<Company, Double> fractionOwners = new HashMap<Company, Double>();
218
219 /**
220 * @param terminalFrom origin terminal
221 * @param terminalTo destination terminal
222 * @param numberAnnual annual number of containers per year
223 * @param fraction20ft fraction of 20 ft containers (rest is 40 ft)
224 * @param fractionEmpty fraction of empty containers (rest is full)
225 * @param fractionOwners map of owners with fraction (adding up to 1.0) indicating who owns the containers
226 */
227 public DemandCell(final Terminal terminalFrom, final Terminal terminalTo, final int numberAnnual,
228 final double fraction20ft, final double fractionEmpty, final Map<Company, Double> fractionOwners)
229 {
230 super();
231 this.terminalFrom = terminalFrom;
232 this.terminalTo = terminalTo;
233 this.numberAnnual = numberAnnual;
234 this.fraction20ft = fraction20ft;
235 this.fractionEmpty = fractionEmpty;
236 this.fractionOwners = fractionOwners;
237 }
238
239 /**
240 * @return the terminalFrom
241 */
242 public final Terminal getTerminalFrom()
243 {
244 return this.terminalFrom;
245 }
246
247 /**
248 * @return the terminalTo
249 */
250 public final Terminal getTerminalTo()
251 {
252 return this.terminalTo;
253 }
254
255 /**
256 * @return the numberAnnual
257 */
258 public final int getNumberAnnual()
259 {
260 return this.numberAnnual;
261 }
262
263 /**
264 * @return the fraction20ft
265 */
266 public final double getFraction20ft()
267 {
268 return this.fraction20ft;
269 }
270
271 /**
272 * @return the fractionEmpty
273 */
274 public final double getFractionEmpty()
275 {
276 return this.fractionEmpty;
277 }
278
279 /**
280 * @return the fractionOwners
281 */
282 public final Map<Company, Double> getFractionOwners()
283 {
284 return this.fractionOwners;
285 }
286
287 }
288
289 }