public static Plan createPlan() { return getFactory().createPlan() ; }
public static Plan createPlan(Person person) { Plan plan = getFactory().createPlan() ; plan.setPerson(person); return plan ; }
Plan plan = population.getFactory().createPlan(); person.addPlan(plan);
public static void addPersonWithActivity(String actType, Coordinates coordsOfAct, double actEndTime, Scenario scenario) { PopulationFactory populationFactory = scenario.getPopulation() .getFactory(); Coord matSimCoord = new Coord(coordsOfAct.getLongitude(), coordsOfAct.getLatitude()); // Create a new plan Plan plan = populationFactory.createPlan(); // Create a new activity with the end time and add it to the plan Activity act = populationFactory.createActivityFromCoord(actType, matSimCoord); act.setEndTime(actEndTime); plan.addActivity(act); // Add a new leg to the plan. Needed, otherwise MATSim won't add this // leg-less plan to the simulation plan.addLeg(populationFactory.createLeg("car")); // Create a second activity (all plans must end in an activity) act = populationFactory.createActivityFromCoord(actType.toString(), matSimCoord); plan.addActivity(act); Person person = populationFactory.createPerson(Id .createPersonId(personIdPrefix + personId++)); person.addPlan(plan); scenario.getPopulation().addPerson(person); }
Person pers = fac.createPerson(Id.createPersonId(i)); pop.addPerson(pers); Plan plan = fac.createPlan(); pers.addPlan(plan); Activity a0 = fac.createActivityFromLinkId("origin",Id.createLinkId(0));
@Test( expected=NullPointerException.class ) public void testNPEWhenLocationNullInSubtourAnalysis() { // this may sound surprising, but for a long time the algorithm // was perfectly fine with that if assertions were disabled... final Plan plan = populationFactory.createPlan(); // link ids are null plan.addActivity( populationFactory.createActivityFromCoord( "type", new Coord((double) 0, (double) 0)) ); plan.addLeg( populationFactory.createLeg( "mode" ) ); plan.addActivity( populationFactory.createActivityFromCoord( "type", new Coord((double) 0, (double) 0)) ); TripStructureUtils.getSubtours( plan , EmptyStageActivityTypes.INSTANCE ); } }
@Test public void testOverrideAgentFactory() { Config config = ConfigUtils.createConfig(); config.controler().setOverwriteFileSetting(OverwriteFileSetting.deleteDirectoryIfExists); config.controler().setLastIteration(0); Scenario scenario = ScenarioUtils.loadScenario(config); Person person = scenario.getPopulation().getFactory().createPerson(Id.createPersonId("person")); person.addPlan(scenario.getPopulation().getFactory().createPlan()); scenario.getPopulation().addPerson(person); AtomicLong value = new AtomicLong(0); Controler controler = new Controler(scenario); controler.addOverridingQSimModule(new TestQSimModule(value)); controler.run(); Assert.assertTrue(value.get() > 0); }
@Override public Plan createNewPlanInstance() { final PopulationFactory fact = createPopulationFactory(); final Id<Link> id1 = Id.create( 1, Link.class ); final Id<Link> id3 = Id.create( 3, Link.class ); final Plan plan = fact.createPlan(); plan.addActivity( fact.createActivityFromLinkId( "h" , id1 ) ); plan.addLeg( fact.createLeg( TransportMode.walk ) ); plan.addActivity( fact.createActivityFromLinkId( "w" , id3 ) ); plan.addLeg( fact.createLeg( TransportMode.walk ) ); plan.addActivity( fact.createActivityFromLinkId( "h" , id1 ) ); return plan; } };
private void createPopulation() { Population pop = this.scenario.getPopulation(); PopulationFactory f = pop.getFactory(); Person p = f.createPerson(Id.create(1, Person.class)); pop.addPerson(p); Plan plan = f.createPlan(); p.addPlan(plan); Activity act = f.createActivityFromLinkId("home", Id.create(1, Link.class)); act.setEndTime(2000.0); plan.addActivity(act); Leg leg = f.createLeg(TransportMode.car); plan.addLeg(leg); act = f.createActivityFromLinkId("home", Id.create(34, Link.class)); plan.addActivity(act); }
private void createPopulation(){ for(int i=1;i<3;i++){ Id<Person> id = Id.createPersonId(i); Person p = population.getFactory().createPerson(id); Plan plan = population.getFactory().createPlan(); p.addPlan(plan); Activity a1 = population.getFactory().createActivityFromLinkId("h", link1.getId()); a1.setEndTime(0*3600); Leg leg = population.getFactory().createLeg(TransportMode.car); plan.addActivity(a1); plan.addLeg(leg); LinkNetworkRouteFactory factory = new LinkNetworkRouteFactory(); NetworkRoute route; List<Id<Link>> linkIds = new ArrayList<Id<Link>>(); route= (NetworkRoute) factory.createRoute(link1.getId(), link3.getId()); linkIds.add(link2.getId()); route.setLinkIds(link1.getId(), linkIds, link3.getId()); leg.setRoute(route); Activity a2 = population.getFactory().createActivityFromLinkId("w", link3.getId()); plan.addActivity(a2); population.addPerson(p); } } }
public void testWriteGenericRoute() { MutableScenario scenario = (MutableScenario) ScenarioUtils.createScenario(super.loadConfig(null)); Network network = scenario.getNetwork(); new MatsimNetworkReader(scenario.getNetwork()).readFile("test/scenarios/equil/network.xml"); Link link1 = network.getLinks().get(Id.create(1, Link.class)); Link link2 = network.getLinks().get(Id.create(2, Link.class)); Scenario tmpScenario = new ScenarioBuilder(ConfigUtils.createConfig()).setNetwork(network).build() ; Population pop = tmpScenario.getPopulation(); PopulationFactory pb = pop.getFactory(); Person person = pb.createPerson(Id.create(1, Person.class)); Plan plan = (Plan) pb.createPlan(); plan.setPerson(person); plan.addActivity(pb.createActivityFromLinkId("h", link1.getId())); Leg leg = pb.createLeg("undefined"); Route route = RouteUtils.createGenericRouteImpl(link1.getId(), link2.getId()); route.setTravelTime(123); leg.setRoute(route); plan.addLeg(leg); plan.addActivity(pb.createActivityFromLinkId("h", Id.create(1, Link.class))); person.addPlan(plan); pop.addPerson(person); String filename = getOutputDirectory() + "population.xml"; new PopulationWriter(pop, network).writeV4(filename); Population pop2 = scenario.getPopulation(); new PopulationReader(scenario).readFile(filename); Person person2 = pop2.getPersons().get(Id.create(1, Person.class)); Leg leg2 = (Leg) person2.getPlans().get(0).getPlanElements().get(1); Route route2 = leg2.getRoute(); assertEquals(123, route2.getTravelTime(), EPSILON); // if this succeeds, we know that writing/reading the data works }
private Person createPerson(Scenario scenario, String id) { Person person = scenario.getPopulation().getFactory().createPerson(Id.create(id, Person.class)); Activity from = scenario.getPopulation().getFactory().createActivityFromLinkId("home", Id.create("l0", Link.class)); Leg leg = scenario.getPopulation().getFactory().createLeg(TransportMode.car); Activity to = scenario.getPopulation().getFactory().createActivityFromLinkId("home", Id.create("l2", Link.class)); from.setEndTime(8*3600); leg.setDepartureTime(8*3600); RouteFactory routeFactory = new LinkNetworkRouteFactory(); Id<Link> startLinkId = Id.create("l0", Link.class); Id<Link> endLinkId = Id.create("l2", Link.class); NetworkRoute route = (NetworkRoute) routeFactory.createRoute(startLinkId, endLinkId); List<Id<Link>> linkIds = new ArrayList<Id<Link>>(); linkIds.add(Id.create("l1", Link.class)); route.setLinkIds(startLinkId, linkIds, endLinkId); leg.setRoute(route); Plan plan = scenario.getPopulation().getFactory().createPlan(); plan.addActivity(from); plan.addLeg(leg); plan.addActivity(to); person.addPlan(plan); return person; }
private Person createPersonWithPlan(PopulationFactory factory, String id, double homeX, double homeY, double workX, double workY) { Person person = factory.createPerson(Id.create(id, Person.class)); Plan plan = factory.createPlan(); person.addPlan(plan); Activity home1 = factory.createActivityFromCoord("home", new Coord(homeX, homeY)); home1.setEndTime(8*3600); Activity work = factory.createActivityFromCoord("work", new Coord(workX, workY)); work.setEndTime(17*3600); Activity home2 = factory.createActivityFromCoord("home", new Coord(homeX, homeY)); plan.addActivity(home1); plan.addLeg(factory.createLeg("car")); plan.addActivity(work); plan.addLeg(factory.createLeg("car")); plan.addActivity(home2); return person; }
/** * create a population of 5000 persons traveling from link 0 to link 2. * they all start 50 minutes after midnight and then queue on link 0 which lets one of them depart every second. * for analysis we then use flow values from the second hour. * */ private static void initPopulation(Population population) { // create enough persons to be able to check simulated capacity of link 1 for (int i = 0; i < 5000; i++) { Person person = population.getFactory().createPerson(Id.createPersonId(i)); // create a start activity at link 0 with end time 50*60 Activity startAct = population.getFactory().createActivityFromLinkId("dummy", Id.createLinkId("0")); startAct.setEndTime(50*60); // create a drain activity at link 2 Activity drainAct = population.getFactory().createActivityFromLinkId( "dummy", Id.createLinkId("2")); // create a dummy leg Leg leg = population.getFactory().createLeg(TransportMode.car); leg.setRoute(RouteUtils.createLinkNetworkRouteImpl(Id.createLinkId("0"), Collections.singletonList(Id.createLinkId("1")), Id.createLinkId("2"))); // create a plan for the person that contains all this information Plan plan = population.getFactory().createPlan(); plan.addActivity(startAct); plan.addLeg(leg); plan.addActivity(drainAct); // store information in population person.addPlan(plan); population.addPerson(person); } }
@Test public void testCoord3dIO() { final Population population = PopulationUtils.createPopulation(ConfigUtils.createConfig() ); final Person person = population.getFactory().createPerson(Id.createPersonId( "Donald Trump")); population.addPerson( person ); final Plan plan = population.getFactory().createPlan(); person.addPlan( plan ); plan.addActivity(population.getFactory().createActivityFromCoord( "speech" , new Coord( 0 , 0 ) )); plan.addActivity(population.getFactory().createActivityFromCoord( "tweet" , new Coord( 0 , 0 , -100 ) )); final String file = utils.getOutputDirectory()+"/population.xml"; new PopulationWriter( population ).writeV6( file ); final Scenario readScenario = ScenarioUtils.createScenario( ConfigUtils.createConfig() ); new PopulationReader( readScenario ).readFile( file ); final Person readPerson = readScenario.getPopulation().getPersons().get( Id.createPersonId( "Donald Trump" ) ); final Activity readSpeach = (Activity) readPerson.getSelectedPlan().getPlanElements().get( 0 ); final Activity readTweet = (Activity) readPerson.getSelectedPlan().getPlanElements().get( 1 ); Assert.assertFalse( "did not expect Z value in "+readSpeach.getCoord() , readSpeach.getCoord().hasZ() ); Assert.assertTrue( "did expect T value in "+readTweet.getCoord() , readTweet.getCoord().hasZ() ); Assert.assertEquals( "unexpected Z value in "+readTweet.getCoord(), -100, readTweet.getCoord().getZ(), MatsimTestUtils.EPSILON ); }
@Test public void testLegAttributesIO() { final Population population = PopulationUtils.createPopulation(ConfigUtils.createConfig() ); final Person person = population.getFactory().createPerson(Id.createPersonId( "Donald Trump")); population.addPerson( person ); final Plan plan = population.getFactory().createPlan(); person.addPlan( plan ); final Leg leg = population.getFactory().createLeg( "SUV" ); plan.addActivity( population.getFactory().createActivityFromLinkId( "speech" , Id.createLinkId( 1 ))); plan.addLeg( leg ); plan.addActivity( population.getFactory().createActivityFromLinkId( "tweet" , Id.createLinkId( 2 ))); leg.getAttributes().putAttribute( "mpg" , 0.000001d ); final String file = utils.getOutputDirectory()+"/population.xml"; new PopulationWriter( population ).writeV6( file ); final Scenario readScenario = ScenarioUtils.createScenario( ConfigUtils.createConfig() ); new PopulationReader( readScenario ).readFile( file ); final Person readPerson = readScenario.getPopulation().getPersons().get( Id.createPersonId( "Donald Trump" ) ); final Leg readLeg = (Leg) readPerson.getSelectedPlan().getPlanElements().get( 1 ); Assert.assertEquals( "Unexpected Double attribute in " + readLeg.getAttributes(), leg.getAttributes().getAttribute( "mpg" ) , readLeg.getAttributes().getAttribute( "mpg" ) ); }
@Test public void testActivityAttributesIO() { final Population population = PopulationUtils.createPopulation(ConfigUtils.createConfig() ); final Person person = population.getFactory().createPerson(Id.createPersonId( "Donald Trump")); population.addPerson( person ); final Plan plan = population.getFactory().createPlan(); person.addPlan( plan ); final Activity act = population.getFactory().createActivityFromCoord( "speech" , new Coord( 0 , 0 ) ); plan.addActivity( act ); act.getAttributes().putAttribute( "makes sense" , false ); act.getAttributes().putAttribute( "length" , 1895L ); final String file = utils.getOutputDirectory()+"/population.xml"; new PopulationWriter( population ).writeV6( file ); final Scenario readScenario = ScenarioUtils.createScenario( ConfigUtils.createConfig() ); new PopulationReader( readScenario ).readFile( file ); final Person readPerson = readScenario.getPopulation().getPersons().get( Id.createPersonId( "Donald Trump" ) ); final Activity readAct = (Activity) readPerson.getSelectedPlan().getPlanElements().get( 0 ); Assert.assertEquals( "Unexpected boolean attribute in " + readAct.getAttributes(), act.getAttributes().getAttribute( "makes sense" ) , readAct.getAttributes().getAttribute( "makes sense" ) ); Assert.assertEquals( "Unexpected Long attribute in " + readAct.getAttributes(), act.getAttributes().getAttribute( "length" ) , readAct.getAttributes().getAttribute( "length" ) ); }
@Test public void testPlanAttributesIO() { final Population population = PopulationUtils.createPopulation(ConfigUtils.createConfig() ); final Person person = population.getFactory().createPerson(Id.createPersonId( "Donald Trump")); population.addPerson( person ); final Plan plan = population.getFactory().createPlan(); person.addPlan( plan ); final Leg leg = population.getFactory().createLeg( "SUV" ); plan.addActivity( population.getFactory().createActivityFromLinkId( "speech" , Id.createLinkId( 1 ))); plan.addLeg( leg ); plan.addActivity( population.getFactory().createActivityFromLinkId( "tweet" , Id.createLinkId( 2 ))); plan.getAttributes().putAttribute( "beauty" , 0.000001d ); final String file = utils.getOutputDirectory()+"/population.xml"; new PopulationWriter( population ).writeV6( file ); final Scenario readScenario = ScenarioUtils.createScenario( ConfigUtils.createConfig() ); new PopulationReader( readScenario ).readFile( file ); final Person readPerson = readScenario.getPopulation().getPersons().get( Id.createPersonId( "Donald Trump" ) ); final Plan readPlan = readPerson.getSelectedPlan() ; Assert.assertEquals( plan.getAttributes().getAttribute( "beauty" ) , readPlan.getAttributes().getAttribute( "beauty" ) ); }
@Test public void testWithRoute_StartEndOnSameLink() { Scenario scenario = ScenarioUtils.createScenario(ConfigUtils.createConfig()); Network network = scenario.getNetwork(); Population population = scenario.getPopulation(); NetworkFactory nf = network.getFactory(); Node n1, n2; network.addNode(n1 = nf.createNode(Id.create("1", Node.class), new Coord((double) 0, (double) 0))); network.addNode(n2 = nf.createNode(Id.create("2", Node.class), new Coord((double) 50, (double) 0))); Link l1 = nf.createLink(Id.create("1", Link.class), n1, n2); l1.setLength(50); network.addLink(l1); PopulationFactory pf = population.getFactory(); Plan plan = pf.createPlan(); Activity act1 = pf.createActivityFromLinkId("h", l1.getId()); Leg leg = pf.createLeg(TransportMode.car); NetworkRoute route = RouteUtils.createLinkNetworkRouteImpl(l1.getId(), l1.getId()); route.setLinkIds(l1.getId(), new ArrayList<Id<Link>>(0), l1.getId()); leg.setRoute(route); Activity act2 = pf.createActivityFromLinkId("w", l1.getId()); plan.addActivity(act1); plan.addLeg(leg); plan.addActivity(act2); // test simple route, none of the links should be included, as there is no real traffic CalcAverageTripLength catl = new CalcAverageTripLength(network); catl.run(plan); Assert.assertEquals(0.0, catl.getAverageTripLength(), MatsimTestUtils.EPSILON); }
@Override protected void setUp() throws Exception { super.setUp(); final Config config = ConfigUtils.createConfig(); final Scenario scenario = ScenarioUtils.createScenario( config ); this.facilities = scenario.getActivityFacilities() ; // create facilities, activities in it and open times final ActivityFacilitiesFactory facilitiesFactory = this.facilities.getFactory(); Coord defaultCoord = new Coord(0.0, 0.0); ActivityFacility testFacility = facilitiesFactory.createActivityFacility(Id.create(0, ActivityFacility.class), defaultCoord) ; this.facilities.addActivityFacility(testFacility); ActivityOption ao = facilitiesFactory.createActivityOption("shop") ; testFacility.addActivityOption(ao); ao.addOpeningTime(new OpeningTimeImpl(6.0 * 3600, 11.0 * 3600)); ao.addOpeningTime(new OpeningTimeImpl(13.0 * 3600, 19.0 * 3600)); // here, we don't test the scoring function itself, but just the method to retrieve opening times // we don't really need persons and plans, they're just used to initialize the ScoringFunction object final PopulationFactory pf = scenario.getPopulation().getFactory(); this.person = pf.createPerson(Id.create(1, Person.class)); Plan plan = pf.createPlan() ; this.person.addPlan(plan); Activity act = pf.createActivityFromCoord("shop", defaultCoord ) ; plan.addActivity(act); act.setFacilityId(testFacility.getId()); act.setStartTime(8.0 * 3600); act.setEndTime(16.0 * 3600); }