/************************************************************************************ * Copyright (C) 2008 by Politehnica University of Bucharest and Rutgers University * All rights reserved. * Refer to LICENSE for terms and conditions of use. ***********************************************************************************/ package vnsim.vehicular.generator; import java.util.*; import java.io.*; import vnsim.applications.adaptiveTL.WirelessTrafficLight; import vnsim.core.*; import vnsim.map.object.*; import vnsim.vehicular.routePlan.RouteComputingUtils; import vnsim.vehicular.scenarios.*; import vnsim.vehicular.simulator.*; import vnsim.vehicular.simulator.intersections.*; import vnsim.gui.*; import java.util.logging.*; public class Mobility { /** Logger used by this class */ private static final transient Logger logger = Logger.getLogger("generator.Mobility"); /* public static void main(String args[]) throws Exception { File f=new File("map.dat"); if(!f.exists()) { System.out.println("NOT EXIST!"); // Globals.map = new trafficview.object.Map("Poli.RT1","Poli.RT2"); Globals.map = new trafficview.object.Map("Test.RT1","Test.RT2"); //Globals.map = new trafficview.object.Map("TGR34023.RT1","TGR34023.RT2"); ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("map.dat")); oos.writeObject(Globals.map); } else { System.out.println("EXIST!"); ObjectInputStream ois=new ObjectInputStream(new FileInputStream("map.dat")); Globals.map=(trafficview.object.Map)ois.readObject(); } //System.out.println("OK!:"+Globals.map.roads); // for(int i=0;i (); for(int k=0;kj) NCARS--; //it will be moved again when that road will be processed Globals.map.roads.get(car.getRoadIdx()).carsOnThisRoad.addCarInstance(car); } else { //it's still on the same road newVector.addCarInstance(Globals.map.roads.get(j).carsOnThisRoad.get(k)); } NCARS++; } //loop through the new vector and set the indices for(int k=0;k route=RoutingModule.getUnmergedDijkstraPath(new RouteSegment((short)roadIndex, (short)newPointIndex, (short)newPointIndex), // new RouteSegment((short)newRoadDest,(short)newPointDest,(short)newPointDest)); // System.out.println("ROUTE IS:"+route); // newC.setRoute(route); // newC.startOnRoad(); // //r.carsOnThisRoad.setElementAt(i, newC); // r.carsOnThisRoad.remove(newC.ID); // Globals.map.roads.get(newC.getRoadIdx()).carsOnThisRoad.addCarInstance(newC); // break; // } // } // } * * */ public static void initCars(Engine e) { // createNewCar(0,80,1,2,0.0, new AggresivePersonality(), 0.0, 1); // createNewCar(0,30,1,1,0.0, new NotMovingPersonality(),0.0, 2); // createNewCar(0,90,1,1,0.0, new AggresivePersonality(),0.0, 3); // createNewCar(0,70,1,1,0.0, new CalmPersonality(),0.0, 4); // createNewCar(0,70,1,2,0.0, new CalmPersonality(),0.0, 5); // createNewCar(0,90,1,2,0.0, new AggresivePersonality(),0.0, 6); // createNewCar(0,80,1,1,0.0, new AggresivePersonality(),0.0, 7); // createNewCar(0,75,1,2,0.0, new CalmPersonality(),0.0, 8); e.addCar(createNewCar(0,20,1,1,0.0, new AggresivePersonality(), 0.0, 1, 1, 235)); e.addCar(createNewCar(0,20,1,2,0.0, new CalmPersonality(), 0.0, 2, 1, 235)); e.addCar(createNewCar(0,20,1,3,0.0, new CalmPersonality(), 0.0, 3, 2, 20)); // e.addCar(createNewCar(0,90,1,2,0.0, new AggresivePersonality(), 0.0, 4, 0, 10)); // e.addCar(createNewCar(0,80,1,1,0.0, new CalmPersonality(), 0.0, 5, 0, 10)); // e.addCar(createNewCar(0,70,1,2,0.0, new CalmPersonality(), 0.0, 6, 0, 10)); // e.addCar(createNewCar(0,80,1,2,0.0, new AggresivePersonality(), 0.0, 7, 0, 10)); // // e.addCar(createNewCar(5,30,1,1,0.0, new AggresivePersonality(), 0.0, 8, 0, 10)); // e.addCar(createNewCar(5,30,1,2,0.0, new CalmPersonality(), 0.0, 9, 0, 10)); // // for(int i=0;i<30;i++) { // if(i%2==0) // e.addCar(createNewCar(3, i*3, 0, 1, 0.0, new AggresivePersonality(), 0.0, i+10, 4, 50)); // else // e.addCar(createNewCar(3, i*3, 0, 1, 0.0, new CalmPersonality(), 0.0, i+10, 4, 50)); // } // // for(int i=0;i<30;i++) { // if(i%2==0) // e.addCar(createNewCar(4, i*3, 0, 1, 0.0, new AggresivePersonality(), 0.0, i+40, 10, 50)); // else // e.addCar(createNewCar(4, i*3, 0, 1, 0.0, new CalmPersonality(), 0.0, i+40, 10, 50)); // } } public static void initCarsAutostrada(Engine e) { Random generator = new Random(System.currentTimeMillis()); for(int i=0;i<1500;i++) { if(generator.nextBoolean()) { e.addCar(createNewCar(0, i*6+1, 0, 1, 0.0, new AggresivePersonality(), 0.0, i, 0, 9950)); } else { e.addCar(createNewCar(0, i*6+1, 0, 1, 0.0, new CalmPersonality(), 0.0, i, 0, 9950)); } } } static Random generator = new Random(System.currentTimeMillis()); public static void initCarsTest(Engine e) { for(int i=0;i route=RoutingUtils.getUnmergedDijkstraPath(new Location((short)roadIndex, (short)pointIndex), new Location((short)roadDest,(short)pointDest)); System.out.println("ROUTE FOR ID="+ID+"IS:"+route); car.setRoute(route); car.startOnRoad(); Globals.map.roads.get(car.getRoadIdx()).carsOnThisRoad.addCarInstance(car); System.out.println("new car added 2 "+car.ID); return car; } public static CarInstance createNewCar(int roadIndex, int pointIndex, int direction, int lane, double offset, Personality p, double speed, int ID, int roadDest, int pointDest, ArrayList route) { //is there a cross exactly at (roadIndex-pointIndex)?; Road r=Globals.map.roads.get(roadIndex); for(int i=0;i route, int sp) { System.out.println("Start creating new car self routed"); // is there a cross exactly at (roadIndex-pointIndex)?; // is there a cross exactly at (roadIndex-pointIndex)?; Road r = Globals.map.roads.get(roadIndex); for (int i = 0; i < r.crosses.size(); i++) { if (r.crosses.get(i).getPointIndex() == pointIndex){ System.out.println("cros"); return null; } } CarInstance car = new CarInstance((short) roadIndex, (short) pointIndex, p); System.out.println("Been in CarInstance"); car.queryTime=(int)Math.abs((double)generator.nextInt(3000)); car.queryTime=(int)car.queryTime/((Globals.executionFPS / Globals.FPS)); car.queryTime++; car.queryTime=car.queryTime*(Globals.executionFPS / Globals.FPS); car.setDirection((byte) direction); // reverse car.setLane((byte) lane); car.setOffset(offset); car.setSpeed(speed); car.ID = ID; car.index = Globals.map.roads.get(roadIndex).carsOnThisRoad.size(); car.startTime = (Globals.engine.crtTime * 1000) / Globals.engine.fps; Route myRoute = new Route(); myRoute.entry = new Location(roadIndex, pointIndex); myRoute.exit = new Location(roadDest, pointDest); System.out.println("Been in Route and Location"); if (sp == 0) { System.out.println("sp=0"); car.routingType = 0; ArrayList rs = null; rs = RoutingUtils.getMergedDijkstraPath(new Location(roadIndex, pointIndex), new Location(roadDest, pointDest)); if (rs == null) { System.out.println("fail cu shortPath la "); return null; //carReceivingMsg.setRoute(RoutingUtils.splitRoute(route)); } else { int percent = generator.nextInt(100); percent = (int) Math.abs((double) percent); if (percent <= Globals.routePlanConstants.PERCENT_DYNAMIC_ROUTE) { percent = 1;// Shortest Path with dynamic search //System.out.println("Gen carReceivingMsg 1 cu id"+carReceivingMsg.ID); } else { percent = 0; // SortestPath without dynamic serch } car.routingType = percent; System.out.println("Before splitRoute(rs)"); myRoute.route = RoutingUtils.splitRoute(rs); System.out.println("Preparing to set route"); car.setRoute(myRoute.route); // System.out.println("Creez cu shortPath la "+carReceivingMsg.ID); } } else { System.out.println("sp!=0"); car.routingType = 1; Route allRutes = null; //Route bestR = null; RouteComputingUtils rc = new RouteComputingUtils(); // ////////////// System.out.println("Before allRoutes"); allRutes = rc.bestRoute(new Location(roadIndex, pointIndex), new Location(roadDest, pointDest)); System.out.println("Calculated allRoutes"); if (allRutes != null) { int percent = generator.nextInt(100); percent = (int) Math.abs((double) percent); if (percent <= Globals.routePlanConstants.PERCENT_DYNAMIC_ROUTE) { percent = 3;// Best Path with dynamic search //System.out.println("Gen carReceivingMsg 3 cu id"+carReceivingMsg.ID); } else { percent = 2; // BestPath without dynamic serch } car.routingType = percent; myRoute.route = RoutingUtils.splitRoute(allRutes.route); car.setRoute(myRoute.route); // System.out.println("Creez cu routingType la "+carReceivingMsg.ID); } else { //System.out.println("fail cu bestPath la "+carReceivingMsg.ID); return null; //carReceivingMsg.setRoute(RoutingUtils.splitRoute(route)); // System.out.println("fail la best la "+carReceivingMsg.ID); } } System.out.println("new car added"); car.startOnRoad(); Globals.map.roads.get(car.getRoadIdx()).carsOnThisRoad .addCarInstance(car); System.out.println("new car added "+car.ID); return car; } public static CarInstance createNewInvisibleCar(int roadIndex, int pointIndex, int direction, int lane, double offset, Personality p, double speed, int ID, int roadDest, int pointDest, RouteSegment routeStart, int sp) { // is there a cross exactly at (roadIndex-pointIndex)?; //Road r = Globals.map.roads.get(roadIndex); CarInstance car = new CarInstance((short) roadIndex, (short) pointIndex, p); car.queryTime = (int) Math.abs((double) generator.nextInt(3000)); car.setDirection((byte) direction); // reverse car.setLane((byte) lane); car.setOffset(offset); car.setSpeed(speed); car.ID = ID; car.index = -1; car.startTime = (Globals.engine.crtTime * 1000) / Globals.engine.fps; car.source = new Location(roadIndex, pointIndex); car.destination = new Location(roadDest, pointDest); car.route = new RouteSegment[1]; car.route[0] = routeStart; //carReceivingMsg.startOnRoad(); car.routingType=5; System.out.println("new car added invisible "+car.ID); return car; } public static void printRoad(int i) { System.out.println("*************"); System.out.println("Here's ROAD "+i+": "); ((Road)(Globals.map.roads.get(i))).printRoad(false); Road r=(Road) Globals.map.roads.get(i); Point last=(Point)r.points.get(r.points.size()-1); System.out.println("Length:"+last.getDistance()+"; numPoints="+r.points.size()); System.out.println("Crosses:"); for(int j=0;j entryScenarios; public static ArrayList entryFlowVariations = null; public static SortedEventsVector events = null; /*public static void loadScenario(Scenario sc) throws Exception { try { ObjectInputStream ois=new ObjectInputStream(new FileInputStream(sc.mapFileName)); Globals.map=(vnsim.map.object.Map)ois.readObject(); ois.close(); } catch (Exception ex) { // ex.printStackTrace(); throw ex; } Globals.scenarioName = sc.name; //sc.theMap; entryScenarios=sc.entryScenarios; events = new SortedEventsVector(); for(int i=0;i (); if(Globals.variationsFile==null) return; try { String line=null; String full=""; RandomAccessFile f=new RandomAccessFile("maps//variations//" + Globals.variationsFile,"r"); while( (line=f.readLine())!=null ) { full=full+line; } String delim="\n \t\r"; for(char c='a';c<='z';c++) delim=delim+c; for(char c='A';c<='Z';c++) delim=delim+c; StringTokenizer st=new StringTokenizer(full,delim); int nEntries=Integer.parseInt(st.nextToken()); if(nEntries!=entryScenarios.size()) { System.err.println("WARNING! NENTRIES="+nEntries+" DIFFERENT FROM ENTRYSCENARIOS="+entryScenarios.size()); } for(int i=0;i (); if(Globals.variationsFile == null) { logger.warning("No variation file specified"); return; } try { String line=null; String full=""; BufferedReader f = null; File fb = new File(System.getProperty("user.home")+File.separatorChar+"maps"+File.separatorChar+"variations"+File.separatorChar+Globals.variationsFile); if (fb.exists() && fb.isFile() && fb.canRead()) f = new BufferedReader(new InputStreamReader(new FileInputStream(fb))); else f = new BufferedReader(new InputStreamReader(Utils.getInstance().openStream("maps/variations/"+Globals.variationsFile))); while( (line=f.readLine())!=null ) { full=full+line; } String delim="\n \t\r"; for(char c='a';c<='z';c++) delim=delim+c; for(char c='A';c<='Z';c++) delim=delim+c; StringTokenizer st=new StringTokenizer(full,delim); int nEntries=Integer.parseInt(st.nextToken()); if(nEntries!=entryScenarios.size()) { logger.severe("WARNING! NENTRIES="+nEntries+" DIFFERENT FROM ENTRYSCENARIOS="+entryScenarios.size()); } for(int i=0;i route) { // // is there a cross exactly at (roadIndex-pointIndex)?; Road r = Globals.map.roads.get(roadIndex); for (int i = 0; i < r.crosses.size(); i++) { if (r.crosses.get(i).getPointIndex() == pointIndex) { return null; } } CarInstance car = new CarInstance((short) roadIndex, (short) pointIndex, p); car.queryTime = (int) Math.abs((double) generator.nextInt(100)); car.setDirection((byte) direction); // reverse car.setLane((byte) lane); car.setOffset(offset); car.setSpeed(speed); car.ID = ID; car.index = Globals.map.roads.get(roadIndex).carsOnThisRoad.size(); car.startTime = (Globals.engine.crtTime * 1000) / Globals.engine.fps; car.source = new Location(roadIndex, pointIndex); car.destination = new Location(roadDest, pointDest); car.routingType = 5; ArrayList rs = null; rs = RoutingUtils.getMergedDijkstraPath(new Location(roadIndex, pointIndex), new Location(roadDest, pointDest)); if (rs == null) { return null; } else { ArrayList routeTp = RoutingUtils.splitRoute(rs); ArrayList firstSegment = new ArrayList(); firstSegment.add(routeTp.get(0)); car.setRoute(firstSegment); } car.startOnRoad(); Globals.map.roads.get(car.getRoadIdx()).carsOnThisRoad .addCarInstance(car); System.out.println("new car added 3 "+car.ID); return car; } public static CarInstance createNewCityCarCommunicatingWithInfrastucture( int roadIndex, int pointIndex, int direction, int lane, double offset, Personality p, double speed, int ID, int roadDest, int pointDest, ArrayList route) { // // is there a cross exactly at (roadIndex-pointIndex)?; Road r = Globals.map.roads.get(roadIndex); for (int i = 0; i < r.crosses.size(); i++) { if (r.crosses.get(i).getPointIndex() == pointIndex) { return null; } } CarInstance car = new CarInstance((short) roadIndex, (short) pointIndex, p); car.queryTime = (int) Math.abs((double) generator.nextInt(100)); car.setDirection((byte) direction); // reverse car.setLane((byte) lane); car.setOffset(offset); car.setSpeed(speed); car.ID = ID; car.index = Globals.map.roads.get(roadIndex).carsOnThisRoad.size(); car.startTime = (Globals.engine.crtTime * 1000) / Globals.engine.fps; car.source = new Location(roadIndex, pointIndex); car.destination = new Location(roadDest, pointDest); car.routingType = 6; car.startMonitor = true; car.startTime = Globals.engine.crtTime; ArrayList rs = null; //todo: calculate the route based on road congestions rs = RoutingUtils.getMergedDijkstraPath(new Location(roadIndex, pointIndex), new Location(roadDest, pointDest)); if (rs == null) { return null; } ArrayList routeTp = RoutingUtils.splitRoute(rs); car.setRoute(routeTp); car.startOnRoad(); Globals.map.roads.get(car.getRoadIdx()).carsOnThisRoad .addCarInstance(car); System.out.println("new car added 4"+car.ID); return car; } public static CarInstance createNewEZCabClient( int roadIndex, int pointIndex, int direction, int lane, double offset, Personality p, double speed, int ID, int roadDest, int pointDest, ArrayList route) { Road r = Globals.map.roads.get(roadIndex); for (int i = 0; i < r.crosses.size(); i++) { if (r.crosses.get(i).getPointIndex() == pointIndex) { return null; } } CarInstance car = new CarInstance((short) roadIndex, (short) pointIndex, p); car.queryTime = (int) Math.abs((double) generator.nextInt(100)); car.setDirection((byte) direction); // reverse car.setLane((byte) lane); car.setOffset(offset); car.setSpeed(speed); car.ID = ID; car.index = Globals.map.roads.get(roadIndex).carsOnThisRoad.size(); car.startTime = (Globals.engine.crtTime * 1000) / Globals.engine.fps; car.source = new Location(roadIndex, pointIndex); car.destination = new Location(roadDest, pointDest); car.routingType = 6; car.startMonitor = true; car.startTime = Globals.engine.crtTime; ArrayList rs = null; //todo: calculate the route based on road congestions rs = RoutingUtils.getMergedDijkstraPath(new Location(roadIndex, pointIndex), new Location(roadDest, pointDest)); if (rs == null) { return null; } ArrayList routeTp = RoutingUtils.splitRoute(rs); car.setRoute(routeTp); car.startOnRoad(); Globals.map.roads.get(car.getRoadIdx()).carsOnThisRoad .addCarInstance(car); System.out.println("Create new EzCab client"+car.ID); return car; } }