Skip to content

Commit

Permalink
Merge pull request #18 from matsim-vsp/kmt_UpdateOldRunOutputs
Browse files Browse the repository at this point in the history
Script to update old runs with new MATSim events
  • Loading branch information
kt86 authored Feb 21, 2024
2 parents 079a564 + 0ecb25a commit 4781cde
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 3 deletions.
4 changes: 3 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<description>zeroCUTS project</description>

<properties>
<matsim.version>16.0-PR3117</matsim.version>
<matsim.version>16.0-PR3120</matsim.version>
<!-- <matsim.version>16.0-SNAPSHOT</matsim.version>-->

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down Expand Up @@ -87,6 +87,8 @@
</dependency>




<dependency>
<groupId>org.matsim.contrib</groupId>
<artifactId>emissions</artifactId>
Expand Down
129 changes: 129 additions & 0 deletions src/main/java/org/matsim/vsp/freight/food/UpdateRunOutputs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package org.matsim.vsp.freight.food;

import java.io.IOException;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.Scenario;
import org.matsim.core.config.Config;
import org.matsim.core.config.ConfigUtils;
import org.matsim.core.controler.Controler;
import org.matsim.core.controler.OutputDirectoryHierarchy;
import org.matsim.core.scenario.ScenarioUtils;
import org.matsim.freight.carriers.Carrier;
import org.matsim.freight.carriers.CarrierPlan;
import org.matsim.freight.carriers.CarrierPlanWriter;
import org.matsim.freight.carriers.CarriersUtils;
import org.matsim.freight.carriers.FreightCarriersConfigGroup;
import org.matsim.freight.carriers.ScheduledTour;
import org.matsim.freight.carriers.Tour;
import org.matsim.freight.carriers.analysis.RunFreightAnalysisEventBased;
import org.matsim.freight.carriers.controler.CarrierModule;

