LinkWeight.java

  1. package org.opentrafficsim.core.network;

  2. import org.jgrapht.alg.interfaces.AStarAdmissibleHeuristic;

  3. /**
  4.  * Interface to determine a link weight.
  5.  * <p>
  6.  * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  7.  * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  8.  * </p>
  9.  * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  10.  * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
  11.  * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
  12.  */
  13. public interface LinkWeight
  14. {

  15.     /** Default link weight using link length. */
  16.     LinkWeight LENGTH = new LinkWeight()
  17.     {
  18.         /** {@inheritDoc} */
  19.         @Override
  20.         public double getWeight(final Link link)
  21.         {
  22.             return link.getLength().si;
  23.         }

  24.         /** {@inheritDoc} */
  25.         @Override
  26.         public String toString()
  27.         {
  28.             return "LENGTH";
  29.         }
  30.     };

  31.     /** Link weight with very high penalty for Connectors. */
  32.     LinkWeight LENGTH_NO_CONNECTORS = new LinkWeight()
  33.     {
  34.         /**
  35.          * Length that should encourage Dijkstra to not include links that have this length. On the other hand, this value (when
  36.          * on an unavoidable link) should not cause underflow problems.
  37.          */
  38.         static final double PROHIBITIVE_CONNECTOR_LENGTH = 1000000;

  39.         /** {@inheritDoc} */
  40.         @Override
  41.         public double getWeight(final Link link)
  42.         {
  43.             if (link.isConnector())
  44.             {
  45.                 return PROHIBITIVE_CONNECTOR_LENGTH;
  46.             }
  47.             return link.getLength().si;
  48.         }

  49.         /** {@inheritDoc} */
  50.         @Override
  51.         public String toString()
  52.         {
  53.             return "LENGTH_NO_CONNECTORS";
  54.         }
  55.     };

  56.     /** Link weight with very high penalty for Connectors. */
  57.     LinkWeight ASTAR_LENGTH_NO_CONNECTORS = new LinkWeight()
  58.     {
  59.         /** {@inheritDoc} */
  60.         @Override
  61.         public double getWeight(final Link link)
  62.         {
  63.             return LENGTH_NO_CONNECTORS.getWeight(link);
  64.         }

  65.         /** {@inheritDoc} */
  66.         @Override
  67.         public AStarAdmissibleHeuristic<Node> getAStarHeuristic()
  68.         {
  69.             return EUCLIDEAN_DISTANCE;
  70.         }

  71.         /** {@inheritDoc} */
  72.         @Override
  73.         public String toString()
  74.         {
  75.             return "ASTAR_LENGTH_NO_CONNECTORS";
  76.         }
  77.     };

  78.     /**
  79.      * Heuristic for the A* algorithm that uses Euclidean distance.
  80.      */
  81.     AStarAdmissibleHeuristic<Node> EUCLIDEAN_DISTANCE = new AStarAdmissibleHeuristic<>()
  82.     {
  83.         /** {@inheritDoc} */
  84.         @Override
  85.         public double getCostEstimate(final Node sourceVertex, final Node targetVertex)
  86.         {
  87.             return sourceVertex.getPoint().distance(targetVertex.getPoint());
  88.         }
  89.     };

  90.     /**
  91.      * Returns the link weight.
  92.      * @param link Link; link
  93.      * @return double; link weight
  94.      */
  95.     double getWeight(Link link);

  96.     /**
  97.      * Return a heuristic for the A* algorithm. The default value is {@code null} in which case {@code Network} will use a
  98.      * regular Dijkstra shortest path algorithm.
  99.      * @return AStarAdmissibleHeuristic&lt;Node&gt;; heuristic for the A* algorithm, default is {@code null}.
  100.      */
  101.     default AStarAdmissibleHeuristic<Node> getAStarHeuristic()
  102.     {
  103.         return null;
  104.     }

  105.     /**
  106.      * Returns whether the link weights are static. In that case caching may be done on shortest routes.
  107.      * @return boolean; whether the link weights are static.
  108.      */
  109.     default boolean isStatic()
  110.     {
  111.         return true;
  112.     }
  113. }