1 | /************************************************************************************ |
---|
2 | * Copyright (C) 2008 by Politehnica University of Bucharest and Rutgers University |
---|
3 | * All rights reserved. |
---|
4 | * Refer to LICENSE for terms and conditions of use. |
---|
5 | ***********************************************************************************/ |
---|
6 | package vnsim.vehicular.generator; |
---|
7 | |
---|
8 | |
---|
9 | import java.util.*; |
---|
10 | |
---|
11 | |
---|
12 | import java.io.*; |
---|
13 | |
---|
14 | import vnsim.applications.adaptiveTL.WirelessTrafficLight; |
---|
15 | import vnsim.core.*; |
---|
16 | import vnsim.map.object.*; |
---|
17 | import vnsim.vehicular.routePlan.RouteComputingUtils; |
---|
18 | import vnsim.vehicular.scenarios.*; |
---|
19 | import vnsim.vehicular.simulator.*; |
---|
20 | import vnsim.vehicular.simulator.intersections.*; |
---|
21 | import vnsim.gui.*; |
---|
22 | import java.util.logging.*; |
---|
23 | |
---|
24 | |
---|
25 | |
---|
26 | |
---|
27 | |
---|
28 | public class Mobility { |
---|
29 | |
---|
30 | /** Logger used by this class */ |
---|
31 | private static final transient Logger logger = Logger.getLogger("generator.Mobility"); |
---|
32 | |
---|
33 | /* |
---|
34 | public static void main(String args[]) throws Exception { |
---|
35 | |
---|
36 | File f=new File("map.dat"); |
---|
37 | if(!f.exists()) { |
---|
38 | System.out.println("NOT EXIST!"); |
---|
39 | // Globals.map = new trafficview.object.Map("Poli.RT1","Poli.RT2"); |
---|
40 | |
---|
41 | Globals.map = new trafficview.object.Map("Test.RT1","Test.RT2"); |
---|
42 | |
---|
43 | //Globals.map = new trafficview.object.Map("TGR34023.RT1","TGR34023.RT2"); |
---|
44 | ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("map.dat")); |
---|
45 | oos.writeObject(Globals.map); |
---|
46 | } else { |
---|
47 | System.out.println("EXIST!"); |
---|
48 | ObjectInputStream ois=new ObjectInputStream(new FileInputStream("map.dat")); |
---|
49 | Globals.map=(trafficview.object.Map)ois.readObject(); |
---|
50 | } |
---|
51 | |
---|
52 | //System.out.println("OK!:"+Globals.map.roads); |
---|
53 | |
---|
54 | // for(int i=0;i<Globals.map.roads.size();i++) { |
---|
55 | // printRoad(i); |
---|
56 | // } |
---|
57 | |
---|
58 | //Globals.map = new Map("TGR34023.RT1","TGR34023.RT2"); |
---|
59 | |
---|
60 | //System.exit(0); |
---|
61 | File fText=new File(Globals.inFile+".txt"); |
---|
62 | File fBin=new File(Globals.inFile); |
---|
63 | fText.createNewFile(); |
---|
64 | fBin.createNewFile(); |
---|
65 | PrintWriter pwText=new PrintWriter(new FileOutputStream(fText)); |
---|
66 | DataOutputStream outBin=new DataOutputStream(new FileOutputStream(fBin)); |
---|
67 | |
---|
68 | // initCars(); |
---|
69 | |
---|
70 | // initCarsTest(); |
---|
71 | |
---|
72 | pwText.println(""+Globals.FPS); |
---|
73 | outBin.writeInt(Globals.FPS); |
---|
74 | |
---|
75 | for(int i=0;i<Globals.N;i++) { |
---|
76 | |
---|
77 | //System.out.println("Iteratia "+i+":"); |
---|
78 | |
---|
79 | //loop through all IntersectionWithTrafficLights and set correct values for |
---|
80 | //the "isGreen" vector |
---|
81 | |
---|
82 | int thisSec=(int)(i/(Globals.FPS)); |
---|
83 | for(int j=0;j<Globals.map.lightsIndices.size();j++) { |
---|
84 | IntersectionWithTrafficLights iwtl=(IntersectionWithTrafficLights)Globals.map.allIntersections.get(Globals.map.lightsIndices.get(j)); |
---|
85 | int aux=thisSec%iwtl.totalTime; |
---|
86 | iwtl.currentColor=new ArrayList<Integer> (); |
---|
87 | for(int k=0;k<iwtl.segments.size();k++) { |
---|
88 | TrafficLightInfo info=iwtl.lightInfos.get(k); |
---|
89 | iwtl.currentColor.addElement(new Integer(info.getColor(aux))); |
---|
90 | } |
---|
91 | } |
---|
92 | |
---|
93 | // for(int j=0;j<Globals.map.lightsIndices.size();j++) { |
---|
94 | // System.out.println(Globals.map.allIntersections.get(Globals.map.lightsIndices.get(j))); |
---|
95 | // } |
---|
96 | |
---|
97 | int NCARS=0; |
---|
98 | |
---|
99 | // if(i==750) { |
---|
100 | // changeCar(0, 2, 40, 1, 1, 0.0, new CalmPersonality(), 0.0, 0, 0); |
---|
101 | // } |
---|
102 | // if(i==1000) { |
---|
103 | // changeCar(0, 3, 45, 1, 1, 0.0, new CalmPersonality(), 0.0, 0, 0); |
---|
104 | // } |
---|
105 | |
---|
106 | for(int j=0;j<Globals.map.roads.size();j++) { |
---|
107 | // System.out.println("DRUMUL "+j+":"+Globals.map.roads.get(j).carsOnThisRoad);; |
---|
108 | |
---|
109 | SortedCarVector newVector=new SortedCarVector(); |
---|
110 | for(int k=0;k<Globals.map.roads.get(j).carsOnThisRoad.size();k++) { |
---|
111 | CarInstance car=Globals.map.roads.get(j).carsOnThisRoad.get(k); |
---|
112 | if(car.finished) |
---|
113 | continue; |
---|
114 | try { |
---|
115 | car.move(); |
---|
116 | } catch (Exception ex) { |
---|
117 | ex.printStackTrace(); |
---|
118 | } |
---|
119 | //if it has changed road, place it in the vector of its new road |
---|
120 | if(car.getRoadIdx()!=j) { |
---|
121 | if(car.getRoadIdx()>j) |
---|
122 | NCARS--; //it will be moved again when that road will be processed |
---|
123 | Globals.map.roads.get(car.getRoadIdx()).carsOnThisRoad.addCarInstance(car); |
---|
124 | } else { |
---|
125 | //it's still on the same road |
---|
126 | newVector.addCarInstance(Globals.map.roads.get(j).carsOnThisRoad.get(k)); |
---|
127 | } |
---|
128 | NCARS++; |
---|
129 | } |
---|
130 | |
---|
131 | //loop through the new vector and set the indices |
---|
132 | for(int k=0;k<newVector.size();k++) { |
---|
133 | newVector.get(k).index=k; |
---|
134 | } |
---|
135 | Globals.map.roads.get(j).carsOnThisRoad=newVector; |
---|
136 | } |
---|
137 | |
---|
138 | //output text |
---|
139 | pwText.print(NCARS+" "); |
---|
140 | outBin.writeInt(NCARS); |
---|
141 | for(int j=0;j<Globals.map.roads.size();j++) { |
---|
142 | for(int k=0;k<Globals.map.roads.get(j).carsOnThisRoad.size();k++) { |
---|
143 | CarInstance ci=Globals.map.roads.get(j).carsOnThisRoad.get(k); |
---|
144 | pwText.print(ci.ID+" "+ci.getRoadIdx()+" "+ci.getPointIdx()+" "+ci.getOffset()+" "+ci.getDirection()+" "+ci.getLane()+" "); |
---|
145 | outBin.writeInt(ci.ID); |
---|
146 | outBin.writeInt(ci.getRoadIdx()); |
---|
147 | outBin.writeInt(ci.getPointIdx()); |
---|
148 | outBin.writeDouble(ci.getOffset()); |
---|
149 | outBin.writeInt(ci.getDirection()); |
---|
150 | outBin.writeInt(ci.getLane()); |
---|
151 | outBin.writeDouble(ci.getSpeed()); |
---|
152 | } |
---|
153 | } |
---|
154 | pwText.println(); |
---|
155 | } |
---|
156 | pwText.close(); |
---|
157 | outBin.close(); |
---|
158 | } |
---|
159 | |
---|
160 | // public static void changeCar(int roadIndex, int ID, int newPointIndex, |
---|
161 | // int newDirection, int newLane, double newOffset, Personality newP, |
---|
162 | // double newSpeed, int newRoadDest, int newPointDest) { |
---|
163 | // Road r=Globals.map.roads.get(roadIndex); |
---|
164 | // for(int i=0;i<r.carsOnThisRoad.size();i++) { |
---|
165 | // CarInstance c=r.carsOnThisRoad.get(i); |
---|
166 | // if(c.ID==ID) { |
---|
167 | // CarInstance newC=new CarInstance((short)roadIndex, (short)newPointIndex, newP); |
---|
168 | // newC.setDirection((byte)newDirection); //reverse |
---|
169 | // newC.setLane((byte)newLane); |
---|
170 | // newC.setOffset(newOffset); |
---|
171 | // newC.setSpeed(newSpeed); |
---|
172 | // newC.ID=ID; |
---|
173 | // newC.index=i; |
---|
174 | // |
---|
175 | // Vector<RouteSegment> route=RoutingModule.getUnmergedDijkstraPath(new RouteSegment((short)roadIndex, (short)newPointIndex, (short)newPointIndex), |
---|
176 | // new RouteSegment((short)newRoadDest,(short)newPointDest,(short)newPointDest)); |
---|
177 | // System.out.println("ROUTE IS:"+route); |
---|
178 | // newC.setRoute(route); |
---|
179 | // newC.startOnRoad(); |
---|
180 | // //r.carsOnThisRoad.setElementAt(i, newC); |
---|
181 | // r.carsOnThisRoad.remove(newC.ID); |
---|
182 | // Globals.map.roads.get(newC.getRoadIdx()).carsOnThisRoad.addCarInstance(newC); |
---|
183 | // break; |
---|
184 | // } |
---|
185 | // } |
---|
186 | // } |
---|
187 | * |
---|
188 | * */ |
---|
189 | |
---|
190 | |
---|
191 | public static void initCars(Engine e) { |
---|
192 | // createNewCar(0,80,1,2,0.0, new AggresivePersonality(), 0.0, 1); |
---|
193 | // createNewCar(0,30,1,1,0.0, new NotMovingPersonality(),0.0, 2); |
---|
194 | // createNewCar(0,90,1,1,0.0, new AggresivePersonality(),0.0, 3); |
---|
195 | // createNewCar(0,70,1,1,0.0, new CalmPersonality(),0.0, 4); |
---|
196 | // createNewCar(0,70,1,2,0.0, new CalmPersonality(),0.0, 5); |
---|
197 | // createNewCar(0,90,1,2,0.0, new AggresivePersonality(),0.0, 6); |
---|
198 | // createNewCar(0,80,1,1,0.0, new AggresivePersonality(),0.0, 7); |
---|
199 | // createNewCar(0,75,1,2,0.0, new CalmPersonality(),0.0, 8); |
---|
200 | |
---|
201 | |
---|
202 | e.addCar(createNewCar(0,20,1,1,0.0, new AggresivePersonality(), 0.0, 1, 1, 235)); |
---|
203 | e.addCar(createNewCar(0,20,1,2,0.0, new CalmPersonality(), 0.0, 2, 1, 235)); |
---|
204 | e.addCar(createNewCar(0,20,1,3,0.0, new CalmPersonality(), 0.0, 3, 2, 20)); |
---|
205 | // e.addCar(createNewCar(0,90,1,2,0.0, new AggresivePersonality(), 0.0, 4, 0, 10)); |
---|
206 | // e.addCar(createNewCar(0,80,1,1,0.0, new CalmPersonality(), 0.0, 5, 0, 10)); |
---|
207 | // e.addCar(createNewCar(0,70,1,2,0.0, new CalmPersonality(), 0.0, 6, 0, 10)); |
---|
208 | // e.addCar(createNewCar(0,80,1,2,0.0, new AggresivePersonality(), 0.0, 7, 0, 10)); |
---|
209 | // |
---|
210 | // e.addCar(createNewCar(5,30,1,1,0.0, new AggresivePersonality(), 0.0, 8, 0, 10)); |
---|
211 | // e.addCar(createNewCar(5,30,1,2,0.0, new CalmPersonality(), 0.0, 9, 0, 10)); |
---|
212 | // |
---|
213 | // for(int i=0;i<30;i++) { |
---|
214 | // if(i%2==0) |
---|
215 | // e.addCar(createNewCar(3, i*3, 0, 1, 0.0, new AggresivePersonality(), 0.0, i+10, 4, 50)); |
---|
216 | // else |
---|
217 | // e.addCar(createNewCar(3, i*3, 0, 1, 0.0, new CalmPersonality(), 0.0, i+10, 4, 50)); |
---|
218 | // } |
---|
219 | // |
---|
220 | // for(int i=0;i<30;i++) { |
---|
221 | // if(i%2==0) |
---|
222 | // e.addCar(createNewCar(4, i*3, 0, 1, 0.0, new AggresivePersonality(), 0.0, i+40, 10, 50)); |
---|
223 | // else |
---|
224 | // e.addCar(createNewCar(4, i*3, 0, 1, 0.0, new CalmPersonality(), 0.0, i+40, 10, 50)); |
---|
225 | // } |
---|
226 | } |
---|
227 | |
---|
228 | public static void initCarsAutostrada(Engine e) { |
---|
229 | Random generator = new Random(System.currentTimeMillis()); |
---|
230 | |
---|
231 | for(int i=0;i<1500;i++) { |
---|
232 | if(generator.nextBoolean()) { |
---|
233 | e.addCar(createNewCar(0, i*6+1, 0, 1, 0.0, new AggresivePersonality(), 0.0, i, 0, 9950)); |
---|
234 | } else { |
---|
235 | e.addCar(createNewCar(0, i*6+1, 0, 1, 0.0, new CalmPersonality(), 0.0, i, 0, 9950)); |
---|
236 | } |
---|
237 | } |
---|
238 | } |
---|
239 | |
---|
240 | static Random generator = new Random(System.currentTimeMillis()); |
---|
241 | |
---|
242 | public static void initCarsTest(Engine e) { |
---|
243 | |
---|
244 | for(int i=0;i<Globals.map.roads.size();i++) { |
---|
245 | printRoad(i); |
---|
246 | } |
---|
247 | |
---|
248 | |
---|
249 | //road 0, low points |
---|
250 | for(int i=0;i<100;i++) { |
---|
251 | if(i%2==0) { |
---|
252 | if(generator.nextBoolean()) { |
---|
253 | e.addCar(createNewCar(0, i, 0, 1, 0.0, new AggresivePersonality(), 0.0, i, 1, 500)); |
---|
254 | } else { |
---|
255 | e.addCar(createNewCar(0, i, 0, 1, 0.0, new AggresivePersonality(), 0.0, i, 0, 500)); |
---|
256 | } |
---|
257 | } else { |
---|
258 | if(generator.nextBoolean()) { |
---|
259 | e.addCar(createNewCar(0, i, 0, 1, 0.0, new CalmPersonality(), 0.0, i, 1, 500)); |
---|
260 | } else { |
---|
261 | e.addCar(createNewCar(0, i, 0, 1, 0.0, new CalmPersonality(), 0.0, i, 0, 500)); |
---|
262 | } |
---|
263 | } |
---|
264 | } |
---|
265 | |
---|
266 | //road 0, high points |
---|
267 | for(int i=0;i<100;i++) { |
---|
268 | if(i%2==0) { |
---|
269 | if(generator.nextBoolean()) { |
---|
270 | e.addCar(createNewCar(0, i+400, 0, 1, 0.0, new AggresivePersonality(), 0.0, i+100, 0, 50)); |
---|
271 | } else { |
---|
272 | e.addCar(createNewCar(0, i+400, 0, 1, 0.0, new AggresivePersonality(), 0.0, i+100, 1, 500)); |
---|
273 | } |
---|
274 | } else { |
---|
275 | if(generator.nextBoolean()) { |
---|
276 | e.addCar(createNewCar(0, i+400, 0, 1, 0.0, new CalmPersonality(), 0.0, i+100, 0, 50)); |
---|
277 | } else { |
---|
278 | e.addCar(createNewCar(0, i+400, 0, 1, 0.0, new CalmPersonality(), 0.0, i+100, 1, 500)); |
---|
279 | } |
---|
280 | } |
---|
281 | } |
---|
282 | |
---|
283 | //road 1, low points |
---|
284 | for(int i=0;i<100;i++) { |
---|
285 | if(i%2==0) { |
---|
286 | if(generator.nextBoolean()) { |
---|
287 | e.addCar(createNewCar(1, i, 0, 1, 0.0, new AggresivePersonality(), 0.0, i+200, 1, 500)); |
---|
288 | } else { |
---|
289 | e.addCar(createNewCar(1, i, 0, 1, 0.0, new AggresivePersonality(), 0.0, i+200, 0, 500)); |
---|
290 | } |
---|
291 | } else { |
---|
292 | if(generator.nextBoolean()) { |
---|
293 | e.addCar(createNewCar(1, i, 0, 1, 0.0, new CalmPersonality(), 0.0, i+200, 1, 500)); |
---|
294 | } else { |
---|
295 | e.addCar(createNewCar(1, i, 0, 1, 0.0, new CalmPersonality(), 0.0, i+200, 0, 500)); |
---|
296 | } |
---|
297 | } |
---|
298 | } |
---|
299 | |
---|
300 | //road 1, high points |
---|
301 | for(int i=0;i<100;i++) { |
---|
302 | if(i%2==0) { |
---|
303 | if(generator.nextBoolean()) { |
---|
304 | e.addCar(createNewCar(1, i+400, 0, 1, 0.0, new AggresivePersonality(), 0.0, i+300, 1, 50)); |
---|
305 | } else { |
---|
306 | e.addCar(createNewCar(1, i+400, 0, 1, 0.0, new AggresivePersonality(), 0.0, i+300, 0, 500)); |
---|
307 | } |
---|
308 | } else { |
---|
309 | if(generator.nextBoolean()) { |
---|
310 | e.addCar(createNewCar(1, i+400, 0, 1, 0.0, new CalmPersonality(), 0.0, i+300, 1, 50)); |
---|
311 | } else { |
---|
312 | e.addCar(createNewCar(1, i+400, 0, 1, 0.0, new CalmPersonality(), 0.0, i+300, 0, 500)); |
---|
313 | } |
---|
314 | } |
---|
315 | } |
---|
316 | } |
---|
317 | |
---|
318 | public static CarInstance createNewCar(int roadIndex, int pointIndex, int direction, |
---|
319 | int lane, double offset, Personality p, double speed, int ID, |
---|
320 | int roadDest, int pointDest) { |
---|
321 | |
---|
322 | //is there a cross exactly at (roadIndex-pointIndex)?; |
---|
323 | Road r=Globals.map.roads.get(roadIndex); |
---|
324 | for(int i=0;i<r.crosses.size();i++) { |
---|
325 | if(r.crosses.get(i).getPointIndex()==pointIndex) |
---|
326 | return null; |
---|
327 | } |
---|
328 | |
---|
329 | CarInstance car=new CarInstance((short)roadIndex, (short)pointIndex, p); |
---|
330 | car.setDirection((byte)direction); //reverse |
---|
331 | car.setLane((byte)lane); |
---|
332 | car.setOffset(offset); |
---|
333 | car.setSpeed(speed); |
---|
334 | car.ID=ID; |
---|
335 | car.index=Globals.map.roads.get(roadIndex).carsOnThisRoad.size(); |
---|
336 | |
---|
337 | ArrayList<RouteSegment> route=RoutingUtils.getUnmergedDijkstraPath(new Location((short)roadIndex, (short)pointIndex), |
---|
338 | new Location((short)roadDest,(short)pointDest)); |
---|
339 | System.out.println("ROUTE FOR ID="+ID+"IS:"+route); |
---|
340 | car.setRoute(route); |
---|
341 | car.startOnRoad(); |
---|
342 | Globals.map.roads.get(car.getRoadIdx()).carsOnThisRoad.addCarInstance(car); |
---|
343 | |
---|
344 | System.out.println("new car added 2 "+car.ID); |
---|
345 | |
---|
346 | return car; |
---|
347 | } |
---|
348 | |
---|
349 | public static CarInstance createNewCar(int roadIndex, int pointIndex, int direction, |
---|
350 | int lane, double offset, Personality p, double speed, int ID, |
---|
351 | int roadDest, int pointDest, ArrayList<RouteSegment> route) { |
---|
352 | |
---|
353 | //is there a cross exactly at (roadIndex-pointIndex)?; |
---|
354 | Road r=Globals.map.roads.get(roadIndex); |
---|
355 | for(int i=0;i<r.crosses.size();i++) { |
---|
356 | if(r.crosses.get(i).getPointIndex()==pointIndex) |
---|
357 | return null; |
---|
358 | } |
---|
359 | |
---|
360 | CarInstance car=new CarInstance((short)roadIndex, (short)pointIndex, p); |
---|
361 | car.setDirection((byte)direction); //reverse |
---|
362 | car.setLane((byte)lane); |
---|
363 | car.setOffset(offset); |
---|
364 | car.setSpeed(speed); |
---|
365 | car.ID=ID; |
---|
366 | car.index=Globals.map.roads.get(roadIndex).carsOnThisRoad.size(); |
---|
367 | |
---|
368 | car.setRoute(RoutingUtils.splitRoute(route)); |
---|
369 | car.startOnRoad(); |
---|
370 | Globals.map.roads.get(car.getRoadIdx()).carsOnThisRoad.addCarInstance(car); |
---|
371 | |
---|
372 | System.out.println("new car added 1 "+car.ID); |
---|
373 | |
---|
374 | return car; |
---|
375 | } |
---|
376 | |
---|
377 | |
---|
378 | |
---|
379 | public static CarInstance createNewCar(int roadIndex, int pointIndex, |
---|
380 | int direction, int lane, double offset, Personality p, |
---|
381 | double speed, int ID, int roadDest, int pointDest, |
---|
382 | ArrayList<RouteSegment> route, int sp) { |
---|
383 | |
---|
384 | System.out.println("Start creating new car self routed"); |
---|
385 | |
---|
386 | // is there a cross exactly at (roadIndex-pointIndex)?; |
---|
387 | // is there a cross exactly at (roadIndex-pointIndex)?; |
---|
388 | Road r = Globals.map.roads.get(roadIndex); |
---|
389 | for (int i = 0; i < r.crosses.size(); i++) { |
---|
390 | if (r.crosses.get(i).getPointIndex() == pointIndex){ |
---|
391 | System.out.println("cros"); |
---|
392 | return null; |
---|
393 | } |
---|
394 | } |
---|
395 | |
---|
396 | CarInstance car = new CarInstance((short) roadIndex, |
---|
397 | (short) pointIndex, p); |
---|
398 | |
---|
399 | System.out.println("Been in CarInstance"); |
---|
400 | |
---|
401 | car.queryTime=(int)Math.abs((double)generator.nextInt(3000)); |
---|
402 | car.queryTime=(int)car.queryTime/((Globals.executionFPS / Globals.FPS)); |
---|
403 | car.queryTime++; |
---|
404 | car.queryTime=car.queryTime*(Globals.executionFPS / Globals.FPS); |
---|
405 | car.setDirection((byte) direction); // reverse |
---|
406 | car.setLane((byte) lane); |
---|
407 | car.setOffset(offset); |
---|
408 | car.setSpeed(speed); |
---|
409 | car.ID = ID; |
---|
410 | car.index = Globals.map.roads.get(roadIndex).carsOnThisRoad.size(); |
---|
411 | car.startTime = (Globals.engine.crtTime * 1000) / Globals.engine.fps; |
---|
412 | Route myRoute = new Route(); |
---|
413 | myRoute.entry = new Location(roadIndex, pointIndex); |
---|
414 | myRoute.exit = new Location(roadDest, pointDest); |
---|
415 | |
---|
416 | System.out.println("Been in Route and Location"); |
---|
417 | |
---|
418 | if (sp == 0) { |
---|
419 | |
---|
420 | System.out.println("sp=0"); |
---|
421 | |
---|
422 | car.routingType = 0; |
---|
423 | ArrayList<RouteSegment> rs = null; |
---|
424 | |
---|
425 | rs = RoutingUtils.getMergedDijkstraPath(new Location(roadIndex, |
---|
426 | pointIndex), new Location(roadDest, pointDest)); |
---|
427 | if (rs == null) { |
---|
428 | |
---|
429 | System.out.println("fail cu shortPath la "); |
---|
430 | return null; |
---|
431 | //carReceivingMsg.setRoute(RoutingUtils.splitRoute(route)); |
---|
432 | |
---|
433 | |
---|
434 | } else { |
---|
435 | int percent = generator.nextInt(100); |
---|
436 | percent = (int) Math.abs((double) percent); |
---|
437 | if (percent <= Globals.routePlanConstants.PERCENT_DYNAMIC_ROUTE) { |
---|
438 | percent = 1;// Shortest Path with dynamic search |
---|
439 | //System.out.println("Gen carReceivingMsg 1 cu id"+carReceivingMsg.ID); |
---|
440 | } else { |
---|
441 | percent = 0; // SortestPath without dynamic serch |
---|
442 | } |
---|
443 | car.routingType = percent; |
---|
444 | |
---|
445 | System.out.println("Before splitRoute(rs)"); |
---|
446 | |
---|
447 | myRoute.route = RoutingUtils.splitRoute(rs); |
---|
448 | |
---|
449 | System.out.println("Preparing to set route"); |
---|
450 | |
---|
451 | car.setRoute(myRoute.route); |
---|
452 | // System.out.println("Creez cu shortPath la "+carReceivingMsg.ID); |
---|
453 | |
---|
454 | } |
---|
455 | } else { |
---|
456 | |
---|
457 | System.out.println("sp!=0"); |
---|
458 | |
---|
459 | car.routingType = 1; |
---|
460 | Route allRutes = null; |
---|
461 | |
---|
462 | //Route bestR = null; |
---|
463 | RouteComputingUtils rc = new RouteComputingUtils(); |
---|
464 | // ////////////// |
---|
465 | |
---|
466 | System.out.println("Before allRoutes"); |
---|
467 | |
---|
468 | allRutes = rc.bestRoute(new Location(roadIndex, pointIndex), |
---|
469 | new Location(roadDest, pointDest)); |
---|
470 | |
---|
471 | System.out.println("Calculated allRoutes"); |
---|
472 | |
---|
473 | if (allRutes != null) { |
---|
474 | |
---|
475 | int percent = generator.nextInt(100); |
---|
476 | percent = (int) Math.abs((double) percent); |
---|
477 | if (percent <= Globals.routePlanConstants.PERCENT_DYNAMIC_ROUTE) { |
---|
478 | percent = 3;// Best Path with dynamic search |
---|
479 | //System.out.println("Gen carReceivingMsg 3 cu id"+carReceivingMsg.ID); |
---|
480 | } else { |
---|
481 | percent = 2; // BestPath without dynamic serch |
---|
482 | } |
---|
483 | car.routingType = percent; |
---|
484 | myRoute.route = RoutingUtils.splitRoute(allRutes.route); |
---|
485 | car.setRoute(myRoute.route); |
---|
486 | // System.out.println("Creez cu routingType la "+carReceivingMsg.ID); |
---|
487 | } else { |
---|
488 | //System.out.println("fail cu bestPath la "+carReceivingMsg.ID); |
---|
489 | return null; |
---|
490 | //carReceivingMsg.setRoute(RoutingUtils.splitRoute(route)); |
---|
491 | // System.out.println("fail la best la "+carReceivingMsg.ID); |
---|
492 | } |
---|
493 | |
---|
494 | } |
---|
495 | System.out.println("new car added"); |
---|
496 | |
---|
497 | car.startOnRoad(); |
---|
498 | Globals.map.roads.get(car.getRoadIdx()).carsOnThisRoad |
---|
499 | .addCarInstance(car); |
---|
500 | |
---|
501 | System.out.println("new car added "+car.ID); |
---|
502 | |
---|
503 | return car; |
---|
504 | } |
---|
505 | |
---|
506 | public static CarInstance createNewInvisibleCar(int roadIndex, |
---|
507 | int pointIndex, int direction, int lane, double offset, |
---|
508 | Personality p, double speed, int ID, int roadDest, int pointDest, |
---|
509 | RouteSegment routeStart, int sp) { |
---|
510 | |
---|
511 | // is there a cross exactly at (roadIndex-pointIndex)?; |
---|
512 | //Road r = Globals.map.roads.get(roadIndex); |
---|
513 | |
---|
514 | |
---|
515 | CarInstance car = new CarInstance((short) roadIndex, |
---|
516 | (short) pointIndex, p); |
---|
517 | car.queryTime = (int) Math.abs((double) generator.nextInt(3000)); |
---|
518 | car.setDirection((byte) direction); // reverse |
---|
519 | car.setLane((byte) lane); |
---|
520 | car.setOffset(offset); |
---|
521 | car.setSpeed(speed); |
---|
522 | car.ID = ID; |
---|
523 | car.index = -1; |
---|
524 | car.startTime = (Globals.engine.crtTime * 1000) / Globals.engine.fps; |
---|
525 | |
---|
526 | car.source = new Location(roadIndex, pointIndex); |
---|
527 | car.destination = new Location(roadDest, pointDest); |
---|
528 | car.route = new RouteSegment[1]; |
---|
529 | car.route[0] = routeStart; |
---|
530 | //carReceivingMsg.startOnRoad(); |
---|
531 | car.routingType=5; |
---|
532 | |
---|
533 | System.out.println("new car added invisible "+car.ID); |
---|
534 | |
---|
535 | return car; |
---|
536 | } |
---|
537 | |
---|
538 | |
---|
539 | |
---|
540 | public static void printRoad(int i) { |
---|
541 | System.out.println("*************"); |
---|
542 | System.out.println("Here's ROAD "+i+": "); |
---|
543 | ((Road)(Globals.map.roads.get(i))).printRoad(false); |
---|
544 | Road r=(Road) Globals.map.roads.get(i); |
---|
545 | Point last=(Point)r.points.get(r.points.size()-1); |
---|
546 | System.out.println("Length:"+last.getDistance()+"; numPoints="+r.points.size()); |
---|
547 | System.out.println("Crosses:"); |
---|
548 | for(int j=0;j<r.crosses.size();j++) { |
---|
549 | Cross c=(Cross) r.crosses.get(j); |
---|
550 | System.out.println("Cross "+j+":point "+c.getPointIndex()+"; road: "+c.getCrossRoadIndex()+";crosspoint="+c.getCrossPointIndex()); |
---|
551 | } |
---|
552 | System.out.println("*************"); |
---|
553 | } |
---|
554 | |
---|
555 | public static void printIntersection(int road, int point) { |
---|
556 | System.out.println("*************"); |
---|
557 | System.out.println("Here's INTERSECTION AT RD= "+road+"; POINT="+point); |
---|
558 | Road r=((Road)(Globals.map.roads.get(road))); |
---|
559 | for(int i=0;i<r.crosses.size();i++) { |
---|
560 | if(r.crosses.get(i).getPointIndex()==point) { |
---|
561 | Intersection inters=Globals.map.allIntersections.get(r.crosses.get(i).intersectionIndex); |
---|
562 | System.out.println(inters); |
---|
563 | System.out.println("THAT WAS IT!"); |
---|
564 | } |
---|
565 | } |
---|
566 | } |
---|
567 | |
---|
568 | public static ArrayList<EntryScenario> entryScenarios; |
---|
569 | |
---|
570 | public static ArrayList<EntryFlowVariation> entryFlowVariations = null; |
---|
571 | |
---|
572 | |
---|
573 | public static SortedEventsVector events = null; |
---|
574 | |
---|
575 | /*public static void loadScenario(Scenario sc) throws Exception { |
---|
576 | |
---|
577 | try { |
---|
578 | ObjectInputStream ois=new ObjectInputStream(new FileInputStream(sc.mapFileName)); |
---|
579 | Globals.map=(vnsim.map.object.Map)ois.readObject(); |
---|
580 | ois.close(); |
---|
581 | } catch (Exception ex) { |
---|
582 | // ex.printStackTrace(); |
---|
583 | |
---|
584 | throw ex; |
---|
585 | } |
---|
586 | Globals.scenarioName = sc.name; |
---|
587 | //sc.theMap; |
---|
588 | entryScenarios=sc.entryScenarios; |
---|
589 | events = new SortedEventsVector(); |
---|
590 | for(int i=0;i<entryScenarios.size();i++) { |
---|
591 | EntryScenario es=entryScenarios.get(i); |
---|
592 | int period=(int)((double)3600*Globals.executionFPS / (double)es.flow); |
---|
593 | NewCarEvent nce=new NewCarEvent(period,i, period); |
---|
594 | events.addEvent(nce); |
---|
595 | } |
---|
596 | |
---|
597 | entryFlowVariations = new ArrayList<EntryFlowVariation> (); |
---|
598 | if(Globals.variationsFile==null) |
---|
599 | return; |
---|
600 | try { |
---|
601 | String line=null; |
---|
602 | String full=""; |
---|
603 | RandomAccessFile f=new RandomAccessFile("maps//variations//" + Globals.variationsFile,"r"); |
---|
604 | while( (line=f.readLine())!=null ) { |
---|
605 | full=full+line; |
---|
606 | } |
---|
607 | String delim="\n \t\r"; |
---|
608 | for(char c='a';c<='z';c++) |
---|
609 | delim=delim+c; |
---|
610 | for(char c='A';c<='Z';c++) |
---|
611 | delim=delim+c; |
---|
612 | StringTokenizer st=new StringTokenizer(full,delim); |
---|
613 | |
---|
614 | int nEntries=Integer.parseInt(st.nextToken()); |
---|
615 | |
---|
616 | if(nEntries!=entryScenarios.size()) { |
---|
617 | System.err.println("WARNING! NENTRIES="+nEntries+" DIFFERENT FROM ENTRYSCENARIOS="+entryScenarios.size()); |
---|
618 | } |
---|
619 | |
---|
620 | for(int i=0;i<nEntries;i++) { |
---|
621 | EntryFlowVariation efv=new EntryFlowVariation(); |
---|
622 | |
---|
623 | System.out.println("Entry "+i); |
---|
624 | |
---|
625 | int nMoments=Integer.parseInt(st.nextToken()); |
---|
626 | for(int j=0;j<nMoments;j++) { |
---|
627 | int mom=Integer.parseInt(st.nextToken()); |
---|
628 | int val=Integer.parseInt(st.nextToken()); |
---|
629 | |
---|
630 | System.out.println("Adding mom="+mom+"; val="+val); |
---|
631 | |
---|
632 | efv.add(mom,val); |
---|
633 | } |
---|
634 | entryFlowVariations.add(efv); |
---|
635 | f.close(); |
---|
636 | } |
---|
637 | } catch (Exception ex) { |
---|
638 | System.out.println("EXCEPTION! variations.txt"); |
---|
639 | ex.printStackTrace(); |
---|
640 | } |
---|
641 | }*/ |
---|
642 | |
---|
643 | public static void loadScenario(Scenario sc) throws Exception { |
---|
644 | try { |
---|
645 | ObjectInputStream ois = null; |
---|
646 | if (sc.mapFileName.startsWith(".")) |
---|
647 | sc.mapFileName = sc.mapFileName.substring(1); |
---|
648 | // try to load it from the disk... |
---|
649 | String fd = System.getProperty("user.home")+File.separatorChar+sc.mapFileName.replace('/', File.separatorChar).replace('\\', File.separatorChar); |
---|
650 | File f = new File(fd); |
---|
651 | if (f.exists() && f.isFile() && f.canRead()) { |
---|
652 | ois = new ObjectInputStream(new FileInputStream(f)); |
---|
653 | } else { |
---|
654 | sc.mapFileName = sc.mapFileName.replace('\\', '/'); |
---|
655 | if (sc.mapFileName.startsWith(".")) sc.mapFileName = sc.mapFileName.substring(1); |
---|
656 | System.out.println("file name " + sc.mapFileName); |
---|
657 | ois = new ObjectInputStream(Utils.getInstance().openStream(sc.mapFileName)); |
---|
658 | } |
---|
659 | Globals.map=(vnsim.map.object.Map)ois.readObject(); |
---|
660 | |
---|
661 | |
---|
662 | /* Szekeres A. START_MODIFY */ |
---|
663 | //System.out.println("No of intersections : " + Globals.map.allIntersections.size()); |
---|
664 | /* change WirelessTrafficLights to EmissionsTrafficLights*/ |
---|
665 | Globals.map.changeToEmissionsTrafficLights(); |
---|
666 | /* Szekeres A. START_MODIFY */ |
---|
667 | |
---|
668 | ois.close(); |
---|
669 | } catch (Exception ex) { |
---|
670 | ex.printStackTrace(); |
---|
671 | throw ex; |
---|
672 | } |
---|
673 | //sc.theMap; |
---|
674 | entryScenarios=sc.entryScenarios; |
---|
675 | events = new SortedEventsVector(); |
---|
676 | for(int i=0;i<entryScenarios.size();i++) { |
---|
677 | EntryScenario es=entryScenarios.get(i); |
---|
678 | int period=(int)((double)3600*Globals.executionFPS / (double)es.flow); |
---|
679 | |
---|
680 | NewCarEvent nce=new NewCarEvent(period,i, period); |
---|
681 | events.addEvent(nce); |
---|
682 | } |
---|
683 | entryFlowVariations = new ArrayList<EntryFlowVariation> (); |
---|
684 | if(Globals.variationsFile == null) { |
---|
685 | logger.warning("No variation file specified"); |
---|
686 | return; |
---|
687 | } |
---|
688 | try { |
---|
689 | String line=null; |
---|
690 | String full=""; |
---|
691 | BufferedReader f = null; |
---|
692 | File fb = new File(System.getProperty("user.home")+File.separatorChar+"maps"+File.separatorChar+"variations"+File.separatorChar+Globals.variationsFile); |
---|
693 | if (fb.exists() && fb.isFile() && fb.canRead()) |
---|
694 | f = new BufferedReader(new InputStreamReader(new FileInputStream(fb))); |
---|
695 | else |
---|
696 | f = new BufferedReader(new InputStreamReader(Utils.getInstance().openStream("maps/variations/"+Globals.variationsFile))); |
---|
697 | while( (line=f.readLine())!=null ) { |
---|
698 | full=full+line; |
---|
699 | } |
---|
700 | String delim="\n \t\r"; |
---|
701 | for(char c='a';c<='z';c++) |
---|
702 | delim=delim+c; |
---|
703 | for(char c='A';c<='Z';c++) |
---|
704 | delim=delim+c; |
---|
705 | StringTokenizer st=new StringTokenizer(full,delim); |
---|
706 | int nEntries=Integer.parseInt(st.nextToken()); |
---|
707 | if(nEntries!=entryScenarios.size()) { |
---|
708 | logger.severe("WARNING! NENTRIES="+nEntries+" DIFFERENT FROM ENTRYSCENARIOS="+entryScenarios.size()); |
---|
709 | } |
---|
710 | for(int i=0;i<nEntries;i++) { |
---|
711 | EntryFlowVariation efv=new EntryFlowVariation(); |
---|
712 | logger.info("Entry "+i); |
---|
713 | int nMoments=Integer.parseInt(st.nextToken()); |
---|
714 | for(int j=0;j<nMoments;j++) { |
---|
715 | int mom=Integer.parseInt(st.nextToken()); |
---|
716 | int val=Integer.parseInt(st.nextToken()); |
---|
717 | logger.info("Adding mom="+mom+"; val="+val); |
---|
718 | efv.add(mom,val); |
---|
719 | } |
---|
720 | entryFlowVariations.add(efv); |
---|
721 | f.close(); |
---|
722 | } |
---|
723 | } catch (Exception ex) { |
---|
724 | logger.log(Level.WARNING, "EXCEPTION! variations.txt", ex); |
---|
725 | } |
---|
726 | } |
---|
727 | |
---|
728 | public static CarInstance createNewCarCommunicatingWithInfrastucture( |
---|
729 | int roadIndex, int pointIndex, int direction, int lane, |
---|
730 | double offset, Personality p, double speed, int ID, int roadDest, |
---|
731 | int pointDest, ArrayList<RouteSegment> route) { |
---|
732 | // |
---|
733 | |
---|
734 | // is there a cross exactly at (roadIndex-pointIndex)?; |
---|
735 | Road r = Globals.map.roads.get(roadIndex); |
---|
736 | for (int i = 0; i < r.crosses.size(); i++) { |
---|
737 | if (r.crosses.get(i).getPointIndex() == pointIndex) { |
---|
738 | return null; |
---|
739 | } |
---|
740 | } |
---|
741 | |
---|
742 | CarInstance car = new CarInstance((short) roadIndex, |
---|
743 | (short) pointIndex, p); |
---|
744 | car.queryTime = (int) Math.abs((double) generator.nextInt(100)); |
---|
745 | car.setDirection((byte) direction); // reverse |
---|
746 | car.setLane((byte) lane); |
---|
747 | car.setOffset(offset); |
---|
748 | car.setSpeed(speed); |
---|
749 | car.ID = ID; |
---|
750 | car.index = Globals.map.roads.get(roadIndex).carsOnThisRoad.size(); |
---|
751 | car.startTime = (Globals.engine.crtTime * 1000) / Globals.engine.fps; |
---|
752 | car.source = new Location(roadIndex, pointIndex); |
---|
753 | car.destination = new Location(roadDest, pointDest); |
---|
754 | car.routingType = 5; |
---|
755 | |
---|
756 | ArrayList<RouteSegment> rs = null; |
---|
757 | |
---|
758 | rs = RoutingUtils.getMergedDijkstraPath(new Location(roadIndex, |
---|
759 | pointIndex), new Location(roadDest, pointDest)); |
---|
760 | if (rs == null) { |
---|
761 | return null; |
---|
762 | } else { |
---|
763 | |
---|
764 | |
---|
765 | ArrayList<RouteSegment> routeTp = RoutingUtils.splitRoute(rs); |
---|
766 | ArrayList<RouteSegment> firstSegment = new ArrayList<RouteSegment>(); |
---|
767 | firstSegment.add(routeTp.get(0)); |
---|
768 | car.setRoute(firstSegment); |
---|
769 | |
---|
770 | } |
---|
771 | |
---|
772 | car.startOnRoad(); |
---|
773 | Globals.map.roads.get(car.getRoadIdx()).carsOnThisRoad |
---|
774 | .addCarInstance(car); |
---|
775 | |
---|
776 | System.out.println("new car added 3 "+car.ID); |
---|
777 | |
---|
778 | return car; |
---|
779 | |
---|
780 | } |
---|
781 | |
---|
782 | public static CarInstance createNewCityCarCommunicatingWithInfrastucture( |
---|
783 | int roadIndex, int pointIndex, int direction, int lane, |
---|
784 | double offset, Personality p, double speed, int ID, int roadDest, |
---|
785 | int pointDest, ArrayList<RouteSegment> route) { |
---|
786 | // |
---|
787 | |
---|
788 | // is there a cross exactly at (roadIndex-pointIndex)?; |
---|
789 | Road r = Globals.map.roads.get(roadIndex); |
---|
790 | for (int i = 0; i < r.crosses.size(); i++) { |
---|
791 | if (r.crosses.get(i).getPointIndex() == pointIndex) { |
---|
792 | return null; |
---|
793 | } |
---|
794 | } |
---|
795 | |
---|
796 | CarInstance car = new CarInstance((short) roadIndex, |
---|
797 | (short) pointIndex, p); |
---|
798 | car.queryTime = (int) Math.abs((double) generator.nextInt(100)); |
---|
799 | car.setDirection((byte) direction); // reverse |
---|
800 | car.setLane((byte) lane); |
---|
801 | car.setOffset(offset); |
---|
802 | car.setSpeed(speed); |
---|
803 | car.ID = ID; |
---|
804 | car.index = Globals.map.roads.get(roadIndex).carsOnThisRoad.size(); |
---|
805 | car.startTime = (Globals.engine.crtTime * 1000) / Globals.engine.fps; |
---|
806 | car.source = new Location(roadIndex, pointIndex); |
---|
807 | car.destination = new Location(roadDest, pointDest); |
---|
808 | car.routingType = 6; |
---|
809 | car.startMonitor = true; |
---|
810 | car.startTime = Globals.engine.crtTime; |
---|
811 | |
---|
812 | ArrayList<RouteSegment> rs = null; |
---|
813 | |
---|
814 | //todo: calculate the route based on road congestions |
---|
815 | rs = RoutingUtils.getMergedDijkstraPath(new Location(roadIndex, |
---|
816 | pointIndex), new Location(roadDest, pointDest)); |
---|
817 | if (rs == null) { |
---|
818 | return null; |
---|
819 | } |
---|
820 | |
---|
821 | ArrayList<RouteSegment> routeTp = RoutingUtils.splitRoute(rs); |
---|
822 | car.setRoute(routeTp); |
---|
823 | |
---|
824 | car.startOnRoad(); |
---|
825 | Globals.map.roads.get(car.getRoadIdx()).carsOnThisRoad |
---|
826 | .addCarInstance(car); |
---|
827 | |
---|
828 | System.out.println("new car added 4"+car.ID); |
---|
829 | |
---|
830 | return car; |
---|
831 | |
---|
832 | } |
---|
833 | |
---|
834 | public static CarInstance createNewEZCabClient( |
---|
835 | int roadIndex, int pointIndex, int direction, int lane, |
---|
836 | double offset, Personality p, double speed, int ID, int roadDest, |
---|
837 | int pointDest, ArrayList<RouteSegment> route) { |
---|
838 | |
---|
839 | |
---|
840 | Road r = Globals.map.roads.get(roadIndex); |
---|
841 | for (int i = 0; i < r.crosses.size(); i++) { |
---|
842 | if (r.crosses.get(i).getPointIndex() == pointIndex) { |
---|
843 | return null; |
---|
844 | } |
---|
845 | } |
---|
846 | |
---|
847 | CarInstance car = new CarInstance((short) roadIndex, |
---|
848 | (short) pointIndex, p); |
---|
849 | car.queryTime = (int) Math.abs((double) generator.nextInt(100)); |
---|
850 | car.setDirection((byte) direction); // reverse |
---|
851 | car.setLane((byte) lane); |
---|
852 | car.setOffset(offset); |
---|
853 | car.setSpeed(speed); |
---|
854 | car.ID = ID; |
---|
855 | car.index = Globals.map.roads.get(roadIndex).carsOnThisRoad.size(); |
---|
856 | car.startTime = (Globals.engine.crtTime * 1000) / Globals.engine.fps; |
---|
857 | car.source = new Location(roadIndex, pointIndex); |
---|
858 | car.destination = new Location(roadDest, pointDest); |
---|
859 | car.routingType = 6; |
---|
860 | car.startMonitor = true; |
---|
861 | car.startTime = Globals.engine.crtTime; |
---|
862 | |
---|
863 | ArrayList<RouteSegment> rs = null; |
---|
864 | |
---|
865 | //todo: calculate the route based on road congestions |
---|
866 | rs = RoutingUtils.getMergedDijkstraPath(new Location(roadIndex, |
---|
867 | pointIndex), new Location(roadDest, pointDest)); |
---|
868 | if (rs == null) { |
---|
869 | return null; |
---|
870 | } |
---|
871 | |
---|
872 | ArrayList<RouteSegment> routeTp = RoutingUtils.splitRoute(rs); |
---|
873 | car.setRoute(routeTp); |
---|
874 | |
---|
875 | car.startOnRoad(); |
---|
876 | Globals.map.roads.get(car.getRoadIdx()).carsOnThisRoad |
---|
877 | .addCarInstance(car); |
---|
878 | |
---|
879 | System.out.println("Create new EzCab client"+car.ID); |
---|
880 | |
---|
881 | return car; |
---|
882 | |
---|
883 | } |
---|
884 | |
---|
885 | |
---|
886 | } |
---|