/**
* Diese Klasse soll den Output von "alten" runs derart updaten,
* dass nun die heutigen Event-based Analysen genutzt werden können.
* D.h. insbesondere, dass die ganzen Carrier-Events nachgeworfen werden.
* @author Kai Martins-Turner (kturner)
*/
public class UpdateRunOutputs {

public static void main(String[] args) throws InterruptedException, ExecutionException {
var listOfRuns = List.of(
"foodRetailing_wo_rangeConstraint/71_ICEVBEV_NwCE_BVWP_10000it_DCoff_noTax/",
"foodRetailing_wo_rangeConstraint/71a_ICEV_NwCE_BVWP_10000it_DCoff_noTax/",
"foodRetailing_wo_rangeConstraint/72_ICEVBEV_NwCE_BVWP_10000it_DCoff_Tax25/",
"foodRetailing_wo_rangeConstraint/73_ICEVBEV_NwCE_BVWP_10000it_DCoff_Tax50/",
"foodRetailing_wo_rangeConstraint/74_ICEVBEV_NwCE_BVWP_10000it_DCoff_Tax100/",
"foodRetailing_wo_rangeConstraint/75_ICEVBEV_NwCE_BVWP_10000it_DCoff_Tax150/",
"foodRetailing_wo_rangeConstraint/76_ICEVBEV_NwCE_BVWP_10000it_DCoff_Tax200/",
"foodRetailing_wo_rangeConstraint/77_ICEVBEV_NwCE_BVWP_10000it_DCoff_Tax250/",
"foodRetailing_wo_rangeConstraint/78_ICEVBEV_NwCE_BVWP_10000it_DCoff_Tax300/",
//nun Runs mit ReichweitenConstraint
"foodRetailing_with_rangeConstraint/21_ICEVBEV_NwCE_BVWP_10000it_DC_noTax/",
"foodRetailing_with_rangeConstraint/22_ICEVBEV_NwCE_BVWP_10000it_DC_Tax25/",
"foodRetailing_with_rangeConstraint/23_ICEVBEV_NwCE_BVWP_10000it_DC_Tax50/",
"foodRetailing_with_rangeConstraint/24_ICEVBEV_NwCE_BVWP_10000it_DC_Tax100/",
"foodRetailing_with_rangeConstraint/25_ICEVBEV_NwCE_BVWP_10000it_DC_Tax150/",
"foodRetailing_with_rangeConstraint/26_ICEVBEV_NwCE_BVWP_10000it_DC_Tax200/",
"foodRetailing_with_rangeConstraint/27_ICEVBEV_NwCE_BVWP_10000it_DC_Tax250/",
"foodRetailing_with_rangeConstraint/28_ICEVBEV_NwCE_BVWP_10000it_DC_Tax300/"
) ;
for (String runDir : listOfRuns) {
run(args, runDir);
}
}
public static void run( String[] args, String runDir )
throws InterruptedException, ExecutionException {

// Path to public repo:
String pathToInput = "https://svn.vsp.tu-berlin.de/repos/public-svn/matsim/scenarios/countries/de/berlin/projects/freight/" + runDir;
// ### config stuff: ###
Config config;
if ( args==null || args.length==0 || args[0]==null ){
config = ConfigUtils.createConfig();
config.network().setInputFile("https://svn.vsp.tu-berlin.de/repos/public-svn/matsim/scenarios/countries/de/berlin/berlin-v5.5-10pct/output-berlinv5.5/berlin-v5.5.3-10pct.output_network.xml.gz");
config.controller().setOutputDirectory( "../shared-svn/projects/freight/studies/UpdateEventsfromEarlierStudies/" + runDir );
config.controller().setLastIteration( 0 ); // no iterations; for iterations see RunFreightWithIterationsExample. kai, jan'23
config.controller().setOverwriteFileSetting(OutputDirectoryHierarchy.OverwriteFileSetting.deleteDirectoryIfExists);
config.global().setCoordinateSystem("EPSG:31468");

FreightCarriersConfigGroup freightConfigGroup = ConfigUtils.addOrGetModule( config, FreightCarriersConfigGroup.class );
freightConfigGroup.setCarriersFile( pathToInput + "output_carriers.xml.gz" );
freightConfigGroup.setCarriersVehicleTypesFile( pathToInput + "output_vehicleTypes.xml.gz" );
} else {
config = ConfigUtils.loadConfig( args, new FreightCarriersConfigGroup() );
}

// load scenario (this is not loading the freight material):
Scenario scenario = ScenarioUtils.loadScenario( config );

//load carriers according to freight config
CarriersUtils.loadCarriersAccordingToFreightConfig( scenario );

// output before jsprit runDir (not necessary)
// new CarrierPlanWriter(CarriersUtils.getCarriers( scenario )).write( "output/unmodifiedCarriers.xml" ) ;
// (this will go into the standard "output" directory. note that this may be removed if this is also used as the configured output dir.)

for (Carrier carrier : CarriersUtils.addOrGetCarriers(scenario).getCarriers().values()) {
final CarrierPlan selectedPlan = carrier.getSelectedPlan();

//Add meaningful TourIds.
int tourIdIndex = 1;
Collection<ScheduledTour> updatedScheduledTours = new LinkedList<>();
for (ScheduledTour scheduledTour : selectedPlan.getScheduledTours().stream().toList()) {
updatedScheduledTours.add(ScheduledTour.newInstance(
scheduledTour.getTour().duplicateWithNewId(Id.create(tourIdIndex, Tour.class)),
scheduledTour.getVehicle(),
scheduledTour.getDeparture() ));
tourIdIndex++;
}
selectedPlan.getScheduledTours().clear();
selectedPlan.getScheduledTours().addAll(updatedScheduledTours);

//put score into JspritScore and Set MATSimscore to -INF.
selectedPlan.setJspritScore(selectedPlan.getScore());
selectedPlan.setScore(Double.NEGATIVE_INFINITY);
}

// new CarrierPlanWriter(CarriersUtils.getCarriers( scenario )).write( "output/updatedCarriersWithTourIds.xml" );

// ## MATSim configuration: ##
final Controler controler = new Controler( scenario ) ;
controler.addOverridingModule(new CarrierModule() );


// ## Start of the MATSim-Run: ##
controler.run();

var analysis = new RunFreightAnalysisEventBased(config.controller().getOutputDirectory() , config.controller().getOutputDirectory()+"Analysis", "EPSG:31468");
try {
analysis.runAnalysis();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ public class FreightAnalyseKT {
// private static final String RUN_DIR = "../shared-svn/projects/freight/studies/WP51_EmissionsFood/output/20200611_fa8d691/51a_ICEV_NwCE_BVWP_2000it_DCoff_noTax/" ;

// private static final String RUN_DIR = "../shared-svn/projects/freight/studies/WP51_EmissionsFood/output/20200611_fa8d691/21a_ICEV_NwCE_BVWP_10000it_DC_noTax/" ;
private static final String RUN_DIR = "../shared-svn/projects/freight/studies/WP51_EmissionsFood/output/20200611_fa8d691/10000it/71a_ICEV_NwCE_BVWP_10000it_DCoff_noTax/" ;
// private static final String RUN_DIR = "../shared-svn/projects/freight/studies/WP51_EmissionsFood/output/20200611_fa8d691/10000it/71a_ICEV_NwCE_BVWP_10000it_DCoff_noTax/" ;

private static final String RUN_DIR = "./output/freightUpdate71-2/" ;


// private static final String RUN_DIR = "../shared-svn/projects/freight/studies/WP51_EmissionsFood/output/Demo1ItDC/" ;
Expand Down Expand Up @@ -109,7 +111,7 @@ private void run() {
// File populationFile = new File(RUN_DIR + "output_plans.xml.gz");
File networkFile = new File(RUN_DIR+ "output_network.xml.gz");
File carrierFile = new File(RUN_DIR+ "output_carriers.xml.gz");
File vehicleTypeFile = new File(RUN_DIR+ "output_vehicleTypes.xml.gz");
File vehicleTypeFile = new File(RUN_DIR+ "output_carriersVehicleTypes.xml.gz");

Network network = NetworkUtils.readNetwork(networkFile.getAbsolutePath());

Expand Down

0 comments on commit 4781cde

Please sign in to comment.