Microscopic Traffic Simulator

- Main Page
- Packages
- Classes
- Files

- File List

GPSRecordsManager.cs
Go to the documentation of this file.
1 using Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.GeneralParameters;
2 using Microscopic_Traffic_Simulator___Model.Utilities;
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 }
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.GpsRecordsManager.Record.Point
Point Point
Car&#39;s location.
Definition: GpsRecordsManager.Record.cs:19
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.GpsRecordsManager.Record.CarHashCode
int CarHashCode
Car&#39;s hash code.
Definition: GpsRecordsManager.Record.cs:34
System
Microscopic_Traffic_Simulator___Model.Utilities
Definition: FileDataStore.cs:3
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.CellularTopologyObjects.GpsRecordsManager.Record.Time
TimeSpan Time
Time of the record.
Definition: GpsRecordsManager.Record.cs:29
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.GpsRecordsManager.LocationAndDirection
Represents tuple containing location and normalized direction vector.
Definition: GpsRecordsManager.LocationAndDirection.cs:13
Microscopic_Traffic_Simulator___Model.Utilities.FileLineDataLoader
Class for loading lines of data from text file.
Definition: FileLineDataLoader.cs:9
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.GeneralParameters.CellularTopologyParameters
Class containing parameters related to cellular topology.
Definition: CellularTopologyParameters.cs:9
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.GeneralParameters
Definition: CellularTopologyParameters.cs:3
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.GeneralParameters.CellularTopologyParameters.P1_CellLength
double P1_CellLength
Cell length parameter.
Definition: CellularTopologyParameters.cs:14
Microscopic_Traffic_Simulator___Model.Utilities.FileDataStore
Class for storing line data to text files.
Definition: FileDataStore.cs:8
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects
Definition: Car.cs:6
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.GpsRecordsManager.Record.DirectionNormalizedVector
Vector DirectionNormalizedVector
Car&#39;s normalized direction vector.
Definition: GpsRecordsManager.Record.cs:24
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.GpsRecordsManager.Record
Structure representing car&#39;s location in time.
Definition: GpsRecordsManager.Record.cs:14
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.CarsManager.CellsWithCarInCurrentStep
ReadOnlyDictionary< Car, Cell > CellsWithCarInCurrentStep
Read only dictionary containing all cars with their first cells which they occupied after last transi...
Definition: CarsManager.cs:37
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.CarsManager
Manages cars in cellular topology.
Definition: CarsManager.cs:11
Microscopic_Traffic_Simulator___Model