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.

186 lines
7.4 KiB

  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;
  88. std::cout << "\n";
  89. bool parsingBackground = false;
  90. bool parsingStateRewards = false;
  91. while (std::getline(infile, line) && !line.empty()) {
  92. if(line.at(0) == '-' && line.at(line.size() - 1) == '-' && parsingBackground) {
  93. parsingStateRewards = true;
  94. parsingBackground = false;
  95. continue;
  96. } else if(line.at(0) == '-' && line.at(line.size() - 1) == '-') {
  97. parsingBackground = true;
  98. continue;
  99. }
  100. if(!parsingBackground && !parsingStateRewards) {
  101. // std::cout << "Reading :\t" << line << "\n";
  102. content += line + "\n";
  103. } else if (parsingBackground) {
  104. // std::cout << "Background:\t" << line << "\n";
  105. background += line + "\n";
  106. } else if(parsingStateRewards) {
  107. rewards += line + "\n";
  108. }
  109. }
  110. std::cout << "\n";
  111. pos_iterator_t contentFirst(content.begin());
  112. pos_iterator_t contentIter = contentFirst;
  113. pos_iterator_t contentLast(content.end());
  114. MinigridParser<pos_iterator_t> contentParser(contentFirst);
  115. pos_iterator_t backgroundFirst(background.begin());
  116. pos_iterator_t backgroundIter = backgroundFirst;
  117. pos_iterator_t backgroundLast(background.end());
  118. MinigridParser<pos_iterator_t> backgroundParser(backgroundFirst);
  119. cells contentCells;
  120. cells backgroundCells;
  121. std::vector<Configuration> configurations;
  122. std::map<coordinates, float> stateRewards;
  123. try {
  124. bool ok = phrase_parse(contentIter, contentLast, contentParser, qi::space, contentCells);
  125. // TODO if(background is not empty) {
  126. ok &= phrase_parse(backgroundIter, backgroundLast, backgroundParser, qi::space, backgroundCells);
  127. // TODO }
  128. if (configFilename->is_set()) {
  129. YamlConfigParser parser(configFilename->value(0));
  130. configurations = parser.parseConfiguration();
  131. }
  132. boost::escaped_list_separator<char> seps('\\', ';', '\n');
  133. Tokenizer csvParser(rewards, seps);
  134. for(auto iter = csvParser.begin(); iter != csvParser.end(); ++iter) {
  135. int x = std::stoi(*iter);
  136. int y = std::stoi(*(++iter));
  137. float reward = std::stof(*(++iter));
  138. stateRewards[std::make_pair(x,y)] = reward;
  139. }
  140. if(ok) {
  141. Grid grid(contentCells, backgroundCells, gridOptions, stateRewards);
  142. //grid.printToPrism(std::cout, prism::ModelType::MDP);
  143. std::stringstream ss;
  144. // grid.printToPrism(file, configurations ,prism::ModelType::MDP);
  145. grid.printToPrism(ss, configurations , gridOptions.getModelType());
  146. std::string str = ss.str();
  147. grid.applyOverwrites(str, configurations);
  148. file << str;
  149. }
  150. } catch(qi::expectation_failure<pos_iterator_t> const& e) {
  151. std::cout << "expected: "; print_info(e.what_);
  152. std::cout << "got: \"" << std::string(e.first, e.last) << '"' << std::endl;
  153. std::cout << "Expectation failure: " << e.what() << " at '" << std::string(e.first,e.last) << "'\n";
  154. } catch(const std::exception& e) {
  155. std::cerr << "Exception '" << typeid(e).name() << "' caught:" << std::endl;
  156. std::cerr << "\t" << e.what() << std::endl;
  157. std::exit(EXIT_FAILURE);
  158. }
  159. return 0;
  160. }