public Activity createFinalActivity(String type, Id<Link> newLinkId) { Activity newAct = this.pf.createActivityFromLinkId(type, newLinkId);; newAct.setEndTime( Double.POSITIVE_INFINITY ) ; return newAct ; } // public Activity createAgentThatKeepsMatsimAlive( String type, Id<Link> newLinkId) {
public static Activity createActivityFromLinkId(String type, Id<Link> linkId) { return getFactory().createActivityFromLinkId(type, linkId) ; }
public static Activity createAndAddActivityFromLinkId( Plan plan, String type, Id<Link> linkId ) { Activity act = getFactory().createActivityFromLinkId(type, linkId) ; plan.addActivity(act); act.setLinkId(linkId); return act ; }
public static Activity createActivity(Activity act) { Activity newAct = getFactory().createActivityFromLinkId(act.getType(), act.getLinkId()) ; copyFromTo(act, newAct); // (this ends up setting type and linkId again) return newAct ; }
protected Activity createActivity(StayTask task) { Activity act = populFactory.createActivityFromLinkId("s", task.getLink().getId()); act.setStartTime(task.getBeginTime()); act.setEndTime(task.getEndTime()); return act; }
@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 static Activity createActivityFromLocationId( final boolean anchorAtFacilities, final PopulationFactory fact, final String type, final Id<?> loc) { final Id<Link> linkLoc = anchorAtFacilities ? Id.create( "nowhere", Link.class ) : Id.create(loc, Link.class); final Activity act = fact.createActivityFromLinkId( type , linkLoc ); if (anchorAtFacilities) { final Id<ActivityFacility> facLoc = Id.create(loc, ActivityFacility.class) ; ((Activity) act).setFacilityId( facLoc ); } return act; }
Activity startAct = population.getFactory().createActivityFromLinkId("dummy", Id.createLinkId(fromLinkId)); Activity drainAct = population.getFactory().createActivityFromLinkId("dummy", Id.createLinkId(toLinkId)); plan.addActivity(drainAct);
Plan plan = fac.createPlan(); pers.addPlan(plan); Activity a0 = fac.createActivityFromLinkId("origin",Id.createLinkId(0)); a0.setEndTime(i); plan.addActivity(a0); Leg leg = fac.createLeg("car"); plan.addLeg(leg); Activity a1 = fac.createActivityFromLinkId("destination",Id.createLinkId(3)); plan.addActivity(a1);
plan.addActivity( fact.createActivityFromLinkId( "h" , id1 ) ); plan.addActivity( fact.createActivityFromLinkId( STAGE , id2 ) ); plan.addLeg( fact.createLeg( TransportMode.pt ) ); plan.addActivity( fact.createActivityFromLinkId( STAGE , id3 ) ); plan.addLeg( fact.createLeg( TransportMode.transit_walk ) ); plan.addActivity( fact.createActivityFromLinkId( "w" , id3 ) ); plan.addActivity( fact.createActivityFromLinkId( STAGE , id2 ) ); plan.addLeg( fact.createLeg( TransportMode.pt ) ); plan.addActivity( fact.createActivityFromLinkId( STAGE , id3 ) ); plan.addLeg( fact.createLeg( TransportMode.transit_walk ) ); plan.addLeg( fact.createLeg( TransportMode.pt ) ); plan.addLeg( fact.createLeg( TransportMode.transit_walk ) ); plan.addActivity( fact.createActivityFromLinkId( STAGE , id2 ) ); plan.addLeg( fact.createLeg( TransportMode.pt ) ); plan.addActivity( fact.createActivityFromLinkId( STAGE , id1 ) ); plan.addLeg( fact.createLeg( TransportMode.transit_walk ) ); plan.addActivity( fact.createActivityFromLinkId( STAGE , id2 ) ); plan.addLeg( fact.createLeg( TransportMode.transit_walk ) ); plan.addActivity( fact.createActivityFromLinkId( "h" , id1 ) );
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); } } }
/** * 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); } }
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; }
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 }
@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 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); }
plan.addActivity( population.getFactory().createActivityFromLinkId( "tweet" , Id.createLinkId( 2 )));
Activity act1 = pf.createActivityFromLinkId("h", l1.getId()); Leg leg = pf.createLeg(TransportMode.car); NetworkRoute route = RouteUtils.createLinkNetworkRouteImpl(l1.getId(), l2.getId()); route.setLinkIds(l1.getId(), new ArrayList<Id<Link>>(0), l2.getId()); leg.setRoute(route); Activity act2 = pf.createActivityFromLinkId("w", l2.getId()); plan.addActivity(act1); plan.addLeg(leg);