/** * Finishes the calculation of the plans' scores and assigns the new scores * to the plans if desired. */ public void finish() { if (iteration == -1) { throw new RuntimeException("Please initialize me before the iteration starts."); } controlerListenerManager.fireControlerAfterMobsimEvent(iteration); scoringFunctionsForPopulation.finishScoringFunctions(); newScoreAssigner.assignNewScores(this.iteration, scoringFunctionsForPopulation, population); finished = true; }
/** * Returns the score of a single agent. This method only returns useful * values if the method {@link #finish() } was called before. description * * @param agentId * The id of the agent the score is requested for. * @return The score of the specified agent. */ public Double getAgentScore(final Id<Person> agentId) { if (!finished) { throw new IllegalStateException("Must call finish first."); } ScoringFunction scoringFunction = scoringFunctionsForPopulation.getScoringFunctionForAgent(agentId); if (scoringFunction == null) return null; return scoringFunction.getScore(); }
events.addHandler(e2legsActs); ScoringFunctionsForPopulation scoringFunctionsForPopulation = new ScoringFunctionsForPopulation( controlerListenerManager, events, scoringFunctionsForPopulation.finishScoringFunctions(); assertEquals(1.0/6.0 * MAX, scoringFunctionsForPopulation.getScoringFunctionForAgent(personId).getScore(), 1.0);
ScoringFunctionFactory scoringFunctionFactory = agentId -> new RecordingScoringFunction(); ScoringFunctionsForPopulation sf = new ScoringFunctionsForPopulation(controlerListenerManager, eventsManager, eventsToActivities, eventsToLegs, population, scoringFunctionFactory); controlerListenerManager.fireControlerIterationStartsEvent(0); ScoringFunction s = sf.getScoringFunctionForAgent(personId); Assert.assertEquals(RecordingScoringFunction.class, s.getClass()); RecordingScoringFunction rs = (RecordingScoringFunction) s; sf.handleActivity(new PersonExperiencedActivity(personId, pf.createActivityFromCoord("home", new Coord(100, 100)))); Assert.assertEquals(0, rs.tripCounter); sf.handleLeg(new PersonExperiencedLeg(personId, pf.createLeg("walk"))); Assert.assertEquals(0, rs.tripCounter); sf.handleActivity(new PersonExperiencedActivity(personId, pf.createActivityFromCoord("work", new Coord(1000, 100)))); Assert.assertEquals(1, rs.tripCounter); Assert.assertEquals(1, rs.lastTrip.getTripElements().size()); Assert.assertEquals("walk", ((Leg) rs.lastTrip.getTripElements().get(0)).getMode()); sf.handleLeg(new PersonExperiencedLeg(personId, pf.createLeg("transit_walk"))); sf.handleActivity(new PersonExperiencedActivity(personId, pf.createActivityFromCoord("pt_interaction", new Coord(1000, 200)))); Assert.assertEquals(1, rs.tripCounter); sf.handleLeg(new PersonExperiencedLeg(personId, pf.createLeg("pt"))); sf.handleActivity(new PersonExperiencedActivity(personId, pf.createActivityFromCoord("pt_interaction", new Coord(1000, 200)))); Assert.assertEquals(1, rs.tripCounter); sf.handleLeg(new PersonExperiencedLeg(personId, pf.createLeg("transit_walk"))); sf.handleActivity(new PersonExperiencedActivity(personId, pf.createActivityFromCoord("leisure", new Coord(1000, 200)))); Assert.assertEquals(2, rs.tripCounter); Assert.assertEquals(5, rs.lastTrip.getTripElements().size());
Id<Person> agentId = o.getAgentId(); Activity activity = o.getActivity(); ScoringFunction scoringFunction = ScoringFunctionsForPopulation.this.getScoringFunctionForAgent(agentId); if (scoringFunction != null) { scoringFunction.handleActivity(activity); if ( !plan.getPlanElements().isEmpty() ) { if (getStageActivities().isStageActivity( activity.getType() ) ) { final List<Trip> trips = TripStructureUtils.getTrips( plan, getStageActivities());
@Test(expected = RuntimeException.class) public void exceptionInScoringFunctionPropagates() { Config config = ConfigUtils.createConfig(); Scenario scenario = ScenarioUtils.createScenario(config); Id<Person> personId = Id.createPersonId(1); scenario.getPopulation().addPerson(scenario.getPopulation().getFactory().createPerson(personId)); EventsManager events = EventsUtils.createEventsManager(config); ControlerListenerManagerImpl controlerListenerManager = new ControlerListenerManagerImpl(); ScoringFunctionFactory throwingScoringFunctionFactory = new ThrowingScoringFunctionFactory(); EventsToActivities e2acts = new EventsToActivities(controlerListenerManager); EventsToLegs e2legs = new EventsToLegs(scenario.getNetwork()); EventsToLegsAndActivities e2legsActs = new EventsToLegsAndActivities(e2legs, e2acts); events.addHandler(e2legsActs); ScoringFunctionsForPopulation scoringFunctionsForPopulation = new ScoringFunctionsForPopulation( controlerListenerManager, events, e2acts, e2legs, scenario.getPopulation(), throwingScoringFunctionFactory ); controlerListenerManager.fireControlerIterationStartsEvent(0); events.processEvent(new PersonMoneyEvent(3600.0, personId, 3.4)); scoringFunctionsForPopulation.finishScoringFunctions(); }
@Override public void notifyIterationEnds(final IterationEndsEvent event) { this.experiencedPlansService.finishIteration(); // (currently sets scores to experienced plans) if(planCalcScoreConfigGroup.isWriteExperiencedPlans()) { final int writePlansInterval = controlerConfigGroup.getWritePlansInterval(); if (writePlansInterval > 0 && event.getIteration() % writePlansInterval == 0) { this.experiencedPlansService.writeExperiencedPlans(controlerIO.getIterationFilename(event.getIteration(), "experienced_plans.xml.gz")); this.scoringFunctionsForPopulation.writePartialScores(controlerIO.getIterationFilename(event.getIteration(), "experienced_plans_scores.txt.gz")); } } if (planCalcScoreConfigGroup.isMemorizingExperiencedPlans() ) { for ( Person person : this.population.getPersons().values() ) { Plan experiencedPlan = this.experiencedPlansService.getExperiencedPlans().get( person.getId() ) ; if ( experiencedPlan==null ) { throw new RuntimeException("experienced plan is null; I don't think this should happen") ; } Plan selectedPlan = person.getSelectedPlan() ; selectedPlan.getCustomAttributes().put(PlanCalcScoreConfigGroup.EXPERIENCED_PLAN_KEY, experiencedPlan ) ; } } }
ScoringFunctionsForPopulation scoringFunctionsForPopulation = new ScoringFunctionsForPopulation( controlerListenerManager, events, scoringFunctionsForPopulation.finishScoringFunctions(); assertEquals(1.0/6.0 * MAX, scoringFunctionsForPopulation.getScoringFunctionForAgent(personId).getScore(), 1.0);
@Override public final void finishIteration() { // I separated this from "writeExperiencedPlans" so that it can be called separately even when nothing is written. Can't say // if the design might be better served by an iteration ends listener. kai, feb'17 for (Map.Entry<Id<Person>, Plan> entry : this.agentRecords.entrySet()) { Plan plan = entry.getValue(); if (scoringFunctionsForPopulation != null) { plan.setScore(scoringFunctionsForPopulation.getScoringFunctionForAgent(entry.getKey()).getScore()); if (plan.getScore().isNaN()) { log.warn("score is NaN; plan:" + plan.toString()); } } } }
@Override public void notifyScoring(final ScoringEvent event) { scoringFunctionsForPopulation.finishScoringFunctions(); NewScoreAssignerImpl newScoreAssigner = new NewScoreAssignerImpl(this.planCalcScoreConfigGroup, this.controlerConfigGroup); newScoreAssigner.assignNewScores(event.getIteration(), this.scoringFunctionsForPopulation, this.population); }
public void finishScoringFunctions() { // Rethrow an exception in a scoring function (user code) if there was one. Throwable throwable = this.exception.get(); if (throwable != null) { if (throwable instanceof RuntimeException) { throw ((RuntimeException) throwable); } else { throw new RuntimeException(throwable); } } for (ScoringFunction sf : this.agentScorers.values()) { sf.finish(); } for (Entry<Id<Person>, TDoubleCollection> entry : this.partialScores.entrySet()) { entry.getValue().add(this.getScoringFunctionForAgent(entry.getKey()).getScore()); } }
@Override synchronized public void handleLeg(PersonExperiencedLeg o) { Id<Person> agentId = o.getAgentId(); Leg leg = o.getLeg(); ScoringFunction scoringFunction = ScoringFunctionsForPopulation.this.getScoringFunctionForAgent(agentId); if (scoringFunction != null) { scoringFunction.handleLeg(leg); TDoubleCollection partialScoresForAgent = this.partialScores.get(agentId); partialScoresForAgent.add(scoringFunction.getScore()); } Plan plan = this.tripRecords.get( agentId ) ; // as container for trip if ( plan!=null ) { plan.addLeg( leg ); } }
ScoringFunction sf = scoringFunctionsForPopulation.getScoringFunctionForAgent(person.getId()); double score = sf.getScore(); Plan plan = person.getSelectedPlan();
ScoringFunction scoringFunction = getScoringFunctionForAgent(((HasPersonId) o).getPersonId()); if (scoringFunction != null) { if (o instanceof PersonStuckEvent) { Id<Vehicle> vehicleId = ((LinkEnterEvent)o).getVehicleId() ; Id<Person> driverId = this.vehicles2Drivers.getDriverOfVehicle(vehicleId) ; ScoringFunction scoringFunction = getScoringFunctionForAgent( driverId );