lazywalksat.h

00001 /* This code is based on the MaxWalkSat package of Kautz et al. */
00002 #ifndef _LAZY_WALKSAT_H_
00003 #define _LAZY_WALKSAT_H_
00004 
00005 #include <iostream>
00006 #include <stdio.h>
00007 #include <stdlib.h>
00008 #include <math.h>
00009 #include <sys/time.h>
00010 #include <sys/resource.h>
00011 #include <signal.h>
00012 
00013 #include "freestoremanager.h"
00014 
00015 #include "array.h"
00016 #include "lwinfo.h"
00017 #include "wsutil.h"
00018 #include "maxwalksatparams.h"
00019 #include "samplesatparams.h"
00020 
00021         /* Atoms start at 1 */
00022         /* Not a is recorded as -1 * a */
00023         /* One dimensional arrays are statically allocated. */
00024         /* Two dimensional arrays are dynamically allocated in */
00025         /* the second dimension only.  */
00026         /* (Arrays are not entirely dynamically allocated, because */
00027         /* doing so slows execution by 25% on SGI workstations.) */
00028 
00029 class LazyWalksat
00030 {
00031          
00032  int numatom;
00033  int basenumatom;  // number of distinct atoms in the first set of unsatisfied clauses
00034  int numclause;
00035  int numliterals;
00036 
00037  int atommem;    //current allocation of memory for atoms
00038  int clausemem;  //current allocation of memory for clauses
00039 
00040  int saRatio;           // saRatio/100: percentage of SA steps
00041  int temperature;       // temperature/100: fixed temperature for SA step
00042  bool latesa;
00043 
00044  int ** clause;                 /* clauses to be satisfied */
00045                                 /* indexed as clause[clause_num][literal_num] */
00046  int * cost;                    /* cost of each clause */
00047  int * size;                    /* length of each clause */
00048  int * falseclause;                     /* clauses which are false */
00049  int * wherefalse;              /* where each clause is listed in false */
00050  int * numtruelit;              /* number of true literals in each clause */
00051 
00052  int ** occurence;      /* where each literal occurs */
00053                                                /* indexed as occurence[literal+MAXATOM][occurence_num] */
00054 
00055  int * numoccurence;        /* number of times each literal occurs */
00056  int * newnumoccurence; /* number of times each literal occurs in the newly added clause set*/
00057  int * occurencemem;        /* memory allocated (in terms of number of occurences) 
00058                                                                    which can be stored for this literal */
00059 
00060 
00061  int * atom;            /* value of each atom */ 
00062  int * initatom; /* initial value of each atom */
00063 
00064  int * lowatom;
00065 
00066  int * changed;         /* step at which atom was last flipped */
00067 
00068  int * breakcost;       /* the cost of clauses that introduces if var is flipped */
00069 
00070  int numfalse;                  /* number of false clauses */
00071  int costofnumfalse;            /* cost associated with the number of false clauses */
00072 
00073 
00074  bool costexpected;       /*indicate whether cost is expected from the input*/
00075  bool abort_flag;
00076 
00077  int heuristic;                 /* heuristic to be used */
00078  int numerator;                 /* make random flip with numerator/denominator frequency */
00079  int denominator;               
00080  int tabu_length;               /* length of tabu list */
00081 
00082  long int numflip;              /* number of changes so far */
00083  long int numnullflip;          /*  number of times a clause was picked, but no  */
00084                                 /*  variable from it was flipped  */
00085 
00086  int highestcost;               /*The highest cost of a clause violation*/
00087  int eqhighest; /*Is there a clause violated with the highest cost*/
00088  int numhighest;      /*Number of violated clauses with the highest cost*/
00089 
00090  bool hard; /* if true never break a highest cost clause */
00091 
00092  bool noApprox; /* If set to true, atoms are not deactivated when mem. is full */
00093 
00094  FreeStoreManager *clauseFSMgr;  //free store manager
00095  int clauseFreeStore; //out of the memory allocated, how much is free for usage
00096  int *clauseStorePtr; //free storage ptr
00097 
00098  FreeStoreManager *atomFSMgr;  //free store manager
00099  int atomFreeStore; //out of the memory allocated, how much is free for usage
00100  int *atomStorePtr; //free storage ptr
00101 
00102  Array<Array<int> *> allClauses;
00103  Array<int> allClauseWeights;
00104  Array<Array<int> *> newClauses;
00105  Array<int> newClauseWeights;
00106 
00107     // Holds the initial set of active clauses for MC-SAT
00108  Array<IntClause *> supersetClauses_;
00109 
00110     // Holds atoms which have been fixed due to unit prop.
00111  //Array<int> fixedAtoms_;
00112  bool * fixedAtoms_;
00113  int maxFixedAtoms;
00114 
00115  LWInfo *lwInfo;
00116 
00117         // Max. amount of memory to use
00118  int memLimit;
00119         // Max. amount of clauses memory can hold
00120  int clauseLimit;
00121         // Indicates whether deactivation of atoms has taken place yet
00122  bool haveDeactivated;
00123         // Initial number of clauses
00124  int initClauseCount;
00125     // Initial number of atoms
00126  int initAtomCount;
00127 
00128 public:
00129 
00130  LazyWalksat(LWInfo *lwInfo, int memLimit); 
00131 
00132  ~LazyWalksat();
00133  
00134  /* perform the lazy samplesat inference */ 
00135  bool sample(const MaxWalksatParams* const & mwsParams,
00136                          const SampleSatParams& sampleSatParams,
00137              const bool& initial);
00138 
00139  /* perform the lazy walksat inference */ 
00140  void infer(const MaxWalksatParams* const & params,
00141                         const int& numSolutions, const bool&  includeUnsatSolutions,
00142                 Array<Array<bool>*>& solutions, Array<int>& numBad,
00143                 const bool& initial);
00144                 
00145  int getNumInitClauses();
00146  
00147  int getNumInitAtoms();
00148 
00149  int getNumClauses();
00150  
00151  void resetSampleSat();
00152  
00153  void randomizeActiveAtoms();
00154  
00155 private:
00156 
00157  /* initialize - add the initial set of clauses*/
00158  void init();
00159 
00160  /* initialize a new run */
00161  void initrun(void);                 
00162 
00163  /* changes an atom to make the given clause true */
00164  void fix(int tofix);                 
00165 
00166  /* added by Parag - add clauses*/
00167  void addNewClauses(bool initial);
00168 
00169  /* Deactivate clauses to save memory */
00170  void trimClauses();
00171 
00172  /* Using this temporarily until all old arrays converted to type Array */
00173  void removeAllNullFromArray(int array[]);
00174 
00175 /* Initialize breakcost in the following: */
00176  void initializeBreakCost(int startclause); 
00177 
00178  /* update the structures */
00179 
00180  /*initialize the clause memory*/
00181  void initializeClauseMemory();
00182 
00183  /*initialize the atom memory */
00184  void initializeAtomMemory();
00185 
00186  /*  allocate memory for clauses */
00187  void allocateClauseMemory(int allocCount);
00188                  
00189  /*  allocate memory for clauses */
00190  void allocateAtomMemory(int allocCount);
00191 
00192  /* delete memory allocated for clauses */
00193  void deleteClauseMemory();
00194 
00195  /* delete memory allocated for atoms */
00196  void deleteAtomMemory();    
00197 
00198  //get the total of the weights of elements in the
00199  //clauseWeights array
00200  int getTotalWeight(Array<int> &clauseWeights);
00201 
00202  /* changes the assignment of the given literal */
00203  void flipatom(int toflip);           
00204 
00205  int selecthigh(int);   /*find the int'th clause with the highest cost*/
00206 
00207  double elapsed_seconds(void);
00208  int countunsat(void);
00209  void countlowunsatcost( int *, int *);
00210  void scanone(int argc, char *argv[], int i, int *varptr);
00211 
00212  int pickrandom(int *numbreak,int clausesize, int tofix);
00213  int pickproductsum(int *numbreak,int clausesize, int tofix);
00214  int pickreciprocal(int *numbreak,int clausesize, int tofix);
00215  int pickadditive(int *numbreak,int clausesize, int tofix);
00216  int pickbest(int *numbreak,int clausesize, int tofix);
00217  int picktabu(int *numbreak,int clausesize, int tofix);
00218  int pickexponential(int *numbreak,int clausesize, int tofix);
00219  int pickzero(int *numbreak,int clausesize);
00220  int pickweight(int *weight,int clausesize);
00221 
00222  bool simAnnealing();
00223  
00224  void undoFixedAtoms();
00225  void initFixedAtoms(Array<Array<int> *> &clauses,
00226                      Array<int> &clauseWeights);
00227 
00228  void print_false_clauses_cost(long int lowbad);
00229  void print_low_assign(long int lowbad);
00230 
00231  void printClause(int clause);
00232 
00233  void save_low_assign(void);
00234 
00235  long super(int i);
00236 
00237 };
00238 
00239 #endif

Generated on Tue Jan 16 05:30:03 2007 for Alchemy by  doxygen 1.5.1