-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimulation_1.cpp
360 lines (327 loc) · 8.86 KB
/
Simulation_1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
See how to get rid of going through sideways problem consider cases form both vehicles Truck and Bus
need to give range for both
*/
#include <iostream>
#include <cmath>
#include <string>
#include <ctime>
#include <fstream>
using namespace std;
//#define RAND_MAX 1
#define maxposvehicles 20
#define maxtype 4
#define maxcolor 11
#define maxvalues 1000
class Road;
class Vehicle;
//Globals
// Car 0 Bike 1 Bus 2 Truck 3
char symbol[maxtype] = {'=','~',':','#'};
string colour[maxcolor] = {"Red","Blue","Green","Yellow","Orange","Purple","Violet","Pink","Black","White","Grey"};
int timestep = 1; //seconds
int duration = 75;
int maxvehicles;
class Vehicle{
public: bool active; //Vehicle present or not
//One time
public: char type; //b bike, C Car , T Truck,
public: int length =2;
public: int width =2;
public: int acceleration=1;
public: int deceleration=1;
public: int maxspeed=1;
public: string color = "Red";
public: int releasetime;
//Road r; //Object of road on which it is
//Dynamic
public: int lane=0;
public: int speed=0;
public: int pos;
public: bool flag=0;
};
Vehicle vehicles[maxposvehicles];
class Road{
public: int id;
public: int width;
public: int length;
public: int lightstate;
public: int lightpos;
public: int lanes;
//Can later inlclude vehicle array here as to facilitate more roads
public: int checkIfOccupied(int vpos,int vend, int vlane){
//cout<<"vpos :"<<vpos<<endl;
//cout<<"vlan :"<<vlane<<endl;
for(int i=0;i<maxvehicles;i++){ //need to check just rear half of vehicle
// cout<<"vehlcepos :"<<(vehicles[i].type)<<" "<<vehicles[i].pos<<endl;
// cout<<"vehlcelane :"<<(vehicles[i].type)<<" "<<vehicles[i].lane<<endl;
//cout<<(vehicles[i].lane/*+vehicles[i].width/2*/ == vlane)<<" <---Lane pos-->"<<((vehicles[i].pos-(vehicles[i].length) < vpos)&&vehicles[i].pos>=vpos)<<endl;
bool front ((vehicles[i].pos-vehicles[i].length <= vpos)&&vehicles[i].pos>=vpos);
bool end = ((vehicles[i].pos <=vpos)&&vehicles[i].pos>=vend);
// front = vpos>vhi
if((front /*||end*/)&&(vehicles[i].lane/*+vehicles[i].width/2*/ == vlane||vehicles[i].lane/*-vehicles[i].width/2*/ == vlane))
return i; //return vehicle id
}
return -1;
}
public: void print(int vlane, int pos){
bool flag=0;
for(int i=0;i<maxvehicles;i++){
if(((vehicles[i].pos-vehicles[i].length < pos)&&(vehicles[i].pos >= pos)) &&(vehicles[i].lane == vlane)&&vehicles[i].active){
cout<<vehicles[i].type;
flag =1;
}
}
if(!flag&&lightpos==pos)
//cout<<vehicles[i].pos;
cout<<"|";
else if(!flag)
cout<<" ";
}
//move everything forward
public: void movefd(){
bool flag ;
int seed=clock();
srand(seed);
int random = rand();
//cout<<random;
if(random%20==0)
flag =1;
else
flag =0;
for(int i=0;i<maxvehicles;i++){
if(!vehicles[i].active) //When vehicles not spawned
continue;
if(lightstate == 0 && vehicles[i].pos >= lightpos){
/*cout<<i<<" stop "<<endl;
cout<<i<<" posL "<<vehicles[i].pos<<endl;
cout<<i<<" speedL "<<vehicles[i].speed<<endl;
cout<<i<<" laneL "<<vehicles[i].lane<<endl;
*/
vehicles[i].speed =0;
continue;
}
if(checkIfOccupied(vehicles[i].pos+vehicles[i].speed*timestep,vehicles[i].pos+vehicles[i].speed*timestep-vehicles[i].length,vehicles[i].lane)>-1||flag){
changeLane(i,flag);
if(vehicles[i].speed<vehicles[i].maxspeed)
vehicles[i].speed+=vehicles[i].acceleration*timestep;
/*
cout<<i<<" posC "<<vehicles[i].pos<<endl;
cout<<i<<" speedC "<<vehicles[i].speed<<endl;
cout<<i<<" laneC "<<vehicles[i].lane<<endl;
*/
}
else {
//cout<<"really"<<vehicles[i].pos<<endl;
vehicles[i].pos+=vehicles[i].speed*timestep;
if(vehicles[i].speed<vehicles[i].maxspeed)
vehicles[i].speed+=vehicles[i].acceleration*timestep;
/*cout<<timestep<<" "<<vehicles[i].acceleration<<endl;
cout<<i<<" pos "<<vehicles[i].pos<<endl;
cout<<i<<" speed "<<vehicles[i].speed<<endl;
cout<<i<<" lane "<<vehicles[i].lane<<endl;
*/
}
}
}
private: void changeLane(int i,bool f){
bool flag ;
unsigned int seed = clock();
srand(seed);
int random = rand();
if(random%2==0)
flag =1;
else
flag =0;
if(vehicles[i].flag ==0){
if(checkIfOccupied(vehicles[i].pos,vehicles[i].pos-vehicles[i].length,vehicles[i].lane+1/*remove 1+vehicles[i].width/2*/)==-1&&(vehicles[i].lane+1/*vehicles[i].width/2*/<=lanes)&&flag){
vehicles[i].lane+=1;//vehicles[i].width/2;
}
else if(checkIfOccupied(vehicles[i].pos,vehicles[i].pos-vehicles[i].length,vehicles[i].lane-1/*-vehicles[i].width/2*/)==-1&&(vehicles[i].lane-1/*vehicles[i].width/2*/ >0)&&flag){
// vehicles[i].flag =1;
vehicles[i].lane-=1;//vehicles[i].width/2;
}
else{
// if(f =0)
// return;
vehicles[i].speed =0;
}
}
if(vehicles[i].flag ==1){
if(checkIfOccupied(vehicles[i].pos,vehicles[i].pos-vehicles[i].length,vehicles[i].lane-1/*-vehicles[i].width/2*/)==-1&&(vehicles[i].lane-1/*vehicles[i].width/2*/ >0)&&flag){
vehicles[i].lane-=1;//vehicles[i].width/2;
}
else if(checkIfOccupied(vehicles[i].pos,vehicles[i].pos-vehicles[i].length,vehicles[i].lane+1/*remove 1+vehicles[i].width/2*/)==-1&&(vehicles[i].lane+1/*vehicles[i].width/2*/<=lanes)&&flag){
vehicles[i].lane+=1;//vehicles[i].width/2;
vehicles[i].flag=0;
}
else{
if(f =0)
return;
vehicles[i].speed =0;
}
}
}
};
Road road;
int vehicletype (string name){
if(name=="Car")
return 0;
else if(name =="Bike")
return 1;
else if(name =="Bus")
return 2;
else if(name =="Truck")
return 3;
else
return 0;
}
int color(string name){
if(name=="Red")
return 0;
else if(name =="Blue")
return 1;
else if(name =="Green")
return 2;
else if(name =="Yellow")
return 3;
else if(name =="Orange")
return 4;
else if(name =="Purple")
return 5;
else if(name =="Violet")
return 6;
else if(name =="Pink")
return 7;
else if(name =="Black")
return 8;
else if(name =="White")
return 9;
else if(name == "Grey")
return 10;
else
return 0;
}
void configure(string name,int *arr){
//input form file
ifstream input(name);
if(!input.is_open()){
cout<<"No such file exists: "<<name<<endl;
}
int var=0,i=0;
string line;
while(getline(input,line,'=')){
//cout<<" ->"<<line<<":"<<endl;
if(line.compare("vehicle")==0){
getline(input,line,' ');
//cout<<line;
arr[i]=vehicletype(line);
i++;
//Input colour
getline(input,line,' ');
//cout<<line;
arr[i]=color(line);
i++;
for(int j=1;j<7;j++){
input>> var ;
arr[i]=var ;
i++;
// cout<<"array clfghue "<<arr[i-1]<<endl;
}
}
else{
input>> var ;
arr[i]=var ;
i++;
// cout<<"a"<<arr[i-1]<<endl;
}
getline(input,line,'\n');
}
input.close();
}
void addVehicle(Vehicle *v,int type,int color,int length , int width,int pos,int lane,int maxspeed,int releasetime){
v->active = false;
v->pos =pos;
v->lane = lane;
v->releasetime =releasetime;
v->maxspeed = maxspeed;
v->speed = v->maxspeed;
v->acceleration = 1;
v->deceleration = 1;
v->length = length;
v->width = width;
v->type = symbol[type];
v->color= colour[color];
}
void spawnVehicles(int time){
for(int i=0;i<maxvehicles;i++){
if(vehicles[i].releasetime == time)
vehicles[i].active =true;
}
}
void printState(int time){
for(int i=1;i<=road.length;i++){
if(i==road.lightpos)
cout<<"|";
else
cout<<"-";
}
cout<<endl;
for(int i=1;i<=road.lanes;i++){
for(int j=1;j<=road.length;j++){
road.print(i,j);
}
cout<<endl;
}
for(int i=1;i<=road.length;i++){
if(i==road.lightpos)
cout<<"|";
else
cout<<"-";
}
cout<<endl;
cout<<"Time = " <<time<<endl;
}
void delay ( int mseconds )
{
clock_t endwait;
endwait = clock () + (mseconds*1.0/1000) * CLOCKS_PER_SEC ;
while (clock() < endwait) {}
}
int main(int argc, char** argv){
int stoptime,gotime;
int readData[maxvalues]={0}; //to store data read from config file
if(argc<1){
cout<<"Configuration file missing";
return 0;
}
string filename = argv[1];
//Read the configuration file and set all the values
configure(filename,readData);
//Road start values
road.id=readData[0];
road.length = readData[1];
road.width =readData[2];
road.lightpos = readData[3];
road.lanes = readData[4];
maxvehicles = readData[5];
timestep = readData[6];
duration = readData[7];
stoptime = readData[8];
gotime = readData[9];
//can store all these values in respective arryas then in a loop initialize all
for(int i =0;i<maxvehicles;i++) addVehicle(&vehicles[i],readData[10+i*8],readData[11+i*8],readData[12+i*8],readData[13+i*8],readData[14+i*8],readData[15+i*8],readData[16+i*8],readData[17+i*8]); //Car
//Start simulation
for(int time =0;time<duration;time+=timestep){
spawnVehicles(time);
road.movefd(); //move vehicles forward
if(time >= stoptime&& time <= gotime)
road.lightstate =0;
else
road.lightstate =1;
//sortPos(); // to sort on basis of position
delay(200);
printState(time);
}
return 0;
}