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