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