Microscopic Traffic Simulator

- Main Page
- Packages
- Classes
- Files

- File List

Sensor.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.Globalization;
6 using System.IO;
7
8 namespace Microscopic_Traffic_Simulator___Model.TrafficObjects
9 {
13 [Serializable]
14 public partial class Sensor : Generator
15 {
19 [NonSerialized]
20 private List<Record> inputRecords;
21
25 [NonSerialized]
26 private List<Record> outputRecords;
27
31 private string pathToInputRecords;
32
36 private string pathToOutputRecords;
37
41 private int actionToGetTimeTo;
42
46 private int actionToPerform;
47
51 protected CellularTopologyParameters cellularTopologyParameters;
55 internal CellularTopologyParameters CellularTopologyParameters
56 {
57 set { cellularTopologyParameters = value; }
58 }
59
65 public Sensor(string pathToInputRecords, string pathToOutputRecords)
66 {
67 this.pathToInputRecords = pathToInputRecords;
68 this.pathToOutputRecords = pathToOutputRecords;
69 }
70
74 internal void LoadInputRecords()
75 {
76 inputRecords = new List<Record>();
77 outputRecords = new List<Record>();
78 actionToGetTimeTo = 0;
79 actionToPerform = 0;
80 foreach (string[] lineItems in new FileLineDataLoader(pathToInputRecords).GetLinesWithData())
81 {
82 inputRecords.Add(new Record()
83 {
84 Time = TimeSpan.Parse(lineItems[0]),
85 Speed = double.Parse(lineItems[1])
86 });
87 }
88 }
89
93 internal void SaveInputRecords()
94 {
95 using (StreamWriter sw = new FileDataStore(pathToOutputRecords).GetStreamWriter())
96 {
97 foreach (Record record in outputRecords)
98 {
99 sw.WriteLine(string.Format(CultureInfo.InvariantCulture, "{0} {1}", record.Time, record.Speed));
100 }
101 }
102 }
103
109 public override TimeSpan GetTimeToNextAction(Random random)
110 {
111 if (actionToGetTimeTo == inputRecords.Count)
112 {
113 return TimeSpan.MaxValue;
114 }
115 else if (actionToGetTimeTo == 0)
116 {
117 return inputRecords[actionToGetTimeTo++].Time;
118 }
119 else
120 {
121 return inputRecords[actionToGetTimeTo].Time - inputRecords[actionToGetTimeTo++ - 1].Time;
122 }
123 }
124
129 public override void PerformAction(Random random)
130 {
131 GenerateTicket(cellularTopologyParameters.FromKphSpeedToSimSpeed(inputRecords[actionToPerform++].Speed));
132 }
133
139 internal void AddOutputRecord(TimeSpan time, int speed)
140 {
141 outputRecords.Add(new Record()
142 {
143 Time = time,
144 Speed = cellularTopologyParameters.FromSimSpeedToKphSpeed(speed)
145 });
146 }
147 }
148 }
Microscopic_Traffic_Simulator___Model.TrafficObjects.Sensor.cellularTopologyParameters
CellularTopologyParameters cellularTopologyParameters
Reference to simulation parameters.
Definition: Sensor.cs:51
Microscopic_Traffic_Simulator___Model.TrafficObjects.Sensor.PerformAction
override void PerformAction(Random random)
Generate new ticket from input records.
Definition: Sensor.cs:129
System
Microscopic_Traffic_Simulator___Model.Utilities
Definition: FileDataStore.cs:3
Microscopic_Traffic_Simulator___Model.TrafficObjects.Generator
Represents generator of cars.
Definition: Generator.cs:13
Microscopic_Traffic_Simulator___Model.TrafficObjects.Sensor
Class representing sensor.
Definition: Sensor.cs:14
Microscopic_Traffic_Simulator___Model.Utilities.FileLineDataLoader
Class for loading lines of data from text file.
Definition: FileLineDataLoader.cs:9
Microscopic_Traffic_Simulator___Model.TrafficObjects.Sensor.GetTimeToNextAction
override TimeSpan GetTimeToNextAction(Random random)
Returns time to generation of new ticket by getting time to the next input record.
Definition: Sensor.cs:109
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.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.TrafficObjects.Sensor.Sensor
Sensor(string pathToInputRecords, string pathToOutputRecords)
Initialize sensor with paths to files with input and output records.
Definition: Sensor.cs:65
Microscopic_Traffic_Simulator___Model