/** * @return sorted map containing containing the persons as values and their ids as keys. */ public static SortedMap<Id<Person>, Person> getSortedPersons(final Population population) { return new TreeMap<>(population.getPersons()); }
@Override public PopulationFactory getFactory() { return population.getFactory(); }
@Override public void endTag(final String name, final String content, final Stack<String> context) { if (PERSON.equals(name)) { this.plans.addPerson(this.currperson); this.currperson = null; } else if (PLAN.equals(name)) { this.currplan = null; } else if (LEG.equals(name)) { this.currleg = null; } else if (ROUTE.equals(name)) { this.routeNodes = content; } }
@Override public void startPlans(final Population plans, final BufferedWriter out) throws IOException { out.write("<population"); if (plans.getName() != null) { out.write(" desc=\"" + plans.getName() + "\""); } out.write(">\n\n"); this.attributesWriter.writeAttributes( "\t" , out , plans.getAttributes() ); out.write("\n\n"); }
/** * Randomly chooses for each person of the population a strategy and uses that * strategy on the person. * */ public final void run(final Population population, final ReplanningContext replanningContext) { beforePopulationRunHook(population, replanningContext); delegate.run(population.getPersons().values(), population.getPersonAttributes(), replanningContext); afterRunHook(population); }
delegate.addPerson(p); delegate.removePerson(p.getId());
public static void populationWithBDIAttributeExample() { { Config config = ConfigUtils.createConfig() ; Scenario scenario = ScenarioUtils.createScenario( config ) ; Population pop = scenario.getPopulation() ; PopulationFactory pf = pop.getFactory() ; for ( int ii=0 ; ii<10 ; ii++ ) { Person person = pf.createPerson( Id.createPersonId(ii)) ; person.getAttributes().putAttribute("isBDIAgent", true ) ; pop.addPerson(person); } PopulationUtils.writePopulation(pop, "pop.xml"); } { Config config = ConfigUtils.createConfig() ; config.plans().setInputFile("pop.xml"); Scenario scenario = ScenarioUtils.loadScenario( config ) ; for ( Person person : scenario.getPopulation().getPersons().values() ) { final String attribute = (String) person.getAttributes().getAttribute("isBDIAgent"); System.out.println( "id=" + person.getId() + "; isBdi=" + attribute ) ; } } }
private void createPopulation() { Population pop = scenario.getPopulation(); PopulationFactory factory = pop.getFactory(); pop.addPerson(createPersonWithPlan(factory, "1", 200, 10, 800, 300)); pop.addPerson(createPersonWithPlan(factory, "2", 500, -10, 800, 300)); pop.addPerson(createPersonWithPlan(factory, "3", 600, 20, 800, 300)); pop.addPerson(createPersonWithPlan(factory, "4", 900, 100, 400, 500)); pop.addPerson(createPersonWithPlan(factory, "5", 1000, 300, 400, 500)); pop.addPerson(createPersonWithPlan(factory, "6", 700, 300, 400, 500)); pop.addPerson(createPersonWithPlan(factory, "7", 800, 400, 400, 500)); pop.addPerson(createPersonWithPlan(factory, "8", 440, 500, 300, 100)); pop.addPerson(createPersonWithPlan(factory, "9", 250, 250, 300, 100)); pop.addPerson(createPersonWithPlan(factory, "0", 0, 100, 300, 100)); }
final Person person = sc.getPopulation().getFactory().createPerson(Id.createPersonId(i)); sc.getPopulation().addPerson(person); sc.getPopulation().getPersonAttributes().putAttribute(person.getId().toString(), "subpopulation", subpop);
@Override public ObjectAttributes getPersonAttributes() { return population.getPersonAttributes(); }
@Override public Attributes getAttributes() { return delegate.getAttributes(); } }
@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" ) ); }
@Override public void handlePlan(final Plan plan) { // Creating a dummy population which only contains the plans which are passed here. // I need to copy the plans because I am not supposed to add a plan to a different Person. // I also need to memorize the plans which are passed here, because I am supposed to mutate them. final Person personWithOnlySelectedPlan = this.exportPopulation.getFactory().createPerson(plan.getPerson().getId()); final Plan planForNewPerson = PopulationUtils.createPlan(personWithOnlySelectedPlan); PopulationUtils.copyFromTo(plan, planForNewPerson); personWithOnlySelectedPlan.addPlan(planForNewPerson); this.exportPopulation.addPerson(personWithOnlySelectedPlan); this.plansToMutate.put(plan.getPerson().getId(), plan); }
@Override public void reset(final int iteration) { delegate.reset(iteration); this.agentDepartures.clear(); this.agentLegs.clear(); for ( StatType type : StatType.values() ) { this.statsContainer.get(type).clear() ; if ( this.sumsContainer.get(type)==null ) { this.sumsContainer.put( type, new DataMap<String>() ) ; } this.sumsContainer.get(type).clear() ; } for ( Person person : this.scenario.getPopulation().getPersons().values() ) { ObjectAttributes attribs = this.scenario.getPopulation().getPersonAttributes() ; attribs.putAttribute( person.getId().toString(), TRAV_TIME, 0. ) ; if ( attribs.getAttribute( person.getId().toString(), CERTAIN_LINKS_CNT ) != null ) { attribs.putAttribute( person.getId().toString(), CERTAIN_LINKS_CNT, 0. ) ; } if ( attribs.getAttribute( person.getId().toString(), PAYMENTS) != null ) { attribs.putAttribute( person.getId().toString(), PAYMENTS, 0. ) ; } } // (yy not sure if I like the above; might be better to just use a local data structure. kai, may'14) controlStatisticsSum = 0. ; controlStatisticsCnt = 0. ; }
@Override public ObjectAttributes getPersonAttributes() { return delegate.getPersonAttributes(); }
@Test public void testPopulationAttributesIO() { final Population population = PopulationUtils.createPopulation(ConfigUtils.createConfig() ); population.getAttributes().putAttribute( "type" , "candidates" ); population.getAttributes().putAttribute( "number" , 2 ); final String file = utils.getOutputDirectory()+"/population.xml"; new PopulationWriter( population ).writeV6( file ); final Scenario readScenario = ScenarioUtils.createScenario( ConfigUtils.createConfig() ); new PopulationReader( readScenario ).readFile( file ); Assert.assertEquals( "Unexpected numeric attribute in " + readScenario.getPopulation().getAttributes(), population.getAttributes().getAttribute( "number" ) , readScenario.getPopulation().getAttributes().getAttribute( "number" ) ); Assert.assertEquals( "Unexpected String attribute in " + readScenario.getPopulation().getAttributes(), population.getAttributes().getAttribute( "type" ) , readScenario.getPopulation().getAttributes().getAttribute( "type" ) ); } }
@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" ) ); }
/** * Sorts the persons in the given population. */ @SuppressWarnings("unchecked") public static void sortPersons(final Population population) { Map<Id<Person>, Person> map = (Map<Id<Person>, Person>) population.getPersons(); if (map instanceof SortedMap) return; Map<Id<Person>, Person> treeMap = new TreeMap<>(map); map.clear(); map.putAll(treeMap); }
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); }
@Override public PopulationFactory getFactory() { return delegate.getFactory(); } @Override public String getName() {