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.

166 lines
6.4 KiB

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