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

          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         128 : MSSOTLPolicy5DFamilyStimulus::MSSOTLPolicy5DFamilyStimulus(std::string keyPrefix,
      32         128 :         const Parameterised::Map& parameters) :
      33         256 :     MSSOTLPolicyDesirability(keyPrefix, parameters) {
      34             : 
      35         128 :     default_values["_STIM_COX"] = "1";
      36         128 :     default_values["_STIM_OFFSET_IN"] = "1";
      37         128 :     default_values["_STIM_OFFSET_OUT"] = "1";
      38         128 :     default_values["_STIM_OFFSET_DISPERSION_IN"] = "1";
      39         128 :     default_values["_STIM_OFFSET_DISPERSION_OUT"] = "1";
      40         128 :     default_values["_STIM_DIVISOR_IN"] = "1";
      41         128 :     default_values["_STIM_DIVISOR_OUT"] = "1";
      42         128 :     default_values["_STIM_DIVISOR_DISPERSION_IN"] = "1";
      43         128 :     default_values["_STIM_DIVISOR_DISPERSION_OUT"] = "1";
      44         128 :     default_values["_STIM_COX_EXP_IN"] = "0";
      45         128 :     default_values["_STIM_COX_EXP_OUT"] = "0";
      46         128 :     default_values["_STIM_COX_EXP_DISPERSION_IN"] = "0";
      47         128 :     default_values["_STIM_COX_EXP_DISPERSION_OUT"] = "0";
      48             : 
      49         128 :     params_names.push_back("_STIM_COX");
      50         128 :     params_names.push_back("_STIM_OFFSET_IN");
      51         128 :     params_names.push_back("_STIM_OFFSET_OUT");
      52         128 :     params_names.push_back("_STIM_OFFSET_DISPERSION_IN");
      53         128 :     params_names.push_back("_STIM_OFFSET_DISPERSION_OUT");
      54         128 :     params_names.push_back("_STIM_DIVISOR_IN");
      55         128 :     params_names.push_back("_STIM_DIVISOR_OUT");
      56         128 :     params_names.push_back("_STIM_DIVISOR_DISPERSION_IN");
      57         128 :     params_names.push_back("_STIM_DIVISOR_DISPERSION_OUT");
      58         128 :     params_names.push_back("_STIM_COX_EXP_IN");
      59         128 :     params_names.push_back("_STIM_COX_EXP_OUT");
      60         128 :     params_names.push_back("_STIM_COX_EXP_DISPERSION_IN");
      61         128 :     params_names.push_back("_STIM_COX_EXP_DISPERSION_OUT");
      62             : 
      63             : 
      64         256 :     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         256 :     for (int i = 0; i < size_family; i++) {
      74         128 :         sliced_maps.push_back(Parameterised::Map());
      75             :     }
      76             : 
      77             :     //For each param list, slice values
      78        1792 :     for (int i = 0; i < (int)params_names.size(); i ++) {
      79        1664 :         std::string key = keyPrefix + params_names[i];
      80        3328 :         std::string param_list = getParameter(key, default_values[params_names[i]]);
      81        4992 :         std::vector<std::string> tokens = StringTokenizer(param_list, ";").getVector();
      82             : 
      83        3328 :         for (int token_counter = 0; token_counter < size_family; ++token_counter) {
      84        1664 :             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        1664 :             sliced_maps[token_counter][key] = tokens[token_counter];
      96             :         }
      97        1664 :     }
      98             : 
      99         256 :     for (int i = 0; i < size_family; i++) {
     100         128 :         Parameterised::Map& ref_map = sliced_maps[i];
     101         128 :         family.push_back(new MSSOTLPolicy5DStimulus(keyPrefix, ref_map));
     102             :     }
     103             : 
     104         128 : }
     105             : 
     106             : 
     107         160 : 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         320 :     for (std::vector<MSSOTLPolicy5DStimulus*>::const_iterator it  = family.begin(); it != family.end(); it++) {
     118         160 :         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         160 :         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         160 :     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 1.14