View Javadoc
1   package org.opentrafficsim.kpi.sampling.indicator;
2   
3   import java.util.LinkedHashMap;
4   import java.util.List;
5   import java.util.Map;
6   
7   import org.djunits.unit.DurationUnit;
8   import org.djunits.value.vdouble.scalar.Duration;
9   import org.djunits.value.vdouble.scalar.Time;
10  import org.djunits.value.vfloat.vector.FloatSpeedVector;
11  import org.djutils.exceptions.Throw;
12  import org.opentrafficsim.kpi.interfaces.GtuData;
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.data.ReferenceSpeed;
18  
19  /**
20   * Delay based on reference speed.
21   * <p>
22   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
23   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
24   * </p>
25   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
26   * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
27   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
28   */
29  public class TotalDelayReference extends AbstractIndicator<Duration>
30  {
31  
32      /** Reference speed extended data type. */
33      private static final ReferenceSpeed REF_SPEED_TYPE = ReferenceSpeed.INSTANCE;
34  
35      @Override
36      protected final <G extends GtuData> Duration calculate(final Query<G, ?> query, final Time startTime, final Time endTime,
37              final List<TrajectoryGroup<G>> trajectoryGroups)
38      {
39          Map<String, Duration> gtuTimes = new LinkedHashMap<>();
40          Map<String, Duration> gtuRefTimes = new LinkedHashMap<>();
41          for (TrajectoryGroup<? extends GtuData> trajectoryGroup : trajectoryGroups)
42          {
43              try
44              {
45                  for (Trajectory<?> trajectory : trajectoryGroup.getTrajectories())
46                  {
47                      Duration sumTime;
48                      Duration sumRefTime;
49                      if (gtuTimes.containsKey(trajectory.getGtuId()))
50                      {
51                          sumTime = gtuTimes.get(trajectory.getGtuId());
52                          sumRefTime = gtuRefTimes.get(trajectory.getGtuId());
53                      }
54                      else
55                      {
56                          sumTime = Duration.ZERO;
57                          sumRefTime = Duration.ZERO;
58                      }
59                      Throw.when(!trajectory.contains(REF_SPEED_TYPE), UnsupportedOperationException.class,
60                              "TotalDelayReference can only work with trajectories that have %s extended data.",
61                              REF_SPEED_TYPE.getId());
62                      FloatSpeedVector refSpeed = trajectory.getExtendedData(REF_SPEED_TYPE);
63                      float[] x = trajectory.getX();
64                      for (int i = 1; i < refSpeed.size(); i++)
65                      {
66                          double refV = refSpeed.get(i - 1).si;
67                          double dx = x[i] - x[i - 1];
68                          sumRefTime = sumRefTime.plus(new Duration(dx / refV, DurationUnit.SI));
69                      }
70                      gtuTimes.put(trajectory.getGtuId(), sumTime.plus(trajectory.getTotalDuration()));
71                      gtuRefTimes.put(trajectory.getGtuId(), sumRefTime);
72                  }
73              }
74              catch (SamplingException exception)
75              {
76                  throw new RuntimeException("Exception while trying to determine delay in trajectory.", exception);
77              }
78          }
79          Duration delaySum = Duration.ZERO;
80          for (String id : gtuTimes.keySet())
81          {
82              Duration gtuTime = gtuTimes.get(id);
83              Duration gtuRefTime = gtuRefTimes.get(id);
84              if (gtuTime.gt(gtuRefTime))
85              {
86                  delaySum = delaySum.plus(gtuTime.minus(gtuRefTime));
87              }
88          }
89          return delaySum;
90      }
91  
92      @Override
93      @SuppressWarnings("checkstyle:designforextension")
94      public String toString()
95      {
96          return "TotalDelayReference";
97      }
98  
99  }