source: vis_dev/sharpSAT/src/src_sharpSAT/MainSolver/InstanceGraph/InstanceGraph.h @ 9

Last change on this file since 9 was 9, checked in by cecile, 13 years ago

add sharpSat

File size: 9.8 KB
Line 
1#ifndef INSTANCE_GRAPH_H
2#define INSTANCE_GRAPH_H
3
4
5#include<vector>
6#include<deque>
7
8#ifdef DEBUG
9#include <assert.h>
10#endif
11
12using namespace std;
13
14#include <Interface/AnalyzerData.h>
15
16#include "../../Basics.h"
17#include "AtomsAndNodes.h"
18
19
20typedef vector<CClauseVertex> DepositOfClauses;
21typedef vector<CVariableVertex> DepositOfVars;
22
23
24extern CRunAnalyzer theRunAn;
25
26class CInstanceGraph
27{
28   /** theLitVector: the literals of all clauses are stored here
29   *   INVARIANT: first and last entries of theLitVector are a SENTINEL_LIT
30   *
31   */
32   vector<LiteralIdT>  theLitVector;
33   
34   /** theInClsVector:
35   * the Vector containing all links from variables
36   * to the clauses they appear in
37   * INVARIANT: for each Variable x the block of clause links looks as follows:
38           SENTINEL_CL nnnnnnnnppppppp SENTINEL_CL
39        where: n ... clause where -x appears; p.. clause where x appears
40   */
41   vector<ClauseIdT> theInClsVector; 
42   
43   DepositOfClauses    theClauses;   
44   unsigned int iOfsBeginConflictClauses;
45   
46   DepositOfVars       theVars;
47   
48   vector<AntecedentT> theConflicted;
49   unsigned int numBinClauses;
50   unsigned int numBinCCls; 
51   
52 protected:
53   
54   vector<LiteralIdT>  theUnitClauses;
55   vector<TriValue>  theUClLookUp;
56   
57   /// THE function to clear all the data in this class
58   void reset()
59   {
60      theLitVector.clear(); 
61      theLitVector.push_back(SENTINEL_LIT);
62     
63      theInClsVector.clear();
64      theInClsVector.push_back(SENTINEL_CL);
65     
66      theClauses.clear();
67      theClauses.push_back(CClauseVertex()); 
68      iOfsBeginConflictClauses = 0;
69     
70      theVars.clear();
71      theVars.push_back(CVariableVertex(0,0)); //initializing the Sentinel
72      theConflicted.clear();
73     
74      numBinClauses = 0;
75      numBinCCls = 0;
76     
77      theUnitClauses.clear();
78      theUClLookUp.clear();
79   }
80   
81   void doVSIDSScoreDiv()
82   {
83     DepositOfVars::iterator it;
84     for(it = beginOfVars();it != endOfVars();it++)
85     {
86       it->scoreVSIDS[0] >>= 1;
87       it->scoreVSIDS[1] >>= 1;             
88     }       
89   }
90
91 protected:
92   void printCClstats()
93   {
94     toSTDOUT("CCls (all/bin/unit):\t");
95     toSTDOUT(theClauses.size() - iOfsBeginConflictClauses);
96     toSTDOUT("/"<< numBinCCls<< "/"<<theUnitClauses.size()<<endl);
97   }
98   bool isUnitCl(const LiteralIdT theLit)
99   {
100     return theUClLookUp[theLit.toVarIdx()] == theLit.polarityTriVal();
101   }
102 
103   unsigned int countBinClauses() const { return numBinClauses;}
104   unsigned int countBinCCls() const { return numBinCCls;}
105   DepositOfClauses::iterator beginOfCCls()
106   {
107     return theClauses.begin() + iOfsBeginConflictClauses;
108   }
109   DepositOfClauses::iterator endOfCCls()
110   {
111     return theClauses.end();
112   }
113   DepositOfClauses::iterator beginOfClauses()
114   {
115     return theClauses.begin()+1;
116   }
117   
118   ClauseIdT toClauseIdT(DepositOfClauses::iterator it)
119   {
120     unsigned int idx = (unsigned int)(it - theClauses.begin());
121     return ClauseIdT(idx);   
122   }
123   
124   
125   DepositOfClauses::iterator endOfClauses()
126   {
127     return theClauses.begin() + iOfsBeginConflictClauses;
128   }
129   
130   DepositOfVars::iterator beginOfVars()
131   {
132    return theVars.begin()+1;
133   }
134   
135   DepositOfVars::iterator endOfVars()
136   {
137     return theVars.end();
138   }
139   vector<LiteralIdT>::iterator begin(CClauseVertex &rCV)
140   { 
141     return theLitVector.begin() + rCV.getLitOfs();
142   }
143   
144   vector<LiteralIdT>::const_iterator begin(const CClauseVertex &rCV) const
145   {
146      return theLitVector.begin() + rCV.getLitOfs();
147   }
148   
149   const LiteralIdT & ClauseEnd() const {return SENTINEL_LIT;}
150     
151   
152   bool substituteLitsOf(CClauseVertex &rCl,const LiteralIdT &oldLit, const LiteralIdT &newLit);
153   bool containsVar(const CClauseVertex &rCl,const VarIdT &theVar) const;       
154   bool containsLit(const CClauseVertex &rCl,const LiteralIdT &theLit) const;
155
156 public:
157   
158   CInstanceGraph();
159   ~CInstanceGraph();
160   
161   /////////////////////////////////////////////////////////
162   // BEGIN access to variables and clauses
163   inline vector<ClauseIdT>::const_iterator var_InClsBegin(VarIdT VarIndex) const
164   {
165     return (theInClsVector.begin() + theVars[VarIndex].getInClsVecOfs(false));
166   }
167   
168   inline vector<ClauseIdT>::iterator var_InClsBegin(VarIdT VarIndex,bool pol) 
169   {
170     return (theInClsVector.begin() + theVars[VarIndex].getInClsVecOfs(pol));
171   }
172   
173   inline vector<ClauseIdT>::iterator var_InClsStart(VarIdT VarIndex,bool pol) 
174   {
175     return (theInClsVector.begin() + theVars[VarIndex].getInClsVecOfs(true) - 1 + (int) pol);
176   }
177   
178   inline int var_InClsStep(bool pol) 
179   {
180     return pol?1:-1;
181   }
182   
183   
184   inline bool var_EraseClsLink(VarIdT VarIndex, bool linkPol ,ClauseIdT idCl)
185   {     
186     CVariableVertex &rV = getVar(VarIndex);
187     vector<ClauseIdT>::iterator it;
188     
189     if(!linkPol)
190     {
191       for(it = var_InClsBegin(VarIndex, true)-1;
192            *it != SENTINEL_CL;it--)       
193         if(*it == idCl)
194         {     
195           while(*it != SENTINEL_CL) {*it = *(it-1); it--;}
196           rV.setInClsVecOfs(false,rV.getInClsVecOfs(false)+1);
197           return true;
198         }
199     } else
200     {
201       for(it = var_InClsBegin(VarIndex, true);*it != SENTINEL_CL;it++)
202         if(*it == idCl)
203         {     
204           while(*it!= SENTINEL_CL) {*it = *(it+1); it++;}
205           return true;
206         }   
207     }     
208     return false;
209   }
210 
211 
212   
213   inline vector<ClauseIdT>::const_iterator var_InClsBegin(CVariableVertex &vv) const
214   {
215     return (theInClsVector.begin() + vv.getInClsVecOfs(false));
216   }
217   
218   
219   inline CVariableVertex &getVar(VarIdT VarIndex)
220   {
221     return theVars[VarIndex];
222   }
223   inline const CVariableVertex &getVar(VarIdT VarIndex) const
224   {
225     return theVars[VarIndex];
226   }
227   
228   inline bool isSatisfied(const LiteralIdT &lit)  const
229   {
230     return theVars[lit.toVarIdx()].getVal() == lit.polarityTriVal();
231   }
232   inline bool isSatisfied(const ClauseIdT &iCl)  const
233   {
234     return isSatisfied(getClause(iCl).idLitA())
235            || isSatisfied(getClause(iCl).idLitB());
236   }
237   
238   inline CVariableVertex &getVar(const LiteralIdT &rLitId)
239   {
240     return theVars[rLitId.toVarIdx()];
241   }
242   
243   inline const CVariableVertex &getVar(const LiteralIdT &rLitId) const
244   {
245     return theVars[rLitId.toVarIdx()];
246   }
247   
248   inline CClauseVertex &getClause(const ClauseIdT &iClauseId)
249   {
250     return theClauses[iClauseId];
251   }
252   
253   inline const CClauseVertex &getClause(const ClauseIdT &iClauseId) const
254   {
255    return theClauses[iClauseId];
256   }
257   
258   // END access to variables and clauses
259   /////////////////////////////////////////////////////////
260   
261   inline const ClauseIdT getLastClauseId()
262   {
263     return ClauseIdT(theClauses.size()-1);       
264   }
265   
266   
267   vector<AntecedentT> &getConflicted()
268   {
269     return theConflicted;
270   }
271   
272   // BEGIN count something
273   inline unsigned int countAllClauses() const
274   {
275     return theClauses.size() - 1  + countBinClauses();
276   }
277   
278   inline unsigned int countCCls() const
279   {
280     return theClauses.size() - iOfsBeginConflictClauses;
281   }
282   
283   inline unsigned int countOriginalClauses() const
284   { 
285     return theClauses.size();
286   }
287   
288   inline unsigned int getMaxOriginalClIdx() const
289   { 
290     return iOfsBeginConflictClauses;
291   }
292   
293   inline unsigned int countAllVars()
294   {
295    return theVars.size() -1;
296   }
297   
298   unsigned int countActiveBinLinks(VarIdT theVar) const;   
299
300   // END count something   
301
302   bool createfromFile(const char* lpstrFileName);
303
304   
305   void print();
306   void printCl(const CClauseVertex &rCl) const;
307   void printActiveClause(const ClauseIdT &idCl) const;
308
309 protected:
310   // cleans up the ClausePool
311   /// NOTE: this does only work correctly if no CCls are present
312   bool prep_CleanUpPool();   
313 
314   
315   // only valid if no conflict clauses are present
316   bool prep_substituteClauses(unsigned int oldIdx, unsigned int newIdx);
317   bool prep_substituteVars(CVariableVertex &rV, unsigned int newIdx);
318
319   bool markCClDeleted(ClauseIdT idCl);
320   
321   
322   ///  only correct if theLit is conteined in idCl
323   bool setCClImplyingLit(ClauseIdT idCl, const LiteralIdT &theLit);
324   bool eraseLiteralFromCl(ClauseIdT idCl,LiteralIdT theLit);
325   
326   bool deleteConflictCls();
327   bool cleanUp_deletedCCls();
328   
329   bool createConflictClause(const vector<LiteralIdT> &theCClause);
330   
331   
332   unsigned int makeVariable(unsigned int VarNum)
333   {
334     theVars.push_back(CVariableVertex(VarNum,theVars.size())); 
335     return theVars.back().getVarIdT();
336   }
337   
338   ClauseIdT makeClause()
339   {     
340      theClauses.push_back(CClauseVertex()); 
341      return ClauseIdT(theClauses.size()-1);
342   }
343   
344   ClauseIdT makeConflictClause()
345   {
346      theClauses.push_back(CClauseVertex()); 
347      return ClauseIdT(theClauses.size()-1);
348   }
349   
350   bool createUnitCl(LiteralIdT Lit)
351   { 
352     theUnitClauses.push_back(Lit);
353     if(theUClLookUp[Lit.toVarIdx()] == Lit.oppositeLit().polarityTriVal()) return false;
354     theUClLookUp[Lit.toVarIdx()] = Lit.polarityTriVal();
355     return true;
356   }
357   
358   bool createBinCl(LiteralIdT LitA,LiteralIdT LitB)
359   {
360     if(getVar(LitA).hasBinLinkTo(LitB,LitA.polarity()))
361       {
362        #ifdef DEBUG
363          assert(getVar(LitB).hasBinLinkTo(LitA,LitB.polarity()));
364          toDEBUGOUT("%");
365        #endif       
366        return false;
367       }
368      getVar(LitA).addBinLink(LitA.polarity(),LitB);
369      getVar(LitB).addBinLink(LitB.polarity(),LitA);   
370      numBinClauses++;
371      return true;
372   }
373
374   bool createBinCCl(LiteralIdT LitA,LiteralIdT LitB)
375   {
376      if(getVar(LitA).hasBinLinkTo(LitB,LitA.polarity()))
377      {
378         #ifdef DEBUG
379           assert(getVar(LitB).hasBinLinkTo(LitA,LitB.polarity()));
380         #endif
381         return false;
382      }
383      getVar(LitA).addBinLinkCCl(LitA.polarity(),LitB);
384      getVar(LitB).addBinLinkCCl(LitB.polarity(),LitA);   
385      numBinCCls++;
386      return true;
387   }
388   
389   
390};
391
392
393
394#endif // CLAUSEPOOL_H
Note: See TracBrowser for help on using the repository browser.