Desire.java

  1. package org.opentrafficsim.road.gtu.lane.tactical.util.lmrs;

  2. import java.io.Serializable;

  3. import org.djunits.value.vdouble.scalar.Dimensionless;
  4. import org.opentrafficsim.core.network.LateralDirectionality;

  5. /**
  6.  * Reflects the level of lane change desire a driver experiences in both the left and right direction. This may be either total
  7.  * desire, or only for a single lane change incentive. Desire is defined as ranging from 0 to 1, where 0 means no desire and 1
  8.  * means full desire. Values above 1 are not valid and should be limited to 1. Values below 0 are allowed and reflect that a
  9.  * lane change is undesired (which is different from not desired).
  10.  * <p>
  11.  * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  12.  * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
  13.  * <p>
  14.  * @version $Revision$, $LastChangedDate$, by $Author$, initial version Apr 13, 2016 <br>
  15.  * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  16.  */
  17. public class Desire implements Serializable
  18. {

  19.     /** */
  20.     private static final long serialVersionUID = 20160413L;

  21.     /** Level of left desire. */
  22.     private final double left;

  23.     /** Level of right desire. */
  24.     private final double right;

  25.     /** Easy access and efficient zero desired. */
  26.     public static final Desire ZERO = new Desire(0.0, 0.0);

  27.     /**
  28.      * Constructor which sets the supplied desire. Desire is limited to a maximum of 1.
  29.      * @param left double; Left desire.
  30.      * @param right double; Right desire.
  31.      */
  32.     public Desire(final double left, final double right)
  33.     {
  34.         this.left = left <= 1 ? left : 1;
  35.         this.right = right <= 1 ? right : 1;
  36.     }

  37.     /**
  38.      * Constructor which sets the supplied desire. Desire is limited to a maximum of 1.
  39.      * @param left Dimensionless; Left desire.
  40.      * @param right Dimensionless; Right desire.
  41.      */
  42.     public Desire(final Dimensionless left, final Dimensionless right)
  43.     {
  44.         this(left.si, right.si);
  45.     }

  46.     /**
  47.      * Returns desire in the given direction.
  48.      * @param dir LateralDirectionality; Direction for the desire to return.
  49.      * @return Desire in the given direction.
  50.      */
  51.     public final double get(final LateralDirectionality dir)
  52.     {
  53.         if (dir.equals(LateralDirectionality.LEFT))
  54.         {
  55.             return this.left;
  56.         }
  57.         if (dir.equals(LateralDirectionality.RIGHT))
  58.         {
  59.             return this.right;
  60.         }
  61.         throw new RuntimeException("Lateral direction may not be NONE.");
  62.     }

  63.     /**
  64.      * Returns lane change desire to left.
  65.      * @return Lane change desire to left.
  66.      */
  67.     public final double getLeft()
  68.     {
  69.         return this.left;
  70.     }

  71.     /**
  72.      * Returns lane change desire to right.
  73.      * @return Lane change desire to right.
  74.      */
  75.     public final double getRight()
  76.     {
  77.         return this.right;
  78.     }

  79.     /**
  80.      * Returns whether the left desire is larger than (or equal to) the right.
  81.      * @return Returns whether the left desire is larger than (or equal to) the right.
  82.      */
  83.     public final boolean leftIsLargerOrEqual()
  84.     {
  85.         return this.left >= this.right;
  86.     }

  87.     /** {@inheritDoc} */
  88.     @Override
  89.     public final String toString()
  90.     {
  91.         return "Desire [left=" + this.left + ", right=" + this.right + "]";
  92.     }

  93. }