GSAT.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //============================================================================
  2. // Name : GSAT.cpp
  3. // Author : Sina M.Baharlou
  4. // Version : 1
  5. // Description : GSAT Solver
  6. //============================================================================
  7. #include <iostream>
  8. #include <string.h>
  9. #include <string>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <vector>
  13. #include <set>
  14. #include <fstream>
  15. #include <random>
  16. #include <math.h>
  17. #include <sys/time.h>
  18. #include "GSAT.hpp"
  19. using namespace std;
  20. #include <algorithm>
  21. char* getCmdOption(char ** begin, char ** end, const std::string & option)
  22. {
  23. char ** itr = std::find(begin, end, option);
  24. if (itr != end && ++itr != end)
  25. {
  26. return *itr;
  27. }
  28. return 0;
  29. }
  30. bool cmdOptionExists(char** begin, char** end, const std::string& option)
  31. {
  32. return std::find(begin, end, option) != end;
  33. }
  34. void printHelp()
  35. {
  36. const char *help = "\n\n// -- GSAT simple solver -- // \n"
  37. "// -- By Sina M.Baharlou -- // \n"
  38. "// -- Oct 2016 \n\n"
  39. "GSAT -i [input_file] <options> \n"
  40. "Available options are : \n"
  41. "\t--iterations [number of iterations] \n"
  42. "\t--flip_max [number of maximum flips per iterations]\n"
  43. "\t--greedy_factor [greedy factor] \n"
  44. "\t--gw_factor [gsat/wsat factor] \n"
  45. "\t--heuristic [heuristic_method]\n"
  46. "\t\t-1- randomly select the heuristic method\n"
  47. "\t\t 0- select the first best choice\n"
  48. "\t\t 1- select randomly among the best choices\n"
  49. "\t\t 2- select according to the cost distribution\n"
  50. "\t\t 3- select randomly among all the choices\n"
  51. "\t\t 4- combination of 1 and 2 according to the greedy factor\n"
  52. "\t\t 5- WALKSAT\n"
  53. "\t\t 6- randomly select between GSAT or WALKSAT\n"
  54. "\t--rnd_method [rnd_method] \n"
  55. "\t\t-1-randomly select the method\n"
  56. "\t\t0-fill with zeros\n"
  57. "\t\t1-fill with ones\n"
  58. "\t\t2-fill uniformly\n"
  59. "\t\t3-STEP method\n"
  60. "\t\t4-PORTION method\n"
  61. "\t\t5-PORTION_EL method\n";
  62. printf("%s",help);
  63. }
  64. void GSAT::initialize(){
  65. formula=new CFormula();
  66. formula->initFormula(options->filename);
  67. formula->setGreedyFactor(options->greedyFactor);
  68. formula->setGWFactor(options->gwFactor);
  69. }
  70. bool GSAT::start(int fill,int heuristic){
  71. return formula->startGSAT(1,options->maxTry,fill,heuristic);
  72. }
  73. Options* GSAT::setParameters(int argc,char* argv[]){
  74. options = new Options();
  75. // -- default variables --
  76. options->iterations=DEFAULT_ITERATIONS;
  77. options->maxTry=DEFAULT_MAX_TRY;
  78. options->greedyFactor=DEFAULT_GREEDY_FACTOR;
  79. options->gwFactor=DEFAULT_GW_FACTOR;
  80. options->rndMethod=DEFAULT_RND_METHOD;
  81. options->hMethod=DEFAULT_H_METHOD;
  82. // --
  83. if (argc<2)
  84. {
  85. printHelp();
  86. exit(0);
  87. }
  88. if (!cmdOptionExists(argv, argv+argc, "-i"))
  89. {
  90. printHelp();
  91. printf("STD Error : no input file has been provided.\n\n");
  92. exit(0);
  93. }
  94. options->filename = getCmdOption(argv, argv + argc, "-i");
  95. if (options->filename==NULL)
  96. {
  97. printf("STD Error : not input file has been provided.\n\n");
  98. exit(0);
  99. }
  100. if (cmdOptionExists(argv, argv+argc, "--iterations"))
  101. options->iterations=stoi(getCmdOption(argv, argv + argc, "--iterations"));
  102. if (cmdOptionExists(argv, argv+argc, "--flip_max"))
  103. options->maxTry=stoi(getCmdOption(argv, argv + argc, "--flip_max"));
  104. if (cmdOptionExists(argv, argv+argc, "--greedy_factor"))
  105. options->greedyFactor=stof(getCmdOption(argv, argv + argc, "--greedy_factor"));
  106. if (cmdOptionExists(argv, argv+argc, "--gw_factor"))
  107. options->gwFactor=stof(getCmdOption(argv, argv + argc, "--gw_factor"));
  108. if (cmdOptionExists(argv, argv+argc, "--rnd_method"))
  109. options->rndMethod=stoi(getCmdOption(argv, argv + argc, "--rnd_method"));
  110. if (cmdOptionExists(argv, argv+argc, "--heuristic"))
  111. options->hMethod=stoi(getCmdOption(argv, argv + argc, "--heuristic"));
  112. return options;
  113. }