Browse Source

basic support for probabiliteis in config

yaml_config_changes
Thomas Knoll 11 months ago
parent
commit
74645d95b3
  1. 5
      main.cpp
  2. 29
      util/ConfigYaml.cpp
  3. 30
      util/ConfigYaml.h

5
main.cpp

@ -110,6 +110,7 @@ int main(int argc, char* argv[]) {
cells contentCells; cells contentCells;
cells backgroundCells; cells backgroundCells;
std::vector<Configuration> configurations; std::vector<Configuration> configurations;
std::vector<Probability> probabilities;
std::map<coordinates, float> stateRewards; std::map<coordinates, float> stateRewards;
float faultyProbability = 0.1; float faultyProbability = 0.1;
float probIntended = 0.9; float probIntended = 0.9;
@ -121,7 +122,9 @@ int main(int argc, char* argv[]) {
// TODO } // TODO }
if (configFilename->is_set()) { if (configFilename->is_set()) {
YamlConfigParser parser(configFilename->value(0)); YamlConfigParser parser(configFilename->value(0));
configurations = parser.parseConfiguration();
auto parseResult = parser.parseConfiguration();
configurations = parseResult.configurations_;
probabilities = parseResult.probabilities_;
} }
boost::escaped_list_separator<char> seps('\\', ';', '\n'); boost::escaped_list_separator<char> seps('\\', ';', '\n');

29
util/ConfigYaml.cpp

@ -185,11 +185,32 @@ bool YAML::convert<Constant>::decode(const YAML::Node& node, Constant& rhs) {
return true; return true;
} }
YAML::Node YAML::convert<Probability>::encode(const Probability& rhs) {
YAML::Node node;
node.push_back(rhs.probability_);
node.push_back(rhs.value_);
return node;
}
bool YAML::convert<Probability>::decode(const YAML::Node& node, Probability& rhs) {
if (!node.IsDefined() || !node["probability"] || !node["value"]) {
return false;
}
rhs.probability_ = node["probability"].as<std::string>();
rhs.value_ = node["value"].as<double>();
return true;
}
const std::string Configuration::configuration_identifier_ { "; // created through configuration"}; const std::string Configuration::configuration_identifier_ { "; // created through configuration"};
const std::string Configuration::overwrite_identifier_{"; // Overwritten through configuration"}; const std::string Configuration::overwrite_identifier_{"; // Overwritten through configuration"};
std::vector<Configuration> YamlConfigParser::parseConfiguration() {
YamlConfigParseResult YamlConfigParser::parseConfiguration() {
std::vector<Configuration> configuration; std::vector<Configuration> configuration;
std::vector<Probability> probabilities;
try { try {
YAML::Node config = YAML::LoadFile(file_); YAML::Node config = YAML::LoadFile(file_);
@ -212,6 +233,10 @@ const std::string Configuration::overwrite_identifier_{"; // Overwritten through
constants = config["constants"].as<std::vector<Constant>>(); constants = config["constants"].as<std::vector<Constant>>();
} }
if (config["probabilities"]) {
probabilities = config["probabilities"].as<std::vector<Probability>>();
}
for (auto& label : labels) { for (auto& label : labels) {
configuration.push_back({label.createExpression(), label.label_ , ConfigType::Label, label.overwrite_}); 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; std::cout << "while parsing configuration " << file_ << std::endl;
} }
return configuration;
return YamlConfigParseResult(configuration, probabilities);
} }

30
util/ConfigYaml.h

@ -2,6 +2,7 @@
#include <vector> #include <vector>
#include <ostream> #include <ostream>
#include <utility>
#include "yaml-cpp/yaml.h" #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 { struct Constant {
private: private:
@ -137,6 +149,22 @@ struct YAML::convert<Constant> {
static bool decode(const YAML::Node& node, Constant& rhs); static bool decode(const YAML::Node& node, Constant& rhs);
}; };
template<>
struct YAML::convert<Probability> {
static YAML::Node encode(const Probability& rhs);
static bool decode(const YAML::Node& node, Probability& rhs);
};
struct YamlConfigParseResult {
YamlConfigParseResult(std::vector<Configuration> configurations, std::vector<Probability> probabilities)
: configurations_(configurations), probabilities_(probabilities) {}
~YamlConfigParseResult() = default;
YamlConfigParseResult(const YamlConfigParseResult&) = default;
std::vector<Configuration> configurations_;
std::vector<Probability> probabilities_;
};
struct YamlConfigParser { struct YamlConfigParser {
public: public:
@ -144,7 +172,7 @@ struct YamlConfigParser {
YamlConfigParser(const YamlConfigParser&) = delete; YamlConfigParser(const YamlConfigParser&) = delete;
~YamlConfigParser() = default; ~YamlConfigParser() = default;
std::vector<Configuration> parseConfiguration();
YamlConfigParseResult parseConfiguration();
private: private:
std::string file_; std::string file_;
Loading…
Cancel
Save