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.routePlan; |
---|
7 | |
---|
8 | |
---|
9 | import java.io.File; |
---|
10 | import java.io.FileOutputStream; |
---|
11 | import java.io.ObjectOutputStream; |
---|
12 | import java.util.ArrayList; |
---|
13 | import java.util.Random; |
---|
14 | import java.util.logging.Logger; |
---|
15 | |
---|
16 | import vnsim.core.Engine; |
---|
17 | import vnsim.gui.Display; |
---|
18 | import vnsim.gui.MapLoader; |
---|
19 | import vnsim.map.object.Globals; |
---|
20 | import vnsim.map.object.Map; |
---|
21 | import vnsim.map.object.Point; |
---|
22 | import vnsim.map.utils.GPSutil; |
---|
23 | import vnsim.vehicular.generator.*; |
---|
24 | import vnsim.vehicular.routePlan.selfRouted.RoadAreaUtils; |
---|
25 | import vnsim.vehicular.scenarios.EntryExitScenario; |
---|
26 | import vnsim.vehicular.scenarios.EntryScenario; |
---|
27 | import vnsim.vehicular.scenarios.Route; |
---|
28 | import vnsim.vehicular.scenarios.Scenario; |
---|
29 | import vnsim.vehicular.scenarios.ScenarioMap; |
---|
30 | import vnsim.vehicular.simulator.Location; |
---|
31 | import vnsim.vehicular.simulator.RouteSegment; |
---|
32 | |
---|
33 | |
---|
34 | |
---|
35 | public class MakeScenarios { |
---|
36 | |
---|
37 | |
---|
38 | /** Logger used by this class */ |
---|
39 | private static final transient Logger logger = Logger.getLogger("vnsim.vehicular.routePlan.MakeScenarios"); |
---|
40 | |
---|
41 | public static RouteComputingUtils rc = new RouteComputingUtils(); |
---|
42 | |
---|
43 | public static void setBestRoute(Scenario s,ArrayList<Location> entries,ArrayList<Location> exits) |
---|
44 | { |
---|
45 | |
---|
46 | int i,j; |
---|
47 | |
---|
48 | EntryScenario es; |
---|
49 | EntryExitScenario ees; |
---|
50 | Route r; |
---|
51 | |
---|
52 | |
---|
53 | for (i = 0; i < entries.size(); i++) |
---|
54 | { |
---|
55 | es = new EntryScenario(entries.get(i)); |
---|
56 | for (j = 0; j < exits.size(); j++) |
---|
57 | { |
---|
58 | ees = new EntryExitScenario(entries.get(i), exits.get(j)); |
---|
59 | ees.percentsEachRoute.add(new Integer(100)); |
---|
60 | |
---|
61 | r = new Route(); |
---|
62 | r.entry = entries.get(i); |
---|
63 | r.exit = exits.get(j); |
---|
64 | r.route = new ArrayList<RouteSegment>(); |
---|
65 | try |
---|
66 | { |
---|
67 | r = rc.bestRoute(r.entry, r.exit); |
---|
68 | } |
---|
69 | catch (Exception e) |
---|
70 | { |
---|
71 | } |
---|
72 | if (r == null) |
---|
73 | { |
---|
74 | r = new Route(); |
---|
75 | r.entry = entries.get(i); |
---|
76 | r.exit = exits.get(j); |
---|
77 | r.route = new ArrayList<RouteSegment>(); |
---|
78 | System.out.println("No route for " + r.entry |
---|
79 | + " to " + r.exit); |
---|
80 | } |
---|
81 | ees.routes.add(r); |
---|
82 | es.entryExits.add(ees); |
---|
83 | s.allRoutes.add(r); |
---|
84 | } |
---|
85 | for (j = 0; j < es.entryExits.size(); j++) |
---|
86 | { |
---|
87 | es.entryExits.get(j).percentOfFlow = (int) (100); |
---|
88 | } |
---|
89 | s.entryScenarios.add(es); |
---|
90 | } |
---|
91 | |
---|
92 | } |
---|
93 | |
---|
94 | public static Scenario createSmallReservation(String name, Location entry, Location exit) |
---|
95 | { |
---|
96 | |
---|
97 | Scenario s; |
---|
98 | ArrayList<Location> entries = new ArrayList<Location>(); |
---|
99 | ArrayList<Location> exits = new ArrayList<Location>(); |
---|
100 | Mobility.events = new SortedEventsVector(); |
---|
101 | |
---|
102 | String mapFileName = System.getProperty("user.home") + File.separatorChar + "maps" + |
---|
103 | File.separatorChar + "map" + File.separatorChar + name + ".map"; |
---|
104 | |
---|
105 | ArrayList<Route> allRoutes = new ArrayList<Route>(); |
---|
106 | |
---|
107 | ArrayList<String> driverTypes = new ArrayList<String>(); |
---|
108 | driverTypes.add("very calm"); |
---|
109 | driverTypes.add("regular"); |
---|
110 | driverTypes.add("aggresive"); |
---|
111 | |
---|
112 | entries.add(entry); |
---|
113 | exits.add(exit); |
---|
114 | |
---|
115 | s = new Scenario(mapFileName, entries, exits, driverTypes, allRoutes, |
---|
116 | name); |
---|
117 | |
---|
118 | EntryScenario es; |
---|
119 | EntryExitScenario ees; |
---|
120 | |
---|
121 | int i, j; |
---|
122 | |
---|
123 | Route r; |
---|
124 | |
---|
125 | for (i = 0; i < entries.size(); i++) |
---|
126 | { |
---|
127 | es = new EntryScenario(entries.get(i)); |
---|
128 | es.flow = 400; |
---|
129 | |
---|
130 | int period=(int)((double)3600*Globals.executionFPS / (double)es.flow); |
---|
131 | |
---|
132 | NewCarEvent nce=new NewCarEvent(period,i, period); |
---|
133 | Mobility.events.addEvent(nce); |
---|
134 | |
---|
135 | es.percentsDriverTypes.add(new Integer(33)); |
---|
136 | es.percentsDriverTypes.add(new Integer(33)); |
---|
137 | es.percentsDriverTypes.add(new Integer(34)); |
---|
138 | for (j = 0; j < exits.size(); j++) |
---|
139 | { |
---|
140 | { |
---|
141 | |
---|
142 | ees = new EntryExitScenario(entries.get(i), exits.get(j)); |
---|
143 | ees.percentsEachRoute.add(new Integer(100)); |
---|
144 | r = new Route(); |
---|
145 | r.entry = entries.get(i); |
---|
146 | r.exit = exits.get(j); |
---|
147 | r.route = new ArrayList<RouteSegment>(); |
---|
148 | try { |
---|
149 | r = rc.bestRoute(r.entry, r.exit); |
---|
150 | |
---|
151 | } catch (Exception e) { |
---|
152 | } |
---|
153 | ees.routes.add(r); |
---|
154 | es.entryExits.add(ees); |
---|
155 | s.allRoutes.add(r); |
---|
156 | |
---|
157 | } |
---|
158 | } |
---|
159 | for (j = 0; j < es.entryExits.size(); j++) |
---|
160 | { |
---|
161 | es.entryExits.get(j).percentOfFlow = (int) (100); |
---|
162 | } |
---|
163 | s.entryScenarios.add(es); |
---|
164 | } |
---|
165 | try |
---|
166 | { |
---|
167 | ObjectOutputStream outStr = new ObjectOutputStream( |
---|
168 | new FileOutputStream(System.getProperty("user.home") + File.separatorChar + "maps" + |
---|
169 | File.separatorChar + "fsc" + File.separatorChar + name + ".fsc")); |
---|
170 | outStr.writeObject(s); |
---|
171 | } catch (Exception e) { |
---|
172 | e.printStackTrace(); |
---|
173 | } |
---|
174 | |
---|
175 | ScenarioMap unconfigured = new ScenarioMap(s); |
---|
176 | unconfigured.imageFileName = new String("Small"+name+"Img.jpg"); |
---|
177 | try { |
---|
178 | ObjectOutputStream outStr = new ObjectOutputStream( |
---|
179 | new FileOutputStream(System.getProperty("user.home") + File.separatorChar + "maps" + |
---|
180 | File.separatorChar + "smf" + File.separatorChar + name + ".smf")); |
---|
181 | outStr.writeObject(unconfigured); |
---|
182 | } catch (Exception e) { |
---|
183 | e.printStackTrace(); |
---|
184 | } |
---|
185 | Mobility.entryScenarios = s.entryScenarios; |
---|
186 | return s; |
---|
187 | } |
---|
188 | |
---|
189 | public static Scenario createScenario(String name, |
---|
190 | ArrayList<Location> entries, ArrayList<Location> exits, |
---|
191 | ArrayList<Integer> entryFlows) { |
---|
192 | String mapFileName = System.getProperty("user.home") + File.separatorChar + "maps" + |
---|
193 | File.separatorChar + "map" + File.separatorChar + name + ".map"; |
---|
194 | ArrayList<Route> allRoutes = new ArrayList<Route>(); |
---|
195 | |
---|
196 | ArrayList<String> driverTypes = new ArrayList<String>(); |
---|
197 | driverTypes.add("very calm"); |
---|
198 | driverTypes.add("regular"); |
---|
199 | driverTypes.add("aggresive"); |
---|
200 | Scenario s = new Scenario(mapFileName, entries, exits, driverTypes, |
---|
201 | allRoutes, name); |
---|
202 | |
---|
203 | if (entryFlows.size() != entries.size()) { |
---|
204 | System.out.println("nb of flows do not match the nb of entries"); |
---|
205 | return null; |
---|
206 | } |
---|
207 | EntryScenario es; |
---|
208 | EntryExitScenario ees; |
---|
209 | int i, j; |
---|
210 | Route r; |
---|
211 | for (i = 0; i < entries.size(); i++) { |
---|
212 | es = new EntryScenario(entries.get(i)); |
---|
213 | es.flow = entryFlows.get(i); |
---|
214 | |
---|
215 | int period=(int)((double)3600*Globals.executionFPS / (double)es.flow); |
---|
216 | NewCarEvent nce=new NewCarEvent(period,i, period); |
---|
217 | Mobility.events.addEvent(nce); |
---|
218 | |
---|
219 | es.percentsDriverTypes.add(new Integer(33)); |
---|
220 | es.percentsDriverTypes.add(new Integer(33)); |
---|
221 | es.percentsDriverTypes.add(new Integer(34)); |
---|
222 | for (j = 0; j < exits.size(); j++) { |
---|
223 | { |
---|
224 | if (entries.get(i).equals(exits.get(j))) { |
---|
225 | continue; |
---|
226 | } |
---|
227 | ees = new EntryExitScenario(entries.get(i), exits.get(j)); |
---|
228 | ees.percentsEachRoute.add(new Integer(100)); |
---|
229 | r = new Route(); |
---|
230 | r.entry = entries.get(i); |
---|
231 | r.exit = exits.get(j); |
---|
232 | r.route = new ArrayList<RouteSegment>(); |
---|
233 | try { |
---|
234 | r = rc.bestRoute(r.entry, r.exit); |
---|
235 | |
---|
236 | } catch (Exception e) { |
---|
237 | } |
---|
238 | ees.routes.add(r); |
---|
239 | es.entryExits.add(ees); |
---|
240 | s.allRoutes.add(r); |
---|
241 | |
---|
242 | } |
---|
243 | } |
---|
244 | for (j = 0; j < es.entryExits.size(); j++) { |
---|
245 | es.entryExits.get(j).percentOfFlow = (int) (100 / es.entryExits |
---|
246 | .size()); |
---|
247 | } |
---|
248 | s.entryScenarios.add(es); |
---|
249 | } |
---|
250 | |
---|
251 | try { |
---|
252 | ObjectOutputStream outStr = new ObjectOutputStream( |
---|
253 | new FileOutputStream(System.getProperty("user.home") + File.separatorChar + "maps" + |
---|
254 | File.separatorChar + "fsc" + File.separatorChar + name + ".fsc")); |
---|
255 | outStr.writeObject(s); |
---|
256 | } catch (Exception e) { |
---|
257 | e.printStackTrace(); |
---|
258 | } |
---|
259 | |
---|
260 | ScenarioMap unconfigured = new ScenarioMap(s); |
---|
261 | unconfigured.imageFileName = new String("Small" + name + "Img.jpg"); |
---|
262 | try { |
---|
263 | ObjectOutputStream outStr = new ObjectOutputStream( |
---|
264 | new FileOutputStream(System.getProperty("user.home") + File.separatorChar + "maps" + |
---|
265 | File.separatorChar + "smf" + File.separatorChar + name + ".smf")); |
---|
266 | outStr.writeObject(unconfigured); |
---|
267 | } catch (Exception e) { |
---|
268 | e.printStackTrace(); |
---|
269 | } |
---|
270 | Mobility.entryScenarios = s.entryScenarios; |
---|
271 | return s; |
---|
272 | } |
---|
273 | |
---|
274 | |
---|
275 | public static void createScenarioHighway() { |
---|
276 | |
---|
277 | Scenario s; |
---|
278 | Location entry, exit; |
---|
279 | ArrayList<Location> entries = new ArrayList<Location>(); |
---|
280 | ArrayList<Location> exits = new ArrayList<Location>(); |
---|
281 | String mapFileName = System.getProperty("user.home") + File.separatorChar + "maps" + |
---|
282 | File.separatorChar + "map" + File.separatorChar + "NJSmall.map"; |
---|
283 | String name = "NJSmall"; |
---|
284 | ArrayList<Route> allRoutes = new ArrayList<Route>(); |
---|
285 | |
---|
286 | ArrayList<String> driverTypes = new ArrayList<String>(); |
---|
287 | driverTypes.add("very calm"); |
---|
288 | driverTypes.add("regular"); |
---|
289 | driverTypes.add("aggresive"); |
---|
290 | |
---|
291 | entries.add(new Location(1, 1)); |
---|
292 | entries.add(new Location(12, 372)); |
---|
293 | entries.add(new Location(2, 1)); |
---|
294 | entries.add(new Location(2, 514)); |
---|
295 | entries.add(new Location(20, 1)); |
---|
296 | entries.add(new Location(18, 110)); |
---|
297 | entries.add(new Location(19, 46)); |
---|
298 | entries.add(new Location(14, 966)); |
---|
299 | entries.add(new Location(22, 2)); |
---|
300 | entries.add(new Location(22, 110)); |
---|
301 | entries.add(new Location(15, 3)); |
---|
302 | entries.add(new Location(31, 4)); |
---|
303 | entries.add(new Location(8, 355)); |
---|
304 | entries.add(new Location(6, 634)); |
---|
305 | entries.add(new Location(30, 1148)); |
---|
306 | entries.add(new Location(31, 865)); |
---|
307 | entries.add(new Location(13, 822)); |
---|
308 | entries.add(new Location(7, 570)); |
---|
309 | entries.add(new Location(24, 455)); |
---|
310 | entries.add(new Location(16, 371)); |
---|
311 | entries.add(new Location(21, 3)); |
---|
312 | entries.add(new Location(4, 3211)); |
---|
313 | entries.add(new Location(4, 5)); |
---|
314 | entries.add(new Location(5, 869)); |
---|
315 | exits.add(new Location(1, 1)); |
---|
316 | exits.add(new Location(12, 372)); |
---|
317 | exits.add(new Location(2, 1)); |
---|
318 | exits.add(new Location(2, 514)); |
---|
319 | exits.add(new Location(20, 1)); |
---|
320 | exits.add(new Location(18, 110)); |
---|
321 | exits.add(new Location(19, 46)); |
---|
322 | exits.add(new Location(14, 966)); |
---|
323 | exits.add(new Location(22, 2)); |
---|
324 | exits.add(new Location(22, 110)); |
---|
325 | exits.add(new Location(15, 3)); |
---|
326 | exits.add(new Location(31, 4)); |
---|
327 | exits.add(new Location(8, 355)); |
---|
328 | exits.add(new Location(6, 634)); |
---|
329 | exits.add(new Location(30, 1148)); |
---|
330 | exits.add(new Location(31, 865)); |
---|
331 | exits.add(new Location(13, 822)); |
---|
332 | exits.add(new Location(7, 570)); |
---|
333 | exits.add(new Location(24, 455)); |
---|
334 | exits.add(new Location(16, 371)); |
---|
335 | exits.add(new Location(21, 3)); |
---|
336 | exits.add(new Location(4, 3211)); |
---|
337 | exits.add(new Location(4, 5)); |
---|
338 | exits.add(new Location(5, 869)); |
---|
339 | |
---|
340 | s = new Scenario(mapFileName, entries, exits, driverTypes, allRoutes, |
---|
341 | name); |
---|
342 | EntryScenario es; |
---|
343 | EntryExitScenario ees; |
---|
344 | int i, j; |
---|
345 | Route r; |
---|
346 | for (i = 0; i < entries.size(); i++) { |
---|
347 | es = new EntryScenario(entries.get(i)); |
---|
348 | es.flow = 400; |
---|
349 | es.percentsDriverTypes.add(new Integer(33)); |
---|
350 | es.percentsDriverTypes.add(new Integer(33)); |
---|
351 | es.percentsDriverTypes.add(new Integer(34)); |
---|
352 | Point p1, p2; |
---|
353 | for (j = 0; j < exits.size(); j++) { |
---|
354 | if (i == j) { |
---|
355 | continue; |
---|
356 | } else { |
---|
357 | p1 = Globals.map.roads.get(entries.get(i).roadIdx).points |
---|
358 | .get(entries.get(i).ptIdx); |
---|
359 | p2 = Globals.map.roads.get(exits.get(j).roadIdx).points |
---|
360 | .get(exits.get(j).ptIdx); |
---|
361 | float y1, y2, x1, x2; |
---|
362 | y2 = (float) GPSutil.getMetersLatitude( |
---|
363 | Globals.map.minPoint, p2); |
---|
364 | x2 = (float) GPSutil.getMetersLongitude( |
---|
365 | Globals.map.minPoint, p2); |
---|
366 | y1 = (float) GPSutil.getMetersLatitude( |
---|
367 | Globals.map.minPoint, p1); |
---|
368 | x1 = (float) GPSutil.getMetersLongitude( |
---|
369 | Globals.map.minPoint, p1); |
---|
370 | double d = Math |
---|
371 | .sqrt((double) ((x2 - x1) * (x2 - x1) + (y2 - y1) |
---|
372 | * (y2 - y1))); |
---|
373 | |
---|
374 | if (d > 1000.0) { |
---|
375 | System.out.println("Pt entry " + i + " si exit " + j); |
---|
376 | ees = new EntryExitScenario(entries.get(i), exits |
---|
377 | .get(j)); |
---|
378 | ees.percentsEachRoute.add(new Integer(100)); |
---|
379 | r = new Route(); |
---|
380 | r.entry = entries.get(i); |
---|
381 | r.exit = exits.get(j); |
---|
382 | r.route = new ArrayList<RouteSegment>(); |
---|
383 | |
---|
384 | r = rc.bestRoute(r.entry, r.exit); |
---|
385 | if (r == null) { |
---|
386 | r = new Route(); |
---|
387 | r.entry = entries.get(i); |
---|
388 | r.exit = exits.get(j); |
---|
389 | r.route = new ArrayList<RouteSegment>(); |
---|
390 | System.out.println("No route for " + r.entry |
---|
391 | + " to " + r.exit); |
---|
392 | } |
---|
393 | |
---|
394 | ees.routes.add(r); |
---|
395 | s.allRoutes.add(r); |
---|
396 | es.entryExits.add(ees); |
---|
397 | |
---|
398 | } |
---|
399 | } |
---|
400 | } |
---|
401 | for (j = 0; j < es.entryExits.size(); j++) { |
---|
402 | es.entryExits.get(j).percentOfFlow = (int) (100 / es.entryExits |
---|
403 | .size()); |
---|
404 | } |
---|
405 | s.entryScenarios.add(es); |
---|
406 | } |
---|
407 | try { |
---|
408 | ObjectOutputStream outStr = new ObjectOutputStream( |
---|
409 | new FileOutputStream(System.getProperty("user.home") + File.separatorChar + "maps" + |
---|
410 | File.separatorChar + "fsc" + File.separatorChar + "NJSmall.fsc")); |
---|
411 | outStr.writeObject(s); |
---|
412 | } catch (Exception e) { |
---|
413 | e.printStackTrace(); |
---|
414 | } |
---|
415 | |
---|
416 | ScenarioMap unconfigured = new ScenarioMap(s); |
---|
417 | unconfigured.imageFileName = new String("NJSmallImg.jpg"); |
---|
418 | try { |
---|
419 | ObjectOutputStream outStr = new ObjectOutputStream( |
---|
420 | new FileOutputStream(System.getProperty("user.home") + File.separatorChar + "maps" + |
---|
421 | File.separatorChar + "smf" + File.separatorChar + "NJSmall.smf")); |
---|
422 | outStr.writeObject(unconfigured); |
---|
423 | } catch (Exception e) { |
---|
424 | e.printStackTrace(); |
---|
425 | } |
---|
426 | System.out.println("GATA"); |
---|
427 | } |
---|
428 | |
---|
429 | public static void createScenarioDownTown() { |
---|
430 | Scenario s; |
---|
431 | Location entry, exit; |
---|
432 | ArrayList<Location> entries = new ArrayList<Location>(); |
---|
433 | ArrayList<Location> exits = new ArrayList<Location>(); |
---|
434 | String mapFileName = System.getProperty("user.home") + File.separatorChar + "maps" + |
---|
435 | File.separatorChar + "map" + File.separatorChar + "DowntownRoutingFull.map"; |
---|
436 | String name = "DowntownRoutingFull"; |
---|
437 | ArrayList<Route> allRoutes = new ArrayList<Route>(); |
---|
438 | |
---|
439 | ArrayList<String> driverTypes = new ArrayList<String>(); |
---|
440 | driverTypes.add("very calm"); |
---|
441 | driverTypes.add("regular"); |
---|
442 | driverTypes.add("aggresive"); |
---|
443 | |
---|
444 | entries.add(new Location(14, 228)); |
---|
445 | entries.add(new Location(1, 64)); |
---|
446 | entries.add(new Location(2, 240)); |
---|
447 | entries.add(new Location(0, 240)); |
---|
448 | entries.add(new Location(9, 256)); |
---|
449 | entries.add(new Location(10, 224)); |
---|
450 | entries.add(new Location(4, 224)); |
---|
451 | entries.add(new Location(5, 256)); |
---|
452 | entries.add(new Location(3, 240)); |
---|
453 | entries.add(new Location(8, 144)); |
---|
454 | entries.add(new Location(6, 136)); |
---|
455 | |
---|
456 | entries.add(new Location(22, 79)); |
---|
457 | entries.add(new Location(21, 64)); |
---|
458 | entries.add(new Location(20, 64)); |
---|
459 | entries.add(new Location(3, 0)); |
---|
460 | entries.add(new Location(5, 0)); |
---|
461 | entries.add(new Location(4, 0)); |
---|
462 | entries.add(new Location(10, 0)); |
---|
463 | entries.add(new Location(9, 0)); |
---|
464 | entries.add(new Location(0, 0)); |
---|
465 | entries.add(new Location(2, 0)); |
---|
466 | entries.add(new Location(16, 0)); |
---|
467 | entries.add(new Location(14, 0)); |
---|
468 | |
---|
469 | entries.add(new Location(18, 208)); |
---|
470 | entries.add(new Location(17, 200)); |
---|
471 | entries.add(new Location(15, 208)); |
---|
472 | entries.add(new Location(11, 207)); |
---|
473 | entries.add(new Location(13, 223)); |
---|
474 | |
---|
475 | entries.add(new Location(18, 0)); |
---|
476 | entries.add(new Location(13, 0)); |
---|
477 | entries.add(new Location(15, 0)); |
---|
478 | entries.add(new Location(11, 0)); |
---|
479 | // ///////////////////////////////// |
---|
480 | |
---|
481 | exits.add(new Location(14, 228)); |
---|
482 | exits.add(new Location(1, 64)); |
---|
483 | exits.add(new Location(2, 240)); |
---|
484 | exits.add(new Location(0, 240)); |
---|
485 | exits.add(new Location(9, 256)); |
---|
486 | exits.add(new Location(10, 224)); |
---|
487 | exits.add(new Location(4, 224)); |
---|
488 | exits.add(new Location(5, 256)); |
---|
489 | exits.add(new Location(3, 240)); |
---|
490 | exits.add(new Location(8, 144)); |
---|
491 | exits.add(new Location(6, 136)); |
---|
492 | |
---|
493 | exits.add(new Location(22, 79)); |
---|
494 | exits.add(new Location(21, 64)); |
---|
495 | exits.add(new Location(20, 64)); |
---|
496 | exits.add(new Location(3, 0)); |
---|
497 | exits.add(new Location(5, 0)); |
---|
498 | exits.add(new Location(4, 0)); |
---|
499 | exits.add(new Location(10, 0)); |
---|
500 | exits.add(new Location(9, 0)); |
---|
501 | exits.add(new Location(0, 0)); |
---|
502 | exits.add(new Location(2, 0)); |
---|
503 | exits.add(new Location(16, 0)); |
---|
504 | exits.add(new Location(14, 0)); |
---|
505 | |
---|
506 | exits.add(new Location(18, 208)); |
---|
507 | exits.add(new Location(17, 200)); |
---|
508 | exits.add(new Location(15, 208)); |
---|
509 | exits.add(new Location(11, 207)); |
---|
510 | exits.add(new Location(13, 223)); |
---|
511 | |
---|
512 | exits.add(new Location(18, 0)); |
---|
513 | exits.add(new Location(13, 0)); |
---|
514 | exits.add(new Location(15, 0)); |
---|
515 | exits.add(new Location(11, 0)); |
---|
516 | |
---|
517 | s = new Scenario(mapFileName, entries, exits, driverTypes, allRoutes, |
---|
518 | name); |
---|
519 | EntryScenario es; |
---|
520 | EntryExitScenario ees; |
---|
521 | int i, j; |
---|
522 | Route r; |
---|
523 | for (i = 0; i < entries.size(); i++) { |
---|
524 | es = new EntryScenario(entries.get(i)); |
---|
525 | es.flow = 200; |
---|
526 | es.percentsDriverTypes.add(new Integer(33)); |
---|
527 | es.percentsDriverTypes.add(new Integer(33)); |
---|
528 | es.percentsDriverTypes.add(new Integer(34)); |
---|
529 | Point p1, p2; |
---|
530 | for (j = 0; j < exits.size(); j++) { |
---|
531 | if (i == j) { |
---|
532 | continue; |
---|
533 | } else { |
---|
534 | p1 = Globals.map.roads.get(entries.get(i).roadIdx).points |
---|
535 | .get(entries.get(i).ptIdx); |
---|
536 | p2 = Globals.map.roads.get(exits.get(j).roadIdx).points |
---|
537 | .get(exits.get(j).ptIdx); |
---|
538 | float y1, y2, x1, x2; |
---|
539 | y2 = (float) GPSutil.getMetersLatitude( |
---|
540 | Globals.map.minPoint, p2); |
---|
541 | x2 = (float) GPSutil.getMetersLongitude( |
---|
542 | Globals.map.minPoint, p2); |
---|
543 | y1 = (float) GPSutil.getMetersLatitude( |
---|
544 | Globals.map.minPoint, p1); |
---|
545 | x1 = (float) GPSutil.getMetersLongitude( |
---|
546 | Globals.map.minPoint, p1); |
---|
547 | double d = Math |
---|
548 | .sqrt((double) ((x2 - x1) * (x2 - x1) + (y2 - y1) |
---|
549 | * (y2 - y1))); |
---|
550 | |
---|
551 | if (d > 250.0) { |
---|
552 | System.out.println("Pt entry " + i + " si exit " + j); |
---|
553 | ees = new EntryExitScenario(entries.get(i), exits |
---|
554 | .get(j)); |
---|
555 | ees.percentsEachRoute.add(new Integer(100)); |
---|
556 | r = new Route(); |
---|
557 | r.entry = entries.get(i); |
---|
558 | r.exit = exits.get(j); |
---|
559 | r.route = new ArrayList<RouteSegment>(); |
---|
560 | |
---|
561 | r = rc.bestRoute(r.entry, r.exit); |
---|
562 | if (r == null) { |
---|
563 | r = new Route(); |
---|
564 | r.entry = entries.get(i); |
---|
565 | r.exit = exits.get(j); |
---|
566 | r.route = new ArrayList<RouteSegment>(); |
---|
567 | System.out.println("No route for " + r.entry |
---|
568 | + " to " + r.exit); |
---|
569 | } |
---|
570 | |
---|
571 | |
---|
572 | ees.routes.add(r); |
---|
573 | s.allRoutes.add(r); |
---|
574 | es.entryExits.add(ees); |
---|
575 | } |
---|
576 | } |
---|
577 | } |
---|
578 | for (j = 0; j < es.entryExits.size(); j++) { |
---|
579 | es.entryExits.get(j).percentOfFlow = (int) (100 / es.entryExits |
---|
580 | .size()); |
---|
581 | } |
---|
582 | s.entryScenarios.add(es); |
---|
583 | } |
---|
584 | try { |
---|
585 | ObjectOutputStream outStr = new ObjectOutputStream( |
---|
586 | |
---|
587 | new FileOutputStream(System.getProperty("user.home") + File.separatorChar + "maps" + |
---|
588 | File.separatorChar + "fsc" + File.separatorChar + |
---|
589 | "DowntownRoutingFull.fsc")); |
---|
590 | |
---|
591 | outStr.writeObject(s); |
---|
592 | } catch (Exception e) { |
---|
593 | e.printStackTrace(); |
---|
594 | } |
---|
595 | |
---|
596 | ScenarioMap unconfigured = new ScenarioMap(s); |
---|
597 | unconfigured.imageFileName = new String("DowntownRoutingImg.jpg"); |
---|
598 | try { |
---|
599 | ObjectOutputStream outStr = new ObjectOutputStream( |
---|
600 | new FileOutputStream(System.getProperty("user.home") + File.separatorChar + "maps" + |
---|
601 | File.separatorChar + "smf" + File.separatorChar + |
---|
602 | "DowntownRoutingFull.smf")); |
---|
603 | outStr.writeObject(unconfigured); |
---|
604 | } catch (Exception e) { |
---|
605 | e.printStackTrace(); |
---|
606 | } |
---|
607 | |
---|
608 | System.out.print("gata"); |
---|
609 | |
---|
610 | } |
---|
611 | |
---|
612 | public static void createScenarioHighwaySmall() { |
---|
613 | Scenario s; |
---|
614 | Location entry, exit; |
---|
615 | ArrayList<Location> entries = new ArrayList<Location>(); |
---|
616 | ArrayList<Location> exits = new ArrayList<Location>(); |
---|
617 | String mapFileName = System.getProperty("user.home") + File.separatorChar + "maps" + |
---|
618 | File.separatorChar + "map" + File.separatorChar + "Highway.map"; |
---|
619 | String name = "Highway"; |
---|
620 | ArrayList<Route> allRoutes = new ArrayList<Route>(); |
---|
621 | |
---|
622 | ArrayList<String> driverTypes = new ArrayList<String>(); |
---|
623 | driverTypes.add("very calm"); |
---|
624 | driverTypes.add("regular"); |
---|
625 | driverTypes.add("aggresive"); |
---|
626 | |
---|
627 | entries.add(new Location(0, 310)); |
---|
628 | entries.add(new Location(0, 0)); |
---|
629 | entries.add(new Location(2, 310)); |
---|
630 | entries.add(new Location(2, 0)); |
---|
631 | |
---|
632 | exits.add(new Location(0, 310)); |
---|
633 | exits.add(new Location(0, 0)); |
---|
634 | exits.add(new Location(2, 310)); |
---|
635 | exits.add(new Location(2, 0)); |
---|
636 | |
---|
637 | s = new Scenario(mapFileName, entries, exits, driverTypes, allRoutes, |
---|
638 | name); |
---|
639 | EntryScenario es; |
---|
640 | EntryExitScenario ees; |
---|
641 | int i, j; |
---|
642 | Route r; |
---|
643 | for (i = 0; i < entries.size(); i++) { |
---|
644 | es = new EntryScenario(entries.get(i)); |
---|
645 | es.flow = 400; |
---|
646 | es.percentsDriverTypes.add(new Integer(33)); |
---|
647 | es.percentsDriverTypes.add(new Integer(33)); |
---|
648 | es.percentsDriverTypes.add(new Integer(34)); |
---|
649 | Point p1, p2; |
---|
650 | for (j = 0; j < exits.size(); j++) { |
---|
651 | if (i == j) { |
---|
652 | continue; |
---|
653 | } else { |
---|
654 | p1 = Globals.map.roads.get(entries.get(i).roadIdx).points |
---|
655 | .get(entries.get(i).ptIdx); |
---|
656 | p2 = Globals.map.roads.get(exits.get(j).roadIdx).points |
---|
657 | .get(exits.get(j).ptIdx); |
---|
658 | float y1, y2, x1, x2; |
---|
659 | y2 = (float) GPSutil.getMetersLatitude( |
---|
660 | Globals.map.minPoint, p2); |
---|
661 | x2 = (float) GPSutil.getMetersLongitude( |
---|
662 | Globals.map.minPoint, p2); |
---|
663 | y1 = (float) GPSutil.getMetersLatitude( |
---|
664 | Globals.map.minPoint, p1); |
---|
665 | x1 = (float) GPSutil.getMetersLongitude( |
---|
666 | Globals.map.minPoint, p1); |
---|
667 | double d = Math |
---|
668 | .sqrt((double) ((x2 - x1) * (x2 - x1) + (y2 - y1) |
---|
669 | * (y2 - y1))); |
---|
670 | |
---|
671 | if (d > 1000.0) { |
---|
672 | System.out.println("Pt entry " + i + " si exit " + j); |
---|
673 | ees = new EntryExitScenario(entries.get(i), exits |
---|
674 | .get(j)); |
---|
675 | ees.percentsEachRoute.add(new Integer(100)); |
---|
676 | r = new Route(); |
---|
677 | r.entry = entries.get(i); |
---|
678 | r.exit = exits.get(j); |
---|
679 | r.route = new ArrayList<RouteSegment>(); |
---|
680 | r = rc.bestRoute(r.entry, r.exit); |
---|
681 | if (r == null) { |
---|
682 | r = new Route(); |
---|
683 | r.entry = entries.get(i); |
---|
684 | r.exit = exits.get(j); |
---|
685 | r.route = new ArrayList<RouteSegment>(); |
---|
686 | System.out.println("No route for " + r.entry |
---|
687 | + " to " + r.exit); |
---|
688 | } |
---|
689 | |
---|
690 | ees.routes.add(r); |
---|
691 | s.allRoutes.add(r); |
---|
692 | es.entryExits.add(ees); |
---|
693 | } |
---|
694 | } |
---|
695 | } |
---|
696 | for (j = 0; j < es.entryExits.size(); j++) { |
---|
697 | es.entryExits.get(j).percentOfFlow = (int) (100 / es.entryExits |
---|
698 | .size()); |
---|
699 | } |
---|
700 | s.entryScenarios.add(es); |
---|
701 | } |
---|
702 | try { |
---|
703 | ObjectOutputStream outStr = new ObjectOutputStream( |
---|
704 | new FileOutputStream(System.getProperty("user.home") + File.separatorChar |
---|
705 | + "maps" + File.separatorChar + "fsc" + File.separatorChar |
---|
706 | + "HighwaySmall.fsc")); |
---|
707 | outStr.writeObject(s); |
---|
708 | } catch (Exception e) { |
---|
709 | e.printStackTrace(); |
---|
710 | } |
---|
711 | |
---|
712 | ScenarioMap unconfigured = new ScenarioMap(s); |
---|
713 | unconfigured.imageFileName = new String("HighwaySmallImg.jpg"); |
---|
714 | try { |
---|
715 | ObjectOutputStream outStr = new ObjectOutputStream( |
---|
716 | new FileOutputStream(System.getProperty("user.home") + File.separatorChar + "maps" |
---|
717 | + File.separatorChar + "smf" + File.separatorChar + "HighwaySmall.smf")); |
---|
718 | outStr.writeObject(unconfigured); |
---|
719 | } catch (Exception e) { |
---|
720 | e.printStackTrace(); |
---|
721 | } |
---|
722 | System.out.println("GATA"); |
---|
723 | |
---|
724 | } |
---|
725 | |
---|
726 | /** |
---|
727 | * Crates a Map object from "name.RT1" and "name.RT2" files and splits the |
---|
728 | * map. It also displays the map if "withRun" is true. |
---|
729 | */ |
---|
730 | public static void splitMap() |
---|
731 | { |
---|
732 | RoadAreaUtils mapSpliter = new RoadAreaUtils(); |
---|
733 | int i; |
---|
734 | if(Globals.map==null) |
---|
735 | System.out.println("NULLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL"); |
---|
736 | Globals.map.getMinAndMaxPoint(); |
---|
737 | mapSpliter.splitMap(); |
---|
738 | mapSpliter.selectDistinctRoadAreas(); |
---|
739 | mapSpliter.mergeAreas(); |
---|
740 | Random gen = new Random(System.currentTimeMillis()); |
---|
741 | for (i = 0; i < mapSpliter.distinctAreas.size(); i++) |
---|
742 | { |
---|
743 | mapSpliter.distinctAreas.get(i).setColor(gen.nextFloat(),gen.nextFloat(),gen.nextFloat()); |
---|
744 | } |
---|
745 | for (i = 0; i < mapSpliter.mainRoads.size(); i++) |
---|
746 | { |
---|
747 | System.out.println("Road: " |
---|
748 | + Globals.map.roads.get( |
---|
749 | mapSpliter.mainRoads.get(i).intValue()).getName()); |
---|
750 | } |
---|
751 | |
---|
752 | Globals.map.mapSpliter = mapSpliter; |
---|
753 | } |
---|
754 | |
---|
755 | public static void createMap(String name, boolean withRun, |
---|
756 | boolean withFileSave, int type) throws Exception |
---|
757 | { |
---|
758 | |
---|
759 | String file2 = null; |
---|
760 | if (name.endsWith(".rt1")) |
---|
761 | { |
---|
762 | file2 = name.substring(0, name.length() - 4) + ".rt2"; |
---|
763 | } |
---|
764 | else if (name.endsWith(".RT1")) |
---|
765 | { |
---|
766 | file2 = name.substring(0, name.length() - 4) + ".RT2"; |
---|
767 | } |
---|
768 | else { |
---|
769 | logger.severe("ERROR: file not rt1 or RT1:" + name); |
---|
770 | throw new Exception("ERROR: file not rt1 or RT1:" + name); |
---|
771 | |
---|
772 | } |
---|
773 | |
---|
774 | Globals.map = new Map(System.getProperty("user.home") + File.separatorChar + "maps" + |
---|
775 | File.separatorChar + "rt" + File.separatorChar + name, |
---|
776 | System.getProperty("user.home") + File.separatorChar + "maps" + |
---|
777 | File.separatorChar + "rt" + File.separatorChar + file2); |
---|
778 | |
---|
779 | splitMap(); |
---|
780 | |
---|
781 | |
---|
782 | // RoadAreaUtils mapSpliter = new RoadAreaUtils(); |
---|
783 | // |
---|
784 | // int i; |
---|
785 | // Globals.map.getMinAndMaxPoint(); |
---|
786 | // mapSpliter.splitMap(); |
---|
787 | // mapSpliter.selectDistinctRoadAreas(); |
---|
788 | // mapSpliter.mergeAreas(); |
---|
789 | // Random gen = new Random(System.currentTimeMillis()); |
---|
790 | // for (i = 0; i < mapSpliter.distinctAreas.size(); i++) |
---|
791 | // { |
---|
792 | // mapSpliter.distinctAreas.get(i).setColor(gen.nextFloat(), |
---|
793 | // gen.nextFloat(), gen.nextFloat()); |
---|
794 | // } |
---|
795 | // for (i = 0; i < mapSpliter.mainRoads.size(); i++) |
---|
796 | // { |
---|
797 | // System.out.println("Road: " |
---|
798 | // + Globals.map.roads.get( |
---|
799 | // mapSpliter.mainRoads.get(i).intValue()).getName()); |
---|
800 | // } |
---|
801 | // |
---|
802 | // Globals.map.mapSpliter = mapSpliter; |
---|
803 | |
---|
804 | |
---|
805 | String dir = System.getProperty("user.home")+File.separatorChar+"maps"+File.separatorChar+"map"; |
---|
806 | File dirF = new File(dir); |
---|
807 | if (!dirF.exists()) |
---|
808 | dirF.mkdirs(); |
---|
809 | |
---|
810 | |
---|
811 | else if (!dirF.isDirectory()) |
---|
812 | { |
---|
813 | dirF.renameTo(new File(dir+"~")); |
---|
814 | dirF = new File(dir); |
---|
815 | dirF.delete(); |
---|
816 | dirF.mkdirs(); |
---|
817 | } |
---|
818 | |
---|
819 | File outSave = new File(dir + File.separatorChar + name.substring(0, name.length() - 4) + ".map"); |
---|
820 | |
---|
821 | |
---|
822 | try |
---|
823 | { |
---|
824 | MapLoader.loadExtraInfo(name.substring(0, name.length() - 4) + ".rtx.txt"); |
---|
825 | } |
---|
826 | catch (Exception ex) |
---|
827 | { |
---|
828 | System.out.println("Extra info not loaded: check file! "); |
---|
829 | } |
---|
830 | |
---|
831 | Globals.map.createTrafficLights(new Random()); |
---|
832 | // DelayRecord.initDelays(); |
---|
833 | |
---|
834 | if (withFileSave) |
---|
835 | { |
---|
836 | logger.info("Starting file writting.."); |
---|
837 | try |
---|
838 | { |
---|
839 | ObjectOutputStream ois = new ObjectOutputStream( |
---|
840 | new FileOutputStream(outSave)); |
---|
841 | |
---|
842 | ois.writeObject(Globals.map); |
---|
843 | |
---|
844 | } |
---|
845 | catch (Exception e) |
---|
846 | { |
---|
847 | e.printStackTrace(); |
---|
848 | } |
---|
849 | logger.info("Writting to file ended!"); |
---|
850 | } |
---|
851 | |
---|
852 | if (withRun) // like vnsim.vehicular.scenarios.Main.startGui() |
---|
853 | { |
---|
854 | |
---|
855 | Globals.engine = new Engine(true, true); |
---|
856 | Engine.simulationType=type; |
---|
857 | Globals.engine.init(); |
---|
858 | |
---|
859 | Globals.demo = new Display(); |
---|
860 | Globals.demo.setVisible(true); |
---|
861 | |
---|
862 | } |
---|
863 | } |
---|
864 | } |
---|