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-2022 German Aerospace Center (DLR) and others.
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// https://www.eclipse.org/legal/epl-2.0/
7// This Source Code may also be made available under the following Secondary
8// Licenses when the conditions for such availability set forth in the Eclipse
9// Public License 2.0 are satisfied: GNU General Public License, version 2
10// or later which is available at
11// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13/****************************************************************************/
19// The class for Swarm-based low-level policy
20/****************************************************************************/
21#include <config.h>
22
25//#define SWARM_DEBUG
26
27
28// ===========================================================================
29// method definitions
30// ===========================================================================
32 const Parameterised::Map& 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#ifdef SWARM_DEBUG
66 std::ostringstream str;
67 str << keyPrefix << "\n" << "size fam" << size_family;
68 WRITE_MESSAGE(str.str());
69#endif
70
71 std::vector< std::map <std::string, std::string > > sliced_maps;
72
73 for (int i = 0; i < size_family; i++) {
74 sliced_maps.push_back(Parameterised::Map());
75 }
76
77 //For each param list, slice values
78 for (int i = 0; i < (int)params_names.size(); i ++) {
79 std::string key = keyPrefix + params_names[i];
80 std::string param_list = getParameter(key, default_values[params_names[i]]);
81 std::vector<std::string> tokens = StringTokenizer(param_list, ";").getVector();
82
83 for (int token_counter = 0; token_counter < size_family; ++token_counter) {
84 if (token_counter >= (int)tokens.size()) {
85 std::ostringstream errorMessage;
86 errorMessage << "Error in " << key << ": not enough tokens.";
87 WRITE_ERROR(errorMessage.str());
88 assert(-1);
89 }
90#ifdef SWARM_DEBUG
91 std::ostringstream str;
92 str << "found token " << tokens[token_counter] << " position " << token_counter;
93 WRITE_MESSAGE(str.str());
94#endif
95 sliced_maps[token_counter][key] = tokens[token_counter];
96 }
97 }
98
99 for (int i = 0; i < size_family; i++) {
100 Parameterised::Map& ref_map = sliced_maps[i];
101 family.push_back(new MSSOTLPolicy5DStimulus(keyPrefix, ref_map));
102 }
103
104}
105
106
107double MSSOTLPolicy5DFamilyStimulus::computeDesirability(double vehInMeasure, double vehOutMeasure, double vehInDispersionMeasure, double vehOutDispersionMeasure) {
108 /*DBG(
109 std::ostringstream str;
110 str << "cox=" << getStimCox() << ", cox_exp_in=" << getStimCoxExpIn() << ", cox_exp_out=" << getStimCoxExpOut()
111 << ", off_in=" << getStimOffsetIn() << ", off_out=" << getStimOffsetOut() << ", div_in=" << getStimDivisorIn() << ", div_out=" << getStimDivisorOut(); WRITE_MESSAGE(str.str());)
112 */
113 // it seems to be not enough, a strange segmentation fault appears...
114 // if((getStimCoxExpIn()!=0.0 && getStimDivisorIn()==0.0)||(getStimCoxExpOut()!=0.0 && getStimDivisorOut()==0.0)){
115
116 double best_stimulus = -1;
117 for (std::vector<MSSOTLPolicy5DStimulus*>::const_iterator it = family.begin(); it != family.end(); it++) {
118 double temp_stimulus = (*it)->computeDesirability(vehInMeasure, vehOutMeasure, vehInDispersionMeasure, vehOutDispersionMeasure);
119#ifdef SWARM_DEBUG
120 std::ostringstream str;
121 str << "STIMULUS: " << temp_stimulus;
122 WRITE_MESSAGE(str.str());
123#endif
124 if (temp_stimulus > best_stimulus) {
125 best_stimulus = temp_stimulus;
126 }
127 }
128
129#ifdef SWARM_DEBUG
130 std::ostringstream str;
131 str << "BEST STIMULUS: " << best_stimulus;
132 WRITE_MESSAGE(str.str());
133#endif
134 return best_stimulus;
135}
136
137
138double MSSOTLPolicy5DFamilyStimulus::computeDesirability(double vehInMeasure, double vehOutMeasure) {
139
140 return computeDesirability(vehInMeasure, vehOutMeasure, 0, 0);
141}
142
144 std::ostringstream ot;
145 for (int i = 0; i < (int)family.size(); i++) {
146 ot << " gaussian " << i << ":" << family[i]->getMessage();
147 }
148 return ot.str();
149}
150
151/*
152std::vector<std::string> inline MSSOTLPolicy5DFamilyStimulus::StringSplit(const std::string &source, const char *delimiter = " ", bool keepEmpty = false)
153{
154 std::vector<std::string> results;
155
156 int prev = 0;
157 std::string::size_type next = 0;
158
159 while ((next = source.find_first_of(delimiter, prev)) != std::string::npos)
160 {
161 if (keepEmpty || (next - prev != 0))
162 {
163 results.push_back(source.substr(prev, next - prev));
164 }
165 prev = next + 1;
166 }
167
168 if (prev < source.size())
169 {
170 results.push_back(source.substr(prev));
171 }
172
173 return results;
174}
175*/
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:267
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:274
std::vector< MSSOTLPolicy5DStimulus * > family
virtual double computeDesirability(double vehInMeasure, double vehOutMeasure)
Calculates the desirability of the policy.
std::vector< std::string > params_names
MSSOTLPolicy5DFamilyStimulus(std::string keyPrefix, const Parameterised::Map &parameters)
This class determines the desirability algorithm of a MSSOTLPolicy when used in combination with a hi...
std::map< std::string, std::string > Map
parameters map
Definition: Parameterised.h:45
double getDouble(const std::string &key, const double defaultValue) const
Returns the value for a given key converted to a double.
virtual const std::string getParameter(const std::string &key, const std::string defaultValue="") const
Returns the value for a given key.
std::vector< std::string > getVector()
return vector of strings