SUMO - Simulation of Urban MObility
RODFDetector.cpp
Go to the documentation of this file.
1 /****************************************************************************/
13 // Class representing a detector within the DFROUTER
14 /****************************************************************************/
15 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
16 // Copyright (C) 2006-2014 DLR (http://www.dlr.de/) and contributors
17 /****************************************************************************/
18 //
19 // This file is part of SUMO.
20 // SUMO is free software: you can redistribute it and/or modify
21 // it under the terms of the GNU General Public License as published by
22 // the Free Software Foundation, either version 3 of the License, or
23 // (at your option) any later version.
24 //
25 /****************************************************************************/
26 
27 
28 // ===========================================================================
29 // included modules
30 // ===========================================================================
31 #ifdef _MSC_VER
32 #include <windows_config.h>
33 #else
34 #include <config.h>
35 #endif
36 
37 #include <cassert>
38 #include "RODFDetector.h"
42 #include <utils/common/ToString.h>
43 #include <router/ROEdge.h>
44 #include "RODFEdge.h"
45 #include "RODFRouteDesc.h"
46 #include "RODFRouteCont.h"
47 #include "RODFDetectorFlow.h"
49 #include <utils/common/StdDefs.h>
51 #include <utils/geom/GeomHelper.h>
52 #include "RODFNet.h"
56 
57 #ifdef CHECK_MEMORY_LEAKS
58 #include <foreign/nvwa/debug_new.h>
59 #endif // CHECK_MEMORY_LEAKS
60 
61 
62 // ===========================================================================
63 // method definitions
64 // ===========================================================================
65 RODFDetector::RODFDetector(const std::string& id, const std::string& laneID,
66  SUMOReal pos, const RODFDetectorType type)
67  : Named(id), myLaneID(laneID), myPosition(pos), myType(type), myRoutes(0) {}
68 
69 
70 RODFDetector::RODFDetector(const std::string& id, const RODFDetector& f)
71  : Named(id), myLaneID(f.myLaneID), myPosition(f.myPosition),
72  myType(f.myType), myRoutes(0) {
73  if (f.myRoutes != 0) {
74  myRoutes = new RODFRouteCont(*(f.myRoutes));
75  }
76 }
77 
78 
80  delete myRoutes;
81 }
82 
83 
84 void
86  myType = type;
87 }
88 
89 
92  SUMOReal distance = rd.edges2Pass[0]->getFromNode()->getPosition().distanceTo(rd.edges2Pass.back()->getToNode()->getPosition());
93  SUMOReal length = 0;
94  for (std::vector<ROEdge*>::const_iterator i = rd.edges2Pass.begin(); i != rd.edges2Pass.end(); ++i) {
95  length += (*i)->getLength();
96  }
97  return (distance / length);
98 }
99 
100 
101 void
103  const RODFDetectorFlows& flows,
104  SUMOTime startTime, SUMOTime endTime, SUMOTime stepOffset) {
105  if (myRoutes == 0) {
106  return;
107  }
108  // compute edges to determine split probabilities
109  const std::vector<RODFRouteDesc>& routes = myRoutes->get();
110  std::vector<RODFEdge*> nextDetEdges;
111  std::set<ROEdge*> preSplitEdges;
112  for (std::vector<RODFRouteDesc>::const_iterator i = routes.begin(); i != routes.end(); ++i) {
113  const RODFRouteDesc& rd = *i;
114  bool hadSplit = false;
115  for (std::vector<ROEdge*>::const_iterator j = rd.edges2Pass.begin(); j != rd.edges2Pass.end(); ++j) {
116  if (hadSplit && net->hasDetector(*j)) {
117  if (find(nextDetEdges.begin(), nextDetEdges.end(), *j) == nextDetEdges.end()) {
118  nextDetEdges.push_back(static_cast<RODFEdge*>(*j));
119  }
120  myRoute2Edge[rd.routename] = static_cast<RODFEdge*>(*j);
121  break;
122  }
123  if (!hadSplit) {
124  preSplitEdges.insert(*j);
125  }
126  if ((*j)->getNoFollowing() > 1) {
127  hadSplit = true;
128  }
129  }
130  }
131  std::map<ROEdge*, SUMOReal> inFlows;
132  if (OptionsCont::getOptions().getBool("respect-concurrent-inflows")) {
133  for (std::vector<RODFEdge*>::const_iterator i = nextDetEdges.begin(); i != nextDetEdges.end(); ++i) {
134  std::set<ROEdge*> seen(preSplitEdges);
135  std::vector<ROEdge*> pending;
136  pending.push_back(*i);
137  seen.insert(*i);
138  while (!pending.empty()) {
139  ROEdge* e = pending.back();
140  pending.pop_back();
141  const unsigned int numAppr = e->getNumApproaching();
142  for (unsigned int j = 0; j < numAppr; j++) {
143  ROEdge* e2 = e->getApproaching(j);
144  if (e2->getNoFollowing() == 1 && seen.count(e2) == 0) {
145  if (net->hasDetector(e2)) {
146  inFlows[*i] += detectors.getAggFlowFor(e2, 0, 0, flows);
147  } else {
148  pending.push_back(e2);
149  }
150  seen.insert(e2);
151  }
152  }
153  }
154  }
155  }
156  // compute the probabilities to use a certain direction
157  int index = 0;
158  for (SUMOTime time = startTime; time < endTime; time += stepOffset, ++index) {
159  mySplitProbabilities.push_back(std::map<RODFEdge*, SUMOReal>());
160  SUMOReal overallProb = 0;
161  // retrieve the probabilities
162  for (std::vector<RODFEdge*>::const_iterator i = nextDetEdges.begin(); i != nextDetEdges.end(); ++i) {
163  SUMOReal flow = detectors.getAggFlowFor(*i, time, 60, flows) - inFlows[*i];
164  overallProb += flow;
165  mySplitProbabilities[index][*i] = flow;
166  }
167  // norm probabilities
168  if (overallProb > 0) {
169  for (std::vector<RODFEdge*>::const_iterator i = nextDetEdges.begin(); i != nextDetEdges.end(); ++i) {
170  mySplitProbabilities[index][*i] = mySplitProbabilities[index][*i] / overallProb;
171  }
172  }
173  }
174 }
175 
176 
177 void
179  SUMOTime startTime, SUMOTime endTime, SUMOTime stepOffset,
180  const RODFNet& net,
181  std::map<size_t, RandomDistributor<size_t>* >& into) const {
182  if (myRoutes == 0) {
184  WRITE_ERROR("Missing routes for detector '" + myID + "'.");
185  }
186  return;
187  }
188  std::vector<RODFRouteDesc>& descs = myRoutes->get();
189  // iterate through time (in output interval steps)
190  for (SUMOTime time = startTime; time < endTime; time += stepOffset) {
191  into[time] = new RandomDistributor<size_t>();
192  std::map<ROEdge*, SUMOReal> flowMap;
193  // iterate through the routes
194  size_t index = 0;
195  for (std::vector<RODFRouteDesc>::iterator ri = descs.begin(); ri != descs.end(); ++ri, index++) {
196  SUMOReal prob = 1.;
197  for (std::vector<ROEdge*>::iterator j = (*ri).edges2Pass.begin(); j != (*ri).edges2Pass.end() && prob > 0;) {
198  if (!net.hasDetector(*j)) {
199  ++j;
200  continue;
201  }
202  const RODFDetector& det = detectors.getAnyDetectorForEdge(static_cast<RODFEdge*>(*j));
203  const std::vector<std::map<RODFEdge*, SUMOReal> >& probs = det.getSplitProbabilities();
204  if (probs.size() == 0) {
205  prob = 0;
206  ++j;
207  continue;
208  }
209  const std::map<RODFEdge*, SUMOReal>& tprobs = probs[(time - startTime) / stepOffset];
210  RODFEdge* splitEdge = 0;
211  for (std::map<RODFEdge*, SUMOReal>::const_iterator k = tprobs.begin(); k != tprobs.end(); ++k) {
212  if (find(j, (*ri).edges2Pass.end(), (*k).first) != (*ri).edges2Pass.end()) {
213  prob *= (*k).second;
214  splitEdge = (*k).first;
215  break;
216  }
217  }
218  if (splitEdge != 0) {
219  j = find(j, (*ri).edges2Pass.end(), splitEdge);
220  } else {
221  ++j;
222  }
223  }
224  into[time]->add(prob, index);
225  (*ri).overallProb = prob;
226  }
227  }
228 }
229 
230 
231 const std::vector<RODFRouteDesc>&
233  return myRoutes->get();
234 }
235 
236 
237 void
239  myPriorDetectors.insert(det);
240 }
241 
242 
243 void
245  myFollowingDetectors.insert(det);
246 }
247 
248 
249 const std::set<const RODFDetector*>&
251  return myPriorDetectors;
252 }
253 
254 
255 const std::set<const RODFDetector*>&
257  return myFollowingDetectors;
258 }
259 
260 
261 
262 void
264  delete myRoutes;
265  myRoutes = routes;
266 }
267 
268 
269 void
271  if (myRoutes == 0) {
272  myRoutes = new RODFRouteCont();
273  }
274  myRoutes->addRouteDesc(nrd);
275 }
276 
277 
278 bool
280  return myRoutes != 0 && myRoutes->get().size() != 0;
281 }
282 
283 
284 bool
285 RODFDetector::writeEmitterDefinition(const std::string& file,
286  const std::map<size_t, RandomDistributor<size_t>* >& dists,
287  const RODFDetectorFlows& flows,
288  SUMOTime startTime, SUMOTime endTime,
289  SUMOTime stepOffset,
290  bool includeUnusedRoutes,
291  SUMOReal scale,
292  bool insertionsOnly,
293  SUMOReal defaultSpeed) const {
296  if (getType() != SOURCE_DETECTOR) {
297  out.writeXMLHeader("calibrator");
298  }
299  // routes
300  if (myRoutes != 0 && myRoutes->get().size() != 0) {
301  const std::vector<RODFRouteDesc>& routes = myRoutes->get();
303  bool isEmptyDist = true;
304  for (std::vector<RODFRouteDesc>::const_iterator i = routes.begin(); i != routes.end(); ++i) {
305  if ((*i).overallProb > 0) {
306  isEmptyDist = false;
307  }
308  }
309  for (std::vector<RODFRouteDesc>::const_iterator i = routes.begin(); i != routes.end(); ++i) {
310  if ((*i).overallProb > 0 || includeUnusedRoutes) {
311  out.openTag(SUMO_TAG_ROUTE).writeAttr(SUMO_ATTR_REFID, (*i).routename).writeAttr(SUMO_ATTR_PROB, (*i).overallProb).closeTag();
312  }
313  if (isEmptyDist) {
315  }
316  }
317  out.closeTag(); // routeDistribution
318  } else {
319  WRITE_ERROR("Detector '" + getID() + "' has no routes!?");
320  return false;
321  }
322  // insertions
323  if (insertionsOnly || flows.knows(myID)) {
324  // get the flows for this detector
325  const std::vector<FlowDef>& mflows = flows.getFlowDefs(myID);
326  // go through the simulation seconds
327  unsigned int index = 0;
328  for (SUMOTime time = startTime; time < endTime; time += stepOffset, index++) {
329  // get own (departure flow)
330  assert(index < mflows.size());
331  const FlowDef& srcFD = mflows[index]; // !!! check stepOffset
332  // get flows at end
333  RandomDistributor<size_t>* destDist = dists.find(time) != dists.end() ? dists.find(time)->second : 0;
334  // go through the cars
335  size_t carNo = (size_t)((srcFD.qPKW + srcFD.qLKW) * scale);
336  for (size_t car = 0; car < carNo; ++car) {
337  // get the vehicle parameter
338  SUMOReal v = -1;
339  std::string vtype;
340  int destIndex = destDist != 0 && destDist->getOverallProb() > 0 ? (int) destDist->get() : -1;
341  if (srcFD.isLKW >= 1) {
342  srcFD.isLKW = srcFD.isLKW - (SUMOReal) 1.;
343  v = srcFD.vLKW;
344  vtype = "LKW";
345  } else {
346  v = srcFD.vPKW;
347  vtype = "PKW";
348  }
349  // compute insertion speed
350  if (v <= 0 || v > 250) {
351  v = defaultSpeed;
352  } else {
353  v = (SUMOReal)(v / 3.6);
354  }
355  // compute the departure time
356  SUMOTime ctime = (SUMOTime)(time + ((SUMOReal) stepOffset * (SUMOReal) car / (SUMOReal) carNo));
357 
358  // write
360  if (getType() == SOURCE_DETECTOR) {
361  out.writeAttr(SUMO_ATTR_ID, "emitter_" + myID + "_" + toString(ctime));
362  } else {
363  out.writeAttr(SUMO_ATTR_ID, "calibrator_" + myID + "_" + toString(ctime));
364  }
365  if (oc.getBool("vtype")) {
366  out.writeAttr(SUMO_ATTR_TYPE, vtype);
367  }
369  if (oc.isSet("departlane")) {
370  out.writeNonEmptyAttr(SUMO_ATTR_DEPARTLANE, oc.getString("departlane"));
371  } else {
372  out.writeAttr(SUMO_ATTR_DEPARTLANE, TplConvert::_2int(myLaneID.substr(myLaneID.rfind("_") + 1).c_str()));
373  }
374  if (oc.isSet("departpos")) {
375  std::string posDesc = oc.getString("departpos");
376  if (posDesc.substr(0, 8) == "detector") {
377  SUMOReal position = myPosition;
378  if (posDesc.length() > 8) {
379  if (posDesc[8] == '+') {
380  position += TplConvert::_2SUMOReal(posDesc.substr(9).c_str());
381  } else if (posDesc[8] == '-') {
382  position -= TplConvert::_2SUMOReal(posDesc.substr(9).c_str());
383  } else {
384  throw NumberFormatException();
385  }
386  }
387  out.writeAttr(SUMO_ATTR_DEPARTPOS, position);
388  } else {
390  }
391  } else {
393  }
394  if (oc.isSet("departspeed")) {
395  out.writeNonEmptyAttr(SUMO_ATTR_DEPARTSPEED, oc.getString("departspeed"));
396  } else {
397  if (v > defaultSpeed) {
398  out.writeAttr(SUMO_ATTR_DEPARTSPEED, "max");
399  } else {
401  }
402  }
403  if (oc.isSet("arrivallane")) {
404  out.writeNonEmptyAttr(SUMO_ATTR_ARRIVALLANE, oc.getString("arrivallane"));
405  }
406  if (oc.isSet("arrivalpos")) {
407  out.writeNonEmptyAttr(SUMO_ATTR_ARRIVALPOS, oc.getString("arrivalpos"));
408  }
409  if (oc.isSet("arrivalspeed")) {
410  out.writeNonEmptyAttr(SUMO_ATTR_ARRIVALSPEED, oc.getString("arrivalspeed"));
411  }
412  if (destIndex >= 0) {
413  out.writeAttr(SUMO_ATTR_ROUTE, myRoutes->get()[destIndex].routename);
414  } else {
416  }
417  out.closeTag();
418  srcFD.isLKW += srcFD.fLKW;
419  }
420  }
421  }
422  if (getType() != SOURCE_DETECTOR) {
423  out.close();
424  }
425  return true;
426 }
427 
428 
429 bool
430 RODFDetector::writeRoutes(std::vector<std::string>& saved,
431  OutputDevice& out) {
432  if (myRoutes != 0) {
433  return myRoutes->save(saved, "", out);
434  }
435  return false;
436 }
437 
438 
439 void
441  const RODFDetectorFlows& flows,
442  SUMOTime startTime, SUMOTime endTime,
443  SUMOTime stepOffset, SUMOReal defaultSpeed) {
445  out.writeXMLHeader("vss");
446  const std::vector<FlowDef>& mflows = flows.getFlowDefs(myID);
447  unsigned int index = 0;
448  for (SUMOTime t = startTime; t < endTime; t += stepOffset, index++) {
449  assert(index < mflows.size());
450  const FlowDef& srcFD = mflows[index];
451  SUMOReal speed = MAX2(srcFD.vLKW, srcFD.vPKW);
452  if (speed <= 0 || speed > 250) {
453  speed = defaultSpeed;
454  } else {
455  speed = (SUMOReal)(speed / 3.6);
456  }
458  }
459  out.close();
460 }
461 
462 
463 
464 
465 
466 
467 
468 
469 
470 
472 
473 
475  for (std::vector<RODFDetector*>::iterator i = myDetectors.begin(); i != myDetectors.end(); ++i) {
476  delete *i;
477  }
478 }
479 
480 
481 bool
483  if (myDetectorMap.find(dfd->getID()) != myDetectorMap.end()) {
484  return false;
485  }
486  myDetectorMap[dfd->getID()] = dfd;
487  myDetectors.push_back(dfd);
488  std::string edgeid = dfd->getLaneID().substr(0, dfd->getLaneID().rfind('_'));
489  if (myDetectorEdgeMap.find(edgeid) == myDetectorEdgeMap.end()) {
490  myDetectorEdgeMap[edgeid] = std::vector<RODFDetector*>();
491  }
492  myDetectorEdgeMap[edgeid].push_back(dfd);
493  return true; // !!!
494 }
495 
496 
497 bool
499  for (std::vector<RODFDetector*>::const_iterator i = myDetectors.begin(); i != myDetectors.end(); ++i) {
500  if ((*i)->getType() == TYPE_NOT_DEFINED) {
501  return false;
502  }
503  }
504  return true;
505 }
506 
507 
508 bool
510  for (std::vector<RODFDetector*>::const_iterator i = myDetectors.begin(); i != myDetectors.end(); ++i) {
511  if ((*i)->hasRoutes()) {
512  return true;
513  }
514  }
515  return false;
516 }
517 
518 
519 const std::vector< RODFDetector*>&
521  return myDetectors;
522 }
523 
524 
525 void
526 RODFDetectorCon::save(const std::string& file) const {
528  out.writeXMLHeader("detectors");
529  for (std::vector<RODFDetector*>::const_iterator i = myDetectors.begin(); i != myDetectors.end(); ++i) {
530  out.openTag(SUMO_TAG_DETECTOR_DEFINITION).writeAttr(SUMO_ATTR_ID, StringUtils::escapeXML((*i)->getID())).writeAttr(SUMO_ATTR_LANE, (*i)->getLaneID()).writeAttr(SUMO_ATTR_POSITION, (*i)->getPos());
531  switch ((*i)->getType()) {
532  case BETWEEN_DETECTOR:
533  out.writeAttr(SUMO_ATTR_TYPE, "between");
534  break;
535  case SOURCE_DETECTOR:
536  out.writeAttr(SUMO_ATTR_TYPE, "source");
537  break;
538  case SINK_DETECTOR:
539  out.writeAttr(SUMO_ATTR_TYPE, "sink");
540  break;
541  case DISCARDED_DETECTOR:
542  out.writeAttr(SUMO_ATTR_TYPE, "discarded");
543  break;
544  default:
545  throw 1;
546  }
547  out.closeTag();
548  }
549  out.close();
550 }
551 
552 
553 void
554 RODFDetectorCon::saveAsPOIs(const std::string& file) const {
556  out.writeXMLHeader("pois");
557  for (std::vector<RODFDetector*>::const_iterator i = myDetectors.begin(); i != myDetectors.end(); ++i) {
559  switch ((*i)->getType()) {
560  case BETWEEN_DETECTOR:
561  out.writeAttr(SUMO_ATTR_TYPE, "between_detector_position").writeAttr(SUMO_ATTR_COLOR, RGBColor::BLUE);
562  break;
563  case SOURCE_DETECTOR:
564  out.writeAttr(SUMO_ATTR_TYPE, "source_detector_position").writeAttr(SUMO_ATTR_COLOR, RGBColor::GREEN);
565  break;
566  case SINK_DETECTOR:
567  out.writeAttr(SUMO_ATTR_TYPE, "sink_detector_position").writeAttr(SUMO_ATTR_COLOR, RGBColor::RED);
568  break;
569  case DISCARDED_DETECTOR:
570  out.writeAttr(SUMO_ATTR_TYPE, "discarded_detector_position").writeAttr(SUMO_ATTR_COLOR, RGBColor(51, 51, 51, 255));
571  break;
572  default:
573  throw 1;
574  }
575  out.writeAttr(SUMO_ATTR_LANE, (*i)->getLaneID()).writeAttr(SUMO_ATTR_POSITION, (*i)->getPos()).closeTag();
576  }
577  out.close();
578 }
579 
580 
581 void
582 RODFDetectorCon::saveRoutes(const std::string& file) const {
584  out.writeXMLHeader("routes");
585  std::vector<std::string> saved;
586  // write for source detectors
587  bool lastWasSaved = true;
588  for (std::vector<RODFDetector*>::const_iterator i = myDetectors.begin(); i != myDetectors.end(); ++i) {
589  if ((*i)->getType() != SOURCE_DETECTOR) {
590  // do not build routes for other than sources
591  continue;
592  }
593  if (lastWasSaved) {
594  out << "\n";
595  }
596  lastWasSaved = (*i)->writeRoutes(saved, out);
597  }
598  out << "\n";
599  out.close();
600 }
601 
602 
603 const RODFDetector&
604 RODFDetectorCon::getDetector(const std::string& id) const {
605  return *(myDetectorMap.find(id)->second);
606 }
607 
608 
610 RODFDetectorCon::getModifiableDetector(const std::string& id) const {
611  return *(myDetectorMap.find(id)->second);
612 }
613 
614 
615 bool
616 RODFDetectorCon::knows(const std::string& id) const {
617  return myDetectorMap.find(id) != myDetectorMap.end();
618 }
619 
620 
621 void
622 RODFDetectorCon::writeEmitters(const std::string& file,
623  const RODFDetectorFlows& flows,
624  SUMOTime startTime, SUMOTime endTime,
625  SUMOTime stepOffset, const RODFNet& net,
626  bool writeCalibrators,
627  bool includeUnusedRoutes,
628  SUMOReal scale,
629  bool insertionsOnly) {
630  // compute turn probabilities at detector
631  for (std::vector<RODFDetector*>::const_iterator i = myDetectors.begin(); i != myDetectors.end(); ++i) {
632  (*i)->computeSplitProbabilities(&net, *this, flows, startTime, endTime, stepOffset);
633  }
634  //
636  out.writeXMLHeader("additional", "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://sumo-sim.org/xsd/additional_file.xsd\"");
637  for (std::vector<RODFDetector*>::const_iterator i = myDetectors.begin(); i != myDetectors.end(); ++i) {
638  RODFDetector* det = *i;
639  // get file name for values (emitter/calibrator definition)
640  std::string escapedID = StringUtils::escapeXML(det->getID());
641  std::string defFileName;
642  if (det->getType() == SOURCE_DETECTOR) {
643  defFileName = file;
644  } else if (writeCalibrators && det->getType() == BETWEEN_DETECTOR) {
645  defFileName = FileHelpers::getFilePath(file) + "calibrator_" + escapedID + ".def.xml";
646  } else {
647  defFileName = FileHelpers::getFilePath(file) + "other_" + escapedID + ".def.xml";
648  continue;
649  }
650  // try to write the definition
651  SUMOReal defaultSpeed = net.getEdge(det->getEdgeID())->getSpeed();
652  // ... compute routes' distribution over time
653  std::map<size_t, RandomDistributor<size_t>* > dists;
654  if (!insertionsOnly && flows.knows(det->getID())) {
655  det->buildDestinationDistribution(*this, startTime, endTime, stepOffset, net, dists);
656  }
657  // ... write the definition
658  if (!det->writeEmitterDefinition(defFileName, dists, flows, startTime, endTime, stepOffset, includeUnusedRoutes, scale, insertionsOnly, defaultSpeed)) {
659  // skip if something failed... (!!!)
660  continue;
661  }
662  // ... clear temporary values
663  clearDists(dists);
664  // write the declaration into the file
665  if (writeCalibrators && det->getType() == BETWEEN_DETECTOR) {
666  out.openTag(SUMO_TAG_CALIBRATOR).writeAttr(SUMO_ATTR_ID, "calibrator_" + escapedID).writeAttr(SUMO_ATTR_POSITION, det->getPos());
667  out.writeAttr(SUMO_ATTR_LANE, det->getLaneID()).writeAttr(SUMO_ATTR_FRIENDLY_POS, true).writeAttr(SUMO_ATTR_FILE, defFileName).closeTag();
668  }
669  }
670  out.close();
671 }
672 
673 
674 void
675 RODFDetectorCon::writeEmitterPOIs(const std::string& file,
676  const RODFDetectorFlows& flows) {
678  out.writeXMLHeader("additional", "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://sumo-sim.org/xsd/additional_file.xsd\"");
679  for (std::vector<RODFDetector*>::const_iterator i = myDetectors.begin(); i != myDetectors.end(); ++i) {
680  RODFDetector* det = *i;
681  SUMOReal flow = flows.getFlowSumSecure(det->getID());
682  const unsigned char col = static_cast<unsigned char>(128 * flow / flows.getMaxDetectorFlow() + 128);
683  out.openTag(SUMO_TAG_POI).writeAttr(SUMO_ATTR_ID, StringUtils::escapeXML((*i)->getID()) + ":" + toString(flow));
684  switch ((*i)->getType()) {
685  case BETWEEN_DETECTOR:
686  out.writeAttr(SUMO_ATTR_TYPE, "between_detector_position").writeAttr(SUMO_ATTR_COLOR, RGBColor(0, 0, col, 255));
687  break;
688  case SOURCE_DETECTOR:
689  out.writeAttr(SUMO_ATTR_TYPE, "source_detector_position").writeAttr(SUMO_ATTR_COLOR, RGBColor(0, col, 0, 255));
690  break;
691  case SINK_DETECTOR:
692  out.writeAttr(SUMO_ATTR_TYPE, "sink_detector_position").writeAttr(SUMO_ATTR_COLOR, RGBColor(col, 0, 0, 255));
693  break;
694  case DISCARDED_DETECTOR:
695  out.writeAttr(SUMO_ATTR_TYPE, "discarded_detector_position").writeAttr(SUMO_ATTR_COLOR, RGBColor(51, 51, 51, 255));
696  break;
697  default:
698  throw 1;
699  }
700  out.writeAttr(SUMO_ATTR_LANE, (*i)->getLaneID()).writeAttr(SUMO_ATTR_POSITION, (*i)->getPos()).closeTag();
701  }
702  out.close();
703 }
704 
705 
706 int
708  const RODFDetectorFlows&) const {
709  UNUSED_PARAMETER(period);
710  UNUSED_PARAMETER(time);
711  if (edge == 0) {
712  return 0;
713  }
714 // SUMOReal stepOffset = 60; // !!!
715 // SUMOReal startTime = 0; // !!!
716 // cout << edge->getID() << endl;
717  assert(myDetectorEdgeMap.find(edge->getID()) != myDetectorEdgeMap.end());
718  const std::vector<FlowDef>& flows = static_cast<const RODFEdge*>(edge)->getFlows();
719  SUMOReal agg = 0;
720  for (std::vector<FlowDef>::const_iterator i = flows.begin(); i != flows.end(); ++i) {
721  const FlowDef& srcFD = *i;
722  if (srcFD.qLKW >= 0) {
723  agg += srcFD.qLKW;
724  }
725  if (srcFD.qPKW >= 0) {
726  agg += srcFD.qPKW;
727  }
728  }
729  return (int) agg;
730  /* !!! make this time variable
731  if (flows.size()!=0) {
732  SUMOReal agg = 0;
733  size_t beginIndex = (int)((time/stepOffset) - startTime); // !!! falsch!!!
734  for (SUMOTime t=0; t<period&&beginIndex<flows.size(); t+=(SUMOTime) stepOffset) {
735  const FlowDef &srcFD = flows[beginIndex++];
736  if (srcFD.qLKW>=0) {
737  agg += srcFD.qLKW;
738  }
739  if (srcFD.qPKW>=0) {
740  agg += srcFD.qPKW;
741  }
742  }
743  return (int) agg;
744  }
745  */
746 // return -1;
747 }
748 
749 
750 void
752  const std::string& file,
753  const RODFDetectorFlows& flows,
754  SUMOTime startTime, SUMOTime endTime,
755  SUMOTime stepOffset) {
757  out.writeXMLHeader("additional", "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://sumo-sim.org/xsd/additional_file.xsd\"");
758  for (std::vector<RODFDetector*>::const_iterator i = myDetectors.begin(); i != myDetectors.end(); ++i) {
759  RODFDetector* det = *i;
760  // write the declaration into the file
761  if (det->getType() == SINK_DETECTOR && flows.knows(det->getID())) {
762  std::string filename = FileHelpers::getFilePath(file) + "vss_" + det->getID() + ".def.xml";
764  SUMOReal defaultSpeed = net != 0 ? net->getEdge(det->getEdgeID())->getSpeed() : (SUMOReal) 200.;
765  det->writeSingleSpeedTrigger(filename, flows, startTime, endTime, stepOffset, defaultSpeed);
766  }
767  }
768  out.close();
769 }
770 
771 
772 void
775  out.writeXMLHeader("additional", "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://sumo-sim.org/xsd/additional_file.xsd\"");
776  for (std::vector<RODFDetector*>::const_iterator i = myDetectors.begin(); i != myDetectors.end(); ++i) {
777  RODFDetector* det = *i;
778  // write the declaration into the file
779  if (det->getType() == SINK_DETECTOR) {
781  out.writeAttr(SUMO_ATTR_POSITION, SUMOReal(0)).writeAttr(SUMO_ATTR_FILE, "endrerouter_" + det->getID() + ".def.xml").closeTag();
782  }
783  }
784  out.close();
785 }
786 
787 
788 void
790  bool includeSources,
791  bool singleFile, bool friendly) {
793  out.writeXMLHeader("additional");
794  for (std::vector<RODFDetector*>::const_iterator i = myDetectors.begin(); i != myDetectors.end(); ++i) {
795  RODFDetector* det = *i;
796  // write the declaration into the file
797  if (det->getType() != SOURCE_DETECTOR || includeSources) {
798  SUMOReal pos = det->getPos();
799  if (det->getType() == SOURCE_DETECTOR) {
800  pos += 1;
801  }
804  if (friendly) {
806  }
807  if (!singleFile) {
808  out.writeAttr(SUMO_ATTR_FILE, "validation_det_" + StringUtils::escapeXML(det->getID()) + ".xml");
809  } else {
810  out.writeAttr(SUMO_ATTR_FILE, "validation_dets.xml");
811  }
812  out.closeTag();
813  }
814  }
815  out.close();
816 }
817 
818 
819 void
820 RODFDetectorCon::removeDetector(const std::string& id) {
821  //
822  std::map<std::string, RODFDetector*>::iterator ri1 = myDetectorMap.find(id);
823  RODFDetector* oldDet = (*ri1).second;
824  myDetectorMap.erase(ri1);
825  //
826  std::vector<RODFDetector*>::iterator ri2 =
827  find(myDetectors.begin(), myDetectors.end(), oldDet);
828  myDetectors.erase(ri2);
829  //
830  bool found = false;
831  for (std::map<std::string, std::vector<RODFDetector*> >::iterator rr3 = myDetectorEdgeMap.begin(); !found && rr3 != myDetectorEdgeMap.end(); ++rr3) {
832  std::vector<RODFDetector*>& dets = (*rr3).second;
833  for (std::vector<RODFDetector*>::iterator ri3 = dets.begin(); !found && ri3 != dets.end();) {
834  if (*ri3 == oldDet) {
835  found = true;
836  ri3 = dets.erase(ri3);
837  } else {
838  ++ri3;
839  }
840  }
841  }
842  delete oldDet;
843 }
844 
845 
846 void
848  // routes must be built (we have ensured this in main)
849  // detector followers/prior must be build (we have ensured this in main)
850  //
851  for (std::vector<RODFDetector*>::const_iterator i = myDetectors.begin(); i != myDetectors.end(); ++i) {
852  RODFDetector* det = *i;
853  const std::set<const RODFDetector*>& prior = det->getPriorDetectors();
854  const std::set<const RODFDetector*>& follower = det->getFollowerDetectors();
855  size_t noFollowerWithRoutes = 0;
856  size_t noPriorWithRoutes = 0;
857  // count occurences of detectors with/without routes
858  std::set<const RODFDetector*>::const_iterator j;
859  for (j = prior.begin(); j != prior.end(); ++j) {
860  if (flows.knows((*j)->getID())) {
861  ++noPriorWithRoutes;
862  }
863  }
864  for (j = follower.begin(); j != follower.end(); ++j) {
865  if (flows.knows((*j)->getID())) {
866  ++noFollowerWithRoutes;
867  }
868  }
869 
870  // do not process detectors which have no routes
871  if (!flows.knows(det->getID())) {
872  continue;
873  }
874 
875  // plain case: all of the prior detectors have routes
876  if (noPriorWithRoutes == prior.size()) {
877  // the number of vehicles is the sum of all vehicles on prior
878  continue;
879  }
880 
881  // plain case: all of the follower detectors have routes
882  if (noFollowerWithRoutes == follower.size()) {
883  // the number of vehicles is the sum of all vehicles on follower
884  continue;
885  }
886 
887  }
888 }
889 
890 
891 const RODFDetector&
893  for (std::vector<RODFDetector*>::const_iterator i = myDetectors.begin(); i != myDetectors.end(); ++i) {
894  if ((*i)->getEdgeID() == edge->getID()) {
895  return **i;
896  }
897  }
898  throw 1;
899 }
900 
901 
902 void
903 RODFDetectorCon::clearDists(std::map<size_t, RandomDistributor<size_t>* >& dists) const {
904  for (std::map<size_t, RandomDistributor<size_t>* >::iterator i = dists.begin(); i != dists.end(); ++i) {
905  delete(*i).second;
906  }
907 }
908 
909 
910 void
911 RODFDetectorCon::mesoJoin(const std::string& nid,
912  const std::vector<std::string>& oldids) {
913  // build the new detector
914  const RODFDetector& first = getDetector(*(oldids.begin()));
915  RODFDetector* newDet = new RODFDetector(nid, first);
916  addDetector(newDet);
917  // delete previous
918  for (std::vector<std::string>::const_iterator i = oldids.begin(); i != oldids.end(); ++i) {
919  removeDetector(*i);
920  }
921 }
922 
923 
924 /****************************************************************************/
std::vector< RODFDetector * > myDetectors
Definition: RODFDetector.h:280
RODFDetector & getModifiableDetector(const std::string &id) const
bool knows(const std::string &id) const
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:257
static const RGBColor BLUE
Definition: RGBColor.h:191
void close()
Closes the device and removes it from the dictionary.
void addRoute(RODFRouteDesc &nrd)
void removeDetector(const std::string &id)
SUMOReal getFlowSumSecure(const std::string &id) const
RODFDetectorType
Numerical representation of different detector types.
Definition: RODFDetector.h:65
std::map< std::string, RODFDetector * > myDetectorMap
Definition: RODFDetector.h:281
std::vector< ROEdge * > edges2Pass
The edges the route is made of.
Definition: RODFRouteDesc.h:55
Represents a generic random distribution.
static SUMOReal _2SUMOReal(const E *const data)
Definition: TplConvert.h:223
bool addDetector(RODFDetector *dfd)
void addRoutes(RODFRouteCont *routes)
A source detector.
Definition: RODFDetector.h:76
ROEdge * getEdge(const std::string &name) const
Retrieves an edge from the network.
Definition: RONet.h:102
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
RODFDetector(const std::string &id, const std::string &laneID, SUMOReal pos, const RODFDetectorType type)
Constructor.
static std::string escapeXML(const std::string &orig)
Replaces the standard escapes by their XML entities.
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:61
void buildDestinationDistribution(const RODFDetectorCon &detectors, SUMOTime startTime, SUMOTime endTime, SUMOTime stepOffset, const RODFNet &net, std::map< size_t, RandomDistributor< size_t > * > &into) const
const std::vector< RODFRouteDesc > & getRouteVector() const
T MAX2(T a, T b)
Definition: StdDefs.h:72
SUMOReal getMaxDetectorFlow() const
void addPriorDetector(const RODFDetector *det)
const std::vector< RODFDetector * > & getDetectors() const
void saveRoutes(const std::string &file) const
void writeSpeedTrigger(const RODFNet *const net, const std::string &file, const RODFDetectorFlows &flows, SUMOTime startTime, SUMOTime endTime, SUMOTime stepOffset)
const std::set< const RODFDetector * > & getFollowerDetectors() const
bool writeEmitterDefinition(const std::string &file, const std::map< size_t, RandomDistributor< size_t > * > &dists, const RODFDetectorFlows &flows, SUMOTime startTime, SUMOTime endTime, SUMOTime stepOffset, bool includeUnusedRoutes, SUMOReal scale, bool insertionsOnly, SUMOReal defaultSpeed) const
std::string getEdgeID() const
Returns the id of the edge this detector is placed on.
Definition: RODFDetector.h:133
std::vector< RODFRouteDesc > & get()
Returns the container of stored routes.
SUMOReal computeDistanceFactor(const RODFRouteDesc &rd) const
A container for flows.
A container for RODFDetectors.
Definition: RODFDetector.h:227
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:39
bool hasDetector(ROEdge *edge) const
Definition: RODFNet.cpp:661
void computeSplitProbabilities(const RODFNet *net, const RODFDetectorCon &detectors, const RODFDetectorFlows &flows, SUMOTime startTime, SUMOTime endTime, SUMOTime stepOffset)
RODFRouteCont * myRoutes
Definition: RODFDetector.h:207
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:67
const RODFDetector & getAnyDetectorForEdge(const RODFEdge *const edge) const
bool writeXMLHeader(const std::string &rootElement, const std::string &attrs="", const std::string &comment="")
Writes an XML header with optional configuration.
void writeEmitters(const std::string &file, const RODFDetectorFlows &flows, SUMOTime startTime, SUMOTime endTime, SUMOTime stepOffset, const RODFNet &net, bool writeCalibrators, bool includeUnusedRoutes, SUMOReal scale, bool insertionsOnly)
RODFDetectorType getType() const
Returns the type of the detector.
Definition: RODFDetector.h:150
A not yet defined detector.
Definition: RODFDetector.h:67
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
const std::string & getID() const
Returns the id.
Definition: Named.h:60
std::map< std::string, std::vector< RODFDetector * > > myDetectorEdgeMap
Definition: RODFDetector.h:282
SUMOReal myPosition
Definition: RODFDetector.h:205
bool writeRoutes(std::vector< std::string > &saved, OutputDevice &out)
static const RGBColor GREEN
Definition: RGBColor.h:190
void save(const std::string &file) const
the edges of a route
An in-between detector.
Definition: RODFDetector.h:73
OutputDevice & writeNonEmptyAttr(const SumoXMLAttr attr, const std::string &val)
writes a string attribute only if it is not the empty string and not the string "default" ...
Definition: OutputDevice.h:290
int getAggFlowFor(const ROEdge *edge, SUMOTime time, SUMOTime period, const RODFDetectorFlows &flows) const
T get(MTRand *which=0) const
Draw a sample of the distribution.
A detector which had to be discarded (!!!)
Definition: RODFDetector.h:70
A DFROUTER-network.
Definition: RODFNet.h:51
void writeValidationDetectors(const std::string &file, bool includeSources, bool singleFile, bool friendly)
~RODFDetector()
Destructor.
std::string myLaneID
Definition: RODFDetector.h:204
bool knows(const std::string &det_id) const
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:53
void writeSingleSpeedTrigger(const std::string &file, const RODFDetectorFlows &flows, SUMOTime startTime, SUMOTime endTime, SUMOTime stepOffset, SUMOReal defaultSpeed)
Definition of the traffic during a certain time containing the flows and speeds.
void saveAsPOIs(const std::string &file) const
A route within the DFROUTER.
Definition: RODFRouteDesc.h:53
A basic edge for routing applications.
Definition: ROEdge.h:69
std::vector< std::map< RODFEdge *, SUMOReal > > mySplitProbabilities
Definition: RODFDetector.h:209
Base class for objects which have an id.
Definition: Named.h:45
ROEdge * getApproaching(unsigned int pos) const
Returns the edge at the given position from the list of reachable edges.
Definition: ROEdge.h:311
const std::set< const RODFDetector * > & getPriorDetectors() const
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:205
unsigned int getNoFollowing() const
Returns the number of edges this edge is connected to.
Definition: ROEdge.cpp:285
SUMOReal getOverallProb() const
Return the sum of the probabilites assigned to the members.
static int _2int(const E *const data)
Definition: TplConvert.h:114
std::string myID
The name of the object.
Definition: Named.h:128
static const RGBColor RED
Definition: RGBColor.h:189
RODFDetectorType myType
Definition: RODFDetector.h:206
std::map< std::string, RODFEdge * > myRoute2Edge
Definition: RODFDetector.h:210
static OutputDevice & getDevice(const std::string &name)
Returns the described OutputDevice.
unsigned int getNumApproaching() const
Returns the number of edges this edge is connected to.
Definition: ROEdge.cpp:294
const RODFDetector & getDetector(const std::string &id) const
void guessEmptyFlows(RODFDetectorFlows &flows)
Class representing a detector within the DFROUTER.
Definition: RODFDetector.h:88
A storage for options typed value containers)
Definition: OptionsCont.h:108
A container for DFROUTER-routes.
Definition: RODFRouteCont.h:63
int SUMOTime
Definition: SUMOTime.h:43
SUMOReal qPKW
SUMOReal getPos() const
Returns the position at which the detector lies.
Definition: RODFDetector.h:141
std::string routename
The name of the route.
Definition: RODFRouteDesc.h:57
void writeEmitterPOIs(const std::string &file, const RODFDetectorFlows &flows)
void setType(RODFDetectorType type)
std::set< const RODFDetector * > myPriorDetectors
Definition: RODFDetector.h:208
A variable speed sign.
const std::vector< FlowDef > & getFlowDefs(const std::string &id) const
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
bool closeTag()
Closes the most recently opened tag.
#define SUMOReal
Definition: config.h:215
const std::vector< std::map< RODFEdge *, SUMOReal > > & getSplitProbabilities() const
Definition: RODFDetector.h:194
static std::string getFilePath(const std::string &path)
Removes the file information from the given path.
Definition: FileHelpers.cpp:76
std::set< const RODFDetector * > myFollowingDetectors
Definition: RODFDetector.h:208
void writeEndRerouterDetectors(const std::string &file)
void mesoJoin(const std::string &nid, const std::vector< std::string > &oldids)
void addRouteDesc(RODFRouteDesc &desc)
Adds a route to the container.
void clearDists(std::map< size_t, RandomDistributor< size_t > * > &dists) const
Clears the given distributions map, deleting the timed distributions.
SUMOReal qLKW
bool detectorsHaveRoutes() const
A color information.
void addFollowingDetector(const RODFDetector *det)
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
bool save(std::vector< std::string > &saved, const std::string &prependix, OutputDevice &out)
Saves routes.
bool hasRoutes() const
bool detectorsHaveCompleteTypes() const
const std::string & getLaneID() const
Returns the id of the lane this detector is placed on.
Definition: RODFDetector.h:125
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.