64 for (
RORoute*
const r : cell->pathsVector) {
74ODMatrix::add(
double vehicleNumber,
const std::pair<SUMOTime, SUMOTime>& beginEnd,
75 const std::string& origin,
const std::string& destination,
76 const std::string& vehicleType,
const bool originIsEdge,
const bool destinationIsEdge,
80 WRITE_WARNING(
"Missing origin '" + origin +
"' and destination '" + destination +
"' (" +
toString(vehicleNumber) +
" vehicles).");
85 }
else if (!originIsEdge &&
myDistricts.
get(origin) == 0 && vehicleNumber > 0) {
90 }
else if (!destinationIsEdge &&
myDistricts.
get(destination) == 0 && vehicleNumber > 0) {
91 WRITE_ERROR(
"Missing destination '" + destination +
"' (" +
toString(vehicleNumber) +
" vehicles).");
97 WRITE_ERROR(
"District '" + origin +
"' has no source.");
101 WRITE_ERROR(
"District '" + destination +
"' has no sink.");
106 cell->
begin = beginEnd.first;
107 cell->
end = beginEnd.second;
127 const std::string& fromTaz,
const std::string& toTaz,
128 const std::string& vehicleType,
const bool originIsEdge,
const bool destinationIsEdge) {
135 std::vector<ODCell*>& odList =
myShortCut[std::make_pair(fromTaz, toTaz)];
137 for (std::vector<ODCell*>::const_reverse_iterator c = odList.rbegin(); c != odList.rend(); ++c) {
138 if ((*c)->begin <= depart && (*c)->end > depart && (*c)->vehicleType == vehicleType) {
143 if (cell ==
nullptr) {
145 const int intervalIdx = (int)(depart / interval);
147 if (
add(1., std::make_pair(intervalIdx * interval, (intervalIdx + 1) * interval),
148 fromTaz, toTaz, vehicleType, originIsEdge, destinationIsEdge,
true)) {
150 odList.push_back(cell);
165 int& vehName, std::vector<ODVehicle>& into,
166 const bool uniform,
const bool differSourceSink,
167 const std::string& prefix) {
173 if (vehicles2insert == 0) {
177 const double offset = (double)(cell->
end - cell->
begin) / (double) vehicles2insert / (
double) 2.;
178 for (
int i = 0; i < vehicles2insert; ++i) {
191 }
while (canDiffer && differSourceSink && (veh.
to == veh.
from));
192 if (!canDiffer && differSourceSink && (veh.
to == veh.
from)) {
204 const ODCell*
const cell) {
210 if (oc.
isSet(
"departlane") && oc.
getString(
"departlane") !=
"default") {
213 if (oc.
isSet(
"departpos")) {
216 if (oc.
isSet(
"departspeed") && oc.
getString(
"departspeed") !=
"default") {
219 if (oc.
isSet(
"arrivallane")) {
222 if (oc.
isSet(
"arrivalpos")) {
225 if (oc.
isSet(
"arrivalspeed")) {
234 const bool differSourceSink,
const bool noVtype,
235 const std::string& prefix,
const bool stepLog,
236 bool pedestrians,
bool persontrips,
237 const std::string& modes) {
241 std::map<std::pair<std::string, std::string>,
double> fractionLeft;
246 std::vector<ODCell*>::iterator next =
myContainer.begin();
247 std::vector<ODVehicle> vehicles;
251 std::string personDepartPos = oc.
isSet(
"departpos") ? oc.
getString(
"departpos") :
"random";
252 std::string personArrivalPos = oc.
isSet(
"arrivalpos") ? oc.
getString(
"arrivalpos") :
"random";
257 for (
SUMOTime t = begin; t < end;) {
258 if (stepLog && t - lastOut >=
DELTA_T) {
259 std::cout <<
"Parsing time " +
time2string(t) <<
'\r';
263 bool changed =
false;
264 while (next !=
myContainer.end() && (*next)->begin <= t && (*next)->end > t) {
265 std::pair<std::string, std::string> odID = std::make_pair((*next)->origin, (*next)->destination);
267 if (fractionLeft.find(odID) != fractionLeft.end()) {
268 (*next)->vehicleNumber += fractionLeft[odID];
269 fractionLeft[odID] = 0;
272 const int oldSize = (int)vehicles.size();
273 const double fraction =
computeDeparts(*next, vehName, vehicles, uniform, differSourceSink, prefix);
274 if (oldSize != (
int)vehicles.size()) {
278 fractionLeft[odID] = fraction;
286 for (std::vector<ODVehicle>::reverse_iterator i = vehicles.rbegin(); i != vehicles.rend() && (*i).depart == t; ++i) {
299 }
else if (persontrips) {
321 while (vehicles.size() != 0 && vehicles.back().depart == t) {
324 if (!vehicles.empty()) {
325 t = vehicles.back().depart;
327 if (next !=
myContainer.end() && (t > (*next)->begin || vehicles.empty())) {
330 if (next ==
myContainer.end() && vehicles.empty()) {
340 const std::string& prefix,
341 bool asProbability,
bool pedestrians,
bool persontrips,
342 const std::string& modes) {
350 const ODCell*
const c = *i;
351 if (c->
end > begin && c->
begin < end) {
353 if (probability <= 0) {
360 if (!asProbability) {
363 if (probability > 1) {
364 WRITE_WARNING(
"Flow density of " +
toString(probability) +
" vehicles per second, cannot be represented with a simple probability. Falling back to even spacing.");
377 }
else if (persontrips) {
380 if (!asProbability) {
383 if (probability > 1) {
384 WRITE_WARNING(
"Flow density of " +
toString(probability) +
" vehicles per second, cannot be represented with a simple probability. Falling back to even spacing.");
406 if (!asProbability) {
409 if (probability > 1) {
410 WRITE_WARNING(
"Flow density of " +
toString(probability) +
" vehicles per second, cannot be represented with a simple probability. Falling back to even spacing.");
429 const std::string line = lr.
readLine();
430 if (line[0] !=
'*') {
440 if (time.find(
'.') == std::string::npos) {
443 const std::string hours = time.substr(0, time.find(
'.'));
444 const std::string minutes = time.substr(time.find(
'.') + 1);
449std::pair<SUMOTime, SUMOTime>
459 return std::make_pair(begin, end);
461 throw ProcessError(
"Broken period definition '" + line +
"'.");
463 throw ProcessError(
"Broken period definition '" + line +
"' (" + e.what() +
").");
482 std::string vehType,
bool matrixHasVehType) {
486 if (matrixHasVehType) {
493 const std::pair<SUMOTime, SUMOTime> beginEnd =
readTime(lr);
500 std::vector<std::string> names;
501 while ((
int)names.size() != numDistricts && lr.
hasMore()) {
505 names.push_back(st2.
next());
513 for (std::vector<std::string>::iterator si = names.begin(); si != names.end(); ++si) {
514 std::vector<std::string>::iterator di = names.begin();
519 throw ProcessError(
"Missing line for district " + (*si) +
".");
521 if (line.length() == 0) {
527 assert(di != names.end());
529 if (vehNumber != 0) {
530 add(vehNumber, beginEnd, *si, *di, vehType);
532 if (di == names.end()) {
533 throw ProcessError(
"More entries than districts found.");
538 throw ProcessError(
"Not numeric vehicle number in line '" + line +
"'.");
543 }
while (di != names.end());
551 std::string vehType,
bool matrixHasVehType) {
555 if (matrixHasVehType) {
563 const std::pair<SUMOTime, SUMOTime> beginEnd =
readTime(lr);
569 if (line.length() == 0) {
573 if (st2.
size() == 0) {
577 std::string sourceD = st2.
next();
578 std::string destD = st2.
next();
580 if (vehNumber != 0) {
581 add(vehNumber, beginEnd, sourceD, destD, vehType);
584 throw ProcessError(
"Missing at least one information in line '" + line +
"'.");
586 throw ProcessError(
"Not numeric vehicle number in line '" + line +
"'.");
614 const std::vector<double>& times = ps.
getVals();
615 for (
int i = 0; i < (int)times.size() - 1; ++i) {
623 newCells.push_back(ncell);
632 for (std::vector<ODCell*>::iterator i = oldCells.begin(); i != oldCells.end(); ++i) {
633 std::vector<ODCell*> newCells;
635 copy(newCells.begin(), newCells.end(), back_inserter(
myContainer));
643 std::vector<std::string> files = oc.
getStringVector(
"od-matrix-files");
644 for (std::vector<std::string>::iterator i = files.begin(); i != files.end(); ++i) {
651 if (type.find(
';') != std::string::npos) {
652 type = type.substr(0, type.find(
';'));
655 if (type.length() > 1 && type[1] ==
'V') {
657 if (type.find(
'N') != std::string::npos) {
658 throw ProcessError(
"'" + *i +
"' does not contain the needed information about the time described.");
660 readV(lr, 1, oc.
getString(
"vtype"), type.find(
'M') != std::string::npos);
661 }
else if (type.length() > 1 && type[1] ==
'O') {
663 if (type.find(
'N') != std::string::npos) {
664 throw ProcessError(
"'" + *i +
"' does not contain the needed information about the time described.");
666 readO(lr, 1, oc.
getString(
"vtype"), type.find(
'M') != std::string::npos);
668 throw ProcessError(
"'" + *i +
"' uses an unknown matrix type '" + type +
"'.");
671 std::vector<std::string> amitranFiles = oc.
getStringVector(
"od-amitran-files");
672 for (std::vector<std::string>::iterator i = amitranFiles.begin(); i != amitranFiles.end(); ++i) {
674 throw ProcessError(
"Could not access matrix file '" + *i +
"' to load.");
687 throw ProcessError(
"Could not access matrix file '" + file +
"' to load.");
691 std::vector<SAXWeightsHandler::ToRetrieveDefinition*> retrieverDefs;
704 double val,
double beg,
double end) {
711 std::vector<std::string> routeFiles = oc.
getStringVector(
"route-files");
712 for (std::vector<std::string>::iterator i = routeFiles.begin(); i != routeFiles.end(); ++i) {
714 throw ProcessError(
"Could not access route file '" + *i +
"' to load.");
729 if (timelineDayInHours) {
730 if (def.size() != 24) {
731 throw ProcessError(
"Assuming 24 entries for a day timeline, but got " +
toString(def.size()) +
".");
733 for (
int chour = 0; chour < 24; ++chour) {
736 result.
add(24 * 3600., 0.);
738 for (
int i = 0; i < (int)def.size(); i++) {
740 if (st2.
size() != 2) {
741 throw ProcessError(
"Broken time line definition: missing a value in '" + def[i] +
"'.");
#define WRITE_WARNING(msg)
#define PROGRESS_DONE_MESSAGE()
#define PROGRESS_FAILED_MESSAGE()
#define PROGRESS_BEGIN_MESSAGE(msg)
std::string time2string(SUMOTime t)
convert SUMOTime to string
SUMOTime string2time(const std::string &r)
convert string to SUMOTime
@ SUMO_TAG_FLOW
a flow definitio nusing a from-to edges instead of a route (used by router)
@ SUMO_TAG_TRIP
a single trip definition (used by router)
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
@ SUMO_ATTR_BEGIN
weights: time range begin
@ SUMO_ATTR_END
weights: time range end
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
static bool isReadable(std::string path)
Checks whether the given file is readable.
Retrieves a file linewise and reports the lines to a handler.
bool good() const
Returns the information whether the stream is readable.
bool readLine(LineHandler &lh)
Reads a single (the next) line from the file and reports it to the given LineHandler.
bool hasMore() const
Returns whether another line may be read (the file was not read completely)
std::string getFileName() const
Returns the name of the used file.
T get(const std::string &id) const
Retrieves an item.
An XML-Handler for districts.
A container for districts.
std::string getRandomSourceFromDistrict(const std::string &name) const
Returns the id of a random source from the named district.
std::string getRandomSinkFromDistrict(const std::string &name) const
Returns the id of a random sink from the named district.
int sourceNumber() const
Returns the number of sources.
int sinkNumber() const
Returns the number of sinks.
Used for sorting the cells by the begin time they describe.
Used for sorting vehicles by their departure (latest first)
double getNumLoaded() const
Returns the number of loaded vehicles.
void readV(LineReader &lr, double scale, std::string vehType, bool matrixHasVehType)
read a VISUM-matrix with the V Format
double readFactor(LineReader &lr, double scale)
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".
void addTazRelWeight(const std::string intervalID, const std::string &from, const std::string &to, double val, double beg, double end)
SUMOTime myBegin
parsed time bounds
SUMOTime parseSingleTime(const std::string &time)
double myScale
the scaling factor for traffic
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.
void applyCurve(const Distribution_Points &ps)
Splits the stored cells dividing them on the given time line.
std::map< const std::pair< const std::string, const std::string >, std::vector< ODCell * > > myShortCut
The loaded cells indexed by origin and destination.
std::pair< SUMOTime, SUMOTime > readTime(LineReader &lr)
void readO(LineReader &lr, double scale, std::string vehType, bool matrixHasVehType)
read a VISUM-matrix with the O Format
std::set< std::string > myMissingDistricts
The missing districts already warned about.
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.
const ODDistrictCont & myDistricts
The districts to retrieve sources/sinks from.
Distribution_Points parseTimeLine(const std::vector< std::string > &def, bool timelineDayInHours)
split the given timeline
ODMatrix(const ODDistrictCont &dc, double scale)
Constructor.
void writeDefaultAttrs(OutputDevice &dev, const bool noVtype, const ODCell *const cell)
Helper function for flow and trip output writing the depart and arrival attributes.
double myNumLoaded
Number of loaded vehicles.
double myNumWritten
Number of written vehicles.
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.
void loadMatrix(OptionsCont &oc)
read a matrix in one of several formats
void loadRoutes(OptionsCont &oc, SUMOSAXHandler &handler)
read SUMO routes
double getNumWritten() const
Returns the number of written vehicles.
std::vector< ODCell * > myContainer
The loaded cells.
double myNumDiscarded
Number of discarded vehicles.
double getNumDiscarded() const
Returns the number of discarded vehicles.
std::string myVType
user-defined vType
std::string getNextNonCommentLine(LineReader &lr)
A storage for options typed value containers)
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
const StringVector & getStringVector(const std::string &name) const
Returns the list of string-value of the named option (only for Option_StringVector)
static OptionsCont & getOptions()
Retrieves the options.
Static storage of an output device and its base (abstract) implementation.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
void setPrecision(int precision=gPrecision)
Sets the precision or resets it to default.
A complete router's route.
static double rand(SumoRNG *rng=nullptr)
Returns a random real number in [0, 1)
double getOverallProb() const
Return the sum of the probabilites assigned to the members.
bool add(T val, double prob, bool checkDuplicates=true)
Adds a value with an assigned probability to the distribution.
const std::vector< T > & getVals() const
Returns the members of the distribution.
const std::vector< double > & getProbs() const
Returns the probabilities assigned to the members of the distribution.
Complete definition about what shall be retrieved and where to store it.
An XML-handler for network weights.
SAX-handler base for SUMO-files.
int size() const
returns the number of existing substrings
static const int WHITECHARS
identifier for splitting the given string at all whitespace characters
bool hasNext()
returns the information whether further substrings exist
std::string next()
returns the next substring when it exists. Otherwise the behaviour is undefined
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter
static std::string prune(const std::string &str)
Removes trailing and leading whitechars.
static int toInt(const std::string &sData)
converts a string into the integer value described by it by calling the char-type converter,...
static bool runParser(GenericSAXHandler &handler, const std::string &file, const bool isNet=false, const bool isRoute=false)
Runs the given handler on the given file; returns if everything's ok.
A single O/D-matrix cell.
std::string destination
Name of the destination district.
std::map< SUMOTime, std::vector< std::string > > departures
mapping of departure times to departing vehicles, if already fixed
std::string vehicleType
Name of the vehicle type.
std::string origin
Name of the origin district.
double vehicleNumber
The number of vehicles.
bool originIsEdge
the origin "district" is an edge id
SUMOTime end
The end time this cell describes.
SUMOTime begin
The begin time this cell describes.
bool destinationIsEdge
the destination "district" is an edge id
An internal representation of a single vehicle.
SUMOTime depart
The departure time of the vehicle.
std::string from
The edge the vehicles shall start at.
ODCell * cell
The cell of the ODMatrix which generated the vehicle.
std::string to
The edge the vehicles shall end at.
std::string id
The id of the vehicle.