matsim-org / matsim-libs

Multi-Agent Transport Simulation
www.matsim.org
484 stars 447 forks source link

Bug report: Carsharing crashes when initiated with an old plan containing carsharing routes #1417

Closed ouassimm closed 3 years ago

ouassimm commented 3 years ago

Carsharing crashes when fed an old carsharing plan with the error: org.matsim.core.population.routes.GenericRouteImpl cannot be cast to class org.matsim.contrib.carsharing.router.CarsharingRoute

Routes in the old plan are read as GenericRoute objects whereas Carsharing requires a CarsharingRoute object. A simple instance check + cast operation can solve this.

ouassimm commented 3 years ago

This fixes the problem. Carsharing tests pass with this fix.

if (legToBeRouted.getRoute() instanceof GenericRouteImpl){
    legToBeRouted.setRoute(new CarsharingRoute(startLink.getId(), destinationLink.getId()));
}
balacmi commented 3 years ago

Hi Ouassim,

Where is this happening?

In the main script the code located here is supposed to do exactly that.

ouassimm commented 3 years ago

First here: https://github.com/matsim-org/matsim-libs/blob/d79b4733452f9584839016bed4e88df0bee8ffa5/contribs/carsharing/src/main/java/org/matsim/contrib/carsharing/manager/CarsharingManagerNew.java#L80

The cast is not allowed

ouassimm commented 3 years ago

You can replicate the error by using your RunCarsharing example. Changing the input plans in the Config file to some old plans instead of the 10persons.xml will produce the mentioned error.

balacmi commented 3 years ago

Hi Ouassim,

Yes, I see that. The code i pointed to is supposed to ensure that one can read in the population with carsharing trips. However the scenario has to be loaded after the carsharing route factory is set. Like this: final Scenario sc = ScenarioUtils.createScenario(config); sc.getPopulation().getFactory().getRouteFactories().setRouteFactory(CarsharingRoute.class, new CarsharingRouteFactory()); ScenarioUtils.loadScenario(sc);

I will make the change. Thanks!

ouassimm commented 3 years ago

Glad to help. Thank you!