View Javadoc
1   package org.opentrafficsim.road.network.sampling.indicator;
2   
3   import java.util.HashMap;
4   import java.util.List;
5   import java.util.Map;
6   
7   import org.djunits.unit.TimeUnit;
8   import org.djunits.value.ValueException;
9   import org.djunits.value.vdouble.scalar.Duration;
10  import org.djunits.value.vdouble.scalar.Time;
11  import org.djunits.value.vfloat.scalar.FloatSpeed;
12  import org.djunits.value.vfloat.vector.FloatLengthVector;
13  import org.opentrafficsim.kpi.sampling.Query;
14  import org.opentrafficsim.kpi.sampling.SamplingException;
15  import org.opentrafficsim.kpi.sampling.Trajectory;
16  import org.opentrafficsim.kpi.sampling.TrajectoryGroup;
17  import org.opentrafficsim.kpi.sampling.indicator.AbstractIndicator;
18  import org.opentrafficsim.road.network.sampling.data.ReferenceSpeed;
19  
20  import nl.tudelft.simulation.language.Throw;
21  
22  /**
23   * <p>
24   * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
25   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
26   * <p>
27   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 21 nov. 2016 <br>
28   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
29   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
30   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
31   */
32  
33  public class TotalDelayReference extends AbstractIndicator<Duration>
34  {
35  
36      /** Reference speed extended data type. */
37      private static final ReferenceSpeed REF_SPEED_TYPE = new ReferenceSpeed();
38  
39      /** {@inheritDoc} */
40      @Override
41      protected final Duration calculate(final Query query, final Time startTime, final Time endTime,
42              final List<TrajectoryGroup> trajectoryGroups)
43      {
44          Map<String, Duration> gtuTimes = new HashMap<>();
45          Map<String, Duration> gtuRefTimes = new HashMap<>();
46          for (TrajectoryGroup trajectoryGroup : trajectoryGroups)
47          {
48              try
49              {
50                  for (Trajectory trajectory : trajectoryGroup.getTrajectories())
51                  {
52                      Duration sumTime;
53                      Duration sumRefTime;
54                      if (gtuTimes.containsKey(trajectory.getGtuId()))
55                      {
56                          sumTime = gtuTimes.get(trajectory.getGtuId());
57                          sumRefTime = gtuRefTimes.get(trajectory.getGtuId());
58                      }
59                      else
60                      {
61                          sumTime = Duration.ZERO;
62                          sumRefTime = Duration.ZERO;
63                      }
64                      Throw.when(!trajectory.contains(REF_SPEED_TYPE), UnsupportedOperationException.class,
65                              "TotalDelayReference can only work with trajectories that have %s extended data.",
66                              REF_SPEED_TYPE.getId());
67                      List<FloatSpeed> refSpeed = trajectory.getExtendedData(REF_SPEED_TYPE);
68                      float[] x = trajectory.getX();
69                      for (int i = 1; i < refSpeed.size(); i++)
70                      {
71                          double refV;
72                          if (!Double.isNaN(refSpeed.get(i).si))
73                          {
74                              refV = refSpeed.get(i - 1).si;
75                          }
76                          else
77                          {
78                              refV = (refSpeed.get(i - 1).si + refSpeed.get(i).si) / 2.0;
79                          }
80                          double dx = x[i] - x[i - 1];
81                          sumRefTime = sumRefTime.plus(new Duration(dx / refV, TimeUnit.SI));
82                      }
83                      gtuTimes.put(trajectory.getGtuId(), sumTime.plus(trajectory.getTotalDuration()));
84                      gtuRefTimes.put(trajectory.getGtuId(), sumRefTime);
85                  }
86              }
87              catch (SamplingException exception)
88              {
89                  throw new RuntimeException("Exception while trying to determine delay in trajectory.", exception);
90              }
91          }
92          Duration delaySum = Duration.ZERO;
93          for (String id : gtuTimes.keySet())
94          {
95              Duration gtuTime = gtuTimes.get(id);
96              Duration gtuRefTime = gtuRefTimes.get(id);
97              if (gtuTime.gt(gtuRefTime))
98              {
99                  delaySum = delaySum.plus(gtuTime.minus(gtuRefTime));
100             }
101         }
102         return delaySum;
103     }
104 
105     /** {@inheritDoc} */
106     @Override
107     @SuppressWarnings("checkstyle:designforextension")
108     public String toString()
109     {
110         return "TotalDelayReference";
111     }
112 
113 }