Microscopic Traffic Simulator

- Main Page
- Packages
- Classes
- Files

- File List

Car.cs
Go to the documentation of this file.
1 using Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.CellWalkers;
2 using Microscopic_Traffic_Simulator___Model.GeneralParameters;
3 using System;
4 using System.Diagnostics;
5
6 namespace Microscopic_Traffic_Simulator___Model.CellularTopologyObjects
7 {
11 public class Car
12 {
16 private int previousSpeed;
20 internal int PreviousSpeed { get { return previousSpeed; } }
21
25 private int currentSpeed;
29 internal int CurrentSpeed { get { return currentSpeed; } }
30
34 private int newSpeed;
38 internal int NewSpeed { get { return newSpeed; } }
39
43 private int length;
47 public int Length { get { return length; } }
48
52 private Cell firstCell;
53
57 internal bool IsStillInTopology { get { return outside < length; } }
58
62 private int outside = 0;
66 public int Outside { get { return outside; } }
67
71 private TransitionFunctionParameters parameters;
72
77 private bool isRecordedBySensor = false;
83 internal bool IsRecordedBySensor
84 {
85 get
86 {
87 bool toReturn = isRecordedBySensor;
88 if (!isRecordedBySensor)
89 isRecordedBySensor = true;
90 return toReturn;
91 }
92 }
93
101 internal Car(Cell firstCell, int speed, TransitionFunctionParameters transitionFunctionParameters)
102 {
103 this.firstCell = firstCell;
104 this.previousSpeed = this.currentSpeed = speed;
105 parameters = transitionFunctionParameters;
106 length = 4;
107 }
108
115 internal Cell PerformTransition(Random random)
116 {
117 ComputeNewSpeed(random);
118 return SetToNextCells();
119 }
120
126 private void ComputeNewSpeed(Random random)
127 {
128 //apply speed from previous transition to newSpeed. newSpeed will be updated to new
129 //speed computed from this transition.
130 newSpeed = currentSpeed;
131
132 //acceleration step
133 if (newSpeed < 30)
134 {
135 if (random.NextDouble() < parameters.P7_AccelerationProbability)
136 newSpeed++;
137 }
138
139 //get gap length and acceleration of next vehicle
140 Gap gap = ComputeGap();
141 if (gap.Length == int.MaxValue ||
142 (gap.Length + gap.FollowingCarAcceleration) > newSpeed)
143 {
144 //Car does not need to brake. Random slowing is applied.
145 if (newSpeed > 1)
146 {
147
148 if (newSpeed < parameters.P6_DecProbabilitySpeedBound)
149 {
150 if (random.NextDouble() < parameters.P5_LowSpeedDecProbability)
151 newSpeed--;
152 }
153 else
154 {
155 if (random.NextDouble() < parameters.P8_HighSpeedDecProbability)
156 newSpeed--;
157 }
158 }
159 }
160 else
161 {
162 //braking because of too small gap
163 if (gap.FollowingCarAcceleration > 0)
164 newSpeed = (int)Math.Round((gap.Length + gap.FollowingCarAcceleration) /
165 parameters.P9_DecRateWhenLeadingAcc);
166 else
167 newSpeed = (int)Math.Round((gap.Length + gap.FollowingCarAcceleration) /
168 parameters.P10_DecRateWhenLeadingDec);
169 }
170
171 Debug.Assert(newSpeed <= gap.Length, "Speed larger than gap");
172 //Debug.Assert(newSpeed >= 0, "Negative new speed");
173 }
174
179 private Gap ComputeGap()
180 {
181 LimitedStepsCellWalker cellWalker = new LimitedStepsCellWalker(
182 new ForwardCellWalker(firstCell.FollowingCell), newSpeed * 2);
183 foreach (Cell cell in cellWalker.GetNext())
184 {
185 if (cell.Car != null)
186 {
187 return new Gap(cellWalker.CellsWalked,
188 cell.Car.CurrentSpeed - cell.Car.PreviousSpeed);
189 }
190 }
191 return new Gap(int.MaxValue);
192 }
193
199 private Cell SetToNextCells()
200 {
201 LimitedMinimumStepsCellWalker walker = new LimitedMinimumStepsCellWalker(
202 new ForwardCellWalker(firstCell.FollowingCell), newSpeed);
203 foreach (Cell cell in walker.GetNext())
204 {
205 if (cell == null)
206 outside++;
207 else
208 cell.Car = this;
209 }
210 firstCell = walker.LastCell ?? firstCell;
211 return firstCell;
212 }
213
223 internal void PrepareForNextSimulationStep()
224 {
225 //reassign fields of speeds
226 previousSpeed = currentSpeed;
227 currentSpeed = newSpeed;
228
229 //go backwards the 'length' number of steps
230 LimitedMinimumStepsCellWalker walker = new LimitedMinimumStepsCellWalker(
231 new BackwardCellWalker(firstCell.PreviousCell), length);
232 foreach (Cell cell in walker.GetNext())
233 if (cell == null)
234 return;
235
236 //following backward cells which were occupied by car in previous topology state
237 //are marked as free.
238 RemoveCarFromCells(walker.LastCell);
239 }
240
245 private void RemoveCarFromCells(Cell fromCell)
246 {
247 BackwardCarCellWalker walker = new BackwardCarCellWalker(fromCell, this);
248 foreach (Cell cell in walker.GetNext())
249 {
250 cell.Car = null;
251 }
252 }
253
257 internal void RemoveCarFromCells()
258 {
259 RemoveCarFromCells(firstCell);
260 }
261 }
262 }
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.Gap
Struct containing information about gap.
Definition: Gap.cs:7
Microscopic_Traffic_Simulator___Model.GeneralParameters.TransitionFunctionParameters.P7_AccelerationProbability
double P7_AccelerationProbability
Acceleration probability.
Definition: TransitionFunctionParameters.cs:24
System
Microscopic_Traffic_Simulator___Model.GeneralParameters
Definition: TransitionFunctionParameters.cs:3
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.Cell
Class representing cell.
Definition: Cell.cs:9
Microscopic_Traffic_Simulator___Model.GeneralParameters.TransitionFunctionParameters.P5_LowSpeedDecProbability
double P5_LowSpeedDecProbability
Low speed deceleration probability.
Definition: TransitionFunctionParameters.cs:14
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.CellWalkers.BackwardCarCellWalker
Class representing backward walker through cells taken by car.
Definition: BackwardCarCellWalker.cs:8
Microscopic_Traffic_Simulator___Model.GeneralParameters.TransitionFunctionParameters.P9_DecRateWhenLeadingAcc
double P9_DecRateWhenLeadingAcc
Deceleration rate when leading vehicle accelerates.
Definition: TransitionFunctionParameters.cs:34
Microscopic_Traffic_Simulator___Model.GeneralParameters.TransitionFunctionParameters
Class containing parameters of transition function.
Definition: TransitionFunctionParameters.cs:9
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.Cell.FollowingCell
Cell FollowingCell
Link to following cell.
Definition: Cell.cs:57
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.CellWalkers.BackwardCellWalker
Cell walker in backward direction.
Definition: BackwardCellWalker.cs:8
Microscopic_Traffic_Simulator___Model.GeneralParameters.TransitionFunctionParameters.P8_HighSpeedDecProbability
double P8_HighSpeedDecProbability
High speed deceleration probability.
Definition: TransitionFunctionParameters.cs:29
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.CellWalkers.LimitedStepsCellWalker
Class wrapping a cell walker and applying limit to number of walked cells.
Definition: LimitedStepsCellWalker.cs:8
Microscopic_Traffic_Simulator___Model.GeneralParameters.TransitionFunctionParameters.P6_DecProbabilitySpeedBound
int P6_DecProbabilitySpeedBound
Threshold speed for the p5 and p8.
Definition: TransitionFunctionParameters.cs:19
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.Car
Class representing car
Definition: Car.cs:11
Microscopic_Traffic_Simulator___Model.GeneralParameters.TransitionFunctionParameters.P10_DecRateWhenLeadingDec
double P10_DecRateWhenLeadingDec
Deceleration rate when leading vehicle decelerates.
Definition: TransitionFunctionParameters.cs:39
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.CellWalkers.LimitedMinimumStepsCellWalker
Class wrapping a cell walker and applying limit to number of walked cells but if wrapped cell walker ...
Definition: LimitedMinimumStepsCellWalker.cs:10
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.CellWalkers
Definition: BackwardCarCellWalker.cs:3
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects
Definition: Car.cs:6
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.CellWalkers.ForwardCellWalker
Cell walker in forward direction.
Definition: ForwardCellWalker.cs:8
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.Cell.PreviousCell
Cell PreviousCell
Link to previous cell.
Definition: Cell.cs:48
Microscopic_Traffic_Simulator___Model