001/* *********************************************************************** * 002 * project: org.matsim.* * 003 * * 004 * *********************************************************************** * 005 * * 006 * copyright : (C) 2008 by the members listed in the COPYING, * 007 * LICENSE and WARRANTY file. * 008 * email : info at matsim dot org * 009 * * 010 * *********************************************************************** * 011 * * 012 * This program is free software; you can redistribute it and/or modify * 013 * it under the terms of the GNU General Public License as published by * 014 * the Free Software Foundation; either version 2 of the License, or * 015 * (at your option) any later version. * 016 * See also COPYING, LICENSE and WARRANTY file * 017 * * 018 * *********************************************************************** */ 019package org.matsim.contrib.roadpricing.run; 020 021import org.matsim.api.core.v01.Scenario; 022import org.matsim.contrib.roadpricing.RoadPricingModule; 023import org.matsim.contrib.roadpricing.RoadPricingUtils; 024import org.matsim.core.config.Config; 025import org.matsim.core.config.ConfigUtils; 026import org.matsim.core.controler.Controler; 027import org.matsim.core.controler.OutputDirectoryHierarchy; 028import org.matsim.core.scenario.ScenarioUtils; 029 030/** 031 * Basic "script" to use roadpricing. 032 * 033 * <br><br> 034 * <b>Note:</b> Do not change the class name as it is referenced in the book. 035 * 036 * @author nagel, jwjoubert 037 */ 038public final class RunRoadPricingExample { 039 private static final String TEST_CONFIG = "./contribs/roadpricing/test/input/org/matsim/contrib/roadpricing/AvoidTolledRouteTest/config.xml"; 040 private final String[] args; 041 private Config config; 042 // to not change class name: referenced from book. kai, dec'14 043 044 /** 045 * Executing the road pricing example. 046 * @param args optional arguments, the first which must be a <code>config.xml</code> 047 * file. If no arguments are passed, the class will use the config, and 048 * associated files from a test instance. 049 */ 050 public static void main(String[] args) { 051 new RunRoadPricingExample(args).run(); 052 } 053 054 public RunRoadPricingExample( String[] args ) { 055 if (args.length > 0) { 056 this.args = args; 057 } else{ 058 /* Use a test case's config file. */ 059 this.args = new String[]{TEST_CONFIG}; 060 } 061 } 062 063 public void run() { 064 if (config == null) { 065 config = prepareConfig(); 066 } 067 068 // load the scenario: 069 Scenario scenario = ScenarioUtils.loadScenario(config); 070 071 // instantiate the controler: 072 Controler controler = new Controler(scenario); 073 074 // use the road pricing module. 075 // (loads the road pricing scheme, uses custom travel disutility including tolls, etc.) 076 // controler.setModules(new ControlerDefaultsWithRoadPricingModule()); 077 controler.addOverridingModule( new RoadPricingModule() ); 078 079 // run the controler: 080 controler.run(); 081 } 082 083 public Config prepareConfig() { 084 // load the config, telling it to "materialize" the road pricing section: 085 config = ConfigUtils.loadConfig(args[0], RoadPricingUtils.createConfigGroup()); 086 config.controler().setOverwriteFileSetting(OutputDirectoryHierarchy.OverwriteFileSetting.deleteDirectoryIfExists); 087 config.controler().setLastIteration(10); 088 return config; 089 } 090 091}