SUMO - Simulation of Urban MObility
MSSOTLPolicy5DFamilyStimulus.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2014-2018 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 /****************************************************************************/
16 // The class for Swarm-based low-level policy
17 /****************************************************************************/
18 
19 
20 // ===========================================================================
21 // included modules
22 // ===========================================================================
23 #include <config.h>
24 
27 
28 
29 // ===========================================================================
30 // method definitions
31 // ===========================================================================
33  const std::map<std::string, std::string>& parameters) :
34  MSSOTLPolicyDesirability(keyPrefix, parameters) {
35 
36  default_values["_STIM_COX"] = "1";
37  default_values["_STIM_OFFSET_IN"] = "1";
38  default_values["_STIM_OFFSET_OUT"] = "1";
39  default_values["_STIM_OFFSET_DISPERSION_IN"] = "1";
40  default_values["_STIM_OFFSET_DISPERSION_OUT"] = "1";
41  default_values["_STIM_DIVISOR_IN"] = "1";
42  default_values["_STIM_DIVISOR_OUT"] = "1";
43  default_values["_STIM_DIVISOR_DISPERSION_IN"] = "1";
44  default_values["_STIM_DIVISOR_DISPERSION_OUT"] = "1";
45  default_values["_STIM_COX_EXP_IN"] = "0";
46  default_values["_STIM_COX_EXP_OUT"] = "0";
47  default_values["_STIM_COX_EXP_DISPERSION_IN"] = "0";
48  default_values["_STIM_COX_EXP_DISPERSION_OUT"] = "0";
49 
50  params_names.push_back("_STIM_COX");
51  params_names.push_back("_STIM_OFFSET_IN");
52  params_names.push_back("_STIM_OFFSET_OUT");
53  params_names.push_back("_STIM_OFFSET_DISPERSION_IN");
54  params_names.push_back("_STIM_OFFSET_DISPERSION_OUT");
55  params_names.push_back("_STIM_DIVISOR_IN");
56  params_names.push_back("_STIM_DIVISOR_OUT");
57  params_names.push_back("_STIM_DIVISOR_DISPERSION_IN");
58  params_names.push_back("_STIM_DIVISOR_DISPERSION_OUT");
59  params_names.push_back("_STIM_COX_EXP_IN");
60  params_names.push_back("_STIM_COX_EXP_OUT");
61  params_names.push_back("_STIM_COX_EXP_DISPERSION_IN");
62  params_names.push_back("_STIM_COX_EXP_DISPERSION_OUT");
63 
64 
65  int size_family = int(getDouble(keyPrefix + "_SIZE_FAMILY", 1));
66  DBG(
67 
68  std::ostringstream str;
69  str << keyPrefix << "\n" << "size fam" << size_family;
70  WRITE_MESSAGE(str.str());
71  )
72 
73  std::vector< std::map <std::string, std::string > > sliced_maps;
74 
75  for (int i = 0; i < size_family; i++) {
76  sliced_maps.push_back(std::map<std::string, std::string>());
77  }
78 
79  //For each param list, slice values
80  for (int i = 0; i < (int)params_names.size(); i ++) {
81  std::string key = keyPrefix + params_names[i];
82  std::string param_list = getParameter(key, default_values[params_names[i]]);
83  std::vector<std::string> tokens = StringTokenizer(param_list, ";").getVector();
84 
85  for (int token_counter = 0; token_counter < size_family; ++token_counter) {
86  if (token_counter >= (int)tokens.size()) {
87  std::ostringstream errorMessage;
88  errorMessage << "Error in " << key << ": not enough tokens.";
89  WRITE_ERROR(errorMessage.str());
90  assert(-1);
91  }
92  DBG(
93  std::ostringstream str;
94  str << "found token " << tokens[token_counter] << " position " << token_counter;
95  WRITE_MESSAGE(str.str());
96  )
97  sliced_maps[token_counter][key] = tokens[token_counter];
98  }
99  }
100 
101  for (int i = 0; i < size_family; i++) {
102  std::map<std::string, std::string>& ref_map = sliced_maps[i];
103  family.push_back(new MSSOTLPolicy5DStimulus(keyPrefix, ref_map));
104  }
105 
106 }
107 
108 
109 double MSSOTLPolicy5DFamilyStimulus::computeDesirability(double vehInMeasure, double vehOutMeasure, double vehInDispersionMeasure, double vehOutDispersionMeasure) {
110  /*DBG(
111  std::ostringstream str;
112  str << "cox=" << getStimCox() << ", cox_exp_in=" << getStimCoxExpIn() << ", cox_exp_out=" << getStimCoxExpOut()
113  << ", off_in=" << getStimOffsetIn() << ", off_out=" << getStimOffsetOut() << ", div_in=" << getStimDivisorIn() << ", div_out=" << getStimDivisorOut(); WRITE_MESSAGE(str.str());)
114  */
115  // it seems to be not enough, a strange segmentation fault appears...
116  // if((getStimCoxExpIn()!=0.0 && getStimDivisorIn()==0.0)||(getStimCoxExpOut()!=0.0 && getStimDivisorOut()==0.0)){
117 
118  double best_stimulus = -1;
119  for (std::vector<MSSOTLPolicy5DStimulus*>::const_iterator it = family.begin(); it != family.end(); it++) {
120  double temp_stimulus = (*it)->computeDesirability(vehInMeasure, vehOutMeasure, vehInDispersionMeasure, vehOutDispersionMeasure);
121  DBG(
122  std::ostringstream str;
123  str << "STIMULUS: " << temp_stimulus;
124  WRITE_MESSAGE(str.str());
125  )
126  if (temp_stimulus > best_stimulus) {
127  best_stimulus = temp_stimulus;
128  }
129  }
130 
131  DBG(
132  std::ostringstream str;
133  str << "BEST STIMULUS: " << best_stimulus;
134  WRITE_MESSAGE(str.str());
135  )
136  return best_stimulus;
137 }
138 
139 
140 double MSSOTLPolicy5DFamilyStimulus::computeDesirability(double vehInMeasure, double vehOutMeasure) {
141 
142  return computeDesirability(vehInMeasure, vehOutMeasure, 0, 0);
143 }
144 
146  std::ostringstream ot;
147  for (int i = 0; i < (int)family.size(); i++) {
148  ot << " gaussian " << i << ":" << family[i]->getMessage();
149  }
150  return ot.str();
151 }
152 
153 /*
154 std::vector<std::string> inline MSSOTLPolicy5DFamilyStimulus::StringSplit(const std::string &source, const char *delimiter = " ", bool keepEmpty = false)
155 {
156  std::vector<std::string> results;
157 
158  int prev = 0;
159  std::string::size_type next = 0;
160 
161  while ((next = source.find_first_of(delimiter, prev)) != std::string::npos)
162  {
163  if (keepEmpty || (next - prev != 0))
164  {
165  results.push_back(source.substr(prev, next - prev));
166  }
167  prev = next + 1;
168  }
169 
170  if (prev < source.size())
171  {
172  results.push_back(source.substr(prev));
173  }
174 
175  return results;
176 }
177 */
MSSOTLPolicy5DFamilyStimulus(std::string keyPrefix, const std::map< std::string, std::string > &parameters)
std::map< std::string, std::string > default_values
virtual double computeDesirability(double vehInMeasure, double vehOutMeasure)
Calculates the desirability of the policy.
std::vector< MSSOTLPolicy5DStimulus * > family
#define DBG(X)
Definition: SwarmDebug.h:27
std::vector< std::string > getVector()
double getDouble(const std::string &key, const double defaultValue) const
Returns the value for a given key converted to a double.
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:247
This class determines the desirability algorithm of a MSSOTLPolicy when used in combination with a hi...
std::vector< std::string > params_names
const std::string getParameter(const std::string &key, const std::string &defaultValue="") const
Returns the value for a given key.
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:242