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