StochasticDistractionModel.java

  1. package org.opentrafficsim.road.gtu.lane.perception.mental.sdm;

  2. import java.rmi.RemoteException;
  3. import java.util.LinkedHashMap;
  4. import java.util.LinkedHashSet;
  5. import java.util.LinkedList;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.Queue;
  9. import java.util.Set;

  10. import org.opentrafficsim.core.gtu.Try;
  11. import org.opentrafficsim.core.network.Network;
  12. import org.opentrafficsim.core.network.OTSNetwork;
  13. import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
  14. import org.opentrafficsim.road.gtu.lane.perception.mental.Fuller;
  15. import org.opentrafficsim.road.gtu.lane.perception.mental.Fuller.Task;
  16. import org.opentrafficsim.road.gtu.lane.perception.mental.Mental;

  17. import nl.tudelft.simulation.dsol.SimRuntimeException;
  18. import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface;
  19. import nl.tudelft.simulation.event.EventInterface;
  20. import nl.tudelft.simulation.event.EventListenerInterface;
  21. import nl.tudelft.simulation.language.Throw;

  22. /**
  23.  * Stochastic Distraction Model by Manuel Lindorfer.
  24.  * <p>
  25.  * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  26.  * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
  27.  * <p>
  28.  * @version $Revision$, $LastChangedDate$, by $Author$, initial version 28 jun. 2018 <br>
  29.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  30.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  31.  * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  32.  */
  33. public class StochasticDistractionModel implements EventListenerInterface
  34. {

  35.     /** Whether to allow multi-tasking. */
  36.     private final boolean allowMultiTasking;

  37.     /** List of distractions. */
  38.     private final List<Distraction> distractions;

  39.     /** Simulator. */
  40.     private final DEVSSimulatorInterface.TimeDoubleUnit simulator;

  41.     /** Network. */
  42.     private final OTSNetwork network;

  43.     /** Set of distracted GTUs. */
  44.     private final Set<String> distractedGTUs = new LinkedHashSet<>();

  45.     /** Queue of distractions per GTU. */
  46.     private final Map<String, Queue<Distraction>> distractionQueues = new LinkedHashMap<>();

  47.     /**
  48.      * Constructor. This model will react to GTU's being created in simulation and apply distractions.
  49.      * @param allowMultiTasking boolean; whether to allow multi-tasking
  50.      * @param distractions List; list of distractions
  51.      * @param simulator DEVSSimulatorInterface.TimeDoubleUnit; simulator
  52.      * @param network OTSNetwork; network
  53.      */
  54.     public StochasticDistractionModel(final boolean allowMultiTasking, final List<Distraction> distractions,
  55.             final DEVSSimulatorInterface.TimeDoubleUnit simulator, final OTSNetwork network)
  56.     {
  57.         Throw.whenNull(distractions, "List of tasks may not be null.");
  58.         Throw.whenNull(simulator, "Simulator may not be null.");
  59.         Throw.whenNull(network, "Network may not be null.");
  60.         this.allowMultiTasking = allowMultiTasking;
  61.         this.distractions = distractions;
  62.         this.simulator = simulator;
  63.         this.network = network;
  64.         network.addListener(this, Network.GTU_ADD_EVENT);
  65.         network.addListener(this, Network.GTU_REMOVE_EVENT);
  66.     }

  67.     /**
  68.      * Start a distraction.
  69.      * @param gtu LaneBasedGTU; gtu to start the distraction on
  70.      * @param distraction Distraction; distraction
  71.      * @param scheduleNext boolean; whether to schedule the next distraction (not if starting from queue)
  72.      * @throws SimRuntimeException on time error
  73.      */
  74.     public void startDistraction(final LaneBasedGTU gtu, final Distraction distraction, final boolean scheduleNext)
  75.             throws SimRuntimeException
  76.     {
  77.         if (gtu.isDestroyed())
  78.         {
  79.             return;
  80.         }
  81.         String gtuId = gtu.getId();
  82.         if (this.allowMultiTasking || !this.distractedGTUs.contains(gtuId))
  83.         {
  84.             // start the distraction now
  85.             if (!this.allowMultiTasking)
  86.             {
  87.                 this.distractedGTUs.add(gtuId);
  88.             }
  89.             Task task = distraction.getTask(gtu);
  90.             ((Fuller) gtu.getTacticalPlanner().getPerception().getMental()).addTask(task);
  91.             // stop the distraction
  92.             this.simulator.scheduleEventRel(distraction.nextDuration(), this, this, "stopDistraction",
  93.                     new Object[] { gtu, task });
  94.         }
  95.         else
  96.         {
  97.             // need to queue distraction
  98.             if (!this.distractionQueues.containsKey(gtuId))
  99.             {
  100.                 this.distractionQueues.put(gtuId, new LinkedList<>());
  101.             }
  102.             this.distractionQueues.get(gtuId).add(distraction);
  103.         }
  104.         if (scheduleNext)
  105.         {
  106.             // schedule next distraction
  107.             this.simulator.scheduleEventRel(distraction.nextInterArrival(), this, this, "startDistraction",
  108.                     new Object[] { gtu, distraction, true });
  109.         }
  110.     }

  111.     /**
  112.      * Stops a distraction task.
  113.      * @param gtu LaneBasedGTU; gtu to stop the task for
  114.      * @param task Task; task to stop
  115.      * @throws SimRuntimeException on time error
  116.      */
  117.     public void stopDistraction(final LaneBasedGTU gtu, final Task task) throws SimRuntimeException
  118.     {
  119.         if (gtu.isDestroyed())
  120.         {
  121.             return;
  122.         }
  123.         String gtuId = gtu.getId();
  124.         ((Fuller) gtu.getTacticalPlanner().getPerception().getMental()).removeTask(task);
  125.         // start next distraction if any in queue
  126.         if (!this.allowMultiTasking)
  127.         {
  128.             this.distractedGTUs.remove(gtuId);
  129.             if (this.distractionQueues.containsKey(gtuId))
  130.             {
  131.                 Queue<Distraction> queue = this.distractionQueues.get(gtuId);
  132.                 Distraction distraction = queue.poll();
  133.                 startDistraction(gtu, distraction, false);
  134.                 if (queue.isEmpty())
  135.                 {
  136.                     this.distractionQueues.remove(gtuId);
  137.                 }
  138.             }
  139.         }
  140.     }

  141.     /** {@inheritDoc} */
  142.     @Override
  143.     public void notify(final EventInterface event) throws RemoteException
  144.     {
  145.         if (event.getType().equals(Network.GTU_ADD_EVENT))
  146.         {
  147.             String gtuId = (String) event.getContent();
  148.             LaneBasedGTU gtu = (LaneBasedGTU) this.network.getGTU(gtuId);
  149.             Mental mental = gtu.getTacticalPlanner().getPerception().getMental();
  150.             if (mental != null && mental instanceof Fuller)
  151.             {
  152.                 for (Distraction distraction : this.distractions)
  153.                 {
  154.                     if (distraction.nextExposure())
  155.                     {
  156.                         Try.execute(
  157.                                 () -> this.simulator.scheduleEventRel(distraction.nextInterArrival(), this, this,
  158.                                         "startDistraction", new Object[] { gtu, distraction, true }),
  159.                                 "Exception while scheduling distraction start.");
  160.                     }
  161.                 }
  162.             }
  163.         }
  164.         else if (event.getType().equals(Network.GTU_REMOVE_EVENT))
  165.         {
  166.             String gtuId = (String) event.getContent();
  167.             if (!this.allowMultiTasking)
  168.             {
  169.                 this.distractedGTUs.remove(gtuId);
  170.             }
  171.             this.distractionQueues.remove(gtuId);
  172.         }
  173.     }

  174. }