Microscopic Traffic Simulator
Car.cs
Go to the documentation of this file.
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  {
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  {
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
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 }
Struct containing information about gap.
Definition: Gap.cs:7
Class wrapping a cell walker and applying limit to number of walked cells.
double P10_DecRateWhenLeadingDec
Deceleration rate when leading vehicle decelerates.
Class wrapping a cell walker and applying limit to number of walked cells but if wrapped cell walker ...