Browse Source

overwrite indices as vector

yaml_config_changes
Thomas Knoll 4 months ago
parent
commit
78c42f034e
  1. 48
      exampleConfig.yaml
  2. 2
      main.cpp
  3. 9
      util/ConfigYaml.cpp
  4. 6
      util/ConfigYaml.h
  5. 39
      util/Grid.cpp

48
exampleConfig.yaml

@ -3,34 +3,15 @@ labels:
- label: "AgentIsInGoal"
text: "AgentIsInGoal"
constants:
- constant: "prop_slippery_turn"
type: "double"
value: "9/9"
overwrite: True
- constant: "prop_next_neighbour_turn"
type: "double"
value: "0/9"
overwrite: True
- constant: "prop_slippery_move_forward"
type: "double"
value: "3/4"
overwrite: True
- constant: "prop_direct_neighbour"
type: "double"
value: "1/4"
- constant: "prop_next_neighbour"
type: "double"
value: "1/8"
overwrite: True
- constant: "total_prop"
type: "double"
value: "4"
overwrite: True
# constants:
# - constant: "prop_slippery_turn"
# type: "double"
# value: "9/9"
# overwrite: True
probabilities:
- probability: "FaultProbability"
value: 0.1
value: 0.2
- probability: "ProbForwardIntended"
value: 0.1
- probability: "ProbTurnIntended"
@ -43,13 +24,10 @@ modules:
guard: "AgentIsOnSlippery"
update: "True"
overwrite: True
...
const double prop_zero = 0/9;
const double prop_next_neighbour = 1/9;
const double prop_slippery_move_forward = 7/9;
const double prop_slippery_turn = 6/9;
const double prop_next_neighbour_turn = 1/9;
const double prop_direct_neighbour = 2/9;
const double total_prop = 9;
index: 3
- action: "[Agent_turn_right]"
guard: "AgentIsOnSlippery"
update: "True"
overwrite: True
index: [0,1]
...

2
main.cpp

@ -166,7 +166,7 @@ int main(int argc, char* argv[]) {
if(ok) {
Grid grid(contentCells, backgroundCells, gridOptions, stateRewards, probIntended, faultyProbability);
grid.printToPrism(std::cout, configurations , gridOptions.getModelType());
// grid.printToPrism(std::cout, configurations , gridOptions.getModelType());
std::stringstream ss;
// grid.printToPrism(file, configurations ,prism::ModelType::MDP);
grid.printToPrism(ss, configurations , gridOptions.getModelType());

9
util/ConfigYaml.cpp

@ -103,7 +103,12 @@ bool YAML::convert<Command>::decode(const YAML::Node& node, Command& rhs) {
rhs.overwrite_ = node["overwrite"].as<bool>();
}
if (node["index"]) {
rhs.index_ = node["index"].as<int>();
try {
rhs.index_ = node["index"].as<std::vector<int>>();
}
catch(const std::exception& e) {
rhs.index_ = {node["index"].as<int>()};
}
}
return true;
@ -251,7 +256,7 @@ YamlConfigParseResult YamlConfigParser::parseConfiguration() {
for (auto& constant : constants) {
// std::cout << constant.constant_ << std::endl;
configuration.push_back({constant.createExpression(), "const " + constant.type_ + " " + constant.constant_, ConfigType::Constant, constant.overwrite_});
}
}
}
catch(const std::exception& e) {
std::cout << "Exception '" << typeid(e).name() << "' caught:" << std::endl;

6
util/ConfigYaml.h

@ -22,7 +22,7 @@ struct Configuration
std::string module_ {};
std::string expression_{};
std::string identifier_{};
int index_{};
std::vector<int> index_{0};
ConfigType type_ {ConfigType::Label};
bool overwrite_ {false};
@ -33,7 +33,7 @@ struct Configuration
, ConfigType type
, bool overwrite = false
, std::string module = ""
, int index = 0) : expression_(expression), identifier_(identifier), type_(type), overwrite_(overwrite), module_{module}, index_(index) {}
, std::vector<int> index = {0}) : expression_(expression), identifier_(identifier), type_(type), overwrite_(overwrite), module_{module}, index_(index) {}
~Configuration() = default;
Configuration(const Configuration&) = default;
@ -100,7 +100,7 @@ struct Command {
std::string action_;
std::string guard_;
std::string update_;
int index_{0};
std::vector<int> index_{0};
bool overwrite_ {false};
std::string createExpression() const;

39
util/Grid.cpp

@ -104,29 +104,30 @@ void Grid::applyOverwrites(std::string& str, std::vector<Configuration>& configu
if (!config.overwrite_) {
continue;
}
size_t start_pos;
if (config.type_ == ConfigType::Formula) {
start_pos = str.find("formula " + config.identifier_);
} else if (config.type_ == ConfigType::Label) {
start_pos = str.find("label " + config.identifier_);
} else if (config.type_ == ConfigType::Module) {
auto iter = boost::find_nth(str, config.identifier_, config.index_);
for (auto& index : config.index_) {
size_t start_pos;
std::string search;
if (config.type_ == ConfigType::Formula) {
search = "formula " + config.identifier_;
} else if (config.type_ == ConfigType::Label) {
search = "label " + config.identifier_;
} else if (config.type_ == ConfigType::Module) {
search = config.identifier_;
}
else if (config.type_ == ConfigType::Constant) {
search = config.identifier_;
}
auto iter = boost::find_nth(str, search, index);
start_pos = std::distance(str.begin(), iter.begin());
}
else if (config.type_ == ConfigType::Constant) {
start_pos = str.find(config.identifier_);
size_t end_pos = str.find(';', start_pos) + 1;
if (start_pos == std::string::npos) {
std::cout << "Couldn't find overwrite:" << config.expression_ << std::endl;
if (end_pos != std::string::npos && end_pos != 0) {
std::string expression = config.expression_;
str.replace(start_pos, end_pos - start_pos , expression);
}
}
size_t end_pos = str.find(';', start_pos) + 1;
std::string expression = config.expression_;
str.replace(start_pos, end_pos - start_pos , expression);
}
}
void Grid::printToPrism(std::ostream& os, std::vector<Configuration>& configuration ,const prism::ModelType& modelType) {

Loading…
Cancel
Save