PageRenderTime 240ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main.cpp

https://bitbucket.org/maciekpacut/evo
C++ | 165 lines | 137 code | 28 blank | 0 comment | 27 complexity | ca1068cbeaef80b7ce209a1ecc7ce0ff MD5 | raw file
  1. #include <iostream>
  2. #include <vector>
  3. #include <functional>
  4. #include <boost/program_options.hpp>
  5. namespace po = boost::program_options;
  6. #include "problem.hpp"
  7. #include "crossover.hpp"
  8. po::variables_map read_command_line(po::options_description command_line_args_desc, int argc, char* argv[])
  9. {
  10. po::variables_map args;
  11. po::store(po::parse_command_line(argc, argv, command_line_args_desc), args);
  12. po::notify(args);
  13. return args;
  14. }
  15. po::options_description command_line_args_create()
  16. {
  17. po::options_description command_line_args("Available options");
  18. command_line_args.add_options()
  19. ("help,h", "Produce help message.")
  20. ("debug,d", "Show assignment of command line parameters.")
  21. ("report-population,R", "Reports whole population.")
  22. ("report-best,r", "Reports best found speciman.")
  23. ("report-every-frame,e", po::value<std::string>(), "Reports every frame. Available: best, avg.")
  24. ("max-iter,i", po::value<int>()->default_value(1000), "Maximum number of iterations.")
  25. ("crossover,x", po::value<std::string>()->default_value("pmx"), "Crossover operators. Available: pmx, cx, ox")
  26. ("smart-termination,t", "Use termination condition that does not depend on --max-iter. Also overrides --max-iter")
  27. ("optimum,o", po::value<int>(), "Program will not stop until it reaches given optimal value (or --max-iter). Also prints number of evaluations.")
  28. ("report-var,v", "Similar to --report-every-frame, but instead best/avg specimen, it prints variance of each population.")
  29. ("compare-operators,c", "Executes program with special mode, in which crossover operation is selected randomly with each iteration. Returns statistical 'power' of each operator.")
  30. ("set-seed,S", po::value<int>(), "Sets random number seed.")
  31. ("population-size,p", po::value<int>()->default_value(200), "Population size")
  32. ("ping,a", po::value<int>()->default_value(0), "Gives sing that agorithm is still running fine by printing msg after each arg1 iterations.")
  33. ;
  34. return command_line_args;
  35. }
  36. config interpret_cmd_line_arguments(const po::variables_map& command_line_args)
  37. {
  38. config c;
  39. assert(command_line_args.count("crossover"));
  40. const std::string crossover_operator = command_line_args["crossover"].as<std::string>();
  41. if(crossover_operator == "pmx")
  42. c.crossover_type = crossover::type::PMX;
  43. else if(crossover_operator == "ox")
  44. c.crossover_type = crossover::type::OX;
  45. else if(crossover_operator == "cx")
  46. c.crossover_type = crossover::type::CX;
  47. else
  48. throw std::runtime_error("Crossover operator unspecified.");
  49. if(command_line_args.count("debug"))
  50. c.debug = true;
  51. else
  52. c.debug = false;
  53. if(command_line_args.count("report-population"))
  54. c.report_population = true;
  55. else
  56. c.report_population = false;
  57. if(command_line_args.count("report-best"))
  58. c.report_best = true;
  59. else
  60. c.report_best = false;
  61. if(command_line_args.count("report-every-frame"))
  62. {
  63. const std::string to_report = command_line_args["report-every-frame"].as<std::string>();
  64. if(to_report == "best")
  65. c.report_every = config::report::best;
  66. else if(to_report == "avg")
  67. c.report_every = config::report::avg;
  68. }
  69. else
  70. c.report_every = config::report::none;
  71. if(command_line_args.count("smart-termination"))
  72. c.smart_termination = true;
  73. else
  74. c.smart_termination = false;
  75. if(command_line_args.count("optimum"))
  76. c.optimum = command_line_args["optimum"].as<int>();
  77. else
  78. c.optimum = -1;
  79. if(command_line_args.count("report-var"))
  80. c.report_var = true;
  81. else
  82. c.report_var = false;
  83. if(command_line_args.count("compare-operators"))
  84. c.compare_operators = true;
  85. else
  86. c.compare_operators = false;
  87. if(command_line_args.count("population-size"))
  88. c.population_size=command_line_args["population-size"].as<int>();
  89. else
  90. c.population_size=200;
  91. if(command_line_args.count("ping"))
  92. c.ping_frequency=command_line_args["ping"].as<int>();
  93. else
  94. c.ping_frequency=0;
  95. assert(command_line_args.count("max-iter"));
  96. c.max_iter = command_line_args["max-iter"].as<int>();
  97. if(command_line_args.count("set-seed"))
  98. c.seed = command_line_args["set-seed"].as<int>();
  99. else
  100. c.seed = 0;
  101. return c;
  102. }
  103. void read_cmd_params(int argc, char* argv[])
  104. {
  105. po::options_description command_line_args_desc = command_line_args_create();
  106. po::variables_map command_line_args = read_command_line(command_line_args_desc, argc, argv);
  107. if(command_line_args.count("crossover"))
  108. {
  109. const std::string crossover_operator = command_line_args["crossover"].as<std::string>();
  110. std::cout << "crossover operator: " << crossover_operator << std::endl;
  111. }
  112. if(command_line_args.count("help"))
  113. {
  114. std::cout << command_line_args_create() << "\n";
  115. }
  116. }
  117. int main(int argc, char* argv[])
  118. {
  119. try
  120. {
  121. po::options_description command_line_args_desc = command_line_args_create();
  122. po::variables_map command_line_args = read_command_line(command_line_args_desc, argc, argv);
  123. if(command_line_args.count("help"))
  124. {
  125. std::cout << command_line_args_create() << "\n";
  126. return 0;
  127. }
  128. config cfg = interpret_cmd_line_arguments(command_line_args);
  129. solve_flowshop(cfg);
  130. }
  131. catch(std::exception& e)
  132. {
  133. std::cerr << "Error: " << e.what() << "\n";
  134. return 1;
  135. }
  136. return 0;
  137. }