Eclipse SUMO - Simulation of Urban MObility
ODMatrix.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 // Copyright (C) 2006-2024 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
20 // An O/D (origin/destination) matrix
21 /****************************************************************************/
22 #pragma once
23 #include <config.h>
24 
25 #include <iostream>
26 #include <sstream>
27 #include <fstream>
28 #include <vector>
29 #include <cstdlib>
30 #include <ctime>
31 #include <algorithm>
32 #include <set>
33 #include <string>
34 #include <utils/common/SUMOTime.h>
35 #include "ODCell.h"
36 #include "ODDistrictCont.h"
39 #include <utils/common/SUMOTime.h>
41 
42 // ===========================================================================
43 // class declarations
44 // ===========================================================================
45 class OptionsCont;
46 class OutputDevice;
47 class SUMOSAXHandler;
48 
49 
50 // ===========================================================================
51 // class definitions
52 // ===========================================================================
69 
70 public:
75  ODMatrix(const ODDistrictCont& dc, double scale);
76 
77 
79  ~ODMatrix();
80 
81 
103  bool add(double vehicleNumber, const std::pair<SUMOTime, SUMOTime>& beginEnd,
104  const std::string& origin, const std::string& destination,
105  const std::string& vehicleType,
106  const bool originIsEdge = false, const bool destinationIsEdge = false,
107  bool noScaling = false);
108 
120  bool add(const SUMOVehicleParameter& veh, bool originIsEdge = false, bool destinationIsEdgeconst = false);
121 
129  void writeDefaultAttrs(OutputDevice& dev, const bool noVtype,
130  const ODCell* const cell);
131 
159  void write(SUMOTime begin, const SUMOTime end,
160  OutputDevice& dev, const bool uniform,
161  const bool differSourceSink, const bool noVtype,
162  const std::string& prefix, const bool stepLog,
163  bool pedestrians, bool persontrips,
164  const std::string& modes);
165 
166 
178  void writeFlows(const SUMOTime begin, const SUMOTime end,
179  OutputDevice& dev, const bool noVtype,
180  const std::string& prefix,
181  bool asProbability = false, bool pedestrians = false, bool persontrips = false,
182  const std::string& modes = "");
183 
184 
191  double getNumLoaded() const;
192 
193 
200  double getNumWritten() const;
201 
202 
209  double getNumDiscarded() const;
210 
211 
215  void applyCurve(const Distribution_Points& ps);
216 
217 
221  void readO(LineReader& lr, double scale,
222  std::string vehType, bool matrixHasVehType);
223 
227  void readV(LineReader& lr, double scale,
228  std::string vehType, bool matrixHasVehType);
229 
233  void loadMatrix(OptionsCont& oc);
234 
238  void loadRoutes(OptionsCont& oc, SUMOSAXHandler& handler);
239 
243  Distribution_Points parseTimeLine(const std::vector<std::string>& def, bool timelineDayInHours);
244 
245  const std::vector<ODCell*>& getCells() {
246  return myContainer;
247  }
248 
249  void sortByBeginTime();
250 
251  SUMOTime getBegin() const {
252  return myBegin;
253  }
254 
255  SUMOTime getEnd() const {
256  return myEnd;
257  }
258 
259  void addTazRelWeight(const std::string intervalID, const std::string& from, const std::string& to,
260  double val, double beg, double end);
261 
262 protected:
267  struct ODVehicle {
269  std::string id;
275  std::string from;
277  std::string to;
278 
279  };
280 
281 
307  double computeDeparts(ODCell* cell,
308  int& vehName, std::vector<ODVehicle>& into,
309  const bool uniform, const bool differSourceSink,
310  const std::string& prefix);
311 
312 
328  void applyCurve(const Distribution_Points& ps, ODCell* cell,
329  std::vector<ODCell*>& newCells);
330 
331 
332 private:
336  std::string getNextNonCommentLine(LineReader& lr);
337 
341  SUMOTime parseSingleTime(const std::string& time);
342 
346  std::pair<SUMOTime, SUMOTime> readTime(LineReader& lr);
347 
351  double readFactor(LineReader& lr, double scale);
352 
353 
354 private:
356  std::vector<ODCell*> myContainer;
357 
359  std::map<const std::pair<const std::string, const std::string>, std::vector<ODCell*> > myShortCut;
360 
363 
365  std::set<std::string> myMissingDistricts;
366 
368  double myNumLoaded;
369 
371  double myNumWritten;
372 
375 
378 
380  std::string myVType;
381 
383  double myScale;
384 
390  public:
393 
394 
405  int operator()(ODCell* p1, ODCell* p2) const {
406  if (p1->begin == p2->begin) {
407  if (p1->origin == p2->origin) {
408  return p1->destination < p2->destination;
409  }
410  return p1->origin < p2->origin;
411  }
412  return p1->begin < p2->begin;
413  }
414 
415  };
416 
417 
426  public:
429 
430 
439  bool operator()(const ODVehicle& p1, const ODVehicle& p2) const {
440  if (p1.depart == p2.depart) {
441  return p1.id > p2.id;
442  }
443  return p1.depart > p2.depart;
444  }
445 
446  };
447 
448 private:
450  ODMatrix(const ODMatrix& s);
451 
453  ODMatrix& operator=(const ODMatrix& s) = delete;
454 
455 };
long long int SUMOTime
Definition: GUI.h:35
Retrieves a file linewise and reports the lines to a handler.
Definition: LineReader.h:48
A container for districts.
Used for sorting the cells by the begin time they describe.
Definition: ODMatrix.h:389
int operator()(ODCell *p1, ODCell *p2) const
Comparing operator.
Definition: ODMatrix.h:405
Used for sorting vehicles by their departure (latest first)
Definition: ODMatrix.h:425
bool operator()(const ODVehicle &p1, const ODVehicle &p2) const
Comparing operator.
Definition: ODMatrix.h:439
An O/D (origin/destination) matrix.
Definition: ODMatrix.h:68
SUMOTime myEnd
Definition: ODMatrix.h:377
double getNumLoaded() const
Returns the number of loaded vehicles.
Definition: ODMatrix.cpp:603
void readV(LineReader &lr, double scale, std::string vehType, bool matrixHasVehType)
read a VISUM-matrix with the V Format
Definition: ODMatrix.cpp:489
void sortByBeginTime()
Definition: ODMatrix.cpp:760
double readFactor(LineReader &lr, double scale)
Definition: ODMatrix.cpp:477
double computeDeparts(ODCell *cell, int &vehName, std::vector< ODVehicle > &into, const bool uniform, const bool differSourceSink, const std::string &prefix)
Computes the vehicle departs stored in the given cell and saves them in "into".
Definition: ODMatrix.cpp:168
const std::vector< ODCell * > & getCells()
Definition: ODMatrix.h:245
void addTazRelWeight(const std::string intervalID, const std::string &from, const std::string &to, double val, double beg, double end)
Definition: ODMatrix.cpp:711
SUMOTime myBegin
parsed time bounds
Definition: ODMatrix.h:377
~ODMatrix()
Destructor.
Definition: ODMatrix.cpp:63
SUMOTime parseSingleTime(const std::string &time)
Definition: ODMatrix.cpp:447
double myScale
the scaling factor for traffic
Definition: ODMatrix.h:383
void writeFlows(const SUMOTime begin, const SUMOTime end, OutputDevice &dev, const bool noVtype, const std::string &prefix, bool asProbability=false, bool pedestrians=false, bool persontrips=false, const std::string &modes="")
Writes the flows stored in the matrix.
Definition: ODMatrix.cpp:346
void applyCurve(const Distribution_Points &ps)
Splits the stored cells dividing them on the given time line.
Definition: ODMatrix.cpp:637
SUMOTime getEnd() const
Definition: ODMatrix.h:255
std::map< const std::pair< const std::string, const std::string >, std::vector< ODCell * > > myShortCut
The loaded cells indexed by origin and destination.
Definition: ODMatrix.h:359
std::pair< SUMOTime, SUMOTime > readTime(LineReader &lr)
Definition: ODMatrix.cpp:458
void readO(LineReader &lr, double scale, std::string vehType, bool matrixHasVehType)
read a VISUM-matrix with the O Format
Definition: ODMatrix.cpp:558
std::set< std::string > myMissingDistricts
The missing districts already warned about.
Definition: ODMatrix.h:365
void write(SUMOTime begin, const SUMOTime end, OutputDevice &dev, const bool uniform, const bool differSourceSink, const bool noVtype, const std::string &prefix, const bool stepLog, bool pedestrians, bool persontrips, const std::string &modes)
Writes the vehicles stored in the matrix assigning the sources and sinks.
Definition: ODMatrix.cpp:236
const ODDistrictCont & myDistricts
The districts to retrieve sources/sinks from.
Definition: ODMatrix.h:362
Distribution_Points parseTimeLine(const std::vector< std::string > &def, bool timelineDayInHours)
split the given timeline
Definition: ODMatrix.cpp:735
ODMatrix(const ODDistrictCont &dc, double scale)
Constructor.
Definition: ODMatrix.cpp:52
void writeDefaultAttrs(OutputDevice &dev, const bool noVtype, const ODCell *const cell)
Helper function for flow and trip output writing the depart and arrival attributes.
Definition: ODMatrix.cpp:207
double myNumLoaded
Number of loaded vehicles.
Definition: ODMatrix.h:368
double myNumWritten
Number of written vehicles.
Definition: ODMatrix.h:371
SUMOTime getBegin() const
Definition: ODMatrix.h:251
bool add(double vehicleNumber, const std::pair< SUMOTime, SUMOTime > &beginEnd, const std::string &origin, const std::string &destination, const std::string &vehicleType, const bool originIsEdge=false, const bool destinationIsEdge=false, bool noScaling=false)
Builds a single cell from the given values, verifying them.
Definition: ODMatrix.cpp:75
void loadMatrix(OptionsCont &oc)
read a matrix in one of several formats
Definition: ODMatrix.cpp:650
void loadRoutes(OptionsCont &oc, SUMOSAXHandler &handler)
read SUMO routes
Definition: ODMatrix.cpp:718
double getNumWritten() const
Returns the number of written vehicles.
Definition: ODMatrix.cpp:609
std::vector< ODCell * > myContainer
The loaded cells.
Definition: ODMatrix.h:356
double myNumDiscarded
Number of discarded vehicles.
Definition: ODMatrix.h:374
ODMatrix & operator=(const ODMatrix &s)=delete
invalid assignment operator
double getNumDiscarded() const
Returns the number of discarded vehicles.
Definition: ODMatrix.cpp:615
std::string myVType
user-defined vType
Definition: ODMatrix.h:380
ODMatrix(const ODMatrix &s)
invalid copy constructor
std::string getNextNonCommentLine(LineReader &lr)
Definition: ODMatrix.cpp:435
A storage for options typed value containers)
Definition: OptionsCont.h:89
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:61
Interface for a class which obtains read weights for named edges.
SAX-handler base for SUMO-files.
Structure representing possible vehicle parameter.
A single O/D-matrix cell.
Definition: ODCell.h:49
std::string destination
Name of the destination district.
Definition: ODCell.h:63
std::string origin
Name of the origin district.
Definition: ODCell.h:60
SUMOTime begin
The begin time this cell describes.
Definition: ODCell.h:54
An internal representation of a single vehicle.
Definition: ODMatrix.h:267
SUMOTime depart
The departure time of the vehicle.
Definition: ODMatrix.h:271
std::string from
The edge the vehicles shall start at.
Definition: ODMatrix.h:275
ODCell * cell
The cell of the ODMatrix which generated the vehicle.
Definition: ODMatrix.h:273
std::string to
The edge the vehicles shall end at.
Definition: ODMatrix.h:277
std::string id
The id of the vehicle.
Definition: ODMatrix.h:269