Browse Source

basic support for probabiliteis in config

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

5
main.cpp

@ -110,6 +110,7 @@ int main(int argc, char* argv[]) {
cells contentCells;
cells backgroundCells;
std::vector<Configuration> configurations;
std::vector<Probability> probabilities;
std::map<coordinates, float> 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<char> seps('\\', ';', '\n');

29
util/ConfigYaml.cpp

@ -185,11 +185,32 @@ bool YAML::convert<Constant>::decode(const YAML::Node& node, Constant& rhs) {
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::overwrite_identifier_{"; // Overwritten through configuration"};
std::vector<Configuration> YamlConfigParser::parseConfiguration() {
YamlConfigParseResult YamlConfigParser::parseConfiguration() {
std::vector<Configuration> configuration;
std::vector<Probability> 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<std::vector<Constant>>();
}
if (config["probabilities"]) {
probabilities = config["probabilities"].as<std::vector<Probability>>();
}
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);
}

32
util/ConfigYaml.h

@ -2,6 +2,7 @@
#include <vector>
#include <ostream>
#include <utility>
#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<Constant> {
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 {
public:
@ -144,8 +172,8 @@ struct YamlConfigParser {
YamlConfigParser(const YamlConfigParser&) = delete;
~YamlConfigParser() = default;
std::vector<Configuration> parseConfiguration();
YamlConfigParseResult parseConfiguration();
private:
std::string file_;
};
};
Loading…
Cancel
Save