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