59 : myDistricts(dc), myNumLoaded(0), myNumWritten(0), myNumDiscarded(0) {}
72 SUMOTime end,
const std::string& origin,
const std::string& destination,
73 const std::string& vehicleType) {
76 WRITE_WARNING(
"Missing origin '" + origin +
"' and destination '" + destination +
"' (" +
toString(vehicleNumber) +
" vehicles).");
85 }
else if (
myDistricts.
get(destination) == 0 && vehicleNumber > 0) {
86 WRITE_ERROR(
"Missing destination '" + destination +
"' (" +
toString(vehicleNumber) +
" vehicles).");
92 WRITE_ERROR(
"District '" + origin +
"' has no source.");
96 WRITE_ERROR(
"District '" + destination +
"' has no sink.");
114 const std::pair<const std::string, const std::string>& od,
115 const std::string& vehicleType) {
122 std::vector<ODCell*>& odList =
myShortCut[od];
124 for (std::vector<ODCell*>::const_reverse_iterator c = odList.rbegin(); c != odList.rend(); ++c) {
125 if ((*c)->begin <= depart && (*c)->end > depart && (*c)->vehicleType == vehicleType) {
132 const int intervalIdx = (int)(depart / interval);
133 if (
add(1., intervalIdx * interval, (intervalIdx + 1) * interval, od.first, od.second, vehicleType)) {
135 odList.push_back(cell);
150 int& vehName, std::vector<ODVehicle>& into,
151 const bool uniform,
const bool differSourceSink,
152 const std::string& prefix) {
158 if (vehicles2insert == 0) {
162 const double offset = (double)(cell->
end - cell->
begin) / (double) vehicles2insert / (
double) 2.;
163 for (
int i = 0; i < vehicles2insert; ++i) {
176 }
while (canDiffer && differSourceSink && (veh.
to == veh.
from));
177 if (!canDiffer && differSourceSink && (veh.
to == veh.
from)) {
189 const ODCell*
const cell) {
195 if (oc.
isSet(
"departlane") && oc.
getString(
"departlane") !=
"default") {
198 if (oc.
isSet(
"departpos")) {
201 if (oc.
isSet(
"departspeed") && oc.
getString(
"departspeed") !=
"default") {
204 if (oc.
isSet(
"arrivallane")) {
207 if (oc.
isSet(
"arrivalpos")) {
210 if (oc.
isSet(
"arrivalspeed")) {
219 const bool differSourceSink,
const bool noVtype,
220 const std::string& prefix,
const bool stepLog,
221 bool pedestrians,
bool persontrips) {
225 std::map<std::pair<std::string, std::string>,
double> fractionLeft;
230 std::vector<ODCell*>::iterator next =
myContainer.begin();
231 std::vector<ODVehicle> vehicles;
234 for (
SUMOTime t = begin; t < end;) {
235 if (stepLog && t - lastOut >=
DELTA_T) {
236 std::cout <<
"Parsing time " +
time2string(t) <<
'\r';
240 bool changed =
false;
241 while (next !=
myContainer.end() && (*next)->begin <= t && (*next)->end > t) {
242 std::pair<std::string, std::string> odID = std::make_pair((*next)->origin, (*next)->destination);
244 if (fractionLeft.find(odID) != fractionLeft.end()) {
245 (*next)->vehicleNumber += fractionLeft[odID];
246 fractionLeft[odID] = 0;
249 const int oldSize = (int)vehicles.size();
250 const double fraction =
computeDeparts(*next, vehName, vehicles, uniform, differSourceSink, prefix);
251 if (oldSize != (
int)vehicles.size()) {
255 fractionLeft[odID] = fraction;
262 for (std::vector<ODVehicle>::reverse_iterator i = vehicles.rbegin(); i != vehicles.rend() && (*i).depart == t; ++i) {
273 }
else if (persontrips) {
289 while (vehicles.size() != 0 && vehicles.back().depart == t) {
292 if (!vehicles.empty()) {
293 t = vehicles.back().depart;
295 if (next !=
myContainer.end() && (t > (*next)->begin || vehicles.empty())) {
298 if (next ==
myContainer.end() && vehicles.empty()) {
308 const std::string& prefix,
309 bool asProbability) {
317 const ODCell*
const c = *i;
318 if (c->
end > begin && c->
begin < end) {
322 if (!asProbability) {
326 if (probability > 1) {
327 WRITE_WARNING(
"Flow density of " +
toString(probability) +
" vehicles per second, cannot be represented with a simple probability. Falling back to even spacing.");
345 const std::string line = lr.
readLine();
346 if (line[0] !=
'*') {
356 if (time.find(
'.') == std::string::npos) {
359 std::string hours = time.substr(0, time.find(
'.'));
360 std::string minutes = time.substr(time.find(
'.') + 1);
365 std::pair<SUMOTime, SUMOTime>
373 throw ProcessError(
"Begin time is larger than end time.");
375 return std::make_pair(begin, end);
377 throw ProcessError(
"Broken period definition '" + line +
"'.");
379 throw ProcessError(
"Broken period definition '" + line +
"'.");
397 std::string vehType,
bool matrixHasVehType) {
401 if (matrixHasVehType) {
409 std::pair<SUMOTime, SUMOTime> times =
readTime(lr);
420 std::vector<std::string> names;
421 while ((
int)names.size() != numDistricts) {
425 names.push_back(st2.
next());
430 for (std::vector<std::string>::iterator si = names.begin(); si != names.end(); ++si) {
431 std::vector<std::string>::iterator di = names.begin();
435 if (line.length() == 0) {
441 assert(di != names.end());
443 if (vehNumber != 0) {
444 add(vehNumber, begin, end, *si, *di, vehType);
446 if (di == names.end()) {
447 throw ProcessError(
"More entries than districts found.");
452 throw ProcessError(
"Not numeric vehicle number in line '" + line +
"'.");
457 }
while (di != names.end());
465 std::string vehType,
bool matrixHasVehType) {
469 if (matrixHasVehType) {
478 std::pair<SUMOTime, SUMOTime> times =
readTime(lr);
488 if (line.length() == 0) {
492 if (st2.
size() == 0) {
496 std::string sourceD = st2.
next();
497 std::string destD = st2.
next();
499 if (vehNumber != 0) {
500 add(vehNumber, begin, end, sourceD, destD, vehType);
503 throw ProcessError(
"Missing at least one information in line '" + line +
"'.");
505 throw ProcessError(
"Not numeric vehicle number in line '" + line +
"'.");
533 const std::vector<double>& times = ps.
getVals();
534 for (
int i = 0; i < (int)times.size() - 1; ++i) {
542 newCells.push_back(ncell);
551 for (std::vector<ODCell*>::iterator i = oldCells.begin(); i != oldCells.end(); ++i) {
552 std::vector<ODCell*> newCells;
554 copy(newCells.begin(), newCells.end(), back_inserter(
myContainer));
562 std::vector<std::string> files = oc.
getStringVector(
"od-matrix-files");
563 for (std::vector<std::string>::iterator i = files.begin(); i != files.end(); ++i) {
570 if (type.find(
';') != std::string::npos) {
571 type = type.substr(0, type.find(
';'));
574 if (type.length() > 1 && type[1] ==
'V') {
576 if (type.find(
'N') != std::string::npos) {
577 throw ProcessError(
"'" + *i +
"' does not contain the needed information about the time described.");
580 }
else if (type.length() > 1 && type[1] ==
'O') {
582 if (type.find(
'N') != std::string::npos) {
583 throw ProcessError(
"'" + *i +
"' does not contain the needed information about the time described.");
587 throw ProcessError(
"'" + *i +
"' uses an unknown matrix type '" + type +
"'.");
590 std::vector<std::string> amitranFiles = oc.
getStringVector(
"od-amitran-files");
591 for (std::vector<std::string>::iterator i = amitranFiles.begin(); i != amitranFiles.end(); ++i) {
593 throw ProcessError(
"Could not access matrix file '" + *i +
"' to load.");
608 std::vector<std::string> routeFiles = oc.
getStringVector(
"route-files");
609 for (std::vector<std::string>::iterator i = routeFiles.begin(); i != routeFiles.end(); ++i) {
611 throw ProcessError(
"Could not access route file '" + *i +
"' to load.");
626 if (timelineDayInHours) {
627 if (def.size() != 24) {
628 throw ProcessError(
"Assuming 24 entries for a day timeline, but got " +
toString(def.size()) +
".");
630 for (
int chour = 0; chour < 24; ++chour) {
633 result.
add(24 * 3600., 0.);
635 for (
int i = 0; i < (int)def.size(); i++) {
637 if (st2.
size() != 2) {
638 throw ProcessError(
"Broken time line definition: missing a value in '" + def[i] +
"'.");
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.
void writeDefaultAttrs(OutputDevice &dev, const bool noVtype, const ODCell *const cell)
Helper function for flow and trip output writing the depart and arrival attributes.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Used for sorting the cells by the begin time they describe.
std::vector< std::string > getStringVector(const std::string &name) const
Returns the list of string-vector-value of the named option (only for Option_String) ...
static bool isReadable(std::string path)
Checks whether the given file is readable.
a flow definition (used by router)
double getNumWritten() const
Returns the number of written vehicles.
bool readLine(LineHandler &lh)
Reads a single (the next) line from the file and reports it to the given LineHandler.
static const int WHITECHARS
Retrieves a file linewise and reports the lines to a handler.
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
int sinkNumber() const
Returns the number of sinks.
const ODDistrictCont & myDistricts
The districts to retrieve sources/sinks from.
static double rand(std::mt19937 *rng=0)
Returns a random real number in [0, 1)
An internal representation of a single vehicle.
std::string time2string(SUMOTime t)
weights: time range begin
void setPrecision(int precision=gPrecision)
Sets the precison or resets it to default.
double getOverallProb() const
Return the sum of the probabilites assigned to the members.
double getNumDiscarded() const
Returns the number of discarded vehicles.
const std::vector< double > & getProbs() const
Returns the probabilities assigned to the members of the distribution.
SAX-handler base for SUMO-files.
std::string from
The edge the vehicles shall start at.
static bool runParser(GenericSAXHandler &handler, const std::string &file, const bool isNet=false)
Runs the given handler on the given file; returns if everything's ok.
SUMOTime parseSingleTime(const std::string &time)
double vehicleNumber
The number of vehicles.
#define WRITE_WARNING(msg)
static OptionsCont & getOptions()
Retrieves the options.
const std::vector< T > & getVals() const
Returns the members of the distribution.
void loadMatrix(OptionsCont &oc)
read a matrix in one of several formats
T get(const std::string &id) const
Retrieves an item.
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
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".
#define PROGRESS_FAILED_MESSAGE()
double myNumDiscarded
Number of discarded vehicles.
A single O/D-matrix cell.
double myNumWritten
Number of written vehicles.
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
std::string origin
Name of the origin district.
std::string getRandomSourceFromDistrict(const std::string &name) const
Returns the id of a random source from the named district.
ODCell * cell
The cell of the ODMatrix which generated the vehicle.
void loadRoutes(OptionsCont &oc, SUMOSAXHandler &handler)
read SUMO routes
SUMOTime string2time(const std::string &r)
A container for districts.
#define PROGRESS_BEGIN_MESSAGE(msg)
Used for sorting vehicles by their departure (latest first)
double readFactor(LineReader &lr, double scale)
std::map< SUMOTime, std::vector< std::string > > departures
mapping of departure times to departing vehicles, if already fixed
std::map< const std::pair< const std::string, const std::string >, std::vector< ODCell * > > myShortCut
The loaded cells indexed by origin and destination.
std::vector< ODCell * > myContainer
The loaded cells.
SUMOTime begin
The begin time this cell describes.
bool good() const
Returns the information whether the stream is readable.
std::string getFileName() const
Returns the name of the used file.
std::pair< SUMOTime, SUMOTime > readTime(LineReader &lr)
static int _2int(const E *const data)
converts a char-type array into the integer value described by it
std::string getNextNonCommentLine(LineReader &lr)
SUMOTime depart
The departure time of the vehicle.
static std::string prune(const std::string &str)
Removes trailing and leading whitechars.
bool add(double vehicleNumber, SUMOTime begin, SUMOTime end, const std::string &origin, const std::string &destination, const std::string &vehicleType)
Builds a single cell from the given values, verifying them.
A storage for options typed value containers)
static double _2double(const E *const data)
converts a char-type array into the double value described by it
double getNumLoaded() const
Returns the number of loaded vehicles.
An XML-Handler for districts.
void applyCurve(const Distribution_Points &ps)
Splits the stored cells dividing them on the given time line.
void readV(LineReader &lr, double scale, std::string vehType, bool matrixHasVehType)
read a VISUM-matrix with the V Format
std::string vehicleType
Name of the vehicle type.
void readO(LineReader &lr, double scale, std::string vehType, bool matrixHasVehType)
read a VISUM-matrix with the O Format
Static storage of an output device and its base (abstract) implementation.
std::string destination
Name of the destination district.
bool closeTag()
Closes the most recently opened tag.
a single trip definition (used by router)
bool hasMore() const
Returns whether another line may be read (the file was not read completely)
std::string getRandomSinkFromDistrict(const std::string &name) const
Returns the id of a random sink from the named district.
SUMOTime end
The end time this cell describes.
#define PROGRESS_DONE_MESSAGE()
std::string to
The edge the vehicles shall end at.
bool add(T val, double prob, bool checkDuplicates=true)
Adds a value with an assigned probability to the distribution.
ODMatrix(const ODDistrictCont &dc)
Constructor.
void writeFlows(const SUMOTime begin, const SUMOTime end, OutputDevice &dev, const bool noVtype, const std::string &prefix, bool asProbability=false)
Writes the flows stored in the matrix.
std::set< std::string > myMissingDistricts
The missing districts already warned about.
std::string id
The id of the vehicle.
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
double myNumLoaded
Number of loaded vehicles.
int sourceNumber() const
Returns the number of sources.
Distribution_Points parseTimeLine(const std::vector< std::string > &def, bool timelineDayInHours)
split the given timeline
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.