You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

206 lines
8.2 KiB

11 months ago
11 months ago
11 months ago
11 months ago
  1. #include "util/OptionParser.h"
  2. #include "util/MinigridGrammar.h"
  3. #include "util/Grid.h"
  4. #include "util/ConfigYaml.h"
  5. #include <iostream>
  6. #include <fstream>
  7. #include <filesystem>
  8. #include <sstream>
  9. std::vector<std::string> parseCommaSeparatedString(std::string const& str) {
  10. std::vector<std::string> result;
  11. std::stringstream stream(str);
  12. while(stream.good()) {
  13. std::string substr;
  14. getline(stream, substr, ',');
  15. substr.at(0) = std::toupper(substr.at(0));
  16. result.push_back(substr);
  17. }
  18. return result;
  19. }
  20. struct printer {
  21. typedef boost::spirit::utf8_string string;
  22. void element(string const& tag, string const& value, int depth) const {
  23. for (int i = 0; i < (depth*4); ++i) std::cout << ' ';
  24. std::cout << "tag: " << tag;
  25. if (value != "") std::cout << ", value: " << value;
  26. std::cout << std::endl;
  27. }
  28. };
  29. void print_info(boost::spirit::info const& what) {
  30. using boost::spirit::basic_info_walker;
  31. printer pr;
  32. basic_info_walker<printer> walker(pr, what.tag, 0);
  33. boost::apply_visitor(walker, what.value);
  34. }
  35. int main(int argc, char* argv[]) {
  36. popl::OptionParser optionParser("Allowed options");
  37. auto helpOption = optionParser.add<popl::Switch>("h", "help", "Print this help message.");
  38. auto inputFilename = optionParser.add<popl::Value<std::string>>("i", "input-file", "Filename of the input file.");
  39. auto outputFilename = optionParser.add<popl::Value<std::string>>("o", "output-file", "Filename for the output file.");
  40. auto agentsToBeConsidered = optionParser.add<popl::Value<std::string>, popl::Attribute::optional>("a", "agents", "Which parsed agents should be considered in the output. WIP.");
  41. auto viewForAgents = optionParser.add<popl::Value<std::string>, popl::Attribute::optional>("v", "view", "Agents for which the 'view'('direction') variable should be included. WIP.");
  42. auto probabilisticBehaviourAgents = optionParser.add<popl::Value<std::string>, popl::Attribute::optional>("p", "prob-beh", "Agents for which we want to include probabilistic actions");
  43. auto probabilities = optionParser.add<popl::Value<std::string>, popl::Attribute::optional>("q", "probs", "The probabilities for which probabilistic actions should be added. WIP");
  44. auto enforceOneWays = optionParser.add<popl::Switch>("f", "force-oneways", "Enforce encoding of oneways. This entails that slippery tiles do not allow turning and have prob 1 to shift the agent by one and makes turning impossible if in a one tile wide section of the grid.");
  45. auto configFilename = optionParser.add<popl::Value<std::string>, popl::Attribute::optional>("c", "config-file", "Filename of the predicate configuration file.");
  46. try {
  47. optionParser.parse(argc, argv);
  48. if(helpOption->count() > 0) {
  49. std::cout << optionParser << std::endl;
  50. return EXIT_SUCCESS;
  51. }
  52. } catch (const popl::invalid_option &e) {
  53. return io::printPoplException(e);
  54. } catch (const std::exception &e) {
  55. std::cerr << "Exception: " << e.what() << "\n";
  56. return EXIT_FAILURE;
  57. }
  58. GridOptions gridOptions = { {}, {} };
  59. if(agentsToBeConsidered->is_set()) {
  60. gridOptions.agentsToBeConsidered = parseCommaSeparatedString(agentsToBeConsidered->value(0));
  61. }
  62. if(viewForAgents->is_set()) {
  63. gridOptions.agentsWithView = parseCommaSeparatedString(viewForAgents->value(0));
  64. }
  65. if(enforceOneWays->is_set()) {
  66. gridOptions.enforceOneWays = true;
  67. }
  68. if(probabilisticBehaviourAgents->is_set()) {
  69. gridOptions.agentsWithProbabilisticBehaviour = parseCommaSeparatedString(probabilisticBehaviourAgents->value(0));
  70. for(auto const& a : gridOptions.agentsWithProbabilisticBehaviour) {
  71. std::cout << a << std::endl;
  72. }
  73. if(probabilities->is_set()) {
  74. std::vector<std::string> parsedStrings = parseCommaSeparatedString(probabilities->value(0));
  75. std::transform(parsedStrings.begin(), parsedStrings.end(), std::back_inserter(gridOptions.probabilitiesForActions), [](const std::string& string) {
  76. return std::stof(string);
  77. });
  78. for(auto const& a : gridOptions.probabilitiesForActions) {
  79. std::cout << a << std::endl;
  80. }
  81. } else {
  82. throw std::logic_error{ "When adding agents with probabilistic behaviour, you also need to specify a list of probabilities via --probs." };
  83. }
  84. }
  85. std::fstream file {outputFilename->value(0), file.trunc | file.out};
  86. std::fstream infile {inputFilename->value(0), infile.in};
  87. std::string line, content, background, rewards, properties;
  88. std::cout << "\n";
  89. bool parsingBackground = false;
  90. bool parsingStateRewards = false;
  91. bool parsingEnvironmentProperties = false;
  92. while (std::getline(infile, line) && !line.empty()) {
  93. if(line.at(0) == '-' && line.at(line.size() - 1) == '-' && parsingBackground) {
  94. parsingStateRewards = true;
  95. parsingBackground = false;
  96. continue;
  97. } else if (line.at(0) == '-' && line.at(line.size() - 1 ) == '-' && parsingStateRewards) {
  98. parsingStateRewards = false;
  99. parsingEnvironmentProperties = true;
  100. continue;
  101. } else if(line.at(0) == '-' && line.at(line.size() - 1) == '-') {
  102. parsingBackground = true;
  103. continue;
  104. }
  105. if(!parsingBackground && !parsingStateRewards && !parsingEnvironmentProperties) {
  106. content += line + "\n";
  107. } else if (parsingBackground) {
  108. background += line + "\n";
  109. } else if(parsingStateRewards) {
  110. rewards += line + "\n";
  111. } else if (parsingEnvironmentProperties) {
  112. properties += line + "\n";
  113. }
  114. }
  115. std::cout << "\n";
  116. pos_iterator_t contentFirst(content.begin());
  117. pos_iterator_t contentIter = contentFirst;
  118. pos_iterator_t contentLast(content.end());
  119. MinigridParser<pos_iterator_t> contentParser(contentFirst);
  120. pos_iterator_t backgroundFirst(background.begin());
  121. pos_iterator_t backgroundIter = backgroundFirst;
  122. pos_iterator_t backgroundLast(background.end());
  123. MinigridParser<pos_iterator_t> backgroundParser(backgroundFirst);
  124. cells contentCells;
  125. cells backgroundCells;
  126. std::vector<Configuration> configurations;
  127. std::map<coordinates, float> stateRewards;
  128. float faultyProbability = 0.0;
  129. float probIntended = 0.9;
  130. try {
  131. bool ok = phrase_parse(contentIter, contentLast, contentParser, qi::space, contentCells);
  132. // TODO if(background is not empty) {
  133. ok &= phrase_parse(backgroundIter, backgroundLast, backgroundParser, qi::space, backgroundCells);
  134. // TODO }
  135. if (configFilename->is_set()) {
  136. YamlConfigParser parser(configFilename->value(0));
  137. configurations = parser.parseConfiguration();
  138. }
  139. boost::escaped_list_separator<char> seps('\\', ';', '\n');
  140. Tokenizer csvParser(rewards, seps);
  141. for(auto iter = csvParser.begin(); iter != csvParser.end(); ++iter) {
  142. int x = std::stoi(*iter);
  143. int y = std::stoi(*(++iter));
  144. float reward = std::stof(*(++iter));
  145. stateRewards[std::make_pair(x,y)] = reward;
  146. }
  147. if (!properties.empty()) {
  148. auto faultProbabilityIdentifier = std::string("FaultProbability:");
  149. auto start_pos = properties.find(faultProbabilityIdentifier);
  150. if (start_pos != std::string::npos) {
  151. auto end_pos = properties.find('\n', start_pos);
  152. auto value = properties.substr(start_pos + faultProbabilityIdentifier.length(), end_pos - start_pos - faultProbabilityIdentifier.length());
  153. faultyProbability = std::stod(value);
  154. }
  155. }
  156. if(ok) {
  157. Grid grid(contentCells, backgroundCells, gridOptions, stateRewards, probIntended, faultyProbability);
  158. grid.printToPrism(std::cout, configurations , gridOptions.getModelType());
  159. std::stringstream ss;
  160. // grid.printToPrism(file, configurations ,prism::ModelType::MDP);
  161. grid.printToPrism(ss, configurations , gridOptions.getModelType());
  162. std::string str = ss.str();
  163. grid.applyOverwrites(str, configurations);
  164. file << str;
  165. }
  166. } catch(qi::expectation_failure<pos_iterator_t> const& e) {
  167. std::cout << "expected: "; print_info(e.what_);
  168. std::cout << "got: \"" << std::string(e.first, e.last) << '"' << std::endl;
  169. std::cout << "Expectation failure: " << e.what() << " at '" << std::string(e.first,e.last) << "'\n";
  170. } catch(const std::exception& e) {
  171. std::cerr << "Exception '" << typeid(e).name() << "' caught:" << std::endl;
  172. std::cerr << "\t" << e.what() << std::endl;
  173. std::exit(EXIT_FAILURE);
  174. }
  175. return 0;
  176. }