ADORe
ADORe is a modular open source software library and toolkit for decision making, planning, control and simulation of automated vehicles
evaluator_weighted_sum.h
Go to the documentation of this file.
1 /********************************************************************************
2  * Copyright (C) 2017-2020 German Aerospace Center (DLR).
3  * Eclipse ADORe, Automated Driving Open Research https://eclipse.org/adore
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License 2.0 which is available at
7  * http://www.eclipse.org/legal/epl-2.0.
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  * Daniel Heß - initial API and implementation
13  ********************************************************************************/
14 
15 #pragma once
16 
18 #include <adore/fun/turnstate.h>
19 #include <vector>
20 #include <string>
21 #include <limits>
22 
23 namespace adore
24 {
25  namespace fun
26  {
31  {
32  private:
33  std::vector<std::pair<std::string,double>> objectives_;
34  std::vector<std::pair<int,double>> cost_list_;
35  double max_cost_;
37 
38 
39  public:
40 
42 
43  void setTurnState(Turnstate state)
44  {
45  turnstate_ = state;
46  }
47  void addParameterPair(std::string name,std::string weight_string)
48  {
49  double weight = 0.0;
50  try{weight = std::stod(weight_string);}catch(...){}
51  objectives_.push_back(std::make_pair(name,weight));
52  }
53  void init()
54  {
55  if(objectives_.size()==0)
56  {
57  objectives_.push_back(std::make_pair("MinimumNavigationCostOnLane",1.0));
58  }
59  }
63  double getCost(int id)
64  {
65  int idx = getIndex(id);
66  if(idx==-1)return std::numeric_limits<double>::quiet_NaN();
67  else return cost_list_[idx].second;
68  }
72  int getIndex(int id)
73  {
74  for(int i=0;i<cost_list_.size();i++)
75  {
76  if(cost_list_[i].first==id)return i;
77  }
78  return -1;
79  }
83  int evaluateToBest(const PlanningResultMap & planning_results) override
84  {
85  init();
86  cost_list_.clear();
87  for (auto [id, plan] : planning_results)//go through results
88  {
89  bool all_objectives_present = true;
90  double weighted_sum = 0.0;
91  for( auto [objective_name,objective_weight]: objectives_)//go through relevant objectives
92  {
93  if(objective_weight>0.0)//if the weight is greater then zero
94  {
95  try
96  {
97  weighted_sum += objective_weight * plan.objective_values[objective_name];
98  }
99  catch(...)
100  {
101  all_objectives_present = false;
102  std::cout<<"plan "<<plan.name<<" missing objective "<<objective_name<<"."<<std::endl;
103  }
104  }
105  }
106  //temporal solution: flat bonus if indicator matches
107  if ( plan.indicator_left && turnstate_ == Turnstate::left)
108  {
109  weighted_sum += 2001.0;
110  }
111  if ( plan.indicator_right && turnstate_ == Turnstate::right)
112  {
113  weighted_sum += 2001.0;
114  }
115 
116 
117  if( !all_objectives_present )
118  {
119  std::cout<<"plan "<<plan.name<<" missing objectives."<<std::endl;
120  continue;
121  }
122  if(!(weighted_sum>=0.0))
123  {
124  std::cout<<"plan "<<plan.name<<" weighted_sum not positive: "<<weighted_sum<<"."<<std::endl;
125  continue;
126  }
127  if(!(weighted_sum<max_cost_))
128  {
129  std::cout<<"plan "<<plan.name<<" weighted_sum not smaller max_cost: "<<weighted_sum<<">="<<max_cost_<<"."<<std::endl;
130  continue;
131  }
132  cost_list_.push_back(std::make_pair(id,weighted_sum));
133  }
134  if(cost_list_.size()==0)return -1;
135  double min_id = cost_list_[0].first;
136  double min_value = cost_list_[0].second;
137  for(int i=1;i<cost_list_.size();i++)
138  {
139  if(cost_list_[i].second<min_value)
140  {
141  min_id = cost_list_[i].first;
142  min_value = cost_list_[i].second;
143  }
144  }
145  return min_id;
146  }
147 
148  };
149 
150  } // namespace fun
151 } // namespace adore
Definition: atrajectory_evaluator.h:32
Definition: evaluator_weighted_sum.h:31
double max_cost_
Definition: evaluator_weighted_sum.h:35
std::vector< std::pair< std::string, double > > objectives_
Definition: evaluator_weighted_sum.h:33
void init()
Definition: evaluator_weighted_sum.h:53
std::vector< std::pair< int, double > > cost_list_
Definition: evaluator_weighted_sum.h:34
int getIndex(int id)
Definition: evaluator_weighted_sum.h:72
Turnstate turnstate_
Definition: evaluator_weighted_sum.h:36
int evaluateToBest(const PlanningResultMap &planning_results) override
Definition: evaluator_weighted_sum.h:83
void setTurnState(Turnstate state)
Definition: evaluator_weighted_sum.h:43
void addParameterPair(std::string name, std::string weight_string)
Definition: evaluator_weighted_sum.h:47
EvaluatorWeightedSum()
Definition: evaluator_weighted_sum.h:41
double getCost(int id)
Definition: evaluator_weighted_sum.h:63
@ right
Definition: indicator_hint.h:36
@ left
Definition: indicator_hint.h:35
Turnstate
Definition: turnstate.h:21
std::unordered_map< int, adore::fun::PlanningResult > PlanningResultMap
Definition: atrajectory_evaluator.h:29
Definition: areaofeffectconverter.h:20