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-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  12.  * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  13.  * </p>
  14.  * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
  15.  * @param left double; Left desire.
  16.  * @param right double; Right desire.
  17.  */
  18. public record Desire(double left, double right) implements Serializable
  19. {

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

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

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

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

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

  60.     /**
  61.      * Returns whether the left desire is larger than (or equal to) the right.
  62.      * @return Returns whether the left desire is larger than (or equal to) the right.
  63.      */
  64.     public final boolean leftIsLargerOrEqual()
  65.     {
  66.         return this.left >= this.right;
  67.     }

  68. }