Skip to content

Commit

Permalink
some package reorganisation; some reformatting
Browse files Browse the repository at this point in the history
  • Loading branch information
kainagel committed Jan 10, 2024
1 parent 400cfd3 commit e239202
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,19 @@
/**
*
*/
package org.matsim.contrib.decongestion;
package org.matsim.contrib.decongestion.run;


import java.io.IOException;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.matsim.api.core.v01.Scenario;
import org.matsim.api.core.v01.TransportMode;
import org.matsim.contrib.decongestion.DecongestionConfigGroup;
import org.matsim.contrib.decongestion.DecongestionConfigGroup.DecongestionApproach;
import org.matsim.contrib.decongestion.DecongestionConfigGroup.IntegralApproach;
import org.matsim.contrib.decongestion.DecongestionModule;
import org.matsim.contrib.decongestion.routing.TollTimeDistanceTravelDisutilityFactory;
import org.matsim.core.config.Config;
import org.matsim.core.config.ConfigUtils;
Expand Down Expand Up @@ -70,7 +73,10 @@ public static void main(String[] args) throws IOException {

private void run() {

final DecongestionConfigGroup decongestionSettings = new DecongestionConfigGroup();
Config config = ConfigUtils.loadConfig(configFile);

final DecongestionConfigGroup decongestionSettings = ConfigUtils.addOrGetModule( config, DecongestionConfigGroup.class );

decongestionSettings.setToleratedAverageDelaySec(30.);
decongestionSettings.setFractionOfIterationsToEndPriceAdjustment(1.0);
decongestionSettings.setFractionOfIterationsToStartPriceAdjustment(0.0);
Expand All @@ -92,32 +98,29 @@ private void run() {
// decongestionSettings.setTOLL_ADJUSTMENT(1.0);
// decongestionSettings.setINITIAL_TOLL(1.0);

Config config = ConfigUtils.loadConfig(configFile);
config.addModule(decongestionSettings);

// ---

final Scenario scenario = ScenarioUtils.loadScenario(config);
Controler controler = new Controler(scenario);

// #############################################################
// ---

// congestion toll computation
Controler controler = new Controler(scenario);

controler.addOverridingModule(new DecongestionModule(scenario));
// congestion toll computation
controler.addOverridingModule(new DecongestionModule(scenario) );

// toll-adjusted routing

controler.addOverridingModule(new AbstractModule(){
@Override
public void install() {
final TollTimeDistanceTravelDisutilityFactory travelDisutilityFactory = new TollTimeDistanceTravelDisutilityFactory();
this.bindCarTravelDisutilityFactory().toInstance( travelDisutilityFactory );
addTravelDisutilityFactoryBinding( TransportMode.car ).toInstance( new TollTimeDistanceTravelDisutilityFactory() );
// yyyy try if this could add the class instead of the instance. possibly as singleton. kai, jan'24

}
});

// #############################################################

controler.getConfig().controller().setOverwriteFileSetting(OutputDirectoryHierarchy.OverwriteFileSetting.failIfDirectoryExists);
controler.run();
controler.run();

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@
/**
*
*/
package org.matsim.contrib.decongestion;
package org.matsim.contrib.decongestion.run;


import java.io.IOException;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.matsim.api.core.v01.Scenario;
import org.matsim.api.core.v01.TransportMode;
import org.matsim.contrib.decongestion.DecongestionConfigGroup;
import org.matsim.contrib.decongestion.DecongestionModule;
import org.matsim.contrib.decongestion.routing.TollTimeDistanceTravelDisutilityFactory;
import org.matsim.core.config.Config;
import org.matsim.core.config.ConfigUtils;
Expand Down Expand Up @@ -64,9 +67,9 @@ public static void main(String[] args) throws IOException {
main.run();
}

private void run() throws IOException {
private void run() {

Config config = ConfigUtils.loadConfig(configFile, new DecongestionConfigGroup());
Config config = ConfigUtils.loadConfig(configFile, new DecongestionConfigGroup() );

final Scenario scenario = ScenarioUtils.loadScenario(config);
Controler controler = new Controler(scenario);
Expand All @@ -75,23 +78,20 @@ private void run() throws IOException {

// congestion toll computation

controler.addOverridingModule(new DecongestionModule(scenario));
controler.addOverridingModule(new DecongestionModule(scenario) );

// toll-adjusted routing

final TollTimeDistanceTravelDisutilityFactory travelDisutilityFactory = new TollTimeDistanceTravelDisutilityFactory();

controler.addOverridingModule(new AbstractModule(){
controler.addOverridingModule(new AbstractModule(){
@Override
public void install() {
this.bindCarTravelDisutilityFactory().toInstance( travelDisutilityFactory );
addTravelDisutilityFactoryBinding( TransportMode.car ).toInstance( new TollTimeDistanceTravelDisutilityFactory() );
// yyyy try if this could add the class instead of the instance. possibly as singleton. kai, jan'24

}
});

// #############################################################

controler.getConfig().controller().setOverwriteFileSetting(OutputDirectoryHierarchy.OverwriteFileSetting.failIfDirectoryExists);
controler.run();
controler.run();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,19 @@ public void install() {

final TollTimeDistanceTravelDisutilityFactory travelDisutilityFactory = new TollTimeDistanceTravelDisutilityFactory();

controler.addOverridingModule(new AbstractModule(){
controler.addOverridingModule(new AbstractModule(){
@Override
public void install() {
this.bindCarTravelDisutilityFactory().toInstance( travelDisutilityFactory );
}
});

controler.getConfig().controller().setOverwriteFileSetting(OutputDirectoryHierarchy.OverwriteFileSetting.deleteDirectoryIfExists);
controler.run();
controler.run();

double tt0 = controler.getLinkTravelTimes().getLinkTravelTime(scenario.getNetwork().getLinks().get(Id.createLinkId("link12")), 6 * 3600 + 50. * 60, null, null);
double tt1 = controler.getLinkTravelTimes().getLinkTravelTime(scenario.getNetwork().getLinks().get(Id.createLinkId("link12")), 7 * 3600 + 63, null, null);
double tt2 = controler.getLinkTravelTimes().getLinkTravelTime(scenario.getNetwork().getLinks().get(Id.createLinkId("link12")), 7 * 3600 + 15. * 60, null, null);
double tt0 = controler.getLinkTravelTimes().getLinkTravelTime(scenario.getNetwork().getLinks().get(Id.createLinkId("link12")), 6 * 3600 + 50. * 60, null, null);
double tt1 = controler.getLinkTravelTimes().getLinkTravelTime(scenario.getNetwork().getLinks().get(Id.createLinkId("link12")), 7 * 3600 + 63, null, null);
double tt2 = controler.getLinkTravelTimes().getLinkTravelTime(scenario.getNetwork().getLinks().get(Id.createLinkId("link12")), 7 * 3600 + 15. * 60, null, null);

Assertions.assertEquals(100.0, tt0, MatsimTestUtils.EPSILON, "Wrong travel time. The run output seems to have changed.");
Assertions.assertEquals(150.5, tt1, MatsimTestUtils.EPSILON, "Wrong travel time. The run output seems to have changed.");
Expand All @@ -138,8 +138,8 @@ public void install() {
double avgScore = controler.getScoreStats().getScoreHistory().get( ScoreItem.executed ).get(index);
Assertions.assertEquals(-33.940316666666666, avgScore, MatsimTestUtils.EPSILON, "Wrong average executed score. The tolls seem to have changed.");

System.out.println(info.getlinkInfos().get(Id.createLinkId("link12")).getTime2toll().toString());
System.out.println(info.getlinkInfos().get(Id.createLinkId("link12")).getTime2avgDelay().toString());
System.out.println(info.getlinkInfos().get(Id.createLinkId("link12")).getTime2toll().toString());
System.out.println(info.getlinkInfos().get(Id.createLinkId("link12")).getTime2avgDelay().toString());

Assertions.assertEquals(50.5, info.getlinkInfos().get(Id.createLinkId("link12")).getTime2avgDelay().get(84), MatsimTestUtils.EPSILON, "Wrong average delay (capacity is set in a way that one of the two agents has to wait 101 sec. Thus the average is 50.5");
Assertions.assertEquals(50.5 * 0.0123, info.getlinkInfos().get(Id.createLinkId("link12")).getTime2toll().get(84), MatsimTestUtils.EPSILON, "Wrong toll.");
Expand Down Expand Up @@ -183,19 +183,19 @@ final void test0amodified() {

final TollTimeDistanceTravelDisutilityFactory travelDisutilityFactory = new TollTimeDistanceTravelDisutilityFactory();

controler.addOverridingModule(new AbstractModule(){
controler.addOverridingModule(new AbstractModule(){
@Override
public void install() {
this.bindCarTravelDisutilityFactory().toInstance( travelDisutilityFactory );
}
});

controler.getConfig().controller().setOverwriteFileSetting(OutputDirectoryHierarchy.OverwriteFileSetting.deleteDirectoryIfExists);
controler.run();
controler.run();

double tt0 = controler.getLinkTravelTimes().getLinkTravelTime(scenario.getNetwork().getLinks().get(Id.createLinkId("link12")), 6 * 3600 + 50. * 60, null, null);
double tt1 = controler.getLinkTravelTimes().getLinkTravelTime(scenario.getNetwork().getLinks().get(Id.createLinkId("link12")), 7 * 3600 + 63, null, null);
double tt2 = controler.getLinkTravelTimes().getLinkTravelTime(scenario.getNetwork().getLinks().get(Id.createLinkId("link12")), 7 * 3600 + 15. * 60, null, null);
double tt0 = controler.getLinkTravelTimes().getLinkTravelTime(scenario.getNetwork().getLinks().get(Id.createLinkId("link12")), 6 * 3600 + 50. * 60, null, null);
double tt1 = controler.getLinkTravelTimes().getLinkTravelTime(scenario.getNetwork().getLinks().get(Id.createLinkId("link12")), 7 * 3600 + 63, null, null);
double tt2 = controler.getLinkTravelTimes().getLinkTravelTime(scenario.getNetwork().getLinks().get(Id.createLinkId("link12")), 7 * 3600 + 15. * 60, null, null);

Assertions.assertEquals(100.0, tt0, MatsimTestUtils.EPSILON, "Wrong travel time. The run output seems to have changed.");
Assertions.assertEquals(150.5, tt1, MatsimTestUtils.EPSILON, "Wrong travel time. The run output seems to have changed.");
Expand Down Expand Up @@ -265,19 +265,19 @@ public void install() {

final TollTimeDistanceTravelDisutilityFactory travelDisutilityFactory = new TollTimeDistanceTravelDisutilityFactory();

controler.addOverridingModule(new AbstractModule(){
controler.addOverridingModule(new AbstractModule(){
@Override
public void install() {
this.bindCarTravelDisutilityFactory().toInstance( travelDisutilityFactory );
}
});

controler.getConfig().controller().setOverwriteFileSetting(OutputDirectoryHierarchy.OverwriteFileSetting.deleteDirectoryIfExists);
controler.run();
controler.run();

double tt0 = controler.getLinkTravelTimes().getLinkTravelTime(scenario.getNetwork().getLinks().get(Id.createLinkId("link12")), 6 * 3600 + 50. * 60, null, null);
double tt1 = controler.getLinkTravelTimes().getLinkTravelTime(scenario.getNetwork().getLinks().get(Id.createLinkId("link12")), 7 * 3600 + 63, null, null);
double tt2 = controler.getLinkTravelTimes().getLinkTravelTime(scenario.getNetwork().getLinks().get(Id.createLinkId("link12")), 7 * 3600 + 15. * 60, null, null);
double tt0 = controler.getLinkTravelTimes().getLinkTravelTime(scenario.getNetwork().getLinks().get(Id.createLinkId("link12")), 6 * 3600 + 50. * 60, null, null);
double tt1 = controler.getLinkTravelTimes().getLinkTravelTime(scenario.getNetwork().getLinks().get(Id.createLinkId("link12")), 7 * 3600 + 63, null, null);
double tt2 = controler.getLinkTravelTimes().getLinkTravelTime(scenario.getNetwork().getLinks().get(Id.createLinkId("link12")), 7 * 3600 + 15. * 60, null, null);

Assertions.assertEquals(100.0, tt0, MatsimTestUtils.EPSILON, "Wrong travel time. The run output seems to have changed.");
Assertions.assertEquals(150.5, tt1, MatsimTestUtils.EPSILON, "Wrong travel time. The run output seems to have changed.");
Expand All @@ -287,8 +287,8 @@ public void install() {
double avgScore = controler.getScoreStats().getScoreHistory().get( ScoreItem.executed ).get(index);
Assertions.assertEquals(-134.31916666666666, avgScore, MatsimTestUtils.EPSILON, "Wrong average executed score. The tolls seem to have changed.");

System.out.println(info.getlinkInfos().get(Id.createLinkId("link12")).getTime2toll().toString());
System.out.println(info.getlinkInfos().get(Id.createLinkId("link12")).getTime2avgDelay().toString());
System.out.println(info.getlinkInfos().get(Id.createLinkId("link12")).getTime2toll().toString());
System.out.println(info.getlinkInfos().get(Id.createLinkId("link12")).getTime2avgDelay().toString());

Assertions.assertEquals(50.5, info.getlinkInfos().get(Id.createLinkId("link12")).getTime2avgDelay().get(84), MatsimTestUtils.EPSILON, "Wrong average delay (capacity is set in a way that one of the two agents has to wait 101 sec. Thus the average is 50.5");
Assertions.assertEquals(50.5 * 2, info.getlinkInfos().get(Id.createLinkId("link12")).getTime2toll().get(84), MatsimTestUtils.EPSILON, "Wrong toll.");
Expand Down Expand Up @@ -331,19 +331,19 @@ final void test0bmodified() {

final TollTimeDistanceTravelDisutilityFactory travelDisutilityFactory = new TollTimeDistanceTravelDisutilityFactory();

controler.addOverridingModule(new AbstractModule(){
controler.addOverridingModule(new AbstractModule(){
@Override
public void install() {
this.bindCarTravelDisutilityFactory().toInstance( travelDisutilityFactory );
}
});

controler.getConfig().controller().setOverwriteFileSetting(OutputDirectoryHierarchy.OverwriteFileSetting.deleteDirectoryIfExists);
controler.run();
controler.run();

double tt0 = controler.getLinkTravelTimes().getLinkTravelTime(scenario.getNetwork().getLinks().get(Id.createLinkId("link12")), 6 * 3600 + 50. * 60, null, null);
double tt1 = controler.getLinkTravelTimes().getLinkTravelTime(scenario.getNetwork().getLinks().get(Id.createLinkId("link12")), 7 * 3600 + 63, null, null);
double tt2 = controler.getLinkTravelTimes().getLinkTravelTime(scenario.getNetwork().getLinks().get(Id.createLinkId("link12")), 7 * 3600 + 15. * 60, null, null);
double tt0 = controler.getLinkTravelTimes().getLinkTravelTime(scenario.getNetwork().getLinks().get(Id.createLinkId("link12")), 6 * 3600 + 50. * 60, null, null);
double tt1 = controler.getLinkTravelTimes().getLinkTravelTime(scenario.getNetwork().getLinks().get(Id.createLinkId("link12")), 7 * 3600 + 63, null, null);
double tt2 = controler.getLinkTravelTimes().getLinkTravelTime(scenario.getNetwork().getLinks().get(Id.createLinkId("link12")), 7 * 3600 + 15. * 60, null, null);

Assertions.assertEquals(100.0, tt0, MatsimTestUtils.EPSILON, "Wrong travel time. The run output seems to have changed.");
Assertions.assertEquals(150.5, tt1, MatsimTestUtils.EPSILON, "Wrong travel time. The run output seems to have changed.");
Expand Down Expand Up @@ -405,19 +405,19 @@ public void install() {

final TollTimeDistanceTravelDisutilityFactory travelDisutilityFactory = new TollTimeDistanceTravelDisutilityFactory();

controler.addOverridingModule(new AbstractModule(){
controler.addOverridingModule(new AbstractModule(){
@Override
public void install() {
this.bindCarTravelDisutilityFactory().toInstance( travelDisutilityFactory );
}
});

controler.getConfig().controller().setOverwriteFileSetting(OutputDirectoryHierarchy.OverwriteFileSetting.deleteDirectoryIfExists);
controler.run();
controler.run();

double tt0 = controler.getLinkTravelTimes().getLinkTravelTime(scenario.getNetwork().getLinks().get(Id.createLinkId("link12")), 6 * 3600 + 50. * 60, null, null);
double tt1 = controler.getLinkTravelTimes().getLinkTravelTime(scenario.getNetwork().getLinks().get(Id.createLinkId("link12")), 7 * 3600 + 63, null, null);
double tt2 = controler.getLinkTravelTimes().getLinkTravelTime(scenario.getNetwork().getLinks().get(Id.createLinkId("link12")), 7 * 3600 + 15. * 60, null, null);
double tt0 = controler.getLinkTravelTimes().getLinkTravelTime(scenario.getNetwork().getLinks().get(Id.createLinkId("link12")), 6 * 3600 + 50. * 60, null, null);
double tt1 = controler.getLinkTravelTimes().getLinkTravelTime(scenario.getNetwork().getLinks().get(Id.createLinkId("link12")), 7 * 3600 + 63, null, null);
double tt2 = controler.getLinkTravelTimes().getLinkTravelTime(scenario.getNetwork().getLinks().get(Id.createLinkId("link12")), 7 * 3600 + 15. * 60, null, null);

Assertions.assertEquals(100.0, tt0, MatsimTestUtils.EPSILON, "Wrong travel time. The run output seems to have changed.");
Assertions.assertEquals(150.5, tt1, MatsimTestUtils.EPSILON, "Wrong travel time. The run output seems to have changed.");
Expand All @@ -427,8 +427,8 @@ public void install() {
double avgScore = controler.getScoreStats().getScoreHistory().get( ScoreItem.executed ).get(index);
Assertions.assertEquals(-33.31916666666666, avgScore, MatsimTestUtils.EPSILON, "Wrong average executed score. The tolls seem to have changed.");

System.out.println(info.getlinkInfos().get(Id.createLinkId("link12")).getTime2toll().toString());
System.out.println(info.getlinkInfos().get(Id.createLinkId("link12")).getTime2avgDelay().toString());
System.out.println(info.getlinkInfos().get(Id.createLinkId("link12")).getTime2toll().toString());
System.out.println(info.getlinkInfos().get(Id.createLinkId("link12")).getTime2avgDelay().toString());

Assertions.assertEquals(50.5, info.getlinkInfos().get(Id.createLinkId("link12")).getTime2avgDelay().get(84), MatsimTestUtils.EPSILON, "Wrong average delay (capacity is set in a way that one of the two agents has to wait 101 sec. Thus the average is 50.5");
Assertions.assertNull(info.getlinkInfos().get(Id.createLinkId("link12")).getTime2toll().get(84), "Wrong toll.");
Expand Down Expand Up @@ -545,8 +545,8 @@ public void install() {
}
});

controler.getConfig().controller().setOverwriteFileSetting(OutputDirectoryHierarchy.OverwriteFileSetting.deleteDirectoryIfExists);
controler.run();
controler.getConfig().controller().setOverwriteFileSetting(OutputDirectoryHierarchy.OverwriteFileSetting.deleteDirectoryIfExists);
controler.run();


final int index = config.controller().getLastIteration() - config.controller().getFirstIteration();
Expand Down

0 comments on commit e239202

Please sign in to comment.