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