MATSIM
RunOwnPrepareForSimExample.java
Go to the documentation of this file.
1 /*
2  * *********************************************************************** *
3  * * project: org.matsim.*
4  * * DefaultControlerModules.java
5  * * *
6  * * *********************************************************************** *
7  * * *
8  * * copyright : (C) 2014 by the members listed in the COPYING, *
9  * * LICENSE and WARRANTY file. *
10  * * email : info at matsim dot org *
11  * * *
12  * * *********************************************************************** *
13  * * *
14  * * This program is free software; you can redistribute it and/or modify *
15  * * it under the terms of the GNU General Public License as published by *
16  * * the Free Software Foundation; either version 2 of the License, or *
17  * * (at your option) any later version. *
18  * * See also COPYING, LICENSE and WARRANTY file *
19  * * *
20  * * ***********************************************************************
21  */
22 package tutorial.programming.ownPrepareForSimExample;
23 
24 import org.matsim.api.core.v01.Coord;
25 import org.matsim.api.core.v01.Id;
26 import org.matsim.api.core.v01.Scenario;
38 import org.matsim.core.config.Config;
46 
56 
57  public static void main(String[] args) {
58 
59  Config config = ConfigUtils.createConfig();
60 
61  config.controler().setOutputDirectory("output/ownPrepareForSimExample/");
62  config.controler().setOverwriteFileSetting( OverwriteFileSetting.deleteDirectoryIfExists );
63 
64  config.controler().setLastIteration(0);
65 
66  ActivityParams dummyAct = new ActivityParams("dummy");
67  dummyAct.setTypicalDuration(12 * 3600);
68  config.planCalcScore().addActivityParams(dummyAct);
69 
70  Scenario scenario = ScenarioUtils.createScenario(config);
71 
72  createNetwork(scenario);
73  createPopulation(scenario);
74 
75  Controler controler = new Controler(scenario);
76 
77  controler.addOverridingModule(new AbstractModule() {
78  @Override
79  public void install() {
80  this.bind(PrepareForSim.class).to(MyPrepareForSim.class);
81  }
82  });
83 
84  controler.run();
85  }
86 
87  private static void createPopulation(Scenario scenario) {
88  Population pop = scenario.getPopulation();
89  PopulationFactory fac = pop.getFactory();
90 
91  Person person = fac.createPerson(Id.createPersonId(1));
92  Plan plan = fac.createPlan();
93  Activity dummyAct1 = fac.createActivityFromCoord("dummy", new Coord(0.0, 0.0));
94  dummyAct1.setEndTime(0.0);
95  plan.addActivity(dummyAct1);
96  Leg leg = fac.createLeg(TransportMode.car);
97  plan.addLeg(leg);
98  Activity dummyAct2 = fac.createActivityFromLinkId("dummy", Id.createLinkId("1_4"));
99  plan.addActivity(dummyAct2);
100  person.addPlan(plan);
101  pop.addPerson(person);
102  }
103 
120  private static void createNetwork(Scenario scenario) {
121  Network network = scenario.getNetwork();
122  NetworkFactory fac = network.getFactory();
123 
124  Node node1 = fac.createNode(Id.createNodeId(1), new Coord(-100.0, 1.0));
125  Node node2 = fac.createNode(Id.createNodeId(2), new Coord(100.0, 1.0));
126  Node node3 = fac.createNode(Id.createNodeId(3), new Coord(0.0, 10.0));
127  Node node4 = fac.createNode(Id.createNodeId(4), new Coord(-10.0, 100.0));
128 
129  network.addNode(node1);
130  network.addNode(node2);
131  network.addNode(node3);
132  network.addNode(node4);
133 
134  Link link12 = fac.createLink(Id.createLinkId("1_2"), node1, node2);
135  Link link21 = fac.createLink(Id.createLinkId("2_1"), node2, node1);
136  Link link34 = fac.createLink(Id.createLinkId("3_4"), node3, node4);
137  Link link43 = fac.createLink(Id.createLinkId("4_3"), node4, node3);
138  // create some links to connect network
139  Link link41 = fac.createLink(Id.createLinkId("4_1"), node4, node1);
140  Link link14 = fac.createLink(Id.createLinkId("1_4"), node1, node4);
141  Link link23 = fac.createLink(Id.createLinkId("2_3"), node2, node3);
142  Link link32 = fac.createLink(Id.createLinkId("3_2"), node3, node2);
143 
144  network.addLink(link12);
145  network.addLink(link21);
146  network.addLink(link34);
147  network.addLink(link43);
148  network.addLink(link41);
149  network.addLink(link14);
150  network.addLink(link23);
151  network.addLink(link32);
152  }
153 
154 }
final void addOverridingModule(AbstractModule abstractModule)
Definition: Controler.java:410
abstract void addLeg(final Leg leg)
Activity createActivityFromLinkId(String actType, Id< Link > linkId)
abstract void addActivity(final Activity act)
Link createLink(final Id< Link > id, final Node fromNode, final Node toNode)
ControlerConfigGroup controler
Definition: Config.java:87
static Config createConfig(final String filename)
Node createNode(final Id< Node > id, final Coord coord)
Activity createActivityFromCoord(String actType, Coord coord)
final PlanCalcScoreConfigGroup planCalcScore()
Definition: Config.java:444
static Scenario createScenario(final Config config)
void setEndTime(final double seconds)