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