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.org/sumo
3 // Copyright (C) 2006-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
16 // An O/D (origin/destination) matrix
17 /****************************************************************************/
18 #ifndef ODMatrix_h
19 #define ODMatrix_h
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26 
27 #include <iostream>
28 #include <sstream>
29 #include <fstream>
30 #include <vector>
31 #include <cstdlib>
32 #include <ctime>
33 #include <algorithm>
34 #include <set>
35 #include <string>
36 #include <utils/common/SUMOTime.h>
37 #include "ODCell.h"
38 #include "ODDistrictCont.h"
41 #include <utils/common/SUMOTime.h>
42 
43 // ===========================================================================
44 // class declarations
45 // ===========================================================================
46 class OptionsCont;
47 class OutputDevice;
48 class SUMOSAXHandler;
49 
50 
51 // ===========================================================================
52 // class definitions
53 // ===========================================================================
69 class ODMatrix {
70 public:
75  ODMatrix(const ODDistrictCont& dc);
76 
77 
79  ~ODMatrix();
80 
81 
104  bool add(double vehicleNumber, SUMOTime begin,
105  SUMOTime end, const std::string& origin, const std::string& destination,
106  const std::string& vehicleType,
107  const bool originIsEdge = false, const bool destinationIsEdge = false);
108 
120  bool add(const std::string& id, const SUMOTime depart,
121  const std::string& fromTaz, const std::string& toTaz,
122  const std::string& vehicleType,
123  const bool originIsEdge = false, const bool destinationIsEdge = false);
124 
132  void writeDefaultAttrs(OutputDevice& dev, const bool noVtype,
133  const ODCell* const cell);
134 
162  void write(SUMOTime begin, const SUMOTime end,
163  OutputDevice& dev, const bool uniform,
164  const bool differSourceSink, const bool noVtype,
165  const std::string& prefix, const bool stepLog,
166  bool pedestrians, bool persontrips);
167 
168 
180  void writeFlows(const SUMOTime begin, const SUMOTime end,
181  OutputDevice& dev, const bool noVtype,
182  const std::string& prefix,
183  bool asProbability = false, bool pedestrians = false, bool persontrips = false);
184 
185 
192  double getNumLoaded() const;
193 
194 
201  double getNumWritten() const;
202 
203 
210  double getNumDiscarded() const;
211 
212 
216  void applyCurve(const Distribution_Points& ps);
217 
218 
222  void readO(LineReader& lr, double scale,
223  std::string vehType, bool matrixHasVehType);
224 
228  void readV(LineReader& lr, double scale,
229  std::string vehType, bool matrixHasVehType);
230 
234  void loadMatrix(OptionsCont& oc);
235 
239  void loadRoutes(OptionsCont& oc, SUMOSAXHandler& handler);
240 
244  Distribution_Points parseTimeLine(const std::vector<std::string>& def, bool timelineDayInHours);
245 
246  const std::vector<ODCell*>& getCells() {
247  return myContainer;
248  }
249 
250  void sortByBeginTime();
251 
252  SUMOTime getBegin() const {
253  return myBegin;
254  }
255 
256  SUMOTime getEnd() const {
257  return myEnd;
258  }
259 
260 protected:
265  struct ODVehicle {
267  std::string id;
273  std::string from;
275  std::string to;
276 
277  };
278 
279 
305  double computeDeparts(ODCell* cell,
306  int& vehName, std::vector<ODVehicle>& into,
307  const bool uniform, const bool differSourceSink,
308  const std::string& prefix);
309 
310 
326  void applyCurve(const Distribution_Points& ps, ODCell* cell,
327  std::vector<ODCell*>& newCells);
328 
329 
330 private:
334  std::string getNextNonCommentLine(LineReader& lr);
335 
339  SUMOTime parseSingleTime(const std::string& time);
340 
344  std::pair<SUMOTime, SUMOTime> readTime(LineReader& lr);
345 
349  double readFactor(LineReader& lr, double scale);
350 
351 
352 private:
354  std::vector<ODCell*> myContainer;
355 
357  std::map<const std::pair<const std::string, const std::string>, std::vector<ODCell*> > myShortCut;
358 
361 
363  std::set<std::string> myMissingDistricts;
364 
366  double myNumLoaded;
367 
369  double myNumWritten;
370 
373 
376 
382  public:
385 
386 
397  int operator()(ODCell* p1, ODCell* p2) const {
398  if (p1->begin == p2->begin) {
399  if (p1->origin == p2->origin) {
400  return p1->destination < p2->destination;
401  }
402  return p1->origin < p2->origin;
403  }
404  return p1->begin < p2->begin;
405  }
406 
407  };
408 
409 
418  public:
421 
422 
431  bool operator()(const ODVehicle& p1, const ODVehicle& p2) const {
432  if (p1.depart == p2.depart) {
433  return p1.id > p2.id;
434  }
435  return p1.depart > p2.depart;
436  }
437 
438  };
439 
440 private:
442  ODMatrix(const ODMatrix& s);
443 
445  ODMatrix& operator=(const ODMatrix& s) = delete;
446 
447 };
448 
449 
450 #endif
451 
452 /****************************************************************************/
ODMatrix::parseTimeLine
Distribution_Points parseTimeLine(const std::vector< std::string > &def, bool timelineDayInHours)
split the given timeline
Definition: ODMatrix.cpp:668
LineReader.h
ODMatrix::write
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)
Writes the vehicles stored in the matrix assigning the sources and sinks.
Definition: ODMatrix.cpp:212
ODMatrix::ODMatrix
ODMatrix(const ODDistrictCont &dc)
Constructor.
Definition: ODMatrix.cpp:50
SUMOTime.h
SUMOSAXHandler
SAX-handler base for SUMO-files.
Definition: SUMOSAXHandler.h:41
OutputDevice
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:63
ODMatrix::getEnd
SUMOTime getEnd() const
Definition: ODMatrix.h:256
ODMatrix::getBegin
SUMOTime getBegin() const
Definition: ODMatrix.h:252
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:34
ODMatrix::myDistricts
const ODDistrictCont & myDistricts
The districts to retrieve sources/sinks from.
Definition: ODMatrix.h:360
ODMatrix::myNumWritten
double myNumWritten
Number of written vehicles.
Definition: ODMatrix.h:369
ODMatrix::loadMatrix
void loadMatrix(OptionsCont &oc)
read a matrix in one of several formats
Definition: ODMatrix.cpp:605
ODMatrix::myBegin
SUMOTime myBegin
parsed time bounds
Definition: ODMatrix.h:375
ODMatrix::computeDeparts
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:144
ODMatrix::parseSingleTime
SUMOTime parseSingleTime(const std::string &time)
Definition: ODMatrix.cpp:398
ODMatrix::add
bool add(double vehicleNumber, SUMOTime begin, SUMOTime end, const std::string &origin, const std::string &destination, const std::string &vehicleType, const bool originIsEdge=false, const bool destinationIsEdge=false)
Builds a single cell from the given values, verifying them.
Definition: ODMatrix.cpp:63
ODMatrix::ODVehicle::cell
ODCell * cell
The cell of the ODMatrix which generated the vehicle.
Definition: ODMatrix.h:271
ODMatrix::loadRoutes
void loadRoutes(OptionsCont &oc, SUMOSAXHandler &handler)
read SUMO routes
Definition: ODMatrix.cpp:651
ODMatrix::ODVehicle::id
std::string id
The id of the vehicle.
Definition: ODMatrix.h:267
ODCell
A single O/D-matrix cell.
Definition: ODCell.h:50
ODMatrix::ODVehicle::depart
SUMOTime depart
The departure time of the vehicle.
Definition: ODMatrix.h:269
ODMatrix::ODVehicle
An internal representation of a single vehicle.
Definition: ODMatrix.h:265
ODCell::destination
std::string destination
Name of the destination district.
Definition: ODCell.h:64
ODMatrix::cell_by_begin_comparator::operator()
int operator()(ODCell *p1, ODCell *p2) const
Comparing operator.
Definition: ODMatrix.h:397
ODMatrix::myShortCut
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:357
ODMatrix::descending_departure_comperator
Used for sorting vehicles by their departure (latest first)
Definition: ODMatrix.h:417
OptionsCont
A storage for options typed value containers)
Definition: OptionsCont.h:89
ODMatrix::myContainer
std::vector< ODCell * > myContainer
The loaded cells.
Definition: ODMatrix.h:354
ODMatrix::myNumDiscarded
double myNumDiscarded
Number of discarded vehicles.
Definition: ODMatrix.h:372
ODMatrix::~ODMatrix
~ODMatrix()
Destructor.
Definition: ODMatrix.cpp:54
ODCell.h
LineReader
Retrieves a file linewise and reports the lines to a handler.
Definition: LineReader.h:50
ODMatrix::descending_departure_comperator::descending_departure_comperator
descending_departure_comperator()
constructor
Definition: ODMatrix.h:420
ODMatrix
An O/D (origin/destination) matrix.
Definition: ODMatrix.h:69
ODMatrix::readFactor
double readFactor(LineReader &lr, double scale)
Definition: ODMatrix.cpp:428
ODDistrictCont
A container for districts.
Definition: ODDistrictCont.h:41
ODMatrix::descending_departure_comperator::operator()
bool operator()(const ODVehicle &p1, const ODVehicle &p2) const
Comparing operator.
Definition: ODMatrix.h:431
ODMatrix::ODVehicle::to
std::string to
The edge the vehicles shall end at.
Definition: ODMatrix.h:275
ODMatrix::cell_by_begin_comparator::cell_by_begin_comparator
cell_by_begin_comparator()
constructor
Definition: ODMatrix.h:384
ODMatrix::getNumLoaded
double getNumLoaded() const
Returns the number of loaded vehicles.
Definition: ODMatrix.cpp:558
ODDistrictCont.h
Distribution_Points
Definition: Distribution_Points.h:39
ODMatrix::readTime
std::pair< SUMOTime, SUMOTime > readTime(LineReader &lr)
Definition: ODMatrix.cpp:409
ODCell::origin
std::string origin
Name of the origin district.
Definition: ODCell.h:61
ODMatrix::getNumWritten
double getNumWritten() const
Returns the number of written vehicles.
Definition: ODMatrix.cpp:564
Distribution_Points.h
ODMatrix::getNextNonCommentLine
std::string getNextNonCommentLine(LineReader &lr)
Definition: ODMatrix.cpp:386
ODMatrix::sortByBeginTime
void sortByBeginTime()
Definition: ODMatrix.cpp:693
ODMatrix::readO
void readO(LineReader &lr, double scale, std::string vehType, bool matrixHasVehType)
read a VISUM-matrix with the O Format
Definition: ODMatrix.cpp:508
ODMatrix::getNumDiscarded
double getNumDiscarded() const
Returns the number of discarded vehicles.
Definition: ODMatrix.cpp:570
config.h
ODMatrix::writeFlows
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)
Writes the flows stored in the matrix.
Definition: ODMatrix.cpp:301
ODMatrix::applyCurve
void applyCurve(const Distribution_Points &ps)
Splits the stored cells dividing them on the given time line.
Definition: ODMatrix.cpp:592
ODMatrix::readV
void readV(LineReader &lr, double scale, std::string vehType, bool matrixHasVehType)
read a VISUM-matrix with the V Format
Definition: ODMatrix.cpp:440
ODMatrix::cell_by_begin_comparator
Used for sorting the cells by the begin time they describe.
Definition: ODMatrix.h:381
ODCell::begin
SUMOTime begin
The begin time this cell describes.
Definition: ODCell.h:55
ODMatrix::myEnd
SUMOTime myEnd
Definition: ODMatrix.h:375
ODMatrix::myNumLoaded
double myNumLoaded
Number of loaded vehicles.
Definition: ODMatrix.h:366
ODMatrix::ODVehicle::from
std::string from
The edge the vehicles shall start at.
Definition: ODMatrix.h:273
ODMatrix::writeDefaultAttrs
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:183
ODMatrix::operator=
ODMatrix & operator=(const ODMatrix &s)=delete
invalid assignment operator
ODMatrix::getCells
const std::vector< ODCell * > & getCells()
Definition: ODMatrix.h:246
ODMatrix::myMissingDistricts
std::set< std::string > myMissingDistricts
The missing districts already warned about.
Definition: ODMatrix.h:363