Skip to content

Commit

Permalink
Merge pull request #117 from moia-oss/moia-deployment-fix
Browse files Browse the repository at this point in the history
Moia deployment fix
  • Loading branch information
mfrawley-moia authored Sep 4, 2024
2 parents 815d3bd + fb9d02c commit dac8f06
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 15 deletions.
2 changes: 1 addition & 1 deletion contribs/application/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</repositories>

<properties>
<avro.version>1.11.3</avro.version>
<avro.version>1.12.0</avro.version>
</properties>

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,10 @@ protected void simStep(double now) {
@Override
public void notifyPassengersAreReadyForDeparture(List<MobsimPassengerAgent> passengers, double now) {
var request = getRequestForPassengers(passengers.stream().map(Identifiable::getId).toList());
queuePickup(request, now);
expectedPickups.remove(request.getId());
if(expectedPickups.containsKey(request.getId())) {
queuePickup(request, now);
expectedPickups.remove(request.getId());
}
}

private AcceptedDrtRequest getRequestForPassengers(List<Id<Person>> passengerIds) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ void handleAnEvent(Event event){
case LinkEnterEvent linkEnterEvent -> handleEvent(linkEnterEvent);
case ActivityEndEvent activityEndEvent -> handleEvent(activityEndEvent);
case ActivityStartEvent activityStartEvent -> handleEvent(activityStartEvent);
case VehicleEntersTrafficEvent vehicleEntersTrafficEvent -> handleEvent(vehicleEntersTrafficEvent);
case VehicleLeavesTrafficEvent vehicleLeavesTrafficEvent -> handleEvent(vehicleLeavesTrafficEvent);
case PersonMoneyEvent personMoneyEvent -> handleEvent( personMoneyEvent );
case PersonScoreEvent personScoreEvent -> handleEvent( personScoreEvent );
case null, default -> createAdditionalEvents(event, null, scheduledTour, driverId, planElementCounter);
}
}
Expand Down Expand Up @@ -145,6 +149,33 @@ private void handleEvent( LinkEnterEvent event ){
createAdditionalEvents( event, null, scheduledTour, driverId, planElementCounter );
}

private void handleEvent( PersonScoreEvent event ){
if( scoringFunction != null ){
scoringFunction.handleEvent( event );
}
}

private void handleEvent( PersonMoneyEvent event ){
if( scoringFunction != null ){
scoringFunction.handleEvent( event );
}
}

private void handleEvent( VehicleEntersTrafficEvent event ){
if( scoringFunction != null ){
scoringFunction.handleEvent( event );
}
if ((Event) event instanceof VehicleEntersTrafficEvent vehicleEntersTrafficEvent) {
driver2EventHandler.handleEvent(vehicleEntersTrafficEvent);
}
}

private void handleEvent( VehicleLeavesTrafficEvent event ){
if( scoringFunction != null ){
scoringFunction.handleEvent( event );
}
}

