SUMO - Simulation of Urban MObility
ROJTREdge.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2004-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
20 // An edge the jtr-router may route through
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <algorithm>
34 #include <cassert>
37 #include "ROJTREdge.h"
39 
40 
41 // ===========================================================================
42 // method definitions
43 // ===========================================================================
44 ROJTREdge::ROJTREdge(const std::string& id, RONode* from, RONode* to, int index, const int priority)
45  : ROEdge(id, from, to, index, priority) {}
46 
47 
49  for (FollowerUsageCont::iterator i = myFollowingDefs.begin(); i != myFollowingDefs.end(); ++i) {
50  delete(*i).second;
51  }
52 }
53 
54 
55 void
56 ROJTREdge::addSuccessor(ROEdge* s, std::string) {
58  ROJTREdge* js = static_cast<ROJTREdge*>(s);
59  if (myFollowingDefs.find(js) == myFollowingDefs.end()) {
61  }
62 }
63 
64 
65 void
66 ROJTREdge::addFollowerProbability(ROJTREdge* follower, double begTime,
67  double endTime, double probability) {
68  FollowerUsageCont::iterator i = myFollowingDefs.find(follower);
69  if (i == myFollowingDefs.end()) {
70  WRITE_ERROR("The edges '" + getID() + "' and '" + follower->getID() + "' are not connected.");
71  return;
72  }
73  (*i).second->add(begTime, endTime, probability);
74 }
75 
76 
77 ROJTREdge*
78 ROJTREdge::chooseNext(const ROVehicle* const veh, double time, const std::set<const ROEdge*>& avoid) const {
79  // if no usable follower exist, return 0
80  // their probabilities are not yet regarded
81  if (myFollowingEdges.size() == 0 || (veh != 0 && allFollowersProhibit(veh))) {
82  return 0;
83  }
84  // gather information about the probabilities at this time
86  // use the loaded definitions, first
87  for (FollowerUsageCont::const_iterator i = myFollowingDefs.begin(); i != myFollowingDefs.end(); ++i) {
88  if (avoid.count(i->first) == 0) {
89  if ((veh == 0 || !(*i).first->prohibits(veh)) && (*i).second->describesTime(time)) {
90  dist.add((*i).first, (*i).second->getValue(time));
91  }
92  }
93  }
94  // if no loaded definitions are valid for this time, try to use the defaults
95  if (dist.getOverallProb() == 0) {
96  for (int i = 0; i < (int)myParsedTurnings.size(); ++i) {
97  if (avoid.count(myFollowingEdges[i]) == 0) {
98  if (veh == 0 || !myFollowingEdges[i]->prohibits(veh)) {
99  dist.add(static_cast<ROJTREdge*>(myFollowingEdges[i]), myParsedTurnings[i]);
100  }
101  }
102  }
103  }
104  // if still no valid follower exists, return null
105  if (dist.getOverallProb() == 0) {
106  return 0;
107  }
108  // return one of the possible followers
109  return dist.get();
110 }
111 
112 
113 void
114 ROJTREdge::setTurnDefaults(const std::vector<double>& defs) {
115  // I hope, we'll find a less ridiculous solution for this
116  std::vector<double> tmp(defs.size()*myFollowingEdges.size(), 0);
117  // store in less common multiple
118  for (int i = 0; i < (int)defs.size(); ++i) {
119  for (int j = 0; j < (int)myFollowingEdges.size(); ++j) {
120  tmp[i * myFollowingEdges.size() + j] = (double)(defs[i] / 100.0 / (myFollowingEdges.size()));
121  }
122  }
123  // parse from less common multiple
124  for (int i = 0; i < (int)myFollowingEdges.size(); ++i) {
125  double value = 0;
126  for (int j = 0; j < (int)defs.size(); ++j) {
127  value += tmp[i * defs.size() + j];
128  }
129  myParsedTurnings.push_back((double) value);
130  }
131 }
132 
133 
134 
135 /****************************************************************************/
136 
FollowerUsageCont myFollowingDefs
Storage for the probabilities of using a certain follower over time.
Definition: ROJTREdge.h:117
bool allFollowersProhibit(const ROVehicle *const vehicle) const
Returns whether this edge succeeding edges prohibit the given vehicle to pass them.
Definition: ROEdge.cpp:308
Represents a generic random distribution.
bool prohibits(const ROVehicle *const vehicle) const
Returns whether this edge prohibits the given vehicle to pass it.
Definition: ROEdge.h:269
const std::string & getID() const
Returns the id.
Definition: Named.h:65
std::vector< double > myParsedTurnings
The defaults for turnings.
Definition: ROJTREdge.h:120
ROEdgeVector myFollowingEdges
List of edges that may be approached from this edge.
Definition: ROEdge.h:528
A vehicle as used by router.
Definition: ROVehicle.h:59
T get(std::mt19937 *which=0) const
Draw a sample of the distribution.
An edge the jtr-router may route through.
Definition: ROJTREdge.h:57
void addFollowerProbability(ROJTREdge *follower, double begTime, double endTime, double probability)
adds the information about the percentage of using a certain follower
Definition: ROJTREdge.cpp:66
void addSuccessor(ROEdge *s, std::string dir="")
Adds information about a connected edge.
Definition: ROJTREdge.cpp:56
A basic edge for routing applications.
Definition: ROEdge.h:77
~ROJTREdge()
Destructor.
Definition: ROJTREdge.cpp:48
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:205
ROJTREdge(const std::string &id, RONode *from, RONode *to, int index, const int priority)
Constructor.
Definition: ROJTREdge.cpp:44
virtual void addSuccessor(ROEdge *s, std::string dir="")
Adds information about a connected edge.
Definition: ROEdge.cpp:106
void setTurnDefaults(const std::vector< double > &defs)
Sets the turning definition defaults.
Definition: ROJTREdge.cpp:114
double getOverallProb() const
Return the sum of the probabilites assigned to the members.
ROJTREdge * chooseNext(const ROVehicle *const veh, double time, const std::set< const ROEdge *> &avoid) const
Returns the next edge to use.
Definition: ROJTREdge.cpp:78
Base class for nodes used by the router.
Definition: RONode.h:52
bool add(T val, double prob, bool checkDuplicates=true)
Adds a value with an assigned probability to the distribution.