View Javadoc
1   package org.opentrafficsim.kpi.sampling.indicator;
2   
3   import java.util.HashSet;
4   import java.util.Set;
5   
6   import org.djunits.unit.LengthUnit;
7   import org.djunits.value.vdouble.scalar.Duration;
8   import org.djunits.value.vdouble.scalar.Length;
9   import org.djunits.value.vdouble.scalar.Time;
10  import org.opentrafficsim.kpi.interfaces.LinkDataInterface;
11  import org.opentrafficsim.kpi.sampling.Query;
12  import org.opentrafficsim.kpi.sampling.TrajectoryGroup;
13  
14  /**
15   * Sum of (approximate) link lengths divided by mean speed, divided by link length in km. 
16   * <p>
17   * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
18   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
19   * <p>
20   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 1 okt. 2016 <br>
21   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
22   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
23   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
24   */
25  public class MeanTravelTimePerKm extends AbstractIndicator<Duration>
26  {
27  
28      /** Mean speed indicator. */
29      private final MeanSpeed meanSpeed;
30  
31      /**
32       * @param meanSpeed mean speed indicator
33       */
34      public MeanTravelTimePerKm(final MeanSpeed meanSpeed)
35      {
36          this.meanSpeed = meanSpeed;
37      }
38  
39      /** {@inheritDoc} */
40      @Override
41      public final Duration calculate(final Query query, final Time startTime, final Time endTime)
42      {
43          Length cumulLength = Length.ZERO;
44          Set<LinkDataInterface> links = new HashSet<>();
45          for (TrajectoryGroup trajectoryGroup : query.getTrajectoryGroups(startTime, endTime))
46          {
47              if (!links.contains(trajectoryGroup.getLaneDirection().getLaneData().getLinkData()))
48              {
49                  cumulLength = cumulLength.plus(trajectoryGroup.getLength()); // TODO should be average lane length of link
50                  links.add(trajectoryGroup.getLaneDirection().getLaneData().getLinkData());
51              }
52          }
53          return cumulLength.divideBy(this.meanSpeed.getValue(query, startTime, endTime)).divideBy(cumulLength.getInUnit(LengthUnit.KILOMETER));
54      }
55  
56      /** {@inheritDoc} */
57      @Override
58      @SuppressWarnings("checkstyle:designforextension")
59      public String toString()
60      {
61          return "MeanTravelTime [meanTravelTime=" + this.meanSpeed + " (per km)]";
62      }
63  
64  }