SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RORDGenerator_ODAmounts.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // Class for loading trip amount definitions and route generation
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
12 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <cassert>
34 #include <string>
35 #include <algorithm>
40 #include <utils/common/ToString.h>
43 #include "RORouteDef.h"
44 #include "RONet.h"
45 #include "RORouteDef_OrigDest.h"
47 #include "ROVehicle.h"
48 #include "RORouteDef_Complete.h"
50 
51 #ifdef CHECK_MEMORY_LEAKS
52 #include <foreign/nvwa/debug_new.h>
53 #endif // CHECK_MEMORY_LEAKS
54 
55 
56 // ===========================================================================
57 // method definitions
58 // ===========================================================================
59 /* -------------------------------------------------------------------------
60  * RORDGenerator_ODAmounts::FlowDef - methods
61  * ----------------------------------------------------------------------- */
63  SUMOVTypeParameter* type,
64  RORouteDef* route,
65  SUMOTime intBegin,
66  SUMOTime intEnd,
67  unsigned int vehicles2insert,
68  bool randomize)
69  : myVehicle(vehicle), myVehicleType(type), myRoute(route),
70  myIntervalBegin(intBegin), myIntervalEnd(intEnd),
71  myVehicle2InsertNumber(vehicles2insert), myInserted(0), myRandom(randomize) {
73  if (myRandom) {
76  for (size_t i = 0; i < myVehicle2InsertNumber; ++i) {
77  SUMOTime departure = myIntervalBegin + ((int)(RandHelper::rand(period) / DELTA_T)) * DELTA_T;
78  myDepartures.push_back(departure);
79  }
80  sort(myDepartures.begin(), myDepartures.end());
81  reverse(myDepartures.begin(), myDepartures.end());
82  }
83 }
84 
85 
87  delete myVehicle;
88 }
89 
90 
91 bool
93  return myIntervalBegin <= t && myIntervalEnd > t;
94 }
95 
96 
97 void
99  assert(myIntervalBegin <= t && myIntervalEnd >= t);
100  if (!myRandom) {
101  unsigned int absPerEachStep = myVehicle2InsertNumber / ((myIntervalEnd - myIntervalBegin) / DELTA_T);
102  for (unsigned int i = 0; i < absPerEachStep; i++) {
103  addSingleRoute(net, t);
104  }
105  // fraction
107  if (toInsert > myInserted) {
108  addSingleRoute(net, t);
109  }
110  } else {
111  while (myDepartures.size() > 0 && myDepartures.back() < t + DELTA_T) {
112  addSingleRoute(net, myDepartures.back());
113  myDepartures.pop_back();
114  }
115  }
116 }
117 
118 
119 void
121  std::string id = myVehicle->getID() + "_" + toString<unsigned int>(myInserted);
122  RORouteDef* rd = myRoute->copy(id);
123  net.addRouteDef(rd);
124  ROVehicle* veh = myVehicle->copy(id, t, rd);
125  net.addVehicle(id, veh);
126  ++myInserted;
127 }
128 
129 
130 SUMOTime
132  return myIntervalEnd;
133 }
134 
135 
136 /* -------------------------------------------------------------------------
137  * RORDGenerator_ODAmounts - methods
138  * ----------------------------------------------------------------------- */
140  SUMOTime begin,
141  SUMOTime end,
142  bool emptyDestinationsAllowed,
143  bool randomize,
144  const std::string& fileName)
145  : RORDLoader_TripDefs(net, begin, end, emptyDestinationsAllowed, false, fileName),
146  myRandom(randomize),
148  // read the complete file on initialisation
149  myParser->parseReset(myToken);
150  myParser->parse(getFileName().c_str());
151  myCurrentDepart = begin;
152 }
153 
154 
156  for (FlowDefV::const_iterator i = myFlows.begin(); i != myFlows.end(); i++) {
157  delete(*i);
158  }
159 }
160 
161 
162 bool
164  // skip routes before begin
165  if (until < myBegin) {
166  myCurrentDepart = until;
167  return true;
168  }
169  // build route definitions for the given timestep
170  SUMOTime t;
171  for (t = myCurrentDepart; t < until + 1; t += DELTA_T) {
172  buildForTimeStep(t);
173  }
174  myCurrentDepart = t;
175  return true;
176 }
177 
178 
179 void
181  if (time < myBegin || time >= myEnd) {
182  return;
183  }
184  myEnded = true;
185  for (FlowDefV::const_iterator i = myFlows.begin(); i != myFlows.end(); i++) {
186  FlowDef* fd = *i;
187  // skip flow definitions not valid for the current time
188  if (fd->applicableForTime(time)) {
189  fd->addRoutes(myNet, time);
190  }
191  // check whether any further exists
192  if (fd->getIntervalEnd() > time) {
193  myEnded = false;
194  }
195  }
196 }
197 
198 
199 void
201  const SUMOSAXAttributes& attrs) {
202  RORDLoader_TripDefs::myStartElement(element, attrs);
203  if (element == SUMO_TAG_FLOW) {
204  parseFlowAmountDef(attrs);
205  } else if (element == SUMO_TAG_INTERVAL) {
206  bool ok = true;
207  myUpperIntervalBegin = attrs.getOptSUMOTimeReporting(SUMO_ATTR_BEGIN, 0, ok, -1); // !!!really optional ?
208  myUpperIntervalEnd = attrs.getOptSUMOTimeReporting(SUMO_ATTR_END, 0, ok, -1); // !!!really optional ?
209  }
210 }
211 
212 
213 void
215  // get the vehicle id, the edges, the speed and position and
216  // the departure time and other information
217  std::string id = getVehicleID(attrs);
218  if (myKnownIDs.find(id) != myKnownIDs.end()) {
219  throw ProcessError("The id '" + id + "' appears twice within the flow descriptions.'");
220  }
221  myKnownIDs.insert(id); // !!! a local storage is not save
222  myBeginEdge = getEdge(attrs, "origin", SUMO_ATTR_FROM, id, false);
223  myEndEdge = getEdge(attrs, "destination",
225  try {
227  } catch (ProcessError& e) {
228  throw ProcessError(StringUtils::replace(e.what(), "''", id.c_str()));
229  }
230  myParameter->id = id;
231  bool ok = true;
234  if (attrs.hasAttribute(SUMO_ATTR_NUMBER)) {
236  } else if (attrs.hasAttribute(SUMO_ATTR_NO__DEPRECATED)) {
239  WRITE_WARNING("'" + toString(SUMO_ATTR_NO__DEPRECATED) + "' is deprecated, please use '" + toString(SUMO_ATTR_NUMBER) + "' instead.");
240  }
242  } else {
243  throw ProcessError("Flow '" + id + "' has no vehicle number.");
244  }
245  if (!ok) {
246  throw ProcessError();
247  }
249  throw ProcessError("The interval must be larger than 0.\n The current values are: begin=" + toString<unsigned int>(myIntervalBegin) + " end=" + toString<unsigned int>(myIntervalEnd));
250  }
251 }
252 
253 
254 void
257  if (element == SUMO_TAG_FLOW) {
259  } else if (element == SUMO_TAG_INTERVAL) {
260  myUpperIntervalBegin = 0; // !!! was -1
261  myUpperIntervalEnd = 0; // !!! was: -1
262  }
263 }
264 
265 
266 void
268  if (!MsgHandler::getErrorInstance()->wasInformed()) {
269 
270  if (myIntervalEnd < myBegin) {
271  return;
272  }
273  // add the vehicle type, the vehicle and the route to the net
277  // check whether any errors occured
278  if (MsgHandler::getErrorInstance()->wasInformed()) {
279  return;
280  }
281  // build the vehicle
282  if (myNet.addRouteDef(route)) {
283  myNextRouteRead = true;
284  ROVehicle* vehicle = new ROVehicle(*myParameter, route, type);
285  // add to the container
286  FlowDef* fd = new FlowDef(vehicle, type, route, myIntervalBegin, myIntervalEnd, myVehicle2InsertNumber, myRandom);
287  myFlows.push_back(fd);
288  } else {
289  WRITE_ERROR("The vehicle '" + myParameter->id + "' occurs at least twice.");
290  delete route;
291  }
292  delete myParameter;
293  myParameter = 0;
294  }
295 }
296 
297 
298 /****************************************************************************/
299