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-sim.org/
13 // Copyright (C) 2001-2013 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  ROEdge* e = getDetectorEdge(**i);
355  if (doneEdges.find(e) != doneEdges.end()) {
356  // use previously build routes
357  (*i)->addRoutes(new RODFRouteCont(*doneEdges[e]));
358  continue;
359  }
360  std::vector<ROEdge*> seen;
361  RODFRouteCont* routes = new RODFRouteCont();
362  doneEdges[e] = routes;
363  RODFRouteDesc rd;
364  rd.edges2Pass.push_back(e);
365  rd.duration_2 = (e->getLength() / e->getSpeed());
366  rd.endDetectorEdge = 0;
367  rd.lastDetectorEdge = 0;
368  rd.distance = e->getLength();
369  rd.distance2Last = 0;
370  rd.duration2Last = 0;
371 
372  rd.overallProb = 0;
373 
374  std::vector<ROEdge*> visited;
375  visited.push_back(e);
376  computeRoutesFor(e, rd, 0, keepUnfoundEnds, keepShortestOnly,
377  visited, **i, *routes, detcont, maxFollowingLength, seen);
378  if (allEndFollower) {
379  routes->addAllEndFollower();
380  }
382  (*i)->addRoutes(routes);
383 
384  // add routes to in-between detectors if wished
385  if (includeInBetween) {
386  // go through the routes
387  const std::vector<RODFRouteDesc>& r = routes->get();
388  for (std::vector<RODFRouteDesc>::const_iterator j = r.begin(); j != r.end(); ++j) {
389  const RODFRouteDesc& mrd = *j;
390  SUMOReal duration = mrd.duration_2;
391  SUMOReal distance = mrd.distance;
392  // go through each route's edges
393  std::vector<ROEdge*>::const_iterator routeend = mrd.edges2Pass.end();
394  for (std::vector<ROEdge*>::const_iterator k = mrd.edges2Pass.begin(); k != routeend; ++k) {
395  // check whether any detectors lies on the current edge
396  if (myDetectorsOnEdges.find(*k) == myDetectorsOnEdges.end()) {
397  duration -= (*k)->getLength() / (*k)->getSpeed();
398  distance -= (*k)->getLength();
399  continue;
400  }
401  // get the detectors
402  const std::vector<std::string>& dets = myDetectorsOnEdges.find(*k)->second;
403  // go through the detectors
404  for (std::vector<std::string>::const_iterator l = dets.begin(); l != dets.end(); ++l) {
405  const RODFDetector& m = detcont.getDetector(*l);
406  if (m.getType() == BETWEEN_DETECTOR) {
407  RODFRouteDesc nrd;
408  copy(k, routeend, back_inserter(nrd.edges2Pass));
409  nrd.duration_2 = duration;
412  nrd.distance = distance;
413  nrd.distance2Last = mrd.distance2Last;
414  nrd.duration2Last = mrd.duration2Last;
415  nrd.overallProb = mrd.overallProb;
416  nrd.factor = mrd.factor;
417  ((RODFDetector&) m).addRoute(nrd);
418  }
419  }
420  duration -= (*k)->getLength() / (*k)->getSpeed();
421  distance -= (*k)->getLength();
422  }
423  }
424  }
425 
426  }
427 }
428 
429 
430 void
432  RODFDetectorFlows& flows,
433  SUMOTime startTime, SUMOTime endTime,
434  SUMOTime stepOffset) {
435  {
436  if (flows.knows(detector->getID())) {
437  const std::vector<FlowDef>& detFlows = flows.getFlowDefs(detector->getID());
438  for (std::vector<FlowDef>::const_iterator j = detFlows.begin(); j != detFlows.end(); ++j) {
439  if ((*j).qPKW > 0 || (*j).qLKW > 0) {
440  return;
441  }
442  }
443  }
444  }
445  // ok, there is no information for the whole time;
446  // lets find preceding detectors and rebuild the flows if possible
447  WRITE_WARNING("Detector '" + detector->getID() + "' has no flows.\n Trying to rebuild.");
448  // go back and collect flows
449  std::vector<ROEdge*> previous;
450  {
451  std::vector<IterationEdge> missing;
452  IterationEdge ie;
453  ie.depth = 0;
454  ie.edge = getDetectorEdge(*detector);
455  missing.push_back(ie);
456  bool maxDepthReached = false;
457  while (!missing.empty() && !maxDepthReached) {
458  IterationEdge last = missing.back();
459  missing.pop_back();
460  std::vector<ROEdge*> approaching = myApproachingEdges[last.edge];
461  for (std::vector<ROEdge*>::const_iterator j = approaching.begin(); j != approaching.end(); ++j) {
462  if (hasDetector(*j)) {
463  previous.push_back(*j);
464  } else {
465  ie.depth = last.depth + 1;
466  ie.edge = *j;
467  missing.push_back(ie);
468  if (ie.depth > 5) {
469  maxDepthReached = true;
470  }
471  }
472  }
473  }
474  if (maxDepthReached) {
475  WRITE_WARNING(" Could not build list of previous flows.");
476  }
477  }
478  // Edges with previous detectors are now in "previous";
479  // compute following
480  std::vector<ROEdge*> latter;
481  {
482  std::vector<IterationEdge> missing;
483  for (std::vector<ROEdge*>::const_iterator k = previous.begin(); k != previous.end(); ++k) {
484  IterationEdge ie;
485  ie.depth = 0;
486  ie.edge = *k;
487  missing.push_back(ie);
488  }
489  bool maxDepthReached = false;
490  while (!missing.empty() && !maxDepthReached) {
491  IterationEdge last = missing.back();
492  missing.pop_back();
493  std::vector<ROEdge*> approached = myApproachedEdges[last.edge];
494  for (std::vector<ROEdge*>::const_iterator j = approached.begin(); j != approached.end(); ++j) {
495  if (*j == getDetectorEdge(*detector)) {
496  continue;
497  }
498  if (hasDetector(*j)) {
499  latter.push_back(*j);
500  } else {
501  IterationEdge ie;
502  ie.depth = last.depth + 1;
503  ie.edge = *j;
504  missing.push_back(ie);
505  if (ie.depth > 5) {
506  maxDepthReached = true;
507  }
508  }
509  }
510  }
511  if (maxDepthReached) {
512  WRITE_WARNING(" Could not build list of latter flows.");
513  return;
514  }
515  }
516  // Edges with latter detectors are now in "latter";
517 
518  // lets not validate them by now - surely this should be done
519  // for each time step: collect incoming flows; collect outgoing;
520  std::vector<FlowDef> mflows;
521  int index = 0;
522  for (SUMOTime t = startTime; t < endTime; t += stepOffset, index++) {
523  FlowDef inFlow;
524  inFlow.qLKW = 0;
525  inFlow.qPKW = 0;
526  inFlow.vLKW = 0;
527  inFlow.vPKW = 0;
528  // collect incoming
529  {
530  // !! time difference is missing
531  for (std::vector<ROEdge*>::iterator i = previous.begin(); i != previous.end(); ++i) {
532  const std::vector<FlowDef>& flows = static_cast<const RODFEdge*>(*i)->getFlows();
533  if (flows.size() != 0) {
534  const FlowDef& srcFD = flows[index];
535  inFlow.qLKW += srcFD.qLKW;
536  inFlow.qPKW += srcFD.qPKW;
537  inFlow.vLKW += srcFD.vLKW;
538  inFlow.vPKW += srcFD.vPKW;
539  }
540  }
541  }
542  inFlow.vLKW /= (SUMOReal) previous.size();
543  inFlow.vPKW /= (SUMOReal) previous.size();
544  // collect outgoing
545  FlowDef outFlow;
546  outFlow.qLKW = 0;
547  outFlow.qPKW = 0;
548  outFlow.vLKW = 0;
549  outFlow.vPKW = 0;
550  {
551  // !! time difference is missing
552  for (std::vector<ROEdge*>::iterator i = latter.begin(); i != latter.end(); ++i) {
553  const std::vector<FlowDef>& flows = static_cast<const RODFEdge*>(*i)->getFlows();
554  if (flows.size() != 0) {
555  const FlowDef& srcFD = flows[index];
556  outFlow.qLKW += srcFD.qLKW;
557  outFlow.qPKW += srcFD.qPKW;
558  outFlow.vLKW += srcFD.vLKW;
559  outFlow.vPKW += srcFD.vPKW;
560  }
561  }
562  }
563  outFlow.vLKW /= (SUMOReal) latter.size();
564  outFlow.vPKW /= (SUMOReal) latter.size();
565  //
566  FlowDef mFlow;
567  mFlow.qLKW = inFlow.qLKW - outFlow.qLKW;
568  mFlow.qPKW = inFlow.qPKW - outFlow.qPKW;
569  mFlow.vLKW = (inFlow.vLKW + outFlow.vLKW) / (SUMOReal) 2.;
570  mFlow.vPKW = (inFlow.vPKW + outFlow.vPKW) / (SUMOReal) 2.;
571  mflows.push_back(mFlow);
572  }
573  static_cast<RODFEdge*>(getDetectorEdge(*detector))->setFlows(mflows);
574  flows.setFlows(detector->getID(), mflows);
575 }
576 
577 
578 void
580  RODFDetectorFlows& flows,
581  SUMOTime startTime, SUMOTime endTime,
582  SUMOTime stepOffset) {
583  const std::vector<RODFDetector*>& dets = detectors.getDetectors();
584  for (std::vector<RODFDetector*>::const_iterator i = dets.begin(); i != dets.end(); ++i) {
585  // check whether there is at least one entry with a flow larger than zero
586  revalidateFlows(*i, flows, startTime, endTime, stepOffset);
587  }
588 }
589 
590 
591 
592 void
594  RODFDetectorFlows& flows) {
595  const std::vector<RODFDetector*>& dets = detectors.getDetectors();
596  for (std::vector<RODFDetector*>::const_iterator i = dets.begin(); i != dets.end();) {
597  bool remove = true;
598  // check whether there is at least one entry with a flow larger than zero
599  if (flows.knows((*i)->getID())) {
600  remove = false;
601  }
602  if (remove) {
603  WRITE_MESSAGE("Removed detector '" + (*i)->getID() + "' because no flows for him exist.");
604  flows.removeFlow((*i)->getID());
605  detectors.removeDetector((*i)->getID());
606  i = dets.begin();
607  } else {
608  i++;
609  }
610  }
611 }
612 
613 
614 
615 void
617  RODFDetectorFlows& flows) {
618  const std::vector<RODFDetector*>& dets = detectors.getDetectors();
619  for (std::vector<RODFDetector*>::const_iterator i = dets.begin(); i != dets.end(); ++i) {
620  bool remove = true;
621  // check whether there is at least one entry with a flow larger than zero
622  if (flows.knows((*i)->getID())) {
623  remove = false;
624  }
625  if (remove) {
626  WRITE_MESSAGE("Detector '" + (*i)->getID() + "' has no flow.");
627  }
628  }
629 }
630 
631 
632 
633 ROEdge*
635  std::string edgeName = det.getLaneID();
636  edgeName = edgeName.substr(0, edgeName.rfind('_'));
637  ROEdge* ret = getEdge(edgeName);
638  if (ret == 0) {
639  throw ProcessError("Edge '" + edgeName + "' used by detector '" + det.getID() + "' is not known.");
640  }
641  return ret;
642 }
643 
644 
645 bool
647  return
648  myApproachingEdges.find(edge) != myApproachingEdges.end()
649  &&
650  myApproachingEdges.find(edge)->second.size() != 0;
651 }
652 
653 
654 bool
656  return
657  myApproachedEdges.find(edge) != myApproachedEdges.end()
658  &&
659  myApproachedEdges.find(edge)->second.size() != 0;
660 }
661 
662 
663 bool
665  return
666  myDetectorsOnEdges.find(edge) != myDetectorsOnEdges.end()
667  &&
668  myDetectorsOnEdges.find(edge)->second.size() != 0;
669 }
670 
671 
672 const std::vector<std::string>&
674  return myDetectorsOnEdges.find(edge)->second;
675 }
676 
677 
678 SUMOReal
679 RODFNet::getAbsPos(const RODFDetector& det) const {
680  if (det.getPos() >= 0) {
681  return det.getPos();
682  }
683  return getDetectorEdge(det)->getLength() + det.getPos();
684 }
685 
686 bool
687 RODFNet::isSource(const RODFDetector& det, const RODFDetectorCon& detectors,
688  bool strict) const {
689  std::vector<ROEdge*> seen;
690  return
691  isSource(det, getDetectorEdge(det), seen, detectors, strict);
692 }
693 
694 bool
695 RODFNet::isFalseSource(const RODFDetector& det, const RODFDetectorCon& detectors) const {
696  std::vector<ROEdge*> seen;
697  return
698  isFalseSource(det, getDetectorEdge(det), seen, detectors);
699 }
700 
701 bool
702 RODFNet::isDestination(const RODFDetector& det, const RODFDetectorCon& detectors) const {
703  std::vector<ROEdge*> seen;
704  return isDestination(det, getDetectorEdge(det), seen, detectors);
705 }
706 
707 
708 bool
710  std::vector<ROEdge*>& seen,
711  const RODFDetectorCon& detectors,
712  bool strict) const {
713  if (seen.size() == 1000) { // !!!
714  WRITE_WARNING("Quitting checking for being a source for detector '" + det.getID() + "' due to seen edge limit.");
715  return false;
716  }
717  if (edge == getDetectorEdge(det)) {
718  // maybe there is another detector at the same edge
719  // get the list of this/these detector(s)
720  const std::vector<std::string>& detsOnEdge = myDetectorsOnEdges.find(edge)->second;
721  for (std::vector<std::string>::const_iterator i = detsOnEdge.begin(); i != detsOnEdge.end(); ++i) {
722  if ((*i) == det.getID()) {
723  continue;
724  }
725  const RODFDetector& sec = detectors.getDetector(*i);
726  if (getAbsPos(sec) < getAbsPos(det)) {
727  // ok, there is another detector on the same edge and it is
728  // before this one -> no source
729  return false;
730  }
731  }
732  }
733  // it's a source if no edges are approaching the edge
734  if (!hasApproaching(edge)) {
735  if (edge != getDetectorEdge(det)) {
736  if (hasDetector(edge)) {
737  return false;
738  }
739  }
740  return true;
741  }
742  if (edge != getDetectorEdge(det)) {
743  // ok, we are at one of the edges in front
744  if (myAmInHighwayMode) {
745  if (edge->getSpeed() >= 19.4) {
746  if (hasDetector(edge)) {
747  // we are still on the highway and there is another detector
748  return false;
749  }
750  // the next is a hack for the A100 scenario...
751  // We have to look into further edges herein edges
752  const std::vector<ROEdge*>& appr = myApproachingEdges.find(edge)->second;
753  size_t noOk = 0;
754  size_t noFalse = 0;
755  size_t noSkipped = 0;
756  for (size_t i = 0; i < appr.size(); i++) {
757  if (!hasDetector(appr[i])) {
758  noOk++;
759  } else {
760  noFalse++;
761  }
762  }
763  if ((noFalse + noSkipped) == appr.size()) {
764  return false;
765  }
766  }
767  }
768  }
769 
770  if (myAmInHighwayMode) {
771  if (edge->getSpeed() < 19.4 && edge != getDetectorEdge(det)) {
772  // we have left the highway already
773  // -> the detector will be a highway source
774  if (!hasDetector(edge)) {
775  return true;
776  }
777  }
778  }
779  if (myDetectorsOnEdges.find(edge) != myDetectorsOnEdges.end()
780  &&
781  myDetectorEdges.find(det.getID())->second != edge) {
782  return false;
783  }
784 
785  // let's check the edges in front
786  const std::vector<ROEdge*>& appr = myApproachingEdges.find(edge)->second;
787  size_t noOk = 0;
788  size_t noFalse = 0;
789  size_t noSkipped = 0;
790  seen.push_back(edge);
791  for (size_t i = 0; i < appr.size(); i++) {
792  bool had = std::find(seen.begin(), seen.end(), appr[i]) != seen.end();
793  if (!had) {
794  if (isSource(det, appr[i], seen, detectors, strict)) {
795  noOk++;
796  } else {
797  noFalse++;
798  }
799  } else {
800  noSkipped++;
801  }
802  }
803  if (!strict) {
804  return (noFalse + noSkipped) != appr.size();
805  } else {
806  return (noOk + noSkipped) == appr.size();
807  }
808 }
809 
810 
811 bool
812 RODFNet::isDestination(const RODFDetector& det, ROEdge* edge, std::vector<ROEdge*>& seen,
813  const RODFDetectorCon& detectors) const {
814  if (seen.size() == 1000) { // !!!
815  WRITE_WARNING("Quitting checking for being a destination for detector '" + det.getID() + "' due to seen edge limit.");
816  return false;
817  }
818  if (edge == getDetectorEdge(det)) {
819  // maybe there is another detector at the same edge
820  // get the list of this/these detector(s)
821  const std::vector<std::string>& detsOnEdge = myDetectorsOnEdges.find(edge)->second;
822  for (std::vector<std::string>::const_iterator i = detsOnEdge.begin(); i != detsOnEdge.end(); ++i) {
823  if ((*i) == det.getID()) {
824  continue;
825  }
826  const RODFDetector& sec = detectors.getDetector(*i);
827  if (getAbsPos(sec) > getAbsPos(det)) {
828  // ok, there is another detector on the same edge and it is
829  // after this one -> no destination
830  return false;
831  }
832  }
833  }
834  if (!hasApproached(edge)) {
835  if (edge != getDetectorEdge(det)) {
836  if (hasDetector(edge)) {
837  return false;
838  }
839  }
840  return true;
841  }
842  if (edge != getDetectorEdge(det)) {
843  // ok, we are at one of the edges coming behind
844  if (myAmInHighwayMode) {
845  if (edge->getSpeed() >= 19.4) {
846  if (hasDetector(edge)) {
847  // we are still on the highway and there is another detector
848  return false;
849  }
850  }
851  }
852  }
853 
854  if (myAmInHighwayMode) {
855  if (edge->getSpeed() < 19.4 && edge != getDetectorEdge(det)) {
856  if (hasDetector(edge)) {
857  return true;
858  }
859  if (myApproachedEdges.find(edge)->second.size() > 1) {
860  return true;
861  }
862 
863  }
864  }
865 
866  if (myDetectorsOnEdges.find(edge) != myDetectorsOnEdges.end()
867  &&
868  myDetectorEdges.find(det.getID())->second != edge) {
869  return false;
870  }
871  const std::vector<ROEdge*>& appr = myApproachedEdges.find(edge)->second;
872  bool isall = true;
873  size_t no = 0;
874  seen.push_back(edge);
875  for (size_t i = 0; i < appr.size() && isall; i++) {
876  bool had = std::find(seen.begin(), seen.end(), appr[i]) != seen.end();
877  if (!had) {
878  if (!isDestination(det, appr[i], seen, detectors)) {
879  no++;
880  isall = false;
881  }
882  }
883  }
884  return isall;
885 }
886 
887 bool
888 RODFNet::isFalseSource(const RODFDetector& det, ROEdge* edge, std::vector<ROEdge*>& seen,
889  const RODFDetectorCon& detectors) const {
890  if (seen.size() == 1000) { // !!!
891  WRITE_WARNING("Quitting checking for being a false source for detector '" + det.getID() + "' due to seen edge limit.");
892  return false;
893  }
894  seen.push_back(edge);
895  if (edge != getDetectorEdge(det)) {
896  // ok, we are at one of the edges coming behind
897  if (hasDetector(edge)) {
898  const std::vector<std::string>& dets = myDetectorsOnEdges.find(edge)->second;
899  for (std::vector<std::string>::const_iterator i = dets.begin(); i != dets.end(); ++i) {
900  if (detectors.getDetector(*i).getType() == SINK_DETECTOR) {
901  return false;
902  }
903  if (detectors.getDetector(*i).getType() == BETWEEN_DETECTOR) {
904  return false;
905  }
906  if (detectors.getDetector(*i).getType() == SOURCE_DETECTOR) {
907  return true;
908  }
909  }
910  } else {
911  if (myAmInHighwayMode && edge->getSpeed() < 19.) {
912  return false;
913  }
914  }
915  }
916 
917  if (myApproachedEdges.find(edge) == myApproachedEdges.end()) {
918  return false;
919  }
920 
921  const std::vector<ROEdge*>& appr = myApproachedEdges.find(edge)->second;
922  bool isall = false;
923  for (size_t i = 0; i < appr.size() && !isall; i++) {
924  //printf("checking %s->\n", appr[i].c_str());
925  bool had = std::find(seen.begin(), seen.end(), appr[i]) != seen.end();
926  if (!had) {
927  if (isFalseSource(det, appr[i], seen, detectors)) {
928  isall = true;
929  }
930  }
931  }
932  return isall;
933 }
934 
935 
936 void
938  const RODFDetectorCon& detectors,
939  SUMOTime startTime, SUMOTime endTime,
940  SUMOTime stepOffset) {
941  std::map<ROEdge*, std::vector<std::string>, idComp>::iterator i;
942  for (i = myDetectorsOnEdges.begin(); i != myDetectorsOnEdges.end(); ++i) {
943  ROEdge* into = (*i).first;
944  const std::vector<std::string>& dets = (*i).second;
945  std::map<SUMOReal, std::vector<std::string> > cliques;
946  std::vector<std::string>* maxClique = 0;
947  for (std::vector<std::string>::const_iterator j = dets.begin(); j != dets.end(); ++j) {
948  if (!flows.knows(*j)) {
949  continue;
950  }
951  const RODFDetector& det = detectors.getDetector(*j);
952  bool found = false;
953  for (std::map<SUMOReal, std::vector<std::string> >::iterator k = cliques.begin(); !found && k != cliques.end(); ++k) {
954  if (fabs((*k).first - det.getPos()) < 1) {
955  (*k).second.push_back(*j);
956  if ((*k).second.size() > maxClique->size()) {
957  maxClique = &(*k).second;
958  }
959  found = true;
960  }
961  }
962  if (!found) {
963  cliques[det.getPos()].push_back(*j);
964  maxClique = &cliques[det.getPos()];
965  }
966  }
967  if (maxClique == 0) {
968  continue;
969  }
970  std::vector<FlowDef> mflows; // !!! reserve
971  for (SUMOTime t = startTime; t < endTime; t += stepOffset) {
972  FlowDef fd;
973  fd.qPKW = 0;
974  fd.qLKW = 0;
975  fd.vLKW = 0;
976  fd.vPKW = 0;
977  fd.fLKW = 0;
978  fd.isLKW = 0;
979  mflows.push_back(fd);
980  }
981  for (std::vector<std::string>::iterator l = maxClique->begin(); l != maxClique->end(); ++l) {
982  bool didWarn = false;
983  const std::vector<FlowDef>& dflows = flows.getFlowDefs(*l);
984  int index = 0;
985  for (SUMOTime t = startTime; t < endTime; t += stepOffset, index++) {
986  const FlowDef& srcFD = dflows[index];
987  FlowDef& fd = mflows[index];
988  fd.qPKW += srcFD.qPKW;
989  fd.qLKW += srcFD.qLKW;
990  fd.vLKW += (srcFD.vLKW / (SUMOReal) maxClique->size());
991  fd.vPKW += (srcFD.vPKW / (SUMOReal) maxClique->size());
992  fd.fLKW += (srcFD.fLKW / (SUMOReal) maxClique->size());
993  fd.isLKW += (srcFD.isLKW / (SUMOReal) maxClique->size());
994  if (!didWarn && srcFD.vPKW > 0 && srcFD.vPKW < 255 && srcFD.vPKW / 3.6 > into->getSpeed()) {
995  WRITE_MESSAGE("Detected PKW speed higher than allowed speed at '" + (*l) + "' on '" + into->getID() + "'.");
996  didWarn = true;
997  }
998  if (!didWarn && srcFD.vLKW > 0 && srcFD.vLKW < 255 && srcFD.vLKW / 3.6 > into->getSpeed()) {
999  WRITE_MESSAGE("Detected LKW speed higher than allowed speed at '" + (*l) + "' on '" + into->getID() + "'.");
1000  didWarn = true;
1001  }
1002  }
1003  }
1004  static_cast<RODFEdge*>(into)->setFlows(mflows);
1005  }
1006 }
1007 
1008 
1009 void
1011  // !!! this will not work when several detectors are lying on the same edge on different positions
1012 
1013 
1014  buildDetectorEdgeDependencies(detectors);
1015  // for each detector, compute the lists of predecessor and following detectors
1016  std::map<std::string, ROEdge*>::const_iterator i;
1017  for (i = myDetectorEdges.begin(); i != myDetectorEdges.end(); ++i) {
1018  const RODFDetector& det = detectors.getDetector((*i).first);
1019  if (!det.hasRoutes()) {
1020  continue;
1021  }
1022  // mark current detectors
1023  std::vector<RODFDetector*> last;
1024  {
1025  const std::vector<std::string>& detNames = myDetectorsOnEdges.find((*i).second)->second;
1026  for (std::vector<std::string>::const_iterator j = detNames.begin(); j != detNames.end(); ++j) {
1027  last.push_back((RODFDetector*) &detectors.getDetector(*j));
1028  }
1029  }
1030  // iterate over the current detector's routes
1031  const std::vector<RODFRouteDesc>& routes = det.getRouteVector();
1032  for (std::vector<RODFRouteDesc>::const_iterator j = routes.begin(); j != routes.end(); ++j) {
1033  const std::vector<ROEdge*>& edges2Pass = (*j).edges2Pass;
1034  for (std::vector<ROEdge*>::const_iterator k = edges2Pass.begin() + 1; k != edges2Pass.end(); ++k) {
1035  if (myDetectorsOnEdges.find(*k) != myDetectorsOnEdges.end()) {
1036  const std::vector<std::string>& detNames = myDetectorsOnEdges.find(*k)->second;
1037  // ok, consecutive detector found
1038  for (std::vector<RODFDetector*>::iterator l = last.begin(); l != last.end(); ++l) {
1039  // mark as follower of current
1040  for (std::vector<std::string>::const_iterator m = detNames.begin(); m != detNames.end(); ++m) {
1041  ((RODFDetector*) &detectors.getDetector(*m))->addPriorDetector((RODFDetector*) & (*l));
1042  (*l)->addFollowingDetector((RODFDetector*) &detectors.getDetector(*m));
1043  }
1044  }
1045  last.clear();
1046  for (std::vector<std::string>::const_iterator m = detNames.begin(); m != detNames.end(); ++m) {
1047  last.push_back((RODFDetector*) &detectors.getDetector(*m));
1048  }
1049  }
1050  }
1051  }
1052  }
1053 }
1054 
1055 
1056 void
1058  buildDetectorEdgeDependencies(detectors);
1059  std::map<ROEdge*, std::vector<std::string>, idComp>::iterator i;
1060  for (i = myDetectorsOnEdges.begin(); i != myDetectorsOnEdges.end(); ++i) {
1061  const std::vector<std::string>& dets = (*i).second;
1062  std::map<SUMOReal, std::vector<std::string> > cliques;
1063  // compute detector cliques
1064  for (std::vector<std::string>::const_iterator j = dets.begin(); j != dets.end(); ++j) {
1065  const RODFDetector& det = detectors.getDetector(*j);
1066  bool found = false;
1067  for (std::map<SUMOReal, std::vector<std::string> >::iterator k = cliques.begin(); !found && k != cliques.end(); ++k) {
1068  if (fabs((*k).first - det.getPos()) < 10.) {
1069  (*k).second.push_back(*j);
1070  found = true;
1071  }
1072  }
1073  if (!found) {
1074  cliques[det.getPos()] = std::vector<std::string>();
1075  cliques[det.getPos()].push_back(*j);
1076  }
1077  }
1078  // join detector cliques
1079  for (std::map<SUMOReal, std::vector<std::string> >::iterator m = cliques.begin(); m != cliques.end(); ++m) {
1080  std::vector<std::string> clique = (*m).second;
1081  // do not join if only one
1082  if (clique.size() == 1) {
1083  continue;
1084  }
1085  std::string nid;
1086  for (std::vector<std::string>::iterator n = clique.begin(); n != clique.end(); ++n) {
1087  std::cout << *n << " ";
1088  if (n != clique.begin()) {
1089  nid = nid + "_";
1090  }
1091  nid = nid + *n;
1092  }
1093  std::cout << ":" << nid << std::endl;
1094  flows.mesoJoin(nid, (*m).second);
1095  detectors.mesoJoin(nid, (*m).second);
1096  }
1097  }
1098 }
1099 
1100 
1101 
1102 /****************************************************************************/
1103