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