-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwolmgr.cpp
More file actions
302 lines (260 loc) · 7.15 KB
/
wolmgr.cpp
File metadata and controls
302 lines (260 loc) · 7.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include "wolmgr.h"
#include "wolexp.h"
#include "wolvaluefactory.h"
#include "wolimplmgr.h"
#include "wolvarselmgr.h"
#include "wolvalue.h"
#include "wollog.h"
#include "common.h"
#include "wolstack.h"
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <string>
namespace wolver
{
using namespace std;
std::size_t
sliceExpHash::operator ()(WolNodeSptr const& x) const
{
std::size_t seed = 0;
WolComplexNodeSptr x_comp = dynamic_pointer_cast<WolComplexNode>(x);
seed = (size_t)((x_comp->getChildren())[0]->getId() + x_comp->getHighPrecision() +
x_comp->getLowPrecision());
return seed;
}
bool
sliceExpEqualTo::operator() (WolNodeSptr const& x, WolNodeSptr const& y) const
{
WolComplexNodeSptr x_comp = dynamic_pointer_cast<WolComplexNode>(x);
WolComplexNodeSptr y_comp = dynamic_pointer_cast<WolComplexNode>(y);
if(((x_comp->getChildren()[0]) != (y_comp->getChildren())[0]) ||
(x_comp->getHighPrecision() != y_comp->getHighPrecision()) ||
(y_comp->getLowPrecision() != y_comp->getLowPrecision()))
return false;
return true;
}
bool
expEqualTo::operator()(WolNodeSptr const& x, WolNodeSptr const& y) const
{
WolComplexNodeSptr x_comp = dynamic_pointer_cast<WolComplexNode>(x);
WolComplexNodeSptr y_comp = dynamic_pointer_cast<WolComplexNode>(y);
if ((x_comp->getType() != y_comp->getType()) ||
(x_comp->getArity() != y_comp->getArity()))
return false;
int arity = x_comp->getArity();
for (int i = 0; i < arity; i++)
{
if ((x_comp->getChildren())[i] != (y_comp->getChildren())[i])
return false;
}
return true;
}
std::size_t
expHash::operator()(WolNodeSptr const& x) const
{
std::size_t seed = 0;
WolComplexNodeSptr x_comp = dynamic_pointer_cast<WolComplexNode>(x);
int arity = x_comp->getArity();
for (int i = 0; i < arity; i++)
{
seed += (size_t)(x_comp->getChildren())[i]->getId();
}
return seed;
}
WolMgr::WolMgr()
: _globalId(1),
_valueFactory(NULL),
_evalFactory(NULL)
{
_backtrackStack = new WolStack();
}
WolMgr::~WolMgr()
{
delete _backtrackStack;
}
WolNodeSptr
WolMgr::findSliceExpr (WolNodeSptr exp) {
std::unordered_set<WolNodeSptr, sliceExpHash, sliceExpEqualTo>::iterator get =
_uniqSliceExpTable.find (exp);
if (get == _uniqSliceExpTable.end ())
return NULL;
return *get;
}
WolNodeSptr
WolMgr::findUniqueExpr (WolNodeSptr exp) {
std::unordered_set<WolNodeSptr, expHash, expEqualTo>::iterator get =
_uniqExpTable.find (exp);
if (get == _uniqExpTable.end ())
return NULL;
return *get;
}
void
WolMgr::insertConstExpr(WolNodeSptr exp) {
_uniqConstTable[exp->getName()] = exp;
}
WolNodeSptr
WolMgr::findConstExpr (std::string name) {
std::unordered_map<std::string, WolNodeSptr>::iterator get =
_uniqConstTable.find (name);
if (get == _uniqConstTable.end ())
return NULL;
return get->second;
}
void
WolMgr::insertIdExpr (WolNodeSptr exp) {
_globalId++;
_idToExpMap[_globalId] = exp;
exp->setId (_globalId);
}
WolNodeSptr
WolMgr::findExpr (int id) {
std::unordered_map<int, WolNodeSptr>::iterator get = _idToExpMap.find (id);
if (get == _idToExpMap.end ())
return NULL;
return get->second;
}
bool
WolMgr::initializeOutputNodes(nodeVec outputs) {
for (auto i : outputs) {
auto one = _valueFactory->makeConstValue("1");
if (!i->getValue()->containsOne())
return false;
i->setValue(one);
}
return true;
}
void
WolMgr::setOutputNodes(nodeVec outputsVec) {
_outputs = outputsVec;
}
void
WolMgr::printgv () {
std::ofstream fs;
fs.open("output.gv");
int numOutputs = _outputs.size ();
fs << "digraph G {" << std::endl;
for (int i = 0; i < numOutputs; i++) {
_outputs[i]->unsetPrintFlag();
}
for (int i = 0; i < numOutputs; i++) {
_outputs[i]->printgv(fs);
}
fs << "}" << std::endl;
fs.close();
}
/*
* assign output nodes to true
* create input and output sets
* initial implication
* loop {
* pick a variable
* // if there are no variable left return sat
* pick a value for the variable
* insert a stack entry
* perform implication
* if (!conflict)
* store changes in stack
* continue;
* else
* loop {
* perform conflict generalization (top entry in stack)
* perform backtrack on variable
* if (successful)
* pick a new value for the variable
* perform implication
* if (!conflict)
* store changes in stack
* break;
* else
* continue;
*
* else
* if stack is not empty
* backtrack to next variable
* else return unsat
* }
* }
*/
bool
WolMgr::solve(nodeVec inputsVec, nodeVec outputsVec) {
DEBUG_MSG << "Initializing Output Nodes";
if (!initializeOutputNodes(outputsVec)) {
std::cout << "Obvious unsatisfiability" << std::endl;
return false;
}
nodeL inputs(inputsVec.begin(), inputsVec.end());
nodeL outputs(outputsVec.begin(), outputsVec.end());
// initial implication
DEBUG_MSG << "Begin Initial Implication";
implResult res = WolImplMgr::getInstance().performImplications(inputs, outputs);
if (res.first == false) {
std::cout << "Initial Implication Failed" << std::endl;
return false;
}
DEBUG_MSG << "End Initial Implication";
while(1) {
//pick a variable
WolNodeSptr pNode = WolVarSelMgr::getInstance().pickNextVariable();
if (!pNode) {
for (auto i : inputs) {
std::cout << i->getName() << " has Value " << i->getValue()->getStringRep() << std::endl;
}
return true;
}
// pick a value for the variable
WolValueSptr pValue = pNode->getRandomValue();
DEBUG_MSG << "picked variable " << pNode->getName()
<< " with value assignment "
<< pValue->getStringRep();
// insert a stack entry
_backtrackStack->pushDecisionNode(pNode);
pNode->setValue(pValue);
res = WolImplMgr::getInstance().performImplications(pNode);
if (res.first) {
DEBUG_MSG << "Implication successful for variable assignment";
_backtrackStack->storeImplications(res.second);
continue;
}
else {
DEBUG_MSG << "Implication failed for variable assignment";
while (1) {
WolNodeSptr currNode = _backtrackStack->getTopNode();
WolValueSptr currValue = currNode->getValue();
WolValueSptr adjustValue =
WolImplMgr::getInstance().performGenarilization(currNode, currValue);
WolValueSptr leftOut = _backtrackStack->backtrackOnCurrentVariable(adjustValue);
if (leftOut) {
currValue = leftOut->getRandomValue();
currNode->setValue(currValue);
DEBUG_MSG << "Backtracking to variable "
<< currNode->getName()
<< " with new assignment value "
<< currValue->getStringRep();
res = WolImplMgr::getInstance().performImplications(currNode);
if (res.first) {
_backtrackStack->storeImplications(res.second);
break;
}
else {
continue;
}
}
else {
DEBUG_MSG << "Backtracking failed on variable "
<< currNode->getName();
if (_backtrackStack->getStackSize() == 1) {
std::cout << "The constraints are unsatisfiable" << std::endl;
return false;
}
else {
_backtrackStack->backtrackToNextVariable();
}
}
}
}
}
std::cout << "The constraints are unsatisfiable" << std::endl;
return false;
}
}