Microscopic Traffic Simulator
CellularTopology.cs
Go to the documentation of this file.
7 using System;
8 using System.Collections.Generic;
9 using System.Threading;
10 
11 namespace Microscopic_Traffic_Simulator___Model.CellularTopologyObjects
12 {
16  public class CellularTopology : ISimulationEventsGenerator, IDisposable
17  {
21  private CarsManager carsManager = new CarsManager();
25  public CarsManager CarsManager { get { return carsManager; } }
26 
30  private GeneratorsManager generatorsManager;
34  public GeneratorsManager GeneratorsManager { get { return generatorsManager; } }
35 
39  private GpsRecordsManager gpsRecordsManager;
43  public GpsRecordsManager GpsRecordsManager { get { return gpsRecordsManager; } }
44 
48  private Simulation simulation;
52  public Simulation Simulation { get { return simulation; } }
53 
57  private bool firstSimulationStepPerformed;
58 
63  private Mutex carsDictionariesMutex = new Mutex();
68  public Mutex CarsDictionariesMutex { get { return carsDictionariesMutex; } }
69 
73  private ulong simulationSteps = 0;
77  public ulong SimulationSteps { get { return simulationSteps; } }
78 
82  private ulong simulationStepsToPause = ulong.MaxValue;
86  public ulong SimulationStepsToPause
87  {
88  get { return simulationStepsToPause; }
89  set { simulationStepsToPause = value; }
90  }
95  private bool IsSimulationStepsToPauseReached
96  {
97  get { return simulationSteps >= simulationStepsToPause; }
98  }
99 
103  public event EventHandler SimulationStepsChanged;
104 
108  public event EventHandler<DateTimeEventArgs> NextTransitionFunctionStarted;
109 
113  private const int CellularTopologySimulationPriority = 1;
114 
118  public int Priority { get { return CellularTopologySimulationPriority; } }
119 
123  public bool IsPauseScheduled { get { return IsSimulationStepsToPauseReached; } }
124 
128  private Parameters parameters;
132  public Parameters Parameters { get { return parameters; } }
133 
140  public CellularTopology(GeometricTopology geometricTopology, Parameters parameters)
141  {
142  this.parameters = parameters;
143  gpsRecordsManager = new GpsRecordsManager(carsManager, parameters.CellularTopologyParameters);
144  generatorsManager = new GeneratorsManager(carsManager, parameters.TransitionFunctionParameters,
145  gpsRecordsManager);
146  BuildCellularTopology(geometricTopology, parameters.CellularTopologyParameters);
147  InitializeSimulation(geometricTopology);
148  }
149 
156  private void BuildCellularTopology(GeometricTopology geometricTopology,
157  CellularTopologyParameters cellularTopologyParameters)
158  {
159  new CellularTopologyBuilder(geometricTopology, cellularTopologyParameters).Build();
160  }
161 
167  private void InitializeSimulation(GeometricTopology geometricTopology)
168  {
169  firstSimulationStepPerformed = false;
170  List<ISimulationEventsGenerator> simulationEventsGenerators = new List<ISimulationEventsGenerator>();
171  simulationEventsGenerators.Add(this);
172  foreach (Lane lane in geometricTopology.Lanes)
173  {
174  if (lane.StartNode.ContainsGenerator)
175  {
176  generatorsManager.InitializeGeneratorForSimulation(
177  lane.StartNode.Generator, simulationEventsGenerators);
178  }
179  foreach (InnerNode laneInnerNode in lane.InnerNodes)
180  {
181  if (laneInnerNode is SensorNode)
182  {
183  Sensor sensor = (laneInnerNode as SensorNode).Sensor;
184  generatorsManager.InitializeGeneratorForSimulation(sensor, simulationEventsGenerators);
185  sensor.CellularTopologyParameters = parameters.CellularTopologyParameters;
186  }
187  }
188  }
189  simulation = new Simulation(simulationEventsGenerators);
190  }
191 
197  public TimeSpan GetTimeToNextAction(Random random)
198  {
199  if (firstSimulationStepPerformed)
200  {
202  }
203  else
204  {
205  firstSimulationStepPerformed = true;
206  return TimeSpan.Zero;
207  }
208  }
209 
214  public void PerformAction(Random random)
215  {
216  //store to temporar variable the DateTime of planned performing start
217  //of the last transition function.
218  DateTime dateTimeNow = DateTime.Now;
219  //ensure that car dictionaries are not used by renderer
220  carsDictionariesMutex.WaitOne();
221  //notiy about start of simulation step.
222  OnNextTransitionFunctionStarted(dateTimeNow);
223 
224  carsManager.SwapCarsPreviousAndCurrentDictionaries();
225  carsManager.ProcessCarsOutsideOfTopology();
226  generatorsManager.ProcessGenerators(simulation.CurrentModelTime);
227  carsManager.PerformTransitionForAllCars(random, simulation.CurrentModelTime);
228  gpsRecordsManager.ProcessInputGPSRecords(simulation.CurrentModelTime);
229 
230  //the cars mutex can be now released.
231  carsDictionariesMutex.ReleaseMutex();
232 
233  carsManager.PrepareCarsForNextSimulationStep();
234  gpsRecordsManager.RecordCarsGPSLocations(simulation.CurrentModelTime);
235 
236  //increment number of simulation steps
237  simulationSteps++;
238  OnSimulationStepsChanged();
239  }
240 
244  private void OnSimulationStepsChanged()
245  {
246  if (SimulationStepsChanged != null)
247  {
248  SimulationStepsChanged(this, EventArgs.Empty);
249  }
250  }
251 
255  private void ResetSimulationStepsToPause()
256  {
257  simulationStepsToPause = ulong.MaxValue;
258  }
259 
264  private void ResetSimulationStepsToPauseIfReached()
265  {
266  if (IsSimulationStepsToPauseReached)
267  {
268  ResetSimulationStepsToPause();
269  }
270  }
271 
275  private void RunOrStep()
276  {
277  ResetSimulationStepsToPauseIfReached();
278  if (simulation.SimulationState == SimulationState.NotRunning)
279  {
280  generatorsManager.InitializeGenerators();
281  gpsRecordsManager.LoadInputRecordsAndInitializeOutputRecords();
282  }
283  }
284 
289  public void Run(int? seed = null)
290  {
291  RunOrStep();
292  simulation.Run(seed);
293  }
294 
299  public void StepForward(int? seed = null)
300  {
301  RunOrStep();
302  simulation.StepForward(seed);
303  }
304 
308  public void Stop()
309  {
310  simulation.Stop();
311  simulationSteps = 0;
312  firstSimulationStepPerformed = false;
313  OnSimulationStepsChanged();
314  ResetSimulationStepsToPause();
315  carsManager.ClearCars();
316  generatorsManager.FinalizeGeneratorsWork();
317  gpsRecordsManager.ResetGPSInputRecordsInformationAndSaveInputRecords();
318  }
319 
323  public void Pause()
324  {
325  simulation.Pause();
326  }
327 
332  private void OnNextTransitionFunctionStarted(DateTime dateTimeEventArg)
333  {
334  if (NextTransitionFunctionStarted != null)
335  NextTransitionFunctionStarted(this, new DateTimeEventArgs() { DateTime = dateTimeEventArg });
336  }
337 
342  protected virtual void Dispose(bool disposing)
343  {
344  if (disposing)
345  {
346  if (carsDictionariesMutex != null)
347  {
348  carsDictionariesMutex.Dispose();
349  carsDictionariesMutex = null;
350  }
351  if (simulation != null)
352  {
353  simulation.Dispose();
354  simulation = null;
355  }
356  }
357  }
358 
362  public void Dispose()
363  {
364  Dispose(true);
365  GC.SuppressFinalize(this);
366  }
367  }
368 }
Class representing geometric topology of road network.
void Run(int?seed=null)
Runs simulation. Seed to be used in the simulation.
StartNode StartNode
Node on the input end of lane.
Definition: Lane.cs:23
EventHandler< DateTimeEventArgs > NextTransitionFunctionStarted
Event informing that next transition function already started.
Class representing event arguments consisting of property containing time information.
CellularTopologyParameters CellularTopologyParameters
Reference to cellular topology parameters.
Definition: Parameters.cs:21
EventHandler SimulationStepsChanged
Event handler of change of number of simulation steps.
CellularTopology(GeometricTopology geometricTopology, Parameters parameters)
Constructor when the cellular topology is build and simulation is initialized.
TimeSpan CurrentModelTime
Model time of last tick of simulation timer or last change of simulation run.
Definition: Simulation.cs:49
TimeSpan GetTimeToNextAction(Random random)
Method for getting the time to performing the next transition function.
ReadOnlyCollection< Lane > Lanes
Lanes in geometric topology. </summary
SimulationState SimulationState
Current simulation status of simulation.
Definition: Simulation.cs:96
Node on the lane which is not the start or end node.
Definition: InnerNode.cs:10
void StepForward(int?seed=null)
Perform step in simulation. Seed to be used in the simulation.
TransitionFunctionParameters TransitionFunctionParameters
Reference to transition function parameters.
Definition: Parameters.cs:31
void PerformAction(Random random)
Method for performing transition function.
ReadOnlyCollection< InnerNode > InnerNodes
Inner nodes on the lane.
Definition: Lane.cs:41
Class controlling performing simulation actions by simulation calendar.
Definition: Simulation.cs:12
virtual void Dispose(bool disposing)
Dispose disposable fields.
Every class which performs some repeating action should implement this interface
virtual void Dispose(bool disposing)
Dispose disposable fields.
Definition: Simulation.cs:519
bool ContainsGenerator
Determines whether the node contains generator.
Definition: StartNode.cs:21