Microscopic Traffic Simulator
GPSRecordsManager.cs
Go to the documentation of this file.
3 using System;
4 using System.Collections.Generic;
5 using System.Collections.ObjectModel;
6 using System.Globalization;
7 using System.IO;
8 using System.Windows;
9 
10 namespace Microscopic_Traffic_Simulator___Model.CellularTopologyObjects
11 {
15  public partial class GpsRecordsManager
16  {
20  private CarsManager carsManager;
21 
25  private string pathToInGPSFile = "gpsin.txt";
26 
30  private string pathToOutGPSFile = "gpsout.txt";
31 
35  private List<Record> outputRecords;
36 
40  private List<Record> inputRecords;
41 
45  private Dictionary<int, LocationAndDirection> carPreviousGPSInputLocations =
46  new Dictionary<int, LocationAndDirection>();
50  public ReadOnlyDictionary<int, LocationAndDirection> CarPreviousGPSInputLocations
51  {
52  get { return new ReadOnlyDictionary<int, LocationAndDirection>(carPreviousGPSInputLocations); }
53  }
54 
58  private Dictionary<int, LocationAndDirection> carCurrentGPSInputLocations =
59  new Dictionary<int, LocationAndDirection>();
63  public ReadOnlyDictionary<int, LocationAndDirection> CarCurrentGPSInputLocations
64  {
65  get { return new ReadOnlyDictionary<int, LocationAndDirection>(carCurrentGPSInputLocations); }
66  }
67 
71  private int gpsInputCarsCounter = 0;
72 
77  private HashSet<int> carsInPreviousStepNotInCurrentStep = new HashSet<int>();
78 
82  private CellularTopologyParameters cellularTopologyParameters;
83 
89  internal GpsRecordsManager(CarsManager carsManager, CellularTopologyParameters cellularTopologyParameters)
90  {
91  this.carsManager = carsManager;
92  this.cellularTopologyParameters = cellularTopologyParameters;
93  }
94 
99  internal void AddOutputRecord(Record record)
100  {
101  outputRecords.Add(record);
102  }
103 
108  internal void RecordCarsGPSLocations(TimeSpan currentModelTime)
109  {
110  foreach (KeyValuePair<Car, Cell> frontCellWithCar in carsManager.CellsWithCarInCurrentStep)
111  {
112  AddOutputRecord(new Record()
113  {
114  Point = frontCellWithCar.Value.Location +
115  new Vector(frontCellWithCar.Key.Outside * cellularTopologyParameters.P1_CellLength, 0.0),
116  DirectionNormalizedVector = frontCellWithCar.Value.NormalizedDirectionVector,
117  Time = currentModelTime + cellularTopologyParameters.P2_SimulationStepInterval,
118  CarHashCode = frontCellWithCar.Key.GetHashCode()
119  });
120  }
121  }
122 
127  internal void ProcessInputGPSRecords(TimeSpan currentModelTime)
128  {
129  //check if there are any gps input records left
130  if (gpsInputCarsCounter < inputRecords.Count)
131  {
132  Record record;
133 
134  if (gpsInputCarsCounter == 0)
135  {
136  //initialize processing of gps input records by initializing car current gps input locations
137  //and list of the cars in previous step not present in the current step
138  for (; gpsInputCarsCounter < inputRecords.Count &&
139  (record = inputRecords[gpsInputCarsCounter]).Time == currentModelTime;
140  gpsInputCarsCounter++)
141  {
142  carCurrentGPSInputLocations.Add(record.CarHashCode, new LocationAndDirection(
143  record.Point, record.DirectionNormalizedVector));
144  }
145  }
146 
147  //initialze the set of the cars occured in this step
148  HashSet<int> carsInCurrentStep = null;
149 
150  //perform transition of cars in gps records
151  for (bool isFirstIteration = true;
152  gpsInputCarsCounter < inputRecords.Count &&
153  (record = inputRecords[gpsInputCarsCounter]).Time ==
154  currentModelTime + cellularTopologyParameters.P2_SimulationStepInterval;
155  gpsInputCarsCounter++, isFirstIteration = false)
156  {
157  if (isFirstIteration)
158  {
159  Dictionary<int, LocationAndDirection> helpDictionary = carPreviousGPSInputLocations;
160  carPreviousGPSInputLocations = carCurrentGPSInputLocations;
161  carCurrentGPSInputLocations = helpDictionary;
162  carsInCurrentStep = new HashSet<int>();
163  }
164  if (carCurrentGPSInputLocations.ContainsKey(record.CarHashCode))
165  {
166  carCurrentGPSInputLocations[record.CarHashCode] = new LocationAndDirection(
167  record.Point, record.DirectionNormalizedVector);
168  }
169  else
170  {
171  carCurrentGPSInputLocations.Add(record.CarHashCode, new LocationAndDirection(
172  record.Point, record.DirectionNormalizedVector));
173  }
174  carsInPreviousStepNotInCurrentStep.Remove(record.CarHashCode);
175  carsInCurrentStep.Add(record.CarHashCode);
176  }
177 
178  //remove car gps records of cars which are not in next gps recrods
179  foreach (int carHashCode in carsInPreviousStepNotInCurrentStep)
180  {
181  carCurrentGPSInputLocations.Remove(carHashCode);
182  }
183 
184  //switch cars current steup set to set of cars in previous step
185  if (carsInCurrentStep != null)
186  {
187  carsInPreviousStepNotInCurrentStep = carsInCurrentStep;
188  }
189  }
190  else
191  {
192  //clear gps input locations
193  carPreviousGPSInputLocations.Clear();
194  carCurrentGPSInputLocations.Clear();
195  }
196  }
197 
202  internal void ResetGPSInputRecordsInformationAndSaveInputRecords()
203  {
204  gpsInputCarsCounter = 0;
205  carPreviousGPSInputLocations.Clear();
206  carCurrentGPSInputLocations.Clear();
207  carsInPreviousStepNotInCurrentStep.Clear();
208  using (StreamWriter sw = new FileDataStore(pathToOutGPSFile).GetStreamWriter())
209  {
210  foreach (Record record in outputRecords)
211  {
212  sw.WriteLine(string.Format(CultureInfo.InvariantCulture, "{0} {1} {2} {3}", record.Point,
213  record.DirectionNormalizedVector, record.Time, record.CarHashCode));
214  }
215  }
216  }
217 
222  internal void LoadInputRecordsAndInitializeOutputRecords()
223  {
224  outputRecords = new List<Record>();
225  inputRecords = new List<Record>();
226  foreach (string[] lineItems in new FileLineDataLoader(pathToInGPSFile).GetLinesWithData())
227  {
228  inputRecords.Add(new Record()
229  {
230  Point = Point.Parse(lineItems[0]),
231  DirectionNormalizedVector = Vector.Parse(lineItems[1]),
232  Time = TimeSpan.Parse(lineItems[2]),
233  CarHashCode = int.Parse(lineItems[3])
234  });
235  }
236  }
237  }
238 }
Class for loading lines of data from text file.
Class for storing line data to text files.
Definition: FileDataStore.cs:8
ReadOnlyDictionary< Car, Cell > CellsWithCarInCurrentStep
Read only dictionary containing all cars with their first cells which they occupied after last transi...
Definition: CarsManager.cs:37