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-2018 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 the simulator for scheduling the demand
48       * @param operatingArea 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 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 origin terminal
90       * @param terminalTo destination terminal
91       * @param numberAnnual annual number of containers per year
92       * @param fraction20ft fraction of 20 ft containers (rest is 40 ft)
93       * @param fractionEmpty fraction of empty containers (rest is full)
94       * @param fractionOwners map of owners with fraction (adding up to 1.0) indicating who owns the containers
95       */
96      public final void addDemand(final Terminal terminalFrom, final Terminal terminalTo, final int numberAnnual,
97              final double fraction20ft, final double fractionEmpty, final Map<Company, Double> fractionOwners)
98      {
99          DemandCell demandCell =
100                 new DemandCell(terminalFrom, terminalTo, numberAnnual, fraction20ft, fractionEmpty, fractionOwners);
101         addDemand(demandCell);
102     }
103 
104     /**
105      * @param terminalFrom origin terminal
106      * @param terminalTo destination terminal
107      * @return the demand between two terminals.
108      */
109     public final DemandCell getDemand(final Terminal terminalFrom, final Terminal terminalTo)
110     {
111         if (this.demandMap.containsKey(terminalFrom))
112         {
113             return this.demandMap.get(terminalFrom).get(terminalTo);
114         }
115         return null;
116     }
117 
118     /**
119      * @param terminalFrom origin terminal
120      * @return the map of demands originating at a terminal.
121      */
122     public final Map<Terminal, DemandCell> getDemandMapFrom(final Terminal terminalFrom)
123     {
124         return this.demandMap.get(terminalFrom);
125     }
126 
127     /**
128      * @param terminalTo destination terminal
129      * @return the map of demands for a destination terminal.
130      */
131     public final Map<Terminal, DemandCell> getDemandMapTo(final Terminal terminalTo)
132     {
133         Map<Terminal, DemandCell> toMap = new HashMap<Terminal, DemandCell>();
134         for (Terminal from : this.demandMap.keySet())
135         {
136             for (Terminal to : this.demandMap.get(from).keySet())
137             {
138                 if (terminalTo.equals(to))
139                 {
140                     toMap.put(from, this.demandMap.get(from).get(to));
141                 }
142             }
143         }
144         return toMap;
145     }
146 
147     /**
148      * @param terminalFrom origin terminal
149      * @return the demands originating at a terminal as a set.
150      */
151     public final Set<DemandCell> getDemandSetFrom(final Terminal terminalFrom)
152     {
153         return new HashSet<DemandCell>(this.demandMap.get(terminalFrom).values());
154     }
155 
156     /**
157      * @param terminalTo destination terminal
158      * @return the demands for a destination terminal as a set.
159      */
160     public final Set<DemandCell> getDemandSetTo(final Terminal terminalTo)
161     {
162         Set<DemandCell> toSet = new HashSet<DemandCell>();
163         for (Terminal from : this.demandMap.keySet())
164         {
165             for (Terminal to : this.demandMap.get(from).keySet())
166             {
167                 if (terminalTo.equals(to))
168                 {
169                     toSet.add(this.demandMap.get(from).get(to));
170                 }
171             }
172         }
173         return toSet;
174     }
175 
176     /**
177      * @return the simulator
178      */
179     public final DEVSSimulatorInterface.TimeDoubleUnit getSimulator()
180     {
181         return this.simulator;
182     }
183     
184     /**
185      * A cell of demand from a terminal to a terminal.
186      * <p>
187      * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
188      * <br>
189      * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
190      * </p>
191      * <p>
192      * Based on software from the IDVV project, which is Copyright (c) 2013 Rijkswaterstaat - Dienst Water, Verkeer en
193      * Leefomgeving and licensed without restrictions to Delft University of Technology, including the right to sub-license
194      * sources and derived products to third parties.
195      * </p>
196      * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
197      * initial version Nov 6, 2016 <br>
198      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
199      */
200     public class DemandCell
201     {
202         /** origin terminal. */
203         private Terminal terminalFrom;
204 
205         /** destination terminal. */
206         private Terminal terminalTo;
207 
208         /** number of containers per year. */
209         private int numberAnnual;
210 
211         /** fraction of 20 ft containers of total (rest is 40 ft). */
212         private double fraction20ft;
213 
214         /** fraction of empty containers (rest is full). */
215         private double fractionEmpty;
216 
217         /** table with fractions (adding up to 1.0) who owns the containers. */
218         private Map<Company, Double> fractionOwners = new HashMap<Company, Double>();
219 
220         /**
221          * @param terminalFrom origin terminal
222          * @param terminalTo destination terminal
223          * @param numberAnnual annual number of containers per year
224          * @param fraction20ft fraction of 20 ft containers (rest is 40 ft)
225          * @param fractionEmpty fraction of empty containers (rest is full)
226          * @param fractionOwners map of owners with fraction (adding up to 1.0) indicating who owns the containers
227          */
228         public DemandCell(final Terminal terminalFrom, final Terminal terminalTo, final int numberAnnual,
229                 final double fraction20ft, final double fractionEmpty, final Map<Company, Double> fractionOwners)
230         {
231             super();
232             this.terminalFrom = terminalFrom;
233             this.terminalTo = terminalTo;
234             this.numberAnnual = numberAnnual;
235             this.fraction20ft = fraction20ft;
236             this.fractionEmpty = fractionEmpty;
237             this.fractionOwners = fractionOwners;
238         }
239 
240         /**
241          * @return the terminalFrom
242          */
243         public final Terminal getTerminalFrom()
244         {
245             return this.terminalFrom;
246         }
247 
248         /**
249          * @return the terminalTo
250          */
251         public final Terminal getTerminalTo()
252         {
253             return this.terminalTo;
254         }
255 
256         /**
257          * @return the numberAnnual
258          */
259         public final int getNumberAnnual()
260         {
261             return this.numberAnnual;
262         }
263 
264         /**
265          * @return the fraction20ft
266          */
267         public final double getFraction20ft()
268         {
269             return this.fraction20ft;
270         }
271 
272         /**
273          * @return the fractionEmpty
274          */
275         public final double getFractionEmpty()
276         {
277             return this.fractionEmpty;
278         }
279 
280         /**
281          * @return the fractionOwners
282          */
283         public final Map<Company, Double> getFractionOwners()
284         {
285             return this.fractionOwners;
286         }
287 
288     }
289 
290 }