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