public StrategyManager() { this.delegate = new GenericStrategyManager<>(); }
/** * Adds a strategy to this manager with the specified weight. This weight * compared to the sum of weights of all strategies in this manager defines * the probability this strategy will be used for an agent. * */ public final void addStrategy( final PlanStrategy strategy, final String subpopulation, final double weight) { delegate.addStrategy(strategy, subpopulation, weight); }
/** * Randomly chooses for each person of the population a strategy and uses that * strategy on the person, after adapting the strategies to any pending change * requests for the specified iteration. * * @param iteration the current iteration we're handling */ public final void run( final Iterable<? extends HasPlansAndId<PL, AG>> persons, ObjectAttributes subpopLookup, final int iteration, final ReplanningContext replanningContext ) { handleChangeRequests(iteration); run(persons, subpopLookup, replanningContext); }
travelDisutility, modeTravelTimes.get(TransportMode.car)); final GenericStrategyManager<CarrierPlan, Carrier> strategyManager = new GenericStrategyManager<>(); strategyManager.setMaxPlansPerAgent(5); strategyManager.addStrategy(strategy, null, 1.0); strategy.addStrategyModule(new TimeAllocationMutator()); strategy.addStrategyModule(new ReRouteVehicles(router, network, modeTravelTimes.get(TransportMode.car), 1.)); strategyManager.addStrategy(strategy, null, 0.5);
private static CarrierPlanStrategyManagerFactory createStrategyManagerFactory(final CarrierVehicleTypes types, final MatsimServices controler) { return () -> { final GenericStrategyManager<CarrierPlan, Carrier> strategyManager = new GenericStrategyManager<>(); strategyManager.addStrategy(new GenericPlanStrategyImpl<>(new BestPlanSelector<>()), null, 0.95); strategyManager.addStrategy(new SelectBestPlanAndOptimizeItsVehicleRouteFactory(controler.getScenario().getNetwork(), types, controler.getLinkTravelTimes()).createStrategy(), null, 0.05); return strategyManager; }; }
for (GenericPlanStrategy<PL, AG> strategy : distinctStrategies()) { strategy.init(replanningContext); removePlans( person, this.maxPlansPerAgent); subpopName = (String) subPopLookup.getAttribute(person.getId().toString(), this.subpopulationAttributeName); GenericPlanStrategy<PL, AG> strategy = this.chooseStrategy(person, subpopName); for (GenericPlanStrategy<PL, AG> strategy : distinctStrategies()) { strategy.finish();
@Override public void notifyReplanning(final ReplanningEvent event) { if (carrierPlanStrategyManagerFactory == null) { return; } GenericStrategyManager<CarrierPlan, Carrier> strategyManager = carrierPlanStrategyManagerFactory.createStrategyManager(); Collection<HasPlansAndId<CarrierPlan, Carrier>> collection = new ArrayList<>(); for (Carrier carrier : carriers.getCarriers().values()) { collection.add(carrier); } strategyManager.run(collection, null, event.getIteration(), event.getReplanningContext()); }
/** * Sets the maximal number of plans an agent can memorize. Setting * maxPlansPerAgent to zero means unlimited memory (only limited by RAM). * Agents can have up to maxPlansPerAgent plans plus one additional one with the * currently modified plan they're trying out. * */ public final void setMaxPlansPerAgent(final int maxPlansPerAgent) { delegate.setMaxPlansPerAgent(maxPlansPerAgent); }
/** * changes the weight of the specified strategy * * @return true if the strategy is part of this manager and the weight could * be changed successfully, false otherwise. */ public final boolean changeWeightOfStrategy( final GenericPlanStrategy<Plan, Person> strategy, final String subpopulation, final double newWeight) { return delegate.changeWeightOfStrategy(strategy, subpopulation, newWeight) ; }
/** * Schedules a command for a later iteration. The * change will take place before the strategies are applied. * */ public final void addChangeRequest( final int iteration, final PlanStrategy strategy, final String subpopulation, final double newWeight) { delegate.addChangeRequest(iteration, strategy, subpopulation, newWeight); }
/** * chooses a (weight-influenced) random strategy * * @param person The person for which the strategy should be chosen * @return the chosen strategy */ public GenericPlanStrategy<Plan, Person> chooseStrategy(final Person person, final String subpopulation) { final GenericPlanStrategy<Plan, Person> strategy = delegate.chooseStrategy(person, subpopulation); return strategy; }
public final int getMaxPlansPerAgent() { return delegate.getMaxPlansPerAgent(); }
public final List<GenericPlanStrategy<Plan, Person>> getStrategies(final String subpopulation) { return delegate.getStrategies(subpopulation) ; }
/** * 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); }
/** * modifies the loaded strategies according to pending change requests for this iteration. * */ final void handleChangeRequests(final int iteration) { for ( int ii = 0 ; ii <= iteration ; ii++ ) { // (playing back history for those installations which recreate the strategy manager in every iteration) for ( Map.Entry<String, StrategyWeights<PL, AG>> wentry : weightsPerSubpopulation.entrySet() ) { final String subpop = wentry.getKey(); final StrategyWeights<PL, AG> weights = wentry.getValue(); Map<GenericPlanStrategy<PL, AG>, Double> changes = weights.changeRequests.remove(ii); if (changes != null) { for (Map.Entry<GenericPlanStrategy<PL, AG>, Double> entry : changes.entrySet()) { changeWeightOfStrategy( entry.getKey(), subpop, entry.getValue()); } } } } }