Microscopic Traffic Simulator

- Main Page
- Packages
- Classes
- Files

- File List

CarsManager.cs
Go to the documentation of this file.
1 using Microscopic_Traffic_Simulator___Model.TrafficObjects;
2 using System;
3 using System.Collections.Generic;
4 using System.Collections.ObjectModel;
5
6 namespace Microscopic_Traffic_Simulator___Model.CellularTopologyObjects
7 {
11 public class CarsManager
12 {
17 private Dictionary<Car, Cell> cellsWithCarInPreviousStep = new Dictionary<Car, Cell>();
22 public ReadOnlyDictionary<Car, Cell> CellsWithCarInPreviousStep
23 {
24 get { return new ReadOnlyDictionary<Car, Cell>(cellsWithCarInPreviousStep); }
25 }
26
31 private Dictionary<Car, Cell> cellsWithCarInCurrentStep = new Dictionary<Car, Cell>();
36 public ReadOnlyDictionary<Car, Cell> CellsWithCarInCurrentStep
37 {
38 get { return new ReadOnlyDictionary<Car, Cell>(cellsWithCarInCurrentStep); }
39 }
40
46 internal void SwapCarsPreviousAndCurrentDictionaries()
47 {
48 Dictionary<Car, Cell> helperDict = cellsWithCarInPreviousStep;
49 cellsWithCarInPreviousStep = cellsWithCarInCurrentStep;
50 cellsWithCarInCurrentStep = helperDict;
51 }
52
56 internal void ProcessCarsOutsideOfTopology()
57 {
58 //initialize list of cars which are to be removed from topology because of their
59 //exiting of topology.
60 HashSet<Car> carsToRemove = new HashSet<Car>();
61 //check if any car is already completely outside of topology. If yes add it to
62 //carsToRemove list.
63 foreach (KeyValuePair<Car, Cell> frontCellWithCar in cellsWithCarInCurrentStep)
64 {
65 Car car = frontCellWithCar.Key;
66 if (!car.IsStillInTopology)
67 {
68 car.RemoveCarFromCells();
69 carsToRemove.Add(car);
70 }
71 }
72 //remove cars in carsToRemove list from the both dictionaries of cars' first cells.
73 foreach (Car carToRemove in carsToRemove)
74 {
75 cellsWithCarInPreviousStep.Remove(carToRemove);
76 cellsWithCarInCurrentStep.Remove(carToRemove);
77 }
78 }
79
85 internal void PerformTransitionForAllCars(Random random, TimeSpan currentModelTime)
86 {
87 //perform transition for all cars.
88 foreach (KeyValuePair<Car, Cell> frontCellWithCar in cellsWithCarInPreviousStep)
89 {
90 Car car = frontCellWithCar.Key;
91 Cell newFrontCell = car.PerformTransition(random);
92 //check if car reaches a sensor.
93 Sensor followingSensor = newFrontCell.FollowingCell == null ? null : newFrontCell.FollowingCell.Sensor;
94 if (followingSensor != null)
95 {
96 //check if the sensor has already recorded the car.
97 if (!car.IsRecordedBySensor)
98 {
99 //create new output record in the sensor.
100 followingSensor.AddOutputRecord(currentModelTime, car.NewSpeed);
101 }
102 }
103 cellsWithCarInCurrentStep[car] = newFrontCell;
104 }
105 }
106
110 internal void PrepareCarsForNextSimulationStep()
111 {
112 foreach (KeyValuePair<Car, Cell> frontCellWithCar in cellsWithCarInCurrentStep)
113 {
114 Car car = frontCellWithCar.Key;
115 car.PrepareForNextSimulationStep();
116 }
117 }
118
124 internal void AddNewCar(Car newCar, Cell cell)
125 {
126 cellsWithCarInPreviousStep.Add(newCar, cell);
127 cellsWithCarInCurrentStep.Add(newCar, cell);
128 }
129
133 internal void ClearCars()
134 {
135 foreach (KeyValuePair<Car, Cell> frontCellWithCar in cellsWithCarInPreviousStep)
136 {
137 frontCellWithCar.Key.RemoveCarFromCells();
138 }
139
140 cellsWithCarInPreviousStep.Clear();
141 cellsWithCarInCurrentStep.Clear();
142 }
143 }
144 }
System
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.Cell
Class representing cell.
Definition: Cell.cs:9
Microscopic_Traffic_Simulator___Model.TrafficObjects.Sensor
Class representing sensor.
Definition: Sensor.cs:14
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.Cell.FollowingCell
Cell FollowingCell
Link to following cell.
Definition: Cell.cs:57
Microscopic_Traffic_Simulator___Model.TrafficObjects
Definition: Generator.cs:7
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.Car
Class representing car
Definition: Car.cs:11
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.CarsManager
Manages cars in cellular topology.
Definition: CarsManager.cs:11
Microscopic_Traffic_Simulator___Model