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