SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RODFNet.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // A DFROUTER-network
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
13 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
14 /****************************************************************************/
15 //
16 // This file is part of SUMO.
17 // SUMO is free software: you can redistribute it and/or modify
18 // it under the terms of the GNU General Public License as published by
19 // the Free Software Foundation, either version 3 of the License, or
20 // (at your option) any later version.
21 //
22 /****************************************************************************/
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #include <iostream>
33 #include <map>
34 #include <vector>
35 #include <iterator>
36 #include "RODFNet.h"
37 #include "RODFDetector.h"
38 #include "RODFRouteDesc.h"
39 #include "RODFDetectorFlow.h"
40 #include "RODFEdge.h"
41 #include <cmath>
43 #include <utils/common/ToString.h>
45 #include <utils/geom/GeomHelper.h>
46 
47 #ifdef CHECK_MEMORY_LEAKS
48 #include <foreign/nvwa/debug_new.h>
49 #endif // CHECK_MEMORY_LEAKS
50 
51 
52 // ===========================================================================
53 // method definitions
54 // ===========================================================================
55 RODFNet::RODFNet(bool amInHighwayMode)
56  : RONet(), myAmInHighwayMode(amInHighwayMode),
57  mySourceNumber(0), mySinkNumber(0), myInBetweenNumber(0), myInvalidNumber(0) {
59  myKeepTurnarounds = OptionsCont::getOptions().getBool("keep-turnarounds");
60 }
61 
62 
64 }
65 
66 
67 void
69  const std::map<std::string, ROEdge*> &edges = getEdgeMap();
70  for (std::map<std::string, ROEdge*>::const_iterator rit = edges.begin(); rit != edges.end(); ++rit) {
71  ROEdge* ce = (*rit).second;
72  unsigned int i = 0;
73  unsigned int length_size = ce->getNoFollowing();
74  for (i = 0; i < length_size; i++) {
75  ROEdge* help = ce->getFollower(i);
76  if (find(myDisallowedEdges.begin(), myDisallowedEdges.end(), help->getID()) != myDisallowedEdges.end()) {
77  // edges in sinks will not be used
78  continue;
79  }
80  if (!myKeepTurnarounds && help->getToNode() == ce->getFromNode()) {
81  // do not use turnarounds
82  continue;
83  }
84  // add the connection help->ce to myApproachingEdges
85  if (myApproachingEdges.find(help) == myApproachingEdges.end()) {
86  myApproachingEdges[help] = std::vector<ROEdge*>();
87  }
88  myApproachingEdges[help].push_back(ce);
89  // add the connection ce->help to myApproachingEdges
90  if (myApproachedEdges.find(ce) == myApproachedEdges.end()) {
91  myApproachedEdges[ce] = std::vector<ROEdge*>();
92  }
93  myApproachedEdges[ce].push_back(help);
94  }
95  }
96 }
97 
98 
99 void
101  myDetectorsOnEdges.clear();
102  myDetectorEdges.clear();
103  const std::vector<RODFDetector*> &dets = detcont.getDetectors();
104  for (std::vector<RODFDetector*>::const_iterator i = dets.begin(); i != dets.end(); ++i) {
105  ROEdge* e = getDetectorEdge(**i);
106  myDetectorsOnEdges[e].push_back((*i)->getID());
107  myDetectorEdges[(*i)->getID()] = e;
108  }
109 }
110 
111 
112 void
114  bool sourcesStrict) const {
115  PROGRESS_BEGIN_MESSAGE("Computing detector types");
116  const std::vector< RODFDetector*> &dets = detcont.getDetectors();
117  // build needed information. first
119  // compute detector types then
120  for (std::vector< RODFDetector*>::const_iterator i = dets.begin(); i != dets.end(); ++i) {
121  if (isSource(**i, detcont, sourcesStrict)) {
122  (*i)->setType(SOURCE_DETECTOR);
123  mySourceNumber++;
124  }
125  if (isDestination(**i, detcont)) {
126  (*i)->setType(SINK_DETECTOR);
127  mySinkNumber++;
128  }
129  if ((*i)->getType() == TYPE_NOT_DEFINED) {
130  (*i)->setType(BETWEEN_DETECTOR);
132  }
133  }
134  // recheck sources
135  for (std::vector< RODFDetector*>::const_iterator i = dets.begin(); i != dets.end(); ++i) {
136  if ((*i)->getType() == SOURCE_DETECTOR && isFalseSource(**i, detcont)) {
137  (*i)->setType(DISCARDED_DETECTOR);
138  myInvalidNumber++;
139  mySourceNumber--;
140  }
141  }
142  // print results
144  WRITE_MESSAGE("Computed detector types:");
145  WRITE_MESSAGE(" " + toString(mySourceNumber) + " source detectors");
146  WRITE_MESSAGE(" " + toString(mySinkNumber) + " sink detectors");
147  WRITE_MESSAGE(" " + toString(myInBetweenNumber) + " in-between detectors");
148  WRITE_MESSAGE(" " + toString(myInvalidNumber) + " invalid detectors");
149 }
150 
151 
152 bool
154  const RODFDetectorCon& detectors) const {
155  assert(myDetectorsOnEdges.find(edge) != myDetectorsOnEdges.end());
156  const std::vector<std::string> &detIDs = myDetectorsOnEdges.find(edge)->second;
157  std::vector<std::string>::const_iterator i;
158  for (i = detIDs.begin(); i != detIDs.end(); ++i) {
159  const RODFDetector& det = detectors.getDetector(*i);
160  if (det.getType() != BETWEEN_DETECTOR) {
161  return false;
162  }
163  }
164  return true;
165 }
166 
167 
168 bool
170  const RODFDetectorCon& detectors) const {
171  assert(myDetectorsOnEdges.find(edge) != myDetectorsOnEdges.end());
172  const std::vector<std::string> &detIDs = myDetectorsOnEdges.find(edge)->second;
173  std::vector<std::string>::const_iterator i;
174  for (i = detIDs.begin(); i != detIDs.end(); ++i) {
175  const RODFDetector& det = detectors.getDetector(*i);
176  if (det.getType() == SOURCE_DETECTOR) {
177  return true;
178  }
179  }
180  return false;
181 }
182 
183 
184 
185 void
187  bool keepUnfoundEnds,
188  bool keepShortestOnly,
189  std::vector<ROEdge*> &/*visited*/,
190  const RODFDetector& det, RODFRouteCont& into,
191  const RODFDetectorCon& detectors,
192  int maxFollowingLength,
193  std::vector<ROEdge*> &seen) const {
194  std::vector<RODFRouteDesc> unfoundEnds;
195  std::priority_queue<RODFRouteDesc, std::vector<RODFRouteDesc>, DFRouteDescByTimeComperator> toSolve;
196  std::map<ROEdge*, std::vector<ROEdge*> > dets2Follow;
197  dets2Follow[edge] = std::vector<ROEdge*>();
198  base.passedNo = 0;
199  SUMOReal minDist = OptionsCont::getOptions().getFloat("min-route-length");
200  toSolve.push(base);
201  while (!toSolve.empty()) {
202  RODFRouteDesc current = toSolve.top();
203  toSolve.pop();
204  ROEdge* last = *(current.edges2Pass.end() - 1);
205  if (hasDetector(last)) {
206  if (dets2Follow.find(last) == dets2Follow.end()) {
207  dets2Follow[last] = std::vector<ROEdge*>();
208  }
209  for (std::vector<ROEdge*>::reverse_iterator i = current.edges2Pass.rbegin() + 1; i != current.edges2Pass.rend(); ++i) {
210  if (hasDetector(*i)) {
211  dets2Follow[*i].push_back(last);
212  break;
213  }
214  }
215  }
216 
217  // do not process an edge twice
218  if (find(seen.begin(), seen.end(), last) != seen.end() && keepShortestOnly) {
219  continue;
220  }
221  seen.push_back(last);
222  // end if the edge has no further connections
223  if (!hasApproached(last)) {
224  // ok, no further connections to follow
225  current.factor = 1.;
226  SUMOReal cdist = current.edges2Pass[0]->getFromNode()->getPosition().distanceTo(current.edges2Pass.back()->getToNode()->getPosition());
227  if (minDist < cdist) {
228  into.addRouteDesc(current);
229  }
230  continue;
231  }
232  // check for passing detectors:
233  // if the current last edge is not the one the detector is placed on ...
234  bool addNextNoFurther = false;
235  if (last != getDetectorEdge(det)) {
236  // ... if there is a detector ...
237  if (hasDetector(last)) {
238  if (!hasInBetweenDetectorsOnly(last, detectors)) {
239  // ... and it's not an in-between-detector
240  // -> let's add this edge and the following, but not any further
241  addNextNoFurther = true;
242  current.lastDetectorEdge = last;
243  current.duration2Last = (SUMOTime) current.duration_2;
244  current.distance2Last = current.distance;
245  current.endDetectorEdge = last;
246  if (hasSourceDetector(last, detectors)) {
248  }
249  current.factor = 1.;
250  SUMOReal cdist = current.edges2Pass[0]->getFromNode()->getPosition().distanceTo(current.edges2Pass.back()->getToNode()->getPosition());
251  if (minDist < cdist) {
252  into.addRouteDesc(current);
253  }
254  continue;
255  } else {
256  // ... if it's an in-between-detector
257  // -> mark the current route as to be continued
258  current.passedNo = 0;
259  current.duration2Last = (SUMOTime) current.duration_2;
260  current.distance2Last = current.distance;
261  current.lastDetectorEdge = last;
262  }
263  }
264  }
265  // check for highway off-ramps
266  if (myAmInHighwayMode) {
267  // if it's beside the highway...
268  if (last->getSpeed() < 19.4 && last != getDetectorEdge(det)) {
269  // ... and has more than one following edge
270  if (myApproachedEdges.find(last)->second.size() > 1) {
271  // -> let's add this edge and the following, but not any further
272  addNextNoFurther = true;
273  }
274 
275  }
276  }
277  // check for missing end connections
278  if (!addNextNoFurther) {
279  // ... if this one would be processed, but already too many edge
280  // without a detector occured
281  if (current.passedNo > maxFollowingLength) {
282  // mark not to process any further
283  WRITE_WARNING("Could not close route for '" + det.getID() + "'");
284  unfoundEnds.push_back(current);
285  current.factor = 1.;
286  SUMOReal cdist = current.edges2Pass[0]->getFromNode()->getPosition().distanceTo(current.edges2Pass.back()->getToNode()->getPosition());
287  if (minDist < cdist) {
288  into.addRouteDesc(current);
289  }
290  continue;
291  }
292  }
293  // ... else: loop over the next edges
294  const std::vector<ROEdge*> &appr = myApproachedEdges.find(last)->second;
295  bool hadOne = false;
296  for (size_t i = 0; i < appr.size(); i++) {
297  if (find(current.edges2Pass.begin(), current.edges2Pass.end(), appr[i]) != current.edges2Pass.end()) {
298  // do not append an edge twice (do not build loops)
299  continue;
300  }
301  RODFRouteDesc t(current);
302  t.duration_2 += (appr[i]->getLength() / appr[i]->getSpeed());
303  t.distance += appr[i]->getLength();
304  t.edges2Pass.push_back(appr[i]);
305  if (!addNextNoFurther) {
306  t.passedNo = t.passedNo + 1;
307  toSolve.push(t);
308  } else {
309  if (!hadOne) {
310  t.factor = (SUMOReal) 1. / (SUMOReal) appr.size();
311  SUMOReal cdist = current.edges2Pass[0]->getFromNode()->getPosition().distanceTo(current.edges2Pass.back()->getToNode()->getPosition());
312  if (minDist < cdist) {
313  into.addRouteDesc(t);
314  }
315  hadOne = true;
316  }
317  }
318  }
319  }
320  //
321  if (!keepUnfoundEnds) {
322  std::vector<RODFRouteDesc>::iterator i;
323  std::vector<const ROEdge*> lastDetEdges;
324  for (i = unfoundEnds.begin(); i != unfoundEnds.end(); ++i) {
325  if (find(lastDetEdges.begin(), lastDetEdges.end(), (*i).lastDetectorEdge) == lastDetEdges.end()) {
326  lastDetEdges.push_back((*i).lastDetectorEdge);
327  } else {
328  bool ok = into.removeRouteDesc(*i);
329  assert(ok);
330  }
331  }
332  } else {
333  // !!! patch the factors
334  }
335  while (!toSolve.empty()) {
336 // RODFRouteDesc d = toSolve.top();
337  toSolve.pop();
338 // delete d;
339  }
340 }
341 
342 
343 void
344 RODFNet::buildRoutes(RODFDetectorCon& detcont, bool allEndFollower,
345  bool keepUnfoundEnds, bool includeInBetween,
346  bool keepShortestOnly, int maxFollowingLength) const {
347  // build needed information first
349  // then build the routes
350  std::map<ROEdge*, RODFRouteCont* > doneEdges;
351  const std::vector< RODFDetector*> &dets = detcont.getDetectors();
352  for (std::vector< RODFDetector*>::const_iterator i = dets.begin(); i != dets.end(); ++i) {
353  if ((*i)->getType() != SOURCE_DETECTOR) {
354  // do not build routes for other than sources
355  //continue;
356  }
357  ROEdge* e = getDetectorEdge(**i);
358  if (doneEdges.find(e) != doneEdges.end()) {
359  // use previously build routes
360  (*i)->addRoutes(new RODFRouteCont(*doneEdges[e]));
361  continue;
362  }
363  std::vector<ROEdge*> seen;
364  RODFRouteCont* routes = new RODFRouteCont();
365  doneEdges[e] = routes;
366  RODFRouteDesc rd;
367  rd.edges2Pass.push_back(e);
368  rd.duration_2 = (e->getLength() / e->getSpeed());
369  rd.endDetectorEdge = 0;
370  rd.lastDetectorEdge = 0;
371  rd.distance = e->getLength();
372  rd.distance2Last = 0;
373  rd.duration2Last = 0;
374 
375  rd.overallProb = 0;
376 
377  std::vector<ROEdge*> visited;
378  visited.push_back(e);
379  computeRoutesFor(e, rd, 0, keepUnfoundEnds, keepShortestOnly,
380  visited, **i, *routes, detcont, maxFollowingLength, seen);
381  if (allEndFollower) {
382  routes->addAllEndFollower();
383  }
385  (*i)->addRoutes(routes);
386 
387  // add routes to in-between detectors if wished
388  if (includeInBetween) {
389  // go through the routes
390  const std::vector<RODFRouteDesc> &r = routes->get();
391  for (std::vector<RODFRouteDesc>::const_iterator j = r.begin(); j != r.end(); ++j) {
392  const RODFRouteDesc& mrd = *j;
393  SUMOReal duration = mrd.duration_2;
394  SUMOReal distance = mrd.distance;
395  // go through each route's edges
396  std::vector<ROEdge*>::const_iterator routeend = mrd.edges2Pass.end();
397  for (std::vector<ROEdge*>::const_iterator k = mrd.edges2Pass.begin(); k != routeend; ++k) {
398  // check whether any detectors lies on the current edge
399  if (myDetectorsOnEdges.find(*k) == myDetectorsOnEdges.end()) {
400  duration -= (*k)->getLength() / (*k)->getSpeed();
401  distance -= (*k)->getLength();
402  continue;
403  }
404  // get the detectors
405  const std::vector<std::string> &dets = myDetectorsOnEdges.find(*k)->second;
406  // go through the detectors
407  for (std::vector<std::string>::const_iterator l = dets.begin(); l != dets.end(); ++l) {
408  const RODFDetector& m = detcont.getDetector(*l);
409  if (m.getType() == BETWEEN_DETECTOR) {
410  RODFRouteDesc nrd;
411  copy(k, routeend, back_inserter(nrd.edges2Pass));
412  nrd.duration_2 = duration;
415  nrd.distance = distance;
416  nrd.distance2Last = mrd.distance2Last;
417  nrd.duration2Last = mrd.duration2Last;
418  nrd.overallProb = mrd.overallProb;
419  nrd.factor = mrd.factor;
420  ((RODFDetector&) m).addRoute(nrd);
421  }
422  }
423  duration -= (*k)->getLength() / (*k)->getSpeed();
424  distance -= (*k)->getLength();
425  }
426  }
427  }
428 
429  }
430 }
431 
432 
433 void
435  RODFDetectorFlows& flows,
436  SUMOTime startTime, SUMOTime endTime,
437  SUMOTime stepOffset) {
438  {
439  if (flows.knows(detector->getID())) {
440  const std::vector<FlowDef> &detFlows = flows.getFlowDefs(detector->getID());
441  for (std::vector<FlowDef>::const_iterator j = detFlows.begin(); j != detFlows.end(); ++j) {
442  if ((*j).qPKW > 0 || (*j).qLKW > 0) {
443  return;
444  }
445  }
446  }
447  }
448  // ok, there is no information for the whole time;
449  // lets find preceding detectors and rebuild the flows if possible
450  WRITE_WARNING("Detector '" + detector->getID() + "' has no flows.\n Trying to rebuild.");
451  // go back and collect flows
452  std::vector<ROEdge*> previous;
453  {
454  std::vector<IterationEdge> missing;
455  IterationEdge ie;
456  ie.depth = 0;
457  ie.edge = getDetectorEdge(*detector);
458  missing.push_back(ie);
459  bool maxDepthReached = false;
460  while (!missing.empty() && !maxDepthReached) {
461  IterationEdge last = missing.back();
462  missing.pop_back();
463  std::vector<ROEdge*> approaching = myApproachingEdges[last.edge];
464  for (std::vector<ROEdge*>::const_iterator j = approaching.begin(); j != approaching.end(); ++j) {
465  if (hasDetector(*j)) {
466  previous.push_back(*j);
467  } else {
468  ie.depth = last.depth + 1;
469  ie.edge = *j;
470  missing.push_back(ie);
471  if (ie.depth > 5) {
472  maxDepthReached = true;
473  }
474  }
475  }
476  }
477  if (maxDepthReached) {
478  WRITE_WARNING(" Could not build list of previous flows.");
479  }
480  }
481  // Edges with previous detectors are now in "previous";
482  // compute following
483  std::vector<ROEdge*> latter;
484  {
485  std::vector<IterationEdge> missing;
486  for (std::vector<ROEdge*>::const_iterator k = previous.begin(); k != previous.end(); ++k) {
487  IterationEdge ie;
488  ie.depth = 0;
489  ie.edge = *k;
490  missing.push_back(ie);
491  }
492  bool maxDepthReached = false;
493  while (!missing.empty() && !maxDepthReached) {
494  IterationEdge last = missing.back();
495  missing.pop_back();
496  std::vector<ROEdge*> approached = myApproachedEdges[last.edge];
497  for (std::vector<ROEdge*>::const_iterator j = approached.begin(); j != approached.end(); ++j) {
498  if (*j == getDetectorEdge(*detector)) {
499  continue;
500  }
501  if (hasDetector(*j)) {
502  latter.push_back(*j);
503  } else {
504  IterationEdge ie;
505  ie.depth = last.depth + 1;
506  ie.edge = *j;
507  missing.push_back(ie);
508  if (ie.depth > 5) {
509  maxDepthReached = true;
510  }
511  }
512  }
513  }
514  if (maxDepthReached) {
515  WRITE_WARNING(" Could not build list of latter flows.");
516  return;
517  }
518  }
519  // Edges with latter detectors are now in "latter";
520 
521  // lets not validate them by now - surely this should be done
522  // for each time step: collect incoming flows; collect outgoing;
523  std::vector<FlowDef> mflows;
524  int index = 0;
525  for (SUMOTime t = startTime; t < endTime; t += stepOffset, index++) {
526  FlowDef inFlow;
527  inFlow.qLKW = 0;
528  inFlow.qPKW = 0;
529  inFlow.vLKW = 0;
530  inFlow.vPKW = 0;
531  // collect incoming
532  {
533  // !! time difference is missing
534  for (std::vector<ROEdge*>::iterator i = previous.begin(); i != previous.end(); ++i) {
535  const std::vector<FlowDef> &flows = static_cast<const RODFEdge*>(*i)->getFlows();
536  if (flows.size() != 0) {
537  const FlowDef& srcFD = flows[index];
538  inFlow.qLKW += srcFD.qLKW;
539  inFlow.qPKW += srcFD.qPKW;
540  inFlow.vLKW += srcFD.vLKW;
541  inFlow.vPKW += srcFD.vPKW;
542  }
543  }
544  }
545  inFlow.vLKW /= (SUMOReal) previous.size();
546  inFlow.vPKW /= (SUMOReal) previous.size();
547  // collect outgoing
548  FlowDef outFlow;
549  outFlow.qLKW = 0;
550  outFlow.qPKW = 0;
551  outFlow.vLKW = 0;
552  outFlow.vPKW = 0;
553  {
554  // !! time difference is missing
555  for (std::vector<ROEdge*>::iterator i = latter.begin(); i != latter.end(); ++i) {
556  const std::vector<FlowDef> &flows = static_cast<const RODFEdge*>(*i)->getFlows();
557  if (flows.size() != 0) {
558  const FlowDef& srcFD = flows[index];
559  outFlow.qLKW += srcFD.qLKW;
560  outFlow.qPKW += srcFD.qPKW;
561  outFlow.vLKW += srcFD.vLKW;
562  outFlow.vPKW += srcFD.vPKW;
563  }
564  }
565  }
566  outFlow.vLKW /= (SUMOReal) latter.size();
567  outFlow.vPKW /= (SUMOReal) latter.size();
568  //
569  FlowDef mFlow;
570  mFlow.qLKW = inFlow.qLKW - outFlow.qLKW;
571  mFlow.qPKW = inFlow.qPKW - outFlow.qPKW;
572  mFlow.vLKW = (inFlow.vLKW + outFlow.vLKW) / (SUMOReal) 2.;
573  mFlow.vPKW = (inFlow.vPKW + outFlow.vPKW) / (SUMOReal) 2.;
574  mflows.push_back(mFlow);
575  }
576  static_cast<RODFEdge*>(getDetectorEdge(*detector))->setFlows(mflows);
577  flows.setFlows(detector->getID(), mflows);
578 }
579 
580 
581 void
583  RODFDetectorFlows& flows,
584  SUMOTime startTime, SUMOTime endTime,
585  SUMOTime stepOffset) {
586  const std::vector<RODFDetector*> &dets = detectors.getDetectors();
587  for (std::vector<RODFDetector*>::const_iterator i = dets.begin(); i != dets.end(); ++i) {
588  // check whether there is at least one entry with a flow larger than zero
589  revalidateFlows(*i, flows, startTime, endTime, stepOffset);
590  }
591 }
592 
593 
594 
595 void
597  RODFDetectorFlows& flows) {
598  const std::vector<RODFDetector*> &dets = detectors.getDetectors();
599  for (std::vector<RODFDetector*>::const_iterator i = dets.begin(); i != dets.end();) {
600  bool remove = true;
601  // check whether there is at least one entry with a flow larger than zero
602  if (flows.knows((*i)->getID())) {
603  remove = false;
604  }
605  if (remove) {
606  WRITE_MESSAGE("Removed detector '" + (*i)->getID() + "' because no flows for him exist.");
607  flows.removeFlow((*i)->getID());
608  detectors.removeDetector((*i)->getID());
609  i = dets.begin();
610  } else {
611  i++;
612  }
613  }
614 }
615 
616 
617 
618 void
620  RODFDetectorFlows& flows) {
621  const std::vector<RODFDetector*> &dets = detectors.getDetectors();
622  for (std::vector<RODFDetector*>::const_iterator i = dets.begin(); i != dets.end(); ++i) {
623  bool remove = true;
624  // check whether there is at least one entry with a flow larger than zero
625  if (flows.knows((*i)->getID())) {
626  remove = false;
627  }
628  if (remove) {
629  WRITE_MESSAGE("Detector '" + (*i)->getID() + "' has no flow.");
630  }
631  }
632 }
633 
634 
635 
636 ROEdge*
638  std::string edgeName = det.getLaneID();
639  edgeName = edgeName.substr(0, edgeName.rfind('_'));
640  ROEdge* ret = getEdge(edgeName);
641  if (ret == 0) {
642  throw ProcessError("Edge '" + edgeName + "' used by detector '" + det.getID() + "' is not known.");
643  }
644  return ret;
645 }
646 
647 
648 bool
650  return
651  myApproachingEdges.find(edge) != myApproachingEdges.end()
652  &&
653  myApproachingEdges.find(edge)->second.size() != 0;
654 }
655 
656 
657 bool
659  return
660  myApproachedEdges.find(edge) != myApproachedEdges.end()
661  &&
662  myApproachedEdges.find(edge)->second.size() != 0;
663 }
664 
665 
666 bool
668  return
669  myDetectorsOnEdges.find(edge) != myDetectorsOnEdges.end()
670  &&
671  myDetectorsOnEdges.find(edge)->second.size() != 0;
672 }
673 
674 
675 const std::vector<std::string> &
677  return myDetectorsOnEdges.find(edge)->second;
678 }
679 
680 
681 SUMOReal
682 RODFNet::getAbsPos(const RODFDetector& det) const {
683  if (det.getPos() >= 0) {
684  return det.getPos();
685  }
686  return getDetectorEdge(det)->getLength() + det.getPos();
687 }
688 
689 bool
690 RODFNet::isSource(const RODFDetector& det, const RODFDetectorCon& detectors,
691  bool strict) const {
692  std::vector<ROEdge*> seen;
693  return
694  isSource(det, getDetectorEdge(det), seen, detectors, strict);
695 }
696 
697 bool
698 RODFNet::isFalseSource(const RODFDetector& det, const RODFDetectorCon& detectors) const {
699  std::vector<ROEdge*> seen;
700  return
701  isFalseSource(det, getDetectorEdge(det), seen, detectors);
702 }
703 
704 bool
705 RODFNet::isDestination(const RODFDetector& det, const RODFDetectorCon& detectors) const {
706  std::vector<ROEdge*> seen;
707  return isDestination(det, getDetectorEdge(det), seen, detectors);
708 }
709 
710 
711 bool
713  std::vector<ROEdge*> &seen,
714  const RODFDetectorCon& detectors,
715  bool strict) const {
716  if (seen.size() == 1000) { // !!!
717  WRITE_WARNING("Quitting checking for being a source for detector '" + det.getID() + "' due to seen edge limit.");
718  return false;
719  }
720  if (edge == getDetectorEdge(det)) {
721  // maybe there is another detector at the same edge
722  // get the list of this/these detector(s)
723  const std::vector<std::string> &detsOnEdge = myDetectorsOnEdges.find(edge)->second;
724  for (std::vector<std::string>::const_iterator i = detsOnEdge.begin(); i != detsOnEdge.end(); ++i) {
725  if ((*i) == det.getID()) {
726  continue;
727  }
728  const RODFDetector& sec = detectors.getDetector(*i);
729  if (getAbsPos(sec) < getAbsPos(det)) {
730  // ok, there is another detector on the same edge and it is
731  // before this one -> no source
732  return false;
733  }
734  }
735  }
736  // it's a source if no edges are approaching the edge
737  if (!hasApproaching(edge)) {
738  if (edge != getDetectorEdge(det)) {
739  if (hasDetector(edge)) {
740  return false;
741  }
742  }
743  return true;
744  }
745  if (edge != getDetectorEdge(det)) {
746  // ok, we are at one of the edges in front
747  if (myAmInHighwayMode) {
748  if (edge->getSpeed() >= 19.4) {
749  if (hasDetector(edge)) {
750  // we are still on the highway and there is another detector
751  return false;
752  }
753  // the next is a hack for the A100 scenario...
754  // We have to look into further edges herein edges
755  const std::vector<ROEdge*> &appr = myApproachingEdges.find(edge)->second;
756  size_t noOk = 0;
757  size_t noFalse = 0;
758  size_t noSkipped = 0;
759  for (size_t i = 0; i < appr.size(); i++) {
760  if (!hasDetector(appr[i])) {
761  noOk++;
762  } else {
763  noFalse++;
764  }
765  }
766  if ((noFalse + noSkipped) == appr.size()) {
767  return false;
768  }
769  }
770  }
771  }
772 
773  if (myAmInHighwayMode) {
774  if (edge->getSpeed() < 19.4 && edge != getDetectorEdge(det)) {
775  // we have left the highway already
776  // -> the detector will be a highway source
777  if (!hasDetector(edge)) {
778  return true;
779  }
780  }
781  }
782  if (myDetectorsOnEdges.find(edge) != myDetectorsOnEdges.end()
783  &&
784  myDetectorEdges.find(det.getID())->second != edge) {
785  return false;
786  }
787 
788  // let's check the edges in front
789  const std::vector<ROEdge*> &appr = myApproachingEdges.find(edge)->second;
790  size_t noOk = 0;
791  size_t noFalse = 0;
792  size_t noSkipped = 0;
793  seen.push_back(edge);
794  for (size_t i = 0; i < appr.size(); i++) {
795  bool had = std::find(seen.begin(), seen.end(), appr[i]) != seen.end();
796  if (!had) {
797  if (isSource(det, appr[i], seen, detectors, strict)) {
798  noOk++;
799  } else {
800  noFalse++;
801  }
802  } else {
803  noSkipped++;
804  }
805  }
806  if (!strict) {
807  return (noFalse + noSkipped) != appr.size();
808  } else {
809  return (noOk + noSkipped) == appr.size();
810  }
811 }
812 
813 
814 bool
815 RODFNet::isDestination(const RODFDetector& det, ROEdge* edge, std::vector<ROEdge*> &seen,
816  const RODFDetectorCon& detectors) const {
817  if (seen.size() == 1000) { // !!!
818  WRITE_WARNING("Quitting checking for being a destination for detector '" + det.getID() + "' due to seen edge limit.");
819  return false;
820  }
821  if (edge == getDetectorEdge(det)) {
822  // maybe there is another detector at the same edge
823  // get the list of this/these detector(s)
824  const std::vector<std::string> &detsOnEdge = myDetectorsOnEdges.find(edge)->second;
825  for (std::vector<std::string>::const_iterator i = detsOnEdge.begin(); i != detsOnEdge.end(); ++i) {
826  if ((*i) == det.getID()) {
827  continue;
828  }
829  const RODFDetector& sec = detectors.getDetector(*i);
830  if (getAbsPos(sec) > getAbsPos(det)) {
831  // ok, there is another detector on the same edge and it is
832  // after this one -> no destination
833  return false;
834  }
835  }
836  }
837  if (!hasApproached(edge)) {
838  if (edge != getDetectorEdge(det)) {
839  if (hasDetector(edge)) {
840  return false;
841  }
842  }
843  return true;
844  }
845  if (edge != getDetectorEdge(det)) {
846  // ok, we are at one of the edges coming behind
847  if (myAmInHighwayMode) {
848  if (edge->getSpeed() >= 19.4) {
849  if (hasDetector(edge)) {
850  // we are still on the highway and there is another detector
851  return false;
852  }
853  }
854  }
855  }
856 
857  if (myAmInHighwayMode) {
858  if (edge->getSpeed() < 19.4 && edge != getDetectorEdge(det)) {
859  if (hasDetector(edge)) {
860  return true;
861  }
862  if (myApproachedEdges.find(edge)->second.size() > 1) {
863  return true;
864  }
865 
866  }
867  }
868 
869  if (myDetectorsOnEdges.find(edge) != myDetectorsOnEdges.end()
870  &&
871  myDetectorEdges.find(det.getID())->second != edge) {
872  return false;
873  }
874  const std::vector<ROEdge*> &appr = myApproachedEdges.find(edge)->second;
875  bool isall = true;
876  size_t no = 0;
877  seen.push_back(edge);
878  for (size_t i = 0; i < appr.size() && isall; i++) {
879  bool had = std::find(seen.begin(), seen.end(), appr[i]) != seen.end();
880  if (!had) {
881  if (!isDestination(det, appr[i], seen, detectors)) {
882  no++;
883  isall = false;
884  }
885  }
886  }
887  return isall;
888 }
889 
890 bool
891 RODFNet::isFalseSource(const RODFDetector& det, ROEdge* edge, std::vector<ROEdge*> &seen,
892  const RODFDetectorCon& detectors) const {
893  if (seen.size() == 1000) { // !!!
894  WRITE_WARNING("Quitting checking for being a false source for detector '" + det.getID() + "' due to seen edge limit.");
895  return false;
896  }
897  seen.push_back(edge);
898  if (edge != getDetectorEdge(det)) {
899  // ok, we are at one of the edges coming behind
900  if (hasDetector(edge)) {
901  const std::vector<std::string> &dets = myDetectorsOnEdges.find(edge)->second;
902  for (std::vector<std::string>::const_iterator i = dets.begin(); i != dets.end(); ++i) {
903  if (detectors.getDetector(*i).getType() == SINK_DETECTOR) {
904  return false;
905  }
906  if (detectors.getDetector(*i).getType() == BETWEEN_DETECTOR) {
907  return false;
908  }
909  if (detectors.getDetector(*i).getType() == SOURCE_DETECTOR) {
910  return true;
911  }
912  }
913  } else {
914  if (myAmInHighwayMode && edge->getSpeed() < 19.) {
915  return false;
916  }
917  }
918  }
919 
920  if (myApproachedEdges.find(edge) == myApproachedEdges.end()) {
921  return false;
922  }
923 
924  const std::vector<ROEdge*> &appr = myApproachedEdges.find(edge)->second;
925  bool isall = false;
926  for (size_t i = 0; i < appr.size() && !isall; i++) {
927  //printf("checking %s->\n", appr[i].c_str());
928  bool had = std::find(seen.begin(), seen.end(), appr[i]) != seen.end();
929  if (!had) {
930  if (isFalseSource(det, appr[i], seen, detectors)) {
931  isall = true;
932  }
933  }
934  }
935  return isall;
936 }
937 
938 
939 void
941  const RODFDetectorCon& detectors,
942  SUMOTime startTime, SUMOTime endTime,
943  SUMOTime stepOffset) {
944  std::map<ROEdge*, std::vector<std::string>, idComp>::iterator i;
945  for (i = myDetectorsOnEdges.begin(); i != myDetectorsOnEdges.end(); ++i) {
946  ROEdge* into = (*i).first;
947  const std::vector<std::string> &dets = (*i).second;
948  std::map<SUMOReal, std::vector<std::string> > cliques;
949  std::vector<std::string>* maxClique = 0;
950  for (std::vector<std::string>::const_iterator j = dets.begin(); j != dets.end(); ++j) {
951  if (!flows.knows(*j)) {
952  continue;
953  }
954  const RODFDetector& det = detectors.getDetector(*j);
955  bool found = false;
956  for (std::map<SUMOReal, std::vector<std::string> >::iterator k = cliques.begin(); !found && k != cliques.end(); ++k) {
957  if (fabs((*k).first - det.getPos()) < 1) {
958  (*k).second.push_back(*j);
959  if ((*k).second.size() > maxClique->size()) {
960  maxClique = &(*k).second;
961  }
962  found = true;
963  }
964  }
965  if (!found) {
966  cliques[det.getPos()].push_back(*j);
967  maxClique = &cliques[det.getPos()];
968  }
969  }
970  if (maxClique == 0) {
971  continue;
972  }
973  std::vector<FlowDef> mflows; // !!! reserve
974  for (SUMOTime t = startTime; t < endTime; t += stepOffset) {
975  FlowDef fd;
976  fd.qPKW = 0;
977  fd.qLKW = 0;
978  fd.vLKW = 0;
979  fd.vPKW = 0;
980  fd.fLKW = 0;
981  fd.isLKW = 0;
982  mflows.push_back(fd);
983  }
984  for (std::vector<std::string>::iterator l = maxClique->begin(); l != maxClique->end(); ++l) {
985  bool didWarn = false;
986  const std::vector<FlowDef> &dflows = flows.getFlowDefs(*l);
987  int index = 0;
988  for (SUMOTime t = startTime; t < endTime; t += stepOffset, index++) {
989  const FlowDef& srcFD = dflows[index];
990  FlowDef& fd = mflows[index];
991  fd.qPKW += srcFD.qPKW;
992  fd.qLKW += srcFD.qLKW;
993  fd.vLKW += (srcFD.vLKW / (SUMOReal) maxClique->size());
994  fd.vPKW += (srcFD.vPKW / (SUMOReal) maxClique->size());
995  fd.fLKW += (srcFD.fLKW / (SUMOReal) maxClique->size());
996  fd.isLKW += (srcFD.isLKW / (SUMOReal) maxClique->size());
997  if (!didWarn && srcFD.vPKW > 0 && srcFD.vPKW < 255 && srcFD.vPKW / 3.6 > into->getSpeed()) {
998  WRITE_MESSAGE("Detected PKW speed higher than allowed speed at '" + (*l) + "' on '" + into->getID() + "'.");
999  didWarn = true;
1000  }
1001  if (!didWarn && srcFD.vLKW > 0 && srcFD.vLKW < 255 && srcFD.vLKW / 3.6 > into->getSpeed()) {
1002  WRITE_MESSAGE("Detected LKW speed higher than allowed speed at '" + (*l) + "' on '" + into->getID() + "'.");
1003  didWarn = true;
1004  }
1005  }
1006  }
1007  static_cast<RODFEdge*>(into)->setFlows(mflows);
1008  }
1009 }
1010 
1011 
1012 void
1014  // !!! this will not work when several detectors are lying on the same edge on different positions
1015 
1016 
1017  buildDetectorEdgeDependencies(detectors);
1018  // for each detector, compute the lists of predecessor and following detectors
1019  std::map<std::string, ROEdge*>::const_iterator i;
1020  for (i = myDetectorEdges.begin(); i != myDetectorEdges.end(); ++i) {
1021  const RODFDetector& det = detectors.getDetector((*i).first);
1022  if (!det.hasRoutes()) {
1023  continue;
1024  }
1025  // mark current detectors
1026  std::vector<RODFDetector*> last;
1027  {
1028  const std::vector<std::string> &detNames = myDetectorsOnEdges.find((*i).second)->second;
1029  for (std::vector<std::string>::const_iterator j = detNames.begin(); j != detNames.end(); ++j) {
1030  last.push_back((RODFDetector*) &detectors.getDetector(*j));
1031  }
1032  }
1033  // iterate over the current detector's routes
1034  const std::vector<RODFRouteDesc> &routes = det.getRouteVector();
1035  for (std::vector<RODFRouteDesc>::const_iterator j = routes.begin(); j != routes.end(); ++j) {
1036  const std::vector<ROEdge*> &edges2Pass = (*j).edges2Pass;
1037  for (std::vector<ROEdge*>::const_iterator k = edges2Pass.begin() + 1; k != edges2Pass.end(); ++k) {
1038  if (myDetectorsOnEdges.find(*k) != myDetectorsOnEdges.end()) {
1039  const std::vector<std::string> &detNames = myDetectorsOnEdges.find(*k)->second;
1040  // ok, consecutive detector found
1041  for (std::vector<RODFDetector*>::iterator l = last.begin(); l != last.end(); ++l) {
1042  // mark as follower of current
1043  for (std::vector<std::string>::const_iterator m = detNames.begin(); m != detNames.end(); ++m) {
1044  ((RODFDetector*) &detectors.getDetector(*m))->addPriorDetector((RODFDetector*) & (*l));
1045  (*l)->addFollowingDetector((RODFDetector*) &detectors.getDetector(*m));
1046  }
1047  }
1048  last.clear();
1049  for (std::vector<std::string>::const_iterator m = detNames.begin(); m != detNames.end(); ++m) {
1050  last.push_back((RODFDetector*) &detectors.getDetector(*m));
1051  }
1052  }
1053  }
1054  }
1055  }
1056 }
1057 
1058 
1059 void
1061  buildDetectorEdgeDependencies(detectors);
1062  std::map<ROEdge*, std::vector<std::string>, idComp>::iterator i;
1063  for (i = myDetectorsOnEdges.begin(); i != myDetectorsOnEdges.end(); ++i) {
1064  const std::vector<std::string> &dets = (*i).second;
1065  std::map<SUMOReal, std::vector<std::string> > cliques;
1066  // compute detector cliques
1067  for (std::vector<std::string>::const_iterator j = dets.begin(); j != dets.end(); ++j) {
1068  const RODFDetector& det = detectors.getDetector(*j);
1069  bool found = false;
1070  for (std::map<SUMOReal, std::vector<std::string> >::iterator k = cliques.begin(); !found && k != cliques.end(); ++k) {
1071  if (fabs((*k).first - det.getPos()) < 10.) {
1072  (*k).second.push_back(*j);
1073  found = true;
1074  }
1075  }
1076  if (!found) {
1077  cliques[det.getPos()] = std::vector<std::string>();
1078  cliques[det.getPos()].push_back(*j);
1079  }
1080  }
1081  // join detector cliques
1082  for (std::map<SUMOReal, std::vector<std::string> >::iterator m = cliques.begin(); m != cliques.end(); ++m) {
1083  std::vector<std::string> clique = (*m).second;
1084  // do not join if only one
1085  if (clique.size() == 1) {
1086  continue;
1087  }
1088  std::string nid;
1089  for (std::vector<std::string>::iterator n = clique.begin(); n != clique.end(); ++n) {
1090  std::cout << *n << " ";
1091  if (n != clique.begin()) {
1092  nid = nid + "_";
1093  }
1094  nid = nid + *n;
1095  }
1096  std::cout << ":" << nid << std::endl;
1097  flows.mesoJoin(nid, (*m).second);
1098  detectors.mesoJoin(nid, (*m).second);
1099  }
1100  }
1101 }
1102 
1103 
1104 
1105 /****************************************************************************/
1106