CFormula.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * CFormula.hpp
  3. *
  4. * Created on: Oct 1, 2016
  5. * Author: Sina M.Baharlou
  6. */
  7. #ifndef CFORMULA_HPP_
  8. #define CFORMULA_HPP_
  9. #include <iostream>
  10. #include <string.h>
  11. #include <string>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <vector>
  15. #include <set>
  16. #include <fstream>
  17. #include <random>
  18. #include <math.h>
  19. #include "FillMethod.hpp"
  20. #include "ArrayFiller.hpp"
  21. #include "HeuristicMethod.hpp"
  22. using namespace std;
  23. typedef int Literal;
  24. typedef std::vector<Literal> Clause;
  25. typedef std::vector<Clause> Formulae;
  26. #define COMMENT_FLAG 'c'
  27. #define PROBLEM_FLAG 'p'
  28. #define END_FLAG '%'
  29. #define toggle(x) (x=x^1)
  30. #define DEFAULT_BIAS 1
  31. #define DEFAULT_ITERATIONS 1000
  32. #define DEFAULT_MAX_TRY 255
  33. #define DEFAULT_GREEDY_FACTOR 0.8
  34. #define DEFAULT_GW_FACTOR 0.5
  35. class CFormula
  36. {
  37. public :
  38. string fType; // -- formula type (in this case should be 'cnf')
  39. string fComments; // -- formula comments
  40. unsigned int nVars; // -- number of variables
  41. unsigned int nClauses; // -- number of clauses
  42. struct
  43. {
  44. unsigned int nPosLits; // -- number of positive literals
  45. unsigned int nNegLits; // -- number of negative literals
  46. unsigned int nLits; // -- total literls
  47. }fStats;
  48. public :
  49. bool * fVars; // -- formula variables
  50. float * fPortions; // -- variables' proportions
  51. float tPortion; // -- total positive to negative literal proportion
  52. int * nLit ; // -- total occurrence of specific literal in formula
  53. int * evalArray; // -- flip evaluation array
  54. bool * evalClause; // -- evaluation of clauses // ADDED
  55. std::vector<int>* varMap; // -- mapping between variables and clauses //ADDED
  56. unsigned int clCounter; // -- clause counter variable
  57. float greedyFactor; // -- GSAT greedy factor
  58. float gwFactor; // -- GSAT or WSAT select probability
  59. Formulae cFormula; // -- canonical formula jagged array(vector)
  60. ArrayFiller* arrayFiller; //-- array filler class
  61. std::default_random_engine rndEngine;
  62. std::uniform_real_distribution<float> rndReal; // -- sampling from real uniform distribution
  63. std::uniform_int_distribution<int> rndMethod; // -- sampling from int uniform distribution
  64. std::uniform_int_distribution<int> rndHeuristic;// -- sampling from int uniform distribution
  65. unsigned int nbSatisfiedClausesFill;
  66. unsigned int nbSatisfiedClausesSolve;
  67. unsigned int lastFillMethod;
  68. unsigned int lastHeuristic;
  69. unsigned int ite;
  70. public:
  71. CFormula();
  72. bool initFormula(const char* filename);
  73. bool evalFormulaQ(unsigned int varIndex,int& clNum); // -- faster version of evalFormula (just check the modified clauses)
  74. bool evalFormula(int& clNum);
  75. bool startGSAT(int iterations=DEFAULT_ITERATIONS,int maxTry=DEFAULT_MAX_TRY,int fillMethod=-1,int hMethod=-1);
  76. void setGreedyFactor(float factor);
  77. void setGWFactor(float factor);
  78. inline unsigned int getNbClauses(){return nClauses;}
  79. inline unsigned int getNbVariables(){return nVars;}
  80. inline unsigned int getNbSatisfiedClausesFill(){return nbSatisfiedClausesFill;}
  81. inline unsigned int getNbSatisfiedClausesSolve(){return nbSatisfiedClausesSolve;}
  82. inline unsigned int getHeuristicFill(){return lastFillMethod;}
  83. inline unsigned int getHeuristicSolve(){return lastHeuristic;}
  84. inline unsigned int getNbIterations(){return ite;}
  85. private:
  86. std::vector<string> tokenize(char*input,const char*split);
  87. Clause parseClause(char*input,const char*split);
  88. // -- randomly select the filler method --
  89. FillMethod getRandomMethod();
  90. // -- randomly select the heuristic method --
  91. HeuristicMethod getRandomHeuristic();
  92. // -- If GSAT should take a best action
  93. bool takeBestChoice();
  94. bool takeGW();
  95. void evalMove(int&min,int&max);
  96. std::vector<int> evalUnsatClause(int clIndex,int&min,int&max);
  97. int getBestChoice(bool retFirst=false);
  98. int walkSatChoice(bool retFirst=false);
  99. int getUnsatClause(bool retFirst=false);
  100. int getRndBest(bool exponential=true); // -- randomly select the choice according to each move's reward
  101. };
  102. #endif /* CFORMULA_HPP_ */