LCOV - code coverage report
Current view: top level - src/microsim/traffic_lights - MSSOTLPolicy5DFamilyStimulus.cpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 80.3 % 61 49
Test Date: 2024-11-22 15:46:21 Functions: 50.0 % 4 2

            Line data    Source code
       1              : /****************************************************************************/
       2              : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3              : // Copyright (C) 2014-2024 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              : /****************************************************************************/
      14              : /// @file    MSSOTLPolicy5DFamilyStimulus.cpp
      15              : /// @author  Riccardo Belletti
      16              : /// @author  Simone Bacchilega
      17              : /// @date    2014-09-30
      18              : ///
      19              : // The class for Swarm-based low-level policy
      20              : /****************************************************************************/
      21              : #include <config.h>
      22              : 
      23              : #include <utils/common/StringTokenizer.h>
      24              : #include "MSSOTLPolicy5DFamilyStimulus.h"
      25              : //#define SWARM_DEBUG
      26              : 
      27              : 
      28              : // ===========================================================================
      29              : // method definitions
      30              : // ===========================================================================
      31           64 : MSSOTLPolicy5DFamilyStimulus::MSSOTLPolicy5DFamilyStimulus(std::string keyPrefix,
      32           64 :         const Parameterised::Map& parameters) :
      33          128 :     MSSOTLPolicyDesirability(keyPrefix, parameters) {
      34              : 
      35           64 :     default_values["_STIM_COX"] = "1";
      36           64 :     default_values["_STIM_OFFSET_IN"] = "1";
      37           64 :     default_values["_STIM_OFFSET_OUT"] = "1";
      38           64 :     default_values["_STIM_OFFSET_DISPERSION_IN"] = "1";
      39           64 :     default_values["_STIM_OFFSET_DISPERSION_OUT"] = "1";
      40           64 :     default_values["_STIM_DIVISOR_IN"] = "1";
      41           64 :     default_values["_STIM_DIVISOR_OUT"] = "1";
      42           64 :     default_values["_STIM_DIVISOR_DISPERSION_IN"] = "1";
      43           64 :     default_values["_STIM_DIVISOR_DISPERSION_OUT"] = "1";
      44           64 :     default_values["_STIM_COX_EXP_IN"] = "0";
      45           64 :     default_values["_STIM_COX_EXP_OUT"] = "0";
      46           64 :     default_values["_STIM_COX_EXP_DISPERSION_IN"] = "0";
      47           64 :     default_values["_STIM_COX_EXP_DISPERSION_OUT"] = "0";
      48              : 
      49           64 :     params_names.push_back("_STIM_COX");
      50           64 :     params_names.push_back("_STIM_OFFSET_IN");
      51           64 :     params_names.push_back("_STIM_OFFSET_OUT");
      52           64 :     params_names.push_back("_STIM_OFFSET_DISPERSION_IN");
      53           64 :     params_names.push_back("_STIM_OFFSET_DISPERSION_OUT");
      54           64 :     params_names.push_back("_STIM_DIVISOR_IN");
      55           64 :     params_names.push_back("_STIM_DIVISOR_OUT");
      56           64 :     params_names.push_back("_STIM_DIVISOR_DISPERSION_IN");
      57           64 :     params_names.push_back("_STIM_DIVISOR_DISPERSION_OUT");
      58           64 :     params_names.push_back("_STIM_COX_EXP_IN");
      59           64 :     params_names.push_back("_STIM_COX_EXP_OUT");
      60           64 :     params_names.push_back("_STIM_COX_EXP_DISPERSION_IN");
      61           64 :     params_names.push_back("_STIM_COX_EXP_DISPERSION_OUT");
      62              : 
      63              : 
      64           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          128 :     for (int i = 0; i < size_family; i++) {
      74           64 :         sliced_maps.push_back(Parameterised::Map());
      75              :     }
      76              : 
      77              :     //For each param list, slice values
      78          896 :     for (int i = 0; i < (int)params_names.size(); i ++) {
      79          832 :         std::string key = keyPrefix + params_names[i];
      80         1664 :         std::string param_list = getParameter(key, default_values[params_names[i]]);
      81         2496 :         std::vector<std::string> tokens = StringTokenizer(param_list, ";").getVector();
      82              : 
      83         1664 :         for (int token_counter = 0; token_counter < size_family; ++token_counter) {
      84          832 :             if (token_counter >= (int)tokens.size()) {
      85            0 :                 std::ostringstream errorMessage;
      86            0 :                 errorMessage << "Error in " << key << ": not enough tokens.";
      87            0 :                 WRITE_ERROR(errorMessage.str());
      88              :                 assert(-1);
      89            0 :             }
      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          832 :             sliced_maps[token_counter][key] = tokens[token_counter];
      96              :         }
      97          832 :     }
      98              : 
      99          128 :     for (int i = 0; i < size_family; i++) {
     100           64 :         Parameterised::Map& ref_map = sliced_maps[i];
     101          192 :         family.push_back(new MSSOTLPolicy5DStimulus(keyPrefix, ref_map));
     102              :     }
     103              : 
     104           64 : }
     105              : 
     106              : 
     107           80 : double 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          160 :     for (std::vector<MSSOTLPolicy5DStimulus*>::const_iterator it  = family.begin(); it != family.end(); it++) {
     118           80 :         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           80 :         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           80 :     return best_stimulus;
     135              : }
     136              : 
     137              : 
     138            0 : double MSSOTLPolicy5DFamilyStimulus::computeDesirability(double vehInMeasure, double vehOutMeasure) {
     139              : 
     140            0 :     return computeDesirability(vehInMeasure, vehOutMeasure, 0, 0);
     141              : }
     142              : 
     143            0 : std::string MSSOTLPolicy5DFamilyStimulus::getMessage() {
     144            0 :     std::ostringstream ot;
     145            0 :     for (int i = 0; i < (int)family.size(); i++) {
     146            0 :         ot << " gaussian " << i << ":" << family[i]->getMessage();
     147              :     }
     148            0 :     return ot.str();
     149            0 : }
     150              : 
     151              : /*
     152              : std::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              : */
        

Generated by: LCOV version 2.0-1