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 "RORoute.h"
44 #include "RONet.h"
46 #include "ROVehicle.h"
47 #include "RORouteDef.h"
49 #include <utils/xml/XMLSubSys.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->copyOrigDest(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) {
147  // read the complete file on initialisation
149  myCurrentDepart = begin;
150 }
151 
152 
154  for (FlowDefV::const_iterator i = myFlows.begin(); i != myFlows.end(); i++) {
155  delete(*i);
156  }
157 }
158 
159 
160 bool
162  // skip routes before begin
163  if (until < myBegin) {
164  myCurrentDepart = until;
165  return true;
166  }
167  // build route definitions for the given timestep
168  SUMOTime t;
169  for (t = myCurrentDepart; t < until + 1; t += DELTA_T) {
170  buildForTimeStep(t);
171  }
172  myCurrentDepart = t;
173  return true;
174 }
175 
176 
177 void
179  if (time < myBegin || time >= myEnd) {
180  return;
181  }
182  myEnded = true;
183  for (FlowDefV::const_iterator i = myFlows.begin(); i != myFlows.end(); i++) {
184  FlowDef* fd = *i;
185  // skip flow definitions not valid for the current time
186  if (fd->applicableForTime(time)) {
187  fd->addRoutes(myNet, time);
188  }
189  // check whether any further exists
190  if (fd->getIntervalEnd() > time) {
191  myEnded = false;
192  }
193  }
194 }
195 
196 
197 void
199  const SUMOSAXAttributes& attrs) {
200  RORDLoader_TripDefs::myStartElement(element, attrs);
201  if (element == SUMO_TAG_FLOW) {
202  parseFlowAmountDef(attrs);
203  } else if (element == SUMO_TAG_INTERVAL) {
204  bool ok = true;
205  myUpperIntervalBegin = attrs.getOptSUMOTimeReporting(SUMO_ATTR_BEGIN, 0, ok, -1); // !!!really optional ?
206  myUpperIntervalEnd = attrs.getOptSUMOTimeReporting(SUMO_ATTR_END, 0, ok, -1); // !!!really optional ?
207  }
208 }
209 
210 
211 void
213  // get the vehicle id, the edges, the speed and position and
214  // the departure time and other information
215  std::string id = getVehicleID(attrs);
216  if (myKnownIDs.find(id) != myKnownIDs.end()) {
217  throw ProcessError("The id '" + id + "' appears twice within the flow descriptions.'");
218  }
219  myKnownIDs.insert(id); // !!! a local storage is not save
220  myBeginEdge = getEdge(attrs, "origin", SUMO_ATTR_FROM, id, false);
221  myEndEdge = getEdge(attrs, "destination",
223  try {
225  } catch (ProcessError& e) {
226  throw ProcessError(StringUtils::replace(e.what(), "''", id.c_str()));
227  }
228  myParameter->id = id;
229  bool ok = true;
232  myVehicle2InsertNumber = attrs.getIntReporting(SUMO_ATTR_NUMBER, id.c_str(), ok); // throw ProcessError("Flow '" + id + "' has no vehicle number.");
233  if (!ok) {
234  throw ProcessError();
235  }
237  throw ProcessError("The interval must be larger than 0.\n The current values are: begin=" + toString<unsigned int>(myIntervalBegin) + " end=" + toString<unsigned int>(myIntervalEnd));
238  }
239 }
240 
241 
242 void
245  if (element == SUMO_TAG_FLOW) {
247  } else if (element == SUMO_TAG_INTERVAL) {
248  myUpperIntervalBegin = 0; // !!! was -1
249  myUpperIntervalEnd = 0; // !!! was: -1
250  }
251 }
252 
253 
254 void
256  if (!MsgHandler::getErrorInstance()->wasInformed()) {
257 
258  if (myIntervalEnd < myBegin) {
259  return;
260  }
261  // add the vehicle type, the vehicle and the route to the net
263  RORouteDef* route = new RORouteDef(myParameter->id, 0, true);
264  std::vector<const ROEdge*> edges;
265  edges.push_back(myBeginEdge);
266  edges.push_back(myEndEdge);
267  route->addLoadedAlternative(new RORoute(myParameter->id, 0, 1, edges, col));
269  // check whether any errors occured
270  if (MsgHandler::getErrorInstance()->wasInformed()) {
271  return;
272  }
273  // build the vehicle
274  if (myNet.addRouteDef(route)) {
275  myNextRouteRead = true;
276  ROVehicle* vehicle = new ROVehicle(*myParameter, route, type);
277  // add to the container
278  FlowDef* fd = new FlowDef(vehicle, type, route, myIntervalBegin, myIntervalEnd, myVehicle2InsertNumber, myRandom);
279  myFlows.push_back(fd);
280  } else {
281  WRITE_ERROR("The vehicle '" + myParameter->id + "' occurs at least twice.");
282  delete route;
283  }
284  delete myParameter;
285  myParameter = 0;
286  }
287 }
288 
289 
290 /****************************************************************************/
291