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.

193 lines
7.7 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, 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) == '-') {
  98. parsingBackground = true;
  99. continue;
  100. }
  101. if(!parsingBackground && !parsingStateRewards) {
  102. // std::cout << "Reading :\t" << line << "\n";
  103. content += line + "\n";
  104. } else if (parsingBackground) {
  105. // std::cout << "Background:\t" << line << "\n";
  106. background += line + "\n";
  107. } else if(parsingStateRewards) {
  108. rewards += line + "\n";
  109. } else if (line.at(0) == '-' && line.at(line.size() - 1 ) == '-' && parsingStateRewards) {
  110. parsingStateRewards = false;
  111. parsingEnvironmentProperties = true;
  112. continue;
  113. } else if (parsingEnvironmentProperties) {
  114. properties += line + "\n";
  115. }
  116. }
  117. std::cout << "\n";
  118. pos_iterator_t contentFirst(content.begin());
  119. pos_iterator_t contentIter = contentFirst;
  120. pos_iterator_t contentLast(content.end());
  121. MinigridParser<pos_iterator_t> contentParser(contentFirst);
  122. pos_iterator_t backgroundFirst(background.begin());
  123. pos_iterator_t backgroundIter = backgroundFirst;
  124. pos_iterator_t backgroundLast(background.end());
  125. MinigridParser<pos_iterator_t> backgroundParser(backgroundFirst);
  126. cells contentCells;
  127. cells backgroundCells;
  128. std::vector<Configuration> configurations;
  129. std::map<coordinates, float> stateRewards;
  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(ok) {
  148. Grid grid(contentCells, backgroundCells, gridOptions, stateRewards);
  149. //grid.printToPrism(std::cout, prism::ModelType::MDP);
  150. std::stringstream ss;
  151. // grid.printToPrism(file, configurations ,prism::ModelType::MDP);
  152. grid.printToPrism(ss, configurations , gridOptions.getModelType());
  153. std::string str = ss.str();
  154. grid.applyOverwrites(str, configurations);
  155. file << str;
  156. }
  157. } catch(qi::expectation_failure<pos_iterator_t> const& e) {
  158. std::cout << "expected: "; print_info(e.what_);
  159. std::cout << "got: \"" << std::string(e.first, e.last) << '"' << std::endl;
  160. std::cout << "Expectation failure: " << e.what() << " at '" << std::string(e.first,e.last) << "'\n";
  161. } catch(const std::exception& e) {
  162. std::cerr << "Exception '" << typeid(e).name() << "' caught:" << std::endl;
  163. std::cerr << "\t" << e.what() << std::endl;
  164. std::exit(EXIT_FAILURE);
  165. }
  166. return 0;
  167. }