private void handleEvent( ActivityEndEvent event ){
if( currentActivity == null ){
Activity firstActivity = PopulationUtils.createActivityFromLinkId( event.getActType(), event.getLinkId() );
Expand Down Expand Up @@ -196,7 +227,6 @@ private void createAdditionalEvents( Event event, Activity activity, ScheduledTo
// if( scoringFunction == null ){
// (means "called from LSP". kai, jul'22)

driver2EventHandler.handleAnEvent(event);
Id<Vehicle> vehicleId = driver2EventHandler.getVehicleOfDriver(driverId);

// Reason why this here is needed is that the more informative objects such as ScheduledTour cannot be
Expand Down Expand Up @@ -271,15 +301,6 @@ public Id<Vehicle> getVehicleOfDriver(Id<Person> personId){
return driversVehicles.get(personId);
}

public void handleAnEvent(Event event){
if (event instanceof VehicleEntersTrafficEvent vehicleEntersTrafficEvent) {
driver2EventHandler.handleEvent(vehicleEntersTrafficEvent);
}
if (event instanceof VehicleEntersTrafficEvent vehicleEntersTrafficEvent) {
driver2EventHandler.handleEvent(vehicleEntersTrafficEvent);
}
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ final class PlansScoringImpl implements PlansScoring, ScoringListener, Iteration
@Inject private OutputDirectoryHierarchy controlerIO;
@Inject private ScoringFunctionsForPopulation scoringFunctionsForPopulation;
@Inject private ExperiencedPlansService experiencedPlansService;
@Inject private NewScoreAssigner newScoreAssigner;

@Override
public void notifyScoring(final ScoringEvent event) {
scoringFunctionsForPopulation.finishScoringFunctions();
NewScoreAssignerImpl newScoreAssigner = new NewScoreAssignerImpl(this.scoringConfigGroup, this.controllerConfigGroup);
newScoreAssigner.assignNewScores(event.getIteration(), this.scoringFunctionsForPopulation, this.population);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ public void install() {
bind(ScoringFunctionsForPopulation.class).asEagerSingleton();
bind(PlansScoring.class).to(PlansScoringImpl.class);
bind(ExperiencedPlansService.class).to(ExperiencedPlansServiceImpl.class).asEagerSingleton();
bind(NewScoreAssigner.class).to(NewScoreAssignerImpl.class).asEagerSingleton();
}
}
135 changes: 135 additions & 0 deletions matsim/src/test/java/org/matsim/core/scoring/EventsToScoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,39 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.matsim.api.core.v01.Coord;
import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.Scenario;
import org.matsim.api.core.v01.events.PersonDepartureEvent;
import org.matsim.api.core.v01.events.PersonMoneyEvent;
import org.matsim.api.core.v01.events.handler.PersonDepartureEventHandler;
import org.matsim.api.core.v01.network.Link;
import org.matsim.api.core.v01.network.Network;
import org.matsim.api.core.v01.network.Node;
import org.matsim.api.core.v01.population.Person;
import org.matsim.api.core.v01.population.Plan;
import org.matsim.api.core.v01.population.Population;
import org.matsim.core.api.experimental.events.EventsManager;
import org.matsim.core.config.Config;
import org.matsim.core.config.ConfigUtils;
import org.matsim.core.config.groups.ReplanningConfigGroup;
import org.matsim.core.config.groups.RoutingConfigGroup;
import org.matsim.core.config.groups.ScoringConfigGroup;
import org.matsim.core.controler.AbstractModule;
import org.matsim.core.controler.Controler;
import org.matsim.core.controler.OutputDirectoryHierarchy;
import org.matsim.core.controler.events.IterationEndsEvent;
import org.matsim.core.controler.events.IterationStartsEvent;
import org.matsim.core.controler.listener.IterationEndsListener;
import org.matsim.core.controler.listener.IterationStartsListener;
import org.matsim.core.events.EventsUtils;
import org.matsim.core.mobsim.framework.events.MobsimBeforeCleanupEvent;
import org.matsim.core.mobsim.framework.events.MobsimInitializedEvent;
import org.matsim.core.mobsim.framework.listeners.MobsimBeforeCleanupListener;
import org.matsim.core.mobsim.framework.listeners.MobsimInitializedListener;
import org.matsim.core.mobsim.qsim.AbstractQSimModule;
import org.matsim.core.population.PopulationUtils;
import org.matsim.core.replanning.strategies.DefaultPlanStrategiesModule;
import org.matsim.core.scenario.MutableScenario;
import org.matsim.core.scenario.ScenarioUtils;
import org.matsim.core.scoring.functions.CharyparNagelScoringFunctionFactory;
Expand Down Expand Up @@ -158,6 +180,119 @@ void testMsaAveraging() {
}
}

@Test
void testMsaAveragingWithController() {
Config config = ConfigUtils.createConfig() ;
config.controller().setOverwriteFileSetting(OutputDirectoryHierarchy.OverwriteFileSetting.overwriteExistingFiles);

config.controller().setFirstIteration(10);
config.controller().setLastIteration(110);

config.scoring().setMarginalUtilityOfMoney(1.);
config.scoring().setFractionOfIterationsToStartScoreMSA(0.9);

ReplanningConfigGroup.StrategySettings strat = new ReplanningConfigGroup.StrategySettings();
strat.setStrategyName(DefaultPlanStrategiesModule.DefaultSelector.KeepLastSelected);
strat.setWeight(1.);
config.replanning().addStrategySettings(strat);

config.routing().setNetworkRouteConsistencyCheck(RoutingConfigGroup.NetworkRouteConsistencyCheck.disable);

Scenario scenario = ScenarioUtils.createScenario(config);

Network network = scenario.getNetwork();
Node node0 = network.getFactory().createNode(Id.createNodeId(0), new Coord(0, 0));
Node node1 = network.getFactory().createNode(Id.createNodeId(1), new Coord(1, 0));
network.addNode(node0);
network.addNode(node1);
Link link01 = network.getFactory().createLink(Id.createLinkId("0_1"), node0, node1);
network.addLink(link01);

Population population = scenario.getPopulation();
Person person = PopulationUtils.getFactory().createPerson(Id.create(1, Person.class));
population.addPerson(person);
Plan plan = PopulationUtils.createPlan() ;
plan.addActivity(population.getFactory().createActivityFromLinkId("dummy", link01.getId()));
person.addPlan(plan);

ScoringConfigGroup.ActivityParams dummyAct = new ScoringConfigGroup.ActivityParams("dummy");
dummyAct.setScoringThisActivityAtAll(false);
config.scoring().addActivityParams(dummyAct);

Controler controler = new Controler(scenario);
EventsManager events = controler.getEvents();
controler.addOverridingModule(new AbstractModule() {
@Override
public void install() {
final int[] iteration = new int[1];
this.addControlerListenerBinding().toInstance(new IterationStartsListener() {
@Override
public void notifyIterationStarts(IterationStartsEvent event) {
iteration[0] = event.getIteration();
}
});
this.addMobsimListenerBinding().toInstance(new MobsimInitializedListener() {
@Override
public void notifyMobsimInitialized(MobsimInitializedEvent e) {
events.processEvent(new PersonMoneyEvent(3600.0, person.getId(), iteration[0] -98, "bribe", "contractor" ));
}
});
}
});
controler.addControlerListener(new IterationEndsListener() {
@Override
public void notifyIterationEnds(IterationEndsEvent event) {
System.out.println( "score: " + person.getSelectedPlan().getScore() ) ;

switch(event.getIteration()){
case 99:
Assertions.assertEquals(1.0, person.getSelectedPlan().getScore(), 0);
break ;
case 100:
// first MSA iteration; plain score should be ok:
Assertions.assertEquals(2.0, person.getSelectedPlan().getScore(), 0);
break ;
case 101:
// second MSA iteration
// (2+3)/2 = 2.5
Assertions.assertEquals(2.5, person.getSelectedPlan().getScore(), 0);
break ;
case 102:
// 3rd MSA iteration
// (2+3+4)/3 = 3
Assertions.assertEquals(3.0, person.getSelectedPlan().getScore(), 0);
break ;
case 103:
// (2+3+4+5)/4 = 3.5
Assertions.assertEquals(3.5, person.getSelectedPlan().getScore(), 0);
break ;
case 104:
Assertions.assertEquals(4.0, person.getSelectedPlan().getScore(), 0);
break ;
case 105:
Assertions.assertEquals(4.5, person.getSelectedPlan().getScore(), 0);
break ;
case 106:
Assertions.assertEquals(5.0, person.getSelectedPlan().getScore(), 0);
break ;
case 107:
Assertions.assertEquals(5.5, person.getSelectedPlan().getScore(), 0);
break ;
case 108:
Assertions.assertEquals(6.0, person.getSelectedPlan().getScore(), 0);
break ;
case 109:
Assertions.assertEquals(6.5, person.getSelectedPlan().getScore(), 0);
break ;
case 110:
Assertions.assertEquals(7.0, person.getSelectedPlan().getScore(), 0);
break ;
}
}
});
controler.run();
}

private static class MockScoringFunctionFactory implements ScoringFunctionFactory {

@Override
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@
<dependency>
<groupId>it.unimi.dsi</groupId>
<artifactId>fastutil</artifactId>
<version>8.5.13</version>
<version>8.5.14</version>
</dependency>

<dependency>
Expand Down

0 comments on commit dac8f06

Please sign in to comment.