private void createDemand() { Population pop = sc.getPopulation(); PopulationFactory fact = pop.getFactory(); for (int i = 1; i <= 400; i++) { Person p = fact.createPerson(Id.createPersonId(i)); Plan plan = fact.createPlan(); p.addPlan(plan); Leg leg = fact.createLeg(TransportMode.car); Activity home; Activity work; if (i % 2 == 0) { // o --d1 home = fact.createActivityFromCoord("o1", lo.getCoord()); home.setEndTime(7 * 3600 + i); work = fact.createActivityFromCoord("d1", ld1.getCoord()); } else /*if(i%2==0)*/ { // o --d2 home = fact.createActivityFromCoord("o1", lo.getCoord()); home.setEndTime(7 * 3600 + i); work = fact.createActivityFromCoord("d2", ld2.getCoord()); } plan.addActivity(home); plan.addLeg(leg); plan.addActivity(work); pop.addPerson(p); } new PopulationWriter(pop).write(outputDir + "/input/input_plans.xml.gz"); }
public void testArriveAtStop() { ScenarioImpl scenario = (ScenarioImpl) ScenarioUtils.createScenario(ConfigUtils.createConfig()); NetworkImpl network = (NetworkImpl) scenario.getNetwork(); Node node1 = network.createAndAddNode(Id.create("1", Node.class), new CoordImpl(0, 0)); Node node2 = network.createAndAddNode(Id.create("2", Node.class), new CoordImpl(1000, 0)); Node node3 = network.createAndAddNode(Id.create("3", Node.class), new CoordImpl(2000, 0)); network.createAndAddLink(Id.create("1", Link.class), node1, node2, 1000.0, 10.0, 3600.0, 1); network.createAndAddLink(Id.create("2", Link.class), node2, node3, 1000.0, 10.0, 3600.0, 1); TransitScheduleFactory builder = new TransitScheduleFactoryImpl(); PopulationFactory pb = scenario.getPopulation().getFactory(); Person person = pb.createPerson(Id.create("1", Person.class)); Plan plan = pb.createPlan(); person.addPlan(plan); Activity homeAct = pb.createActivityFromLinkId("home", Id.create("1", Link.class)); Leg leg = pb.createLeg(TransportMode.pt); TransitStopFacility stop1 = builder.createTransitStopFacility( Id.create("1", TransitStopFacility.class), scenario.createCoord(100, 100), false); TransitStopFacility stop2 = builder.createTransitStopFacility( Id.create("2", TransitStopFacility.class), scenario.createCoord(900, 100), false); TransitStopFacility stop3 = builder.createTransitStopFacility( Id.create("3", TransitStopFacility.class), scenario.createCoord(1900, 100), false); TransitLine line1 = builder.createTransitLine(Id.create("L1", TransitLine.class)); leg.setRoute(new ExperimentalTransitRoute(stop1, line1, null, stop2)); Activity workAct = pb.createActivityFromLinkId("work", Id.create("2", Link.class)); plan.addActivity(homeAct); plan.addLeg(leg); plan.addActivity(workAct); QSim sim = (QSim) new QSimFactory().createMobsim(scenario, EventsUtils.createEventsManager()); TransitAgent agent = TransitAgent.createTransitAgent(person, sim); sim.insertAgentIntoMobsim(agent); agent.endActivityAndComputeNextState(10); assertFalse(agent.getExitAtStop(stop1)); assertTrue(agent.getExitAtStop(stop2)); assertFalse(agent.getExitAtStop(stop3)); assertTrue(agent.getExitAtStop(stop2)); // offering the same stop again should yield "true" }
private static Person createPerson(Scenario scenario, Coord home, Coord work, int i) { Random r = MatsimRandom.getRandom(); PopulationFactory f = scenario.getPopulation().getFactory(); Person p = f.createPerson(Id.createPersonId(i)); Plan plan = f.createPlan(); p.addPlan(plan); Activity h1 = f.createActivityFromCoord("home", home); h1.setEndTime(6 * 3600 + r.nextInt(4 * 3600)); Leg leg = f.createLeg("taxibus"); Activity w = f.createActivityFromCoord("work", work); w.setEndTime(12 * 3600 + r.nextInt(6 * 3600)); Leg leg2 = f.createLeg("taxibus"); Activity h2 = f.createActivityFromCoord("home", home); plan.addActivity(h1); plan.addLeg(leg); plan.addActivity(w); plan.addLeg(leg2); plan.addActivity(h2); return p; }
private static void createPopulation(Scenario sc, int destination) { Population pop = sc.getPopulation(); pop.getPersons().clear(); PopulationFactory fac = pop.getFactory(); double t = 0; for (int i = 0; i < NR_AGENTS / 2; i++) { Person pers = fac.createPerson(Id.create("b" + i, Person.class)); Plan plan = fac.createPlan(); pers.addPlan(plan); Activity act0; act0 = fac.createActivityFromLinkId("origin", Id.create("0", Link.class)); act0.setEndTime(t); plan.addActivity(act0); Leg leg = fac.createLeg("walkct"); plan.addLeg(leg); Activity act1 = fac.createActivityFromLinkId("destination", Id.create(destination, Link.class)); plan.addActivity(act1); pop.addPerson(pers); } // for (int i = NR_AGENTS / 2; i < NR_AGENTS; i++) { // Person pers = fac.createPerson(Id.create("b" + i, Person.class)); // Plan plan = fac.createPlan(); // pers.addPlan(plan); // Activity act0; // act0 = fac.createActivityFromLinkId("origin", // Id.create(destination + 1, Link.class)); // act0.setEndTime(t); // plan.addActivity(act0); // Leg leg = fac.createLeg("walkct"); // plan.addLeg(leg); // Activity act1 = fac.createActivityFromLinkId("destination", // Id.create(1, Link.class)); // plan.addActivity(act1); // pop.addPerson(pers); // } }
public static void main(String[] args) { /* * We enter coordinates in the WGS84 reference system, but we want them to appear in the population file * projected to UTM33N, because we also generated the network in UTM33N. */ CoordinateTransformation ct = TransformationFactory.getCoordinateTransformation( TransformationFactory.WGS84, TransformationFactory.WGS84_UTM33N); /* * First, create a new Config and a new Scenario. */ Config config = ConfigUtils.createConfig(); Scenario sc = ScenarioUtils.createScenario(config); /* * Pick the Network and the Population out of the Scenario for convenience. */ Network network = sc.getNetwork(); Population population = sc.getPopulation(); /* * Pick the PopulationFactory out of the Population for convenience. * It contains methods to create new Population items. */ PopulationFactory populationFactory = population.getFactory(); /* * Create a Person designated "1" and add it to the Population. */ long key = 1; for (long i = 1; i <= 5000; i++) { key = i; Person person = populationFactory.createPerson(Id.createPersonId(key)); population.addPerson(person); /* * Create a Plan for the Person */ Plan plan = populationFactory.createPlan(); /* * Create a "home" Activity for the Person. In order to have the Person end its day at the same location, * we keep the home coordinates for later use (see below). * Note that we use the CoordinateTransformation created above. */ Coord homeCoordinates = new Coord(686661.13571, 4827510.51845); Activity activity1 = populationFactory.createActivityFromCoord("home", homeCoordinates); activity1.setEndTime( 21600 + i * 0.72); // leave at 6 o'clock, one vehicle entering after other in a short while // so that there is no peak at one second // activity1.setEndTime(21600); plan.addActivity(activity1); // add the Activity to the Plan /* * Create a Leg. A Leg initially hasn't got many attributes. It just says that a car will be used. */ plan.addLeg(populationFactory.createLeg("car")); /* * Create a "work" Activity, at a different location. */ Activity activity2 = populationFactory.createActivityFromCoord("work", new Coord(689426.65361, 4826700.65288)); activity2.setEndTime(57600); // leave at 4 p.m. plan.addActivity(activity2); System.out.println("Last Departure Time: " + claculateTime(activity1.getEndTime())); /* * Create another car Leg. */ person.addPlan(plan); } for (long i = 1; i <= 5000; i++) { key = i + 5000; Person person = populationFactory.createPerson(Id.createPersonId(key)); population.addPerson(person); Plan plan = populationFactory.createPlan(); Coord homeCoordinates = new Coord(686661.13571, 4826063.88649); Activity activity1 = populationFactory.createActivityFromCoord("home", homeCoordinates); activity1.setEndTime(21600 + i * 1); // leave at 6 o'clock // activity1.setEndTime(21600); plan.addActivity(activity1); // add the Activity to the Plan plan.addLeg(populationFactory.createLeg("car")); Activity activity2 = populationFactory.createActivityFromCoord("work", new Coord(689426.65361, 4826700.65288)); activity2.setEndTime(57600); // leave at 4 p.m. plan.addActivity(activity2); System.out.println("Last Departure Time: " + claculateTime(activity1.getEndTime())); person.addPlan(plan); } /* * Write the population (of 1 Person) to a file. */ MatsimWriter popWriter = new org.matsim.api.core.v01.population.PopulationWriter(population, network); popWriter.write("H:\\Mike Work\\input\\population.xml"); }
/** * Tests that plans with missing routes are completed (=routed) before the mobsim starts. * * @author mrieser */ @Test public void testCalcMissingRoutes() { Config config = this.utils.loadConfig(null); Fixture f = new Fixture(config); /* Create a person with two plans, driving from link 1 to link 3, starting at 7am. */ Population population = f.scenario.getPopulation(); PopulationFactory factory = population.getFactory(); Person person1 = null; Leg leg1 = null; Leg leg2 = null; person1 = factory.createPerson(Id.create("1", Person.class)); // --- plan 1 --- Plan plan1 = factory.createPlan(); person1.addPlan(plan1); Activity a1 = factory.createActivityFromLinkId("h", f.link1.getId()); a1.setEndTime(7.0 * 3600); plan1.addActivity(a1); leg1 = factory.createLeg(TransportMode.car); plan1.addLeg(leg1); // DO NOT CREATE A ROUTE FOR THE LEG!!! plan1.addActivity(factory.createActivityFromLinkId("h", f.link3.getId())); // --- plan 2 --- Plan plan2 = factory.createPlan(); person1.addPlan(plan2); Activity a2 = factory.createActivityFromLinkId("h", f.link1.getId()); a2.setEndTime(7.0 * 3600); plan2.addActivity(a2); leg2 = factory.createLeg(TransportMode.car); plan2.addLeg(leg2); // DO NOT CREATE A ROUTE FOR THE LEG!!! plan2.addActivity(factory.createActivityFromLinkId("h", f.link3.getId())); population.addPerson(person1); // Complete the configuration for our test case // - set scoring parameters ActivityParams actParams = new ActivityParams("h"); actParams.setTypicalDuration(8 * 3600); actParams.setPriority(1.0); config.planCalcScore().addActivityParams(actParams); // - define iterations config.controler().setLastIteration(0); // - make sure we don't use threads, as they are not deterministic config.global().setNumberOfThreads(1); // Now run the simulation Controler controler = new Controler(f.scenario); controler.getConfig().controler().setCreateGraphs(false); controler.getConfig().controler().setWriteEventsInterval(0); controler.addOverridingModule( new AbstractModule() { @Override public void install() { bindMobsim() .toProvider( new Provider<Mobsim>() { @Override public Mobsim get() { return new FakeMobsim(); } }); } }); controler.setDumpDataAtEnd(false); controler.run(); /* if something goes wrong, there will be an exception we don't catch and the test fails, * otherwise, everything is fine. */ // check that BOTH plans have a route set, even when we only run 1 iteration where only one of // them is used. // assertNotNull(leg1.getRoute()); // assertNotNull(leg2.getRoute()); // but do not assume that the leg will be the same instance... for (Plan plan : new Plan[] {plan1, plan2}) { assertEquals( "unexpected plan length in " + plan.getPlanElements(), 3, plan.getPlanElements().size()); assertNotNull( "null route in plan " + plan.getPlanElements(), ((Leg) plan.getPlanElements().get(1)).getRoute()); } }
/** * Tests that the travel times are correctly calculated during the simulation. * * @author mrieser */ @Test public void testTravelTimeCalculation() { Config config = this.utils.loadConfig(null); Fixture f = new Fixture(config); /* Create 2 persons driving from link 1 to link 3, both starting at the * same time at 7am. */ Population population = f.scenario.getPopulation(); PopulationFactory factory = population.getFactory(); Person person1 = null; person1 = factory.createPerson(Id.create("1", Person.class)); Plan plan1 = factory.createPlan(); person1.addPlan(plan1); Activity a1 = factory.createActivityFromLinkId("h", f.link1.getId()); a1.setEndTime(7.0 * 3600); plan1.addActivity(a1); Leg leg1 = factory.createLeg(TransportMode.car); plan1.addLeg(leg1); NetworkRoute route1 = ((PopulationFactoryImpl) f.scenario.getPopulation().getFactory()) .createRoute(NetworkRoute.class, f.link1.getId(), f.link3.getId()); leg1.setRoute(route1); ArrayList<Id<Link>> linkIds = new ArrayList<Id<Link>>(); linkIds.add(f.link2.getId()); route1.setLinkIds(f.link1.getId(), linkIds, f.link3.getId()); plan1.addActivity(factory.createActivityFromLinkId("h", f.link3.getId())); population.addPerson(person1); Person person2 = factory.createPerson(Id.create("2", Person.class)); Plan plan2 = factory.createPlan(); person2.addPlan(plan2); Activity a2 = factory.createActivityFromLinkId("h", f.link1.getId()); a2.setEndTime(7.0 * 3600); plan2.addActivity(a2); Leg leg2 = factory.createLeg(TransportMode.car); plan2.addLeg(leg2); NetworkRoute route2 = ((PopulationFactoryImpl) f.scenario.getPopulation().getFactory()) .createRoute(NetworkRoute.class, f.link1.getId(), f.link3.getId()); leg2.setRoute(route2); route2.setLinkIds(f.link1.getId(), linkIds, f.link3.getId()); plan2.addActivity(factory.createActivityFromLinkId("h", f.link3.getId())); population.addPerson(person2); // Complete the configuration for our test case // - set scoring parameters ActivityParams actParams = new ActivityParams("h"); actParams.setTypicalDuration(8 * 3600); actParams.setPriority(1.0); config.planCalcScore().addActivityParams(actParams); // - define iterations config.controler().setLastIteration(0); // - make sure we don't use threads, as they are not deterministic config.global().setNumberOfThreads(0); // Now run the simulation Controler controler = new Controler(f.scenario); controler.getConfig().controler().setCreateGraphs(false); controler.getConfig().controler().setWriteEventsInterval(0); controler.setDumpDataAtEnd(false); controler.run(); // test if we got the right result // the actual result is 151sec, not 150, as each vehicle "loses" 1sec in the buffer assertEquals( "TravelTimeCalculator has wrong result", 151.0, controler.getLinkTravelTimes().getLinkTravelTime(f.link2, 7 * 3600, null, null), 0.0); // now test that the ReRoute-Strategy also knows about these travel times... config.controler().setLastIteration(1); ConfigGroup strategyParams = config.getModule("strategy"); strategyParams.addParam("maxAgentPlanMemorySize", "4"); strategyParams.addParam("ModuleProbability_1", "1.0"); strategyParams.addParam("Module_1", "ReRoute"); // Run the simulation again controler = new Controler(f.scenario); controler.getConfig().controler().setCreateGraphs(false); controler .getConfig() .controler() .setOverwriteFileSetting( OutputDirectoryHierarchy.OverwriteFileSetting.overwriteExistingFiles); controler.getConfig().controler().setWriteEventsInterval(0); controler.run(); // test that the plans have the correct times assertEquals( "ReRoute seems to have wrong travel times.", 151.0, ((Leg) (person1.getPlans().get(1).getPlanElements().get(1))).getTravelTime(), 0.0); }
/** * Parsing the persons file of the survey. Unless a numerical value is parsed, the codes from * {@link PersonEnums} are used for the different categories. * * @param sc * @param filename * @return */ public void parsePersons(String filename) { LOG.info("Parsing persons from " + filename); int counter = 0; ObjectAttributes pa = sc.getPopulation().getPersonAttributes(); BufferedReader br = IOUtils.getBufferedReader(filename); try { String line = br.readLine(); /* Header. */ line = br.readLine(); /* Header. */ while ((line = br.readLine()) != null) { counter++; String[] sa = line.split(","); if (sa.length == 65) { /* Create the person. */ PopulationFactory pf = sc.getPopulation().getFactory(); Id<Person> id = Id.createPersonId(sa[1] + "_" + sa[2]); Person person = pf.createPerson(id); /* All all the person attributes. */ pa.putAttribute(id.toString(), "yearOfBirth", sa[3].equals("") ? "Unknown" : sa[3]); pa.putAttribute( id.toString(), "gender", PersonEnums.Gender.parseFromCode(sa[4].equals("") ? 0 : Integer.parseInt(sa[4])) .getDescription()); pa.putAttribute( id.toString(), "education", PersonEnums.Education.parseFromCode(sa[5].equals("") ? 0 : Integer.parseInt(sa[5])) .getDescription()); Disability d1 = PersonEnums.Disability.parseFromCode(sa[6].equals("") ? 0 : Integer.parseInt(sa[6])); if (!d1.equals(Disability.NONE)) { pa.putAttribute(id.toString(), "disability1", d1.getDescription()); } Disability d2 = PersonEnums.Disability.parseFromCode(sa[7].equals("") ? 0 : Integer.parseInt(sa[7])); if (!d2.equals(Disability.NONE)) { pa.putAttribute(id.toString(), "disability2", d2.getDescription()); } Disability d3 = PersonEnums.Disability.parseFromCode(sa[8].equals("") ? 0 : Integer.parseInt(sa[8])); if (!d3.equals(Disability.NONE)) { pa.putAttribute(id.toString(), "disability3", d3.getDescription()); } pa.putAttribute( id.toString(), "license_car", PersonEnums.LicenseCar.parseFromCode(sa[9].equals("") ? 0 : Integer.parseInt(sa[9])) .getDescription()); pa.putAttribute( id.toString(), "license_motorcycle", PersonEnums.LicenseMotorcycle.parseFromCode( sa[10].equals("") ? 0 : Integer.parseInt(sa[10])) .getDescription()); pa.putAttribute( id.toString(), "license_heavyVehicle", PersonEnums.LicenseHeavyVehicle.parseFromCode( sa[11].equals("") ? 0 : Integer.parseInt(sa[11])) .getDescription()); pa.putAttribute( id.toString(), "employment", PersonEnums.Employment.parseFromCode(sa[12].equals("") ? 0 : Integer.parseInt(sa[12])) .getDescription()); pa.putAttribute( id.toString(), "travelToPrimary", PersonEnums.TravelToPrimary.parseFromCode( sa[13].equals("") ? 0 : Integer.parseInt(sa[13])) .getDescription()); pa.putAttribute( id.toString(), "workFromHome", PersonEnums.WorkFromHome.parseFromCode( sa[14].equals("") ? 0 : Integer.parseInt(sa[14])) .getDescription()); pa.putAttribute( id.toString(), "travelForWork", PersonEnums.TravelForWork.parseFromCode( sa[15].equals("") ? 0 : Integer.parseInt(sa[15])) .getDescription()); /*TODO We can add more of the variable later, should we wish. */ sc.getPopulation().addPerson(person); } else { LOG.warn(" person: " + counter + " ==> Line with " + sa.length + " elements."); } } } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("Cannot read from " + filename); } finally { try { br.close(); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("Cannot close " + filename); } } LOG.info("Total number of persons parsed: " + sc.getPopulation().getPersons().size()); LOG.info("Done parsing persons."); }
public void CreateChoiceSets() { PopulationFactory populationFactory = population.getFactory(); ArrayList<Person> newPersons = new ArrayList<>(); /*Clean routes*/ PlanRouteStripper planRouteStripper = new PlanRouteStripper(); planRouteStripper.run(population); for (Id<Person> personId : population.getPersons().keySet()) { Plan plan = population.getPersons().get(personId).getSelectedPlan(); plan.getCustomAttributes() .put( "toll", population .getPersonAttributes() .getAttribute(personId.toString(), "selectedPlanToll")); initialPlans.put(personId, plan); population.getPersons().get(personId).createCopyOfSelectedPlanAndMakeSelected(); Plan planTmp = population.getPersons().get(personId).getSelectedPlan(); population.getPersons().get(personId).getPlans().clear(); population.getPersons().get(personId).addPlan(planTmp); } /*Create optimal walk plan and substitute it in the planMap*/ OptimalWalkPlanFinder optimalWalkPlanFinder = new OptimalWalkPlanFinder(controler.getConfig()); for (Id<Person> personId : population.getPersons().keySet()) { /* Plan cloning for each mode*/ HashMap<String, Plan> planMap = new HashMap<>(); Leg firstLeg = (Leg) population.getPersons().get(personId).getSelectedPlan().getPlanElements().get(1); String initialMode = firstLeg.getMode(); // If "No PT" if (initialMode == "pt" && simType.equals("noPT")) initialMode = "walk"; planMap.put(initialMode, population.getPersons().get(personId).getSelectedPlan()); // String[] modes = // controler.getScenario().getConfig().getModule("subtourModeChoice").getValue("modes").split(","); // String[] modes = {"car","walk"}; ArrayList<String> relevantModes = new ArrayList<String>(); relevantModes.add("car"); if (!simType.equals("carOnly") && !simType.equals("noPT")) relevantModes.add("pt"); for (String mode : relevantModes) { if (!initialMode.equals(mode) && !mode.equals("walk")) { population.getPersons().get(personId).createCopyOfSelectedPlanAndMakeSelected(); Plan planForModeChange = population.getPersons().get(personId).getSelectedPlan(); for (int j = 1; j < planForModeChange.getPlanElements().size(); j += 2) { Leg leg = (Leg) planForModeChange.getPlanElements().get(j); leg.setMode(mode); } planMap.put(mode, planForModeChange); } } /*Check if walk is a viable alternative and if so, add it to the choice set*/ if (!initialMode.equals("walk") && optimalWalkPlanFinder.getWalkTravelTime(planMap.get(initialMode)) <= 3600.0 && !simType.equals("carOnly")) { population.getPersons().get(personId).createCopyOfSelectedPlanAndMakeSelected(); Plan planForModeChange = population.getPersons().get(personId).getSelectedPlan(); planForModeChange = optimalWalkPlanFinder.findOptimalWalkPlan(planForModeChange); for (int j = 1; j < planForModeChange.getPlanElements().size(); j += 2) { Leg leg = (Leg) planForModeChange.getPlanElements().get(j); leg.setMode("walk"); } planMap.put("walk", planForModeChange); } ; // String[] relevantModes = {"car","pt"}; /* Departure time modification for each mode*/ for (String mode : relevantModes) { Plan basePlan = planMap.get(mode); population.getPersons().get(personId).setSelectedPlan(basePlan); for (int i = 0; i < 6; i++) { population.getPersons().get(personId).createCopyOfSelectedPlanAndMakeSelected(); } population.getPersons().get(personId).setSelectedPlan(basePlan); int planModCounter = 0; /* Departure time modification */ for (Plan planToModify : population.getPersons().get(personId).getPlans()) { if (((Leg) planToModify.getPlanElements().get(1)).getMode().equals(mode) && !PersonUtils.isSelected(planToModify)) { // Home departure time + 1h if (planModCounter == 0) { Activity act = (Activity) planToModify.getPlanElements().get(0); act.setEndTime(act.getEndTime() + 3600.0); } // Home departure time - 1h if (planModCounter == 1) { Activity act = (Activity) planToModify.getPlanElements().get(0); act.setEndTime(act.getEndTime() - 3600.0); } // Work departure time + 1h if (planModCounter == 2) { Activity act = (Activity) planToModify.getPlanElements().get(2); act.setEndTime(act.getEndTime() + 3600.0); } // Work departure time - 1h if (planModCounter == 3) { Activity act = (Activity) planToModify.getPlanElements().get(2); act.setEndTime(act.getEndTime() - 3600.0); } // Home departure time + 1h, Work departure time +1h if (planModCounter == 4) { Activity act = (Activity) planToModify.getPlanElements().get(0); act.setEndTime(act.getEndTime() + 3600.0); Activity act2 = (Activity) planToModify.getPlanElements().get(2); act2.setEndTime(act2.getEndTime() + 3600.0); } // Home departure time - 1h, Work departure time -1h if (planModCounter == 5) { Activity act = (Activity) planToModify.getPlanElements().get(0); act.setEndTime(act.getEndTime() - 3600.0); Activity act2 = (Activity) planToModify.getPlanElements().get(2); act2.setEndTime(act2.getEndTime() - 3600.0); } // NOT REALISTIC // //Home departure time +1h, Work departure time -1h // if (planModCounter == 6) { // Activity act = (Activity) planToModify.getPlanElements().get(0); // act.setEndTime(act.getEndTime() + 3600.0); // // Activity act2 = (Activity) planToModify.getPlanElements().get(2); // act2.setEndTime(act2.getEndTime() - 3600.0); // } // // //Home departure time - 1h, Work departure time +1h // if (planModCounter == 7) { // Activity act = (Activity) planToModify.getPlanElements().get(0); // act.setEndTime(act.getEndTime() - 3600.0); // // Activity act2 = (Activity) planToModify.getPlanElements().get(2); // act2.setEndTime(act2.getEndTime() + 3600.0); // } planModCounter++; } } } /*Plan split to different persons*/ int count = 0; population.getPersons().get(personId).setSelectedPlan(planMap.get(initialMode)); for (Plan newPlan : population.getPersons().get(personId).getPlans()) { if (personId.toString().equals("1000")) System.out.println(); if (!PersonUtils.isSelected(newPlan)) { count++; Person newPerson = populationFactory.createPerson(Id.createPersonId(personId.toString() + "_" + count)); newPerson.addPlan(newPlan); newPerson.setSelectedPlan(newPlan); newPersons.add(newPerson); population .getPersonAttributes() .putAttribute( newPerson.getId().toString(), "income", population.getPersonAttributes().getAttribute(personId.toString(), "income")); population .getPersonAttributes() .putAttribute( newPerson.getId().toString(), "betaFactor", population.getPersonAttributes().getAttribute(personId.toString(), "betaFactor")); } } /*Clear all plans from initial person*/ // population.getPersons().get(personId).getPlans().clear(); // population.getPersons().get(personId).addPlan(initialPlans.get(personId)); // population.getPersons().get(personId).setSelectedPlan(initialPlans.get(personId)); /*Clear all plans from initial person*/ population.getPersons().get(personId).getPlans().clear(); Person tempPerson = population.getFactory().createPerson(Id.create(20000, Person.class)); tempPerson.addPlan(initialPlans.get(personId)); tempPerson.setSelectedPlan(initialPlans.get(personId)); tempPerson.createCopyOfSelectedPlanAndMakeSelected(); Plan tempPlan = tempPerson.getSelectedPlan(); population.getPersons().get(personId).addPlan(tempPlan); population.getPersons().get(personId).setSelectedPlan(tempPlan); } /*Add new agents to the simulation*/ for (Person newPerson : newPersons) { population.addPerson(newPerson); } /*Write out new population file*/ // System.out.println("New number of persons: " + population.getPersons().size()); // new org.matsim.core.population.PopulationWriter(population, // controler.getScenario().getNetwork()).write("/Volumes/DATA 1 (WD 2 // TB)/output_SelectExp1_5p_"+simType+"_1000it_Dwell/popText.xml"); }
private static void makePopulation( final int nOfPersonFromEachHome, final String networkFilename, final String filename) { Scenario sc = ScenarioUtils.createScenario(ConfigUtils.createConfig()); Population pop = sc.getPopulation(); PopulationFactory pf = pop.getFactory(); Random r = new Random(4711); int pId = 0; double baseX = 1000; double baseY = 1000; for (int i = 0; i < nOfPersonFromEachHome; i++) { pId++; String mode = (r.nextDouble() < 0.50 ? TransportMode.car : TransportMode.pt); Person person = pf.createPerson(Id.create(pId, Person.class)); Plan plan = pf.createPlan(); Coord homeCoord = new Coord( (double) (int) (baseX - 450 + 900 * r.nextDouble()), (double) (int) (baseY - 450 + 900 * r.nextDouble())); fillPlan(plan, mode, r, pf, homeCoord); person.addPlan(plan); pop.addPerson(person); } baseX = 1000; baseY = 4000; for (int i = 0; i < nOfPersonFromEachHome; i++) { pId++; String mode = (r.nextDouble() < 0.50 ? TransportMode.car : TransportMode.pt); Person person = pf.createPerson(Id.create(pId, Person.class)); Plan plan = pf.createPlan(); Coord homeCoord = new Coord( (double) (int) (baseX - 450 + 900 * r.nextDouble()), (double) (int) (baseY - 450 + 900 * r.nextDouble())); fillPlan(plan, mode, r, pf, homeCoord); person.addPlan(plan); pop.addPerson(person); } baseX = 4000; baseY = 4000; for (int i = 0; i < nOfPersonFromEachHome; i++) { pId++; String mode = (r.nextDouble() < 0.50 ? TransportMode.car : TransportMode.pt); Person person = pf.createPerson(Id.create(pId, Person.class)); Plan plan = pf.createPlan(); Coord homeCoord = new Coord( (double) (int) (baseX - 450 + 900.0 * r.nextDouble()), (double) (int) (baseY - 450 + 900.0 * r.nextDouble())); fillPlan(plan, mode, r, pf, homeCoord); person.addPlan(plan); pop.addPerson(person); } new MatsimNetworkReader(sc.getNetwork()).readFile(networkFilename); NetworkImpl carNetwork = NetworkImpl.createNetwork(); new TransportModeNetworkFilter(sc.getNetwork()) .filter(carNetwork, CollectionUtils.stringToSet(TransportMode.car)); new XY2Links(carNetwork, null).run(pop); new PopulationWriter(pop, null).write(filename); }
public void testTransitRouteCopy() { Config config = super.loadConfig(null); config.scenario().setUseTransit(true); config.scenario().setUseVehicles(true); ScenarioImpl scenario = (ScenarioImpl) ScenarioUtils.createScenario(config); Id<Node> nodeId1 = Id.create("1", Node.class); Id<Node> nodeId2 = Id.create("2", Node.class); Id<Node> nodeId3 = Id.create("3", Node.class); Id<Link> linkId1 = Id.create("1", Link.class); Id<Link> linkId2 = Id.create("2", Link.class); // build network Network network = scenario.getNetwork(); NetworkFactory nBuilder = network.getFactory(); Node node1 = nBuilder.createNode(nodeId1, scenario.createCoord(0, 0)); Node node2 = nBuilder.createNode(nodeId2, scenario.createCoord(1000, 0)); Node node3 = nBuilder.createNode(nodeId3, scenario.createCoord(2000, 0)); network.addNode(node1); network.addNode(node2); network.addNode(node3); Link link1 = nBuilder.createLink(linkId1, node1, node2); Link link2 = nBuilder.createLink(linkId2, node2, node3); network.addLink(link1); network.addLink(link2); // build schedule TransitSchedule schedule = scenario.getTransitSchedule(); TransitScheduleFactory sBuilder = schedule.getFactory(); TransitStopFacility stopF1 = sBuilder.createTransitStopFacility( Id.create("1", TransitStopFacility.class), scenario.createCoord(1000.0, 0), false); TransitStopFacility stopF2 = sBuilder.createTransitStopFacility( Id.create("2", TransitStopFacility.class), scenario.createCoord(2000.0, 0), false); stopF1.setLinkId(link1.getId()); stopF2.setLinkId(link2.getId()); schedule.addStopFacility(stopF1); schedule.addStopFacility(stopF2); TransitLine tLine1 = sBuilder.createTransitLine(Id.create("1", TransitLine.class)); TransitRouteStop stop1 = sBuilder.createTransitRouteStop(stopF1, 0, 0); TransitRouteStop stop2 = sBuilder.createTransitRouteStop(stopF2, 100, 100); ArrayList<TransitRouteStop> stops = new ArrayList<TransitRouteStop>(); stops.add(stop1); stops.add(stop2); NetworkRoute netRoute = new LinkNetworkRouteImpl(link1.getId(), link2.getId()); netRoute.setLinkIds(link1.getId(), Collections.<Id<Link>>emptyList(), link2.getId()); TransitRoute tRoute1 = sBuilder.createTransitRoute(Id.create("1", TransitRoute.class), netRoute, stops, "bus"); tRoute1.addDeparture(sBuilder.createDeparture(Id.create("1", Departure.class), 7.0 * 3600)); tLine1.addRoute(tRoute1); schedule.addTransitLine(tLine1); // build vehicles new CreateVehiclesForSchedule(schedule, scenario.getVehicles()).run(); // build population Population population = scenario.getPopulation(); PopulationFactory pBuilder = population.getFactory(); Person person1 = pBuilder.createPerson(Id.create("1", Person.class)); Plan plan = pBuilder.createPlan(); Activity homeAct = pBuilder.createActivityFromLinkId("h", linkId1); homeAct.setEndTime(7.0 * 3600); plan.addActivity(homeAct); Leg leg = pBuilder.createLeg(TransportMode.pt); ExperimentalTransitRoute tRoute = new ExperimentalTransitRoute(stopF1, tLine1, tRoute1, stopF2); leg.setRoute(tRoute); plan.addLeg(leg); plan.addActivity(pBuilder.createActivityFromLinkId("w", linkId2)); person1.addPlan(plan); population.addPerson(person1); // prepare config config.controler().setFirstIteration(0); config.controler().setLastIteration(1); ActivityParams params = new ActivityParams("h"); params.setTypicalDuration(16.0 * 3600); config.planCalcScore().addActivityParams(params); params = new ActivityParams("w"); params.setTypicalDuration(8.0 * 3600); config.planCalcScore().addActivityParams(params); StrategySettings tam = new StrategySettings(Id.create(1, StrategySettings.class)); tam.setStrategyName("TimeAllocationMutator"); tam.setWeight(1.0); config.strategy().addStrategySettings(tam); // run Controler controler = new Controler(scenario); controler.getConfig().controler().setWriteEventsInterval(0); controler.setCreateGraphs(false); controler.run(); // checks assertEquals(1, population.getPersons().size()); assertEquals(2, person1.getPlans().size()); assertEquals( ExperimentalTransitRoute.class, ((Leg) person1.getPlans().get(0).getPlanElements().get(1)).getRoute().getClass()); assertEquals( ExperimentalTransitRoute.class, ((Leg) person1.getPlans().get(1).getPlanElements().get(1)).getRoute().getClass()); }