Microscopic Traffic Simulator

- Main Page
- Packages
- Classes
- Files

- File List

CellularTopology.cs
Go to the documentation of this file.
1 using Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.GeneralParameters;
2 using Microscopic_Traffic_Simulator___Model.GeometricObjects;
3 using Microscopic_Traffic_Simulator___Model.GeometricObjects.Lanes;
4 using Microscopic_Traffic_Simulator___Model.GeometricObjects.Nodes;
5 using Microscopic_Traffic_Simulator___Model.SimulationControl;
6 using Microscopic_Traffic_Simulator___Model.TrafficObjects;
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 {
201 return parameters.CellularTopologyParameters.P2_SimulationStepInterval;
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 }
Microscopic_Traffic_Simulator___Model.GeometricObjects.GeometricTopology
Class representing geometric topology of road network.
Definition: GeometricTopology.cs:14
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.GeneralParameters.Parameters
Class containing all simulator parameters.
Definition: Parameters.cs:12
Microscopic_Traffic_Simulator___Model.GeometricObjects.Nodes
Definition: EndNode.cs:4
Microscopic_Traffic_Simulator___Model.SimulationControl
Definition: ISimulationEventsGenerator.cs:3
System
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.CellularTopology.Run
void Run(int?seed=null)
Runs simulation. Seed to be used in the simulation.
Definition: CellularTopology.cs:289
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.GeneralParameters.CellularTopologyParameters.P2_SimulationStepInterval
TimeSpan P2_SimulationStepInterval
Cell length parameter.
Definition: CellularTopologyParameters.cs:19
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.GpsRecordsManager
Manages GPS records for cellular topology.
Definition: GPSRecordsManager.cs:15
Microscopic_Traffic_Simulator___Model.GeometricObjects.Lanes.Lane.StartNode
StartNode StartNode
Node on the input end of lane.
Definition: Lane.cs:23
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.CellularTopology.NextTransitionFunctionStarted
EventHandler< DateTimeEventArgs > NextTransitionFunctionStarted
Event informing that next transition function already started.
Definition: CellularTopology.cs:108
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.CellularTopology.Pause
void Pause()
Pauses simulation.
Definition: CellularTopology.cs:323
Microscopic_Traffic_Simulator___Model.TrafficObjects.Sensor
Class representing sensor.
Definition: Sensor.cs:14
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.DateTimeEventArgs
Class representing event arguments consisting of property containing time information.
Definition: DateTimeEventArgs.cs:8
Microscopic_Traffic_Simulator___Model.GeometricObjects.Nodes.SensorNode
Node containing sensor.
Definition: SensorNode.cs:11
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.GeneralParameters.Parameters.CellularTopologyParameters
CellularTopologyParameters CellularTopologyParameters
Reference to cellular topology parameters.
Definition: Parameters.cs:21
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.CellularTopology.SimulationStepsChanged
EventHandler SimulationStepsChanged
Event handler of change of number of simulation steps.
Definition: CellularTopology.cs:103
Microscopic_Traffic_Simulator___Model.SimulationControl.SimulationState
SimulationState
Current state of the simulation.
Definition: SimulationState.cs:12
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.CellularTopology.CellularTopology
CellularTopology(GeometricTopology geometricTopology, Parameters parameters)
Constructor when the cellular topology is build and simulation is initialized.
Definition: CellularTopology.cs:140
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.CellularTopologyBuilder
Class for building cellular topology from geometric topology.
Definition: CellularTopologyBuilder.cs:13
Microscopic_Traffic_Simulator___Model.SimulationControl.Simulation.CurrentModelTime
TimeSpan CurrentModelTime
Model time of last tick of simulation timer or last change of simulation run.
Definition: Simulation.cs:49
Microscopic_Traffic_Simulator___Model.TrafficObjects
Definition: Generator.cs:7
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.CellularTopology.GetTimeToNextAction
TimeSpan GetTimeToNextAction(Random random)
Method for getting the time to performing the next transition function.
Definition: CellularTopology.cs:197
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.GeneralParameters.CellularTopologyParameters
Class containing parameters related to cellular topology.
Definition: CellularTopologyParameters.cs:9
Microscopic_Traffic_Simulator___Model.GeometricObjects.Nodes.StartNode.Generator
Generator Generator
Generator connected to node.
Definition: StartNode.cs:16
Microscopic_Traffic_Simulator___Model.GeometricObjects.GeometricTopology.Lanes
ReadOnlyCollection< Lane > Lanes
Lanes in geometric topology. </summary
Definition: GeometricTopology.cs:23
Microscopic_Traffic_Simulator___Model.GeometricObjects.Lanes.Lane
Represent single lane.
Definition: Lane.cs:14
Microscopic_Traffic_Simulator___Model.SimulationControl.Simulation.SimulationState
SimulationState SimulationState
Current simulation status of simulation.
Definition: Simulation.cs:96
Microscopic_Traffic_Simulator___Model.GeometricObjects.Nodes.InnerNode
Node on the lane which is not the start or end node.
Definition: InnerNode.cs:10
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.CellularTopology.StepForward
void StepForward(int?seed=null)
Perform step in simulation. Seed to be used in the simulation.
Definition: CellularTopology.cs:299
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.GeneralParameters.Parameters.TransitionFunctionParameters
TransitionFunctionParameters TransitionFunctionParameters
Reference to transition function parameters.
Definition: Parameters.cs:31
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.GeneralParameters
Definition: CellularTopologyParameters.cs:3
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.CellularTopology.PerformAction
void PerformAction(Random random)
Method for performing transition function.
Definition: CellularTopology.cs:214
Microscopic_Traffic_Simulator___Model.GeometricObjects.Lanes.Lane.InnerNodes
ReadOnlyCollection< InnerNode > InnerNodes
Inner nodes on the lane.
Definition: Lane.cs:41
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.CellularTopology
Class representing cellular topology.
Definition: CellularTopology.cs:16
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects
Definition: Car.cs:6
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.GeneratorsManager
Manages generators and sensors in topology.
Definition: GeneratorsManager.cs:12
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.CellularTopology.Stop
void Stop()
Stops simulation.
Definition: CellularTopology.cs:308
Microscopic_Traffic_Simulator___Model.SimulationControl.Simulation
Class controlling performing simulation actions by simulation calendar.
Definition: Simulation.cs:12
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.CellularTopology.Dispose
virtual void Dispose(bool disposing)
Dispose disposable fields.
Definition: CellularTopology.cs:342
Microscopic_Traffic_Simulator___Model.GeometricObjects.Lanes
Definition: BezierLane.cs:5
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.CellularTopology.Dispose
void Dispose()
Disposes cellular topology resources.
Definition: CellularTopology.cs:362
Microscopic_Traffic_Simulator___Model.GeometricObjects
Definition: GeometricTopology.cs:8
Microscopic_Traffic_Simulator___Model.SimulationControl.ISimulationEventsGenerator
Every class which performs some repeating action should implement this interface
Definition: ISimulationEventsGenerator.cs:8
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.CarsManager
Manages cars in cellular topology.
Definition: CarsManager.cs:11
Microscopic_Traffic_Simulator___Model
Microscopic_Traffic_Simulator___Model.SimulationControl.Simulation.Dispose
virtual void Dispose(bool disposing)
Dispose disposable fields.
Definition: Simulation.cs:519
Microscopic_Traffic_Simulator___Model.GeometricObjects.Nodes.StartNode.ContainsGenerator
bool ContainsGenerator
Determines whether the node contains generator.
Definition: StartNode.cs:21