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.

195 lines
7.7 KiB

  1. #include "util/OptionParser.h"
  2. #include "util/MinigridGrammar.h"
  3. #include "util/Grid.h"
  4. #include "util/ConfigGrammar.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, config;
  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. if (configFilename->is_set()) {
  112. std::fstream configFile {configFilename->value(0), configFile.in};
  113. while (std::getline(configFile, line) && !line.empty()) {
  114. std::cout << "Configuration:\t" << line << "\n";
  115. config += line + "\n";
  116. }
  117. }
  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. pos_iterator_t configFirst(config.begin());
  127. pos_iterator_t configIter = configFirst;
  128. pos_iterator_t configLast(config.end());
  129. ConfigParser<pos_iterator_t> configParser(configFirst);
  130. cells contentCells;
  131. cells backgroundCells;
  132. std::vector<Configuration> configurations;
  133. std::map<coordinates, float> stateRewards;
  134. try {
  135. bool ok = phrase_parse(contentIter, contentLast, contentParser, qi::space, contentCells);
  136. // TODO if(background is not empty) {
  137. ok &= phrase_parse(backgroundIter, backgroundLast, backgroundParser, qi::space, backgroundCells);
  138. // TODO }
  139. if (configFilename->is_set()) {
  140. ok &= phrase_parse(configIter, configLast, configParser, qi::space, configurations);
  141. }
  142. for (auto& config : configurations) {
  143. std::cout << config << std::endl;
  144. }
  145. boost::escaped_list_separator<char> seps('\\', ';', '\n');
  146. Tokenizer csvParser(rewards, seps);
  147. for(auto iter = csvParser.begin(); iter != csvParser.end(); ++iter) {
  148. int x = std::stoi(*iter);
  149. int y = std::stoi(*(++iter));
  150. float reward = std::stof(*(++iter));
  151. stateRewards[std::make_pair(x,y)] = reward;
  152. }
  153. if(ok) {
  154. Grid grid(contentCells, backgroundCells, gridOptions, stateRewards);
  155. //grid.printToPrism(std::cout, prism::ModelType::MDP);
  156. grid.printToPrism(file, configurations ,prism::ModelType::MDP);
  157. }
  158. } catch(qi::expectation_failure<pos_iterator_t> const& e) {
  159. std::cout << "expected: "; print_info(e.what_);
  160. std::cout << "got: \"" << std::string(e.first, e.last) << '"' << std::endl;
  161. std::cout << "Expectation failure: " << e.what() << " at '" << std::string(e.first,e.last) << "'\n";
  162. } catch(const std::exception& e) {
  163. std::cerr << "Exception '" << typeid(e).name() << "' caught:" << std::endl;
  164. std::cerr << "\t" << e.what() << std::endl;
  165. std::exit(EXIT_FAILURE);
  166. }
  167. return 0;
  168. }