| 1 | #ifndef DECISIONSTACK_H |
|---|
| 2 | #define DECISIONSTACK_H |
|---|
| 3 | |
|---|
| 4 | #include<vector> |
|---|
| 5 | |
|---|
| 6 | #ifdef DEBUG |
|---|
| 7 | #include <assert.h> |
|---|
| 8 | #endif |
|---|
| 9 | |
|---|
| 10 | #include <RealNumberTypes.h> |
|---|
| 11 | |
|---|
| 12 | #include "../Basics.h" |
|---|
| 13 | |
|---|
| 14 | #include "InstanceGraph/ComponentTypes.h" |
|---|
| 15 | #include "InstanceGraph/AtomsAndNodes.h" |
|---|
| 16 | #include "InstanceGraph/InstanceGraph.h" |
|---|
| 17 | |
|---|
| 18 | using namespace std; |
|---|
| 19 | |
|---|
| 20 | /** \addtogroup Interna*/ |
|---|
| 21 | /*@{*/ |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | class CDecision |
|---|
| 25 | { |
|---|
| 26 | //////////////////// |
|---|
| 27 | /// active Component |
|---|
| 28 | //////////////////// |
|---|
| 29 | unsigned int refCompId; |
|---|
| 30 | |
|---|
| 31 | // branch |
|---|
| 32 | bool flipped; |
|---|
| 33 | |
|---|
| 34 | // bcp |
|---|
| 35 | unsigned int iImpLitOfs; |
|---|
| 36 | |
|---|
| 37 | // Solutioncount |
|---|
| 38 | CRealNum rnNumSols[2]; |
|---|
| 39 | |
|---|
| 40 | //////////////////// |
|---|
| 41 | /// remaining Components |
|---|
| 42 | //////////////////// |
|---|
| 43 | |
|---|
| 44 | /** |
|---|
| 45 | * [iRemCompOfs,iEndRemComps) defines the interval in allComponentsStack |
|---|
| 46 | * where the child components of the [refCompId] component are stored |
|---|
| 47 | */ |
|---|
| 48 | unsigned int iRemCompOfs; |
|---|
| 49 | unsigned int iEndRemComps; |
|---|
| 50 | |
|---|
| 51 | //this function only to be used by decisionStack |
|---|
| 52 | unsigned int countCompsToProcess() |
|---|
| 53 | { |
|---|
| 54 | #ifdef DEBUG |
|---|
| 55 | assert(iEndRemComps >= iRemCompOfs); |
|---|
| 56 | #endif |
|---|
| 57 | return iEndRemComps - iRemCompOfs; |
|---|
| 58 | } |
|---|
| 59 | void popRemComp() |
|---|
| 60 | { |
|---|
| 61 | #ifdef DEBUG |
|---|
| 62 | assert(iEndRemComps >= iRemCompOfs); |
|---|
| 63 | #endif |
|---|
| 64 | iEndRemComps--; |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | public: |
|---|
| 68 | |
|---|
| 69 | CDecision() |
|---|
| 70 | { |
|---|
| 71 | flipped = false; |
|---|
| 72 | rnNumSols[0] = 0.0; |
|---|
| 73 | rnNumSols[1] = 0.0; |
|---|
| 74 | refCompId = 0; |
|---|
| 75 | iImpLitOfs = (unsigned int) -1; |
|---|
| 76 | iRemCompOfs = (unsigned int) -1; |
|---|
| 77 | iEndRemComps = (unsigned int) -1; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | ~CDecision(){} |
|---|
| 81 | |
|---|
| 82 | bool isFlipped(){return flipped;} |
|---|
| 83 | bool anotherCompProcessible() |
|---|
| 84 | { |
|---|
| 85 | return getBranchSols() !=0.0 && countCompsToProcess() > 0; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | void includeSol(const CRealNum &rnCodedSols) |
|---|
| 89 | { |
|---|
| 90 | if(rnNumSols[flipped] == 0.0) rnNumSols[flipped] = rnCodedSols; |
|---|
| 91 | else |
|---|
| 92 | rnNumSols[flipped] *= rnCodedSols; |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | const CRealNum &getBranchSols() const |
|---|
| 96 | { |
|---|
| 97 | return rnNumSols[flipped]; |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | const CRealNum getOverallSols() const |
|---|
| 101 | { |
|---|
| 102 | return rnNumSols[0] + rnNumSols[1]; |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | friend class CDecisionStack; |
|---|
| 106 | }; |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | class CDecisionStack : vector<CDecision> |
|---|
| 110 | { |
|---|
| 111 | CInstanceGraph &theClPool; |
|---|
| 112 | |
|---|
| 113 | vector<LiteralIdT> allImpliedLits; |
|---|
| 114 | |
|---|
| 115 | vector<CComponentId *> allComponentsStack; |
|---|
| 116 | |
|---|
| 117 | void reactivateTOS(); |
|---|
| 118 | |
|---|
| 119 | // store each cacheEntry where the children of top().refComp are stored |
|---|
| 120 | bool storeTOSCachedChildren(); |
|---|
| 121 | |
|---|
| 122 | unsigned int addToDecLev; |
|---|
| 123 | public: |
|---|
| 124 | /// |
|---|
| 125 | const vector<CComponentId *> & getAllCompStack() {return allComponentsStack;} |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | vector<CComponentId *>::iterator TOSRemComps_begin() {return allComponentsStack.begin() + top().iRemCompOfs;} |
|---|
| 129 | |
|---|
| 130 | CDecisionStack(CInstanceGraph &pool):theClPool(pool){addToDecLev = 0;} |
|---|
| 131 | |
|---|
| 132 | ~CDecisionStack(){} |
|---|
| 133 | |
|---|
| 134 | //begin for implicit BCP |
|---|
| 135 | void beginTentative() {addToDecLev = 1;} |
|---|
| 136 | void endTentative() {addToDecLev = 0;} |
|---|
| 137 | //end for implicit BCP |
|---|
| 138 | |
|---|
| 139 | bool pop(); |
|---|
| 140 | void push(); |
|---|
| 141 | |
|---|
| 142 | inline CDecision &top() {return back();} |
|---|
| 143 | |
|---|
| 144 | bool flipTOS(); |
|---|
| 145 | |
|---|
| 146 | unsigned int countAllImplLits() |
|---|
| 147 | { |
|---|
| 148 | return allImpliedLits.size(); |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | void shrinkImplLitsTo(unsigned int sz) |
|---|
| 152 | { |
|---|
| 153 | if(sz >= allImpliedLits.size()) return; |
|---|
| 154 | allImpliedLits.resize(sz); |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | int TOS_siblingsProcessed() |
|---|
| 158 | { |
|---|
| 159 | #ifdef DEBUG |
|---|
| 160 | assert(allComponentsStack.size() >= top().iEndRemComps); |
|---|
| 161 | #endif |
|---|
| 162 | return (int) allComponentsStack.size() - (int) top().iEndRemComps; |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | unsigned int TOS_countImplLits() |
|---|
| 166 | { |
|---|
| 167 | return allImpliedLits.size() - top().iImpLitOfs; |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | const LiteralIdT &TOS_decLit() const |
|---|
| 172 | { |
|---|
| 173 | return allImpliedLits[back().iImpLitOfs]; |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | void TOS_addImpliedLit(LiteralIdT aLit) |
|---|
| 177 | { |
|---|
| 178 | allImpliedLits.push_back(aLit); |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | void TOS_popImpliedLit() |
|---|
| 182 | { |
|---|
| 183 | allImpliedLits.pop_back(); |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | vector<LiteralIdT>::const_iterator TOS_ImpliedLits_begin() |
|---|
| 187 | { |
|---|
| 188 | return allImpliedLits.begin() + top().iImpLitOfs; |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | vector<LiteralIdT>::const_iterator TOS_ImpliedLits_end() |
|---|
| 192 | { |
|---|
| 193 | return allImpliedLits.end(); |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | CComponentId & TOSRefComp() |
|---|
| 198 | { |
|---|
| 199 | #ifdef DEBUG |
|---|
| 200 | assert(top().refCompId < allComponentsStack.size()); |
|---|
| 201 | #endif |
|---|
| 202 | return *allComponentsStack[top().refCompId]; |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | bool TOS_hasAnyRemComp() |
|---|
| 206 | { |
|---|
| 207 | return allComponentsStack.size() > top().iRemCompOfs; |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | void TOS_popRemComp(){top().popRemComp();} |
|---|
| 211 | |
|---|
| 212 | unsigned int TOS_countRemComps() {return top().countCompsToProcess();} |
|---|
| 213 | |
|---|
| 214 | void TOS_addRemComp() |
|---|
| 215 | { |
|---|
| 216 | allComponentsStack.push_back(new CComponentId()); |
|---|
| 217 | top().iEndRemComps = allComponentsStack.size(); |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | CComponentId & TOS_NextComp() |
|---|
| 221 | { |
|---|
| 222 | #ifdef DEBUG |
|---|
| 223 | assert(top().iEndRemComps <= allComponentsStack.size()); |
|---|
| 224 | assert(allComponentsStack[top().iEndRemComps -1] != NULL); |
|---|
| 225 | #endif |
|---|
| 226 | return *allComponentsStack[top().iEndRemComps - 1]; |
|---|
| 227 | } |
|---|
| 228 | |
|---|
| 229 | void TOS_sortRemComps(); |
|---|
| 230 | |
|---|
| 231 | CComponentId & lastComp() {return *allComponentsStack.back();} |
|---|
| 232 | |
|---|
| 233 | void printStats(); |
|---|
| 234 | |
|---|
| 235 | inline int getDL() const{return size()-1+addToDecLev;} // 0 means pre-1st-decision |
|---|
| 236 | |
|---|
| 237 | void init(unsigned int resSize = 1); |
|---|
| 238 | }; |
|---|
| 239 | |
|---|
| 240 | /*@}*/ |
|---|
| 241 | |
|---|
| 242 | #endif |
|---|
| 243 | |
|---|