From 74645d95b3b1dabd866b467bc61d3824011a3a77 Mon Sep 17 00:00:00 2001 From: Thomas Knoll Date: Wed, 10 Jan 2024 21:29:00 +0100 Subject: [PATCH] basic support for probabiliteis in config --- main.cpp | 5 ++++- util/ConfigYaml.cpp | 29 +++++++++++++++++++++++++++-- util/ConfigYaml.h | 32 ++++++++++++++++++++++++++++++-- 3 files changed, 61 insertions(+), 5 deletions(-) diff --git a/main.cpp b/main.cpp index 4e7a95a..21bcc22 100644 --- a/main.cpp +++ b/main.cpp @@ -110,6 +110,7 @@ int main(int argc, char* argv[]) { cells contentCells; cells backgroundCells; std::vector configurations; + std::vector probabilities; std::map stateRewards; float faultyProbability = 0.1; float probIntended = 0.9; @@ -121,7 +122,9 @@ int main(int argc, char* argv[]) { // TODO } if (configFilename->is_set()) { YamlConfigParser parser(configFilename->value(0)); - configurations = parser.parseConfiguration(); + auto parseResult = parser.parseConfiguration(); + configurations = parseResult.configurations_; + probabilities = parseResult.probabilities_; } boost::escaped_list_separator seps('\\', ';', '\n'); diff --git a/util/ConfigYaml.cpp b/util/ConfigYaml.cpp index 07c2b8e..3a62b37 100644 --- a/util/ConfigYaml.cpp +++ b/util/ConfigYaml.cpp @@ -185,11 +185,32 @@ bool YAML::convert::decode(const YAML::Node& node, Constant& rhs) { return true; } +YAML::Node YAML::convert::encode(const Probability& rhs) { + YAML::Node node; + + node.push_back(rhs.probability_); + node.push_back(rhs.value_); + + return node; +} + +bool YAML::convert::decode(const YAML::Node& node, Probability& rhs) { + if (!node.IsDefined() || !node["probability"] || !node["value"]) { + return false; + } + + rhs.probability_ = node["probability"].as(); + rhs.value_ = node["value"].as(); + + return true; +} + const std::string Configuration::configuration_identifier_ { "; // created through configuration"}; const std::string Configuration::overwrite_identifier_{"; // Overwritten through configuration"}; - std::vector YamlConfigParser::parseConfiguration() { +YamlConfigParseResult YamlConfigParser::parseConfiguration() { std::vector configuration; + std::vector probabilities; try { YAML::Node config = YAML::LoadFile(file_); @@ -211,6 +232,10 @@ const std::string Configuration::overwrite_identifier_{"; // Overwritten through if (config["constants"]) { constants = config["constants"].as>(); } + + if (config["probabilities"]) { + probabilities = config["probabilities"].as>(); + } for (auto& label : labels) { configuration.push_back({label.createExpression(), label.label_ , ConfigType::Label, label.overwrite_}); @@ -234,5 +259,5 @@ const std::string Configuration::overwrite_identifier_{"; // Overwritten through std::cout << "while parsing configuration " << file_ << std::endl; } - return configuration; + return YamlConfigParseResult(configuration, probabilities); } \ No newline at end of file diff --git a/util/ConfigYaml.h b/util/ConfigYaml.h index 6b4e93a..1b28eda 100644 --- a/util/ConfigYaml.h +++ b/util/ConfigYaml.h @@ -2,6 +2,7 @@ #include #include +#include #include "yaml-cpp/yaml.h" @@ -43,6 +44,17 @@ struct Configuration } }; +struct Probability { + Probability() = default; + Probability(const Probability&) = default; + ~Probability() = default; + + std::string probability_; + double value_; + + friend std::ostream& operator <<(std::ostream& os, const Probability& property); +}; + struct Constant { private: @@ -137,6 +149,22 @@ struct YAML::convert { static bool decode(const YAML::Node& node, Constant& rhs); }; +template<> +struct YAML::convert { + static YAML::Node encode(const Probability& rhs); + static bool decode(const YAML::Node& node, Probability& rhs); +}; + +struct YamlConfigParseResult { + YamlConfigParseResult(std::vector configurations, std::vector probabilities) + : configurations_(configurations), probabilities_(probabilities) {} + + ~YamlConfigParseResult() = default; + YamlConfigParseResult(const YamlConfigParseResult&) = default; + + std::vector configurations_; + std::vector probabilities_; +}; struct YamlConfigParser { public: @@ -144,8 +172,8 @@ struct YamlConfigParser { YamlConfigParser(const YamlConfigParser&) = delete; ~YamlConfigParser() = default; - std::vector parseConfiguration(); + YamlConfigParseResult parseConfiguration(); private: std::string file_; -}; \ No newline at end of file +};