-
Notifications
You must be signed in to change notification settings - Fork 0
/
satforhuman.js
196 lines (136 loc) · 4.5 KB
/
satforhuman.js
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
var tablePropositionToNumber = [];
var tableNumberToProposition = [];
var currentNumberOfPropositions = 1;
function getPrettyPrintFromLiteralNumber(literalNumber)
{
if(literalNumber > 0)
return tableNumberToProposition[literalNumber];// + "_{" + literalNumber + "}";
else
return "¬" + tableNumberToProposition[-literalNumber] + "";// + "_{" + literalNumber + "}";
}
function getPrettyPrintFromClauseWithNumber(clauseWithNumber)
{
if(clauseWithNumber === null)
return "NULL";
if(clauseWithNumber === [])
return "bottom";
else
{
var s = "(";
for(var i = 0; i < clauseWithNumber.length - 1; i++)
s += getPrettyPrintFromLiteralNumber(clauseWithNumber[i]) + " ∨ ";
s += getPrettyPrintFromLiteralNumber(clauseWithNumber[clauseWithNumber.length - 1]) + ")";
return s;
}
}
function getNumberForProposition(proposition)
{
if(tablePropositionToNumber[proposition] === undefined)
{
tablePropositionToNumber[proposition] = currentNumberOfPropositions;
tableNumberToProposition[currentNumberOfPropositions] = proposition;
currentNumberOfPropositions++;
}
return tablePropositionToNumber[proposition];
}
function getNumberForLitteral(litteral)
{
if(litteral instanceof Array)
{
if(litteral[0] === "not")
{
return -getNumberForProposition(litteral[1]);
}
else
{
return getNumberForProposition(litteral);
}
}
else
{
return getNumberForProposition(litteral);
}
}
function getListOfChosenLiteralsForSolver(listOfChosenLiterals)
{
var listOfChosenLiteralsSolver = [];
for(i in listOfChosenLiterals)
{
listOfChosenLiteralsSolver.push(getNumberForLitteral(listOfChosenLiterals[i]));
}
return listOfChosenLiteralsSolver;
}
function getClausesForSolverSub(clauseScheme)
{
var clauseSolver = [];
for(i in clauseScheme)
//if(i % 2 == 0)
{
clauseSolver.push(getNumberForLitteral(clauseScheme[i]));
}
return clauseSolver;
}
function getClausesForSolver(formulaCNFScheme)
{
var inputSolver = [];
tablePropositionToNumber = [];
tableNumberToProposition = [];
currentNumberOfPropositions = 1;
for(i in formulaCNFScheme)
//if(i % 2 == 0)
{
if(formulaCNFScheme[i] instanceof Array)
inputSolver.push(getClausesForSolverSub(formulaCNFScheme[i]));
}
return inputSolver;
}
/*input: a string like "(phi1 phi2 phi3 ...)" where phii are formulas like "(bigand...)"
output: the CNF for the solver like ((p a)) for "p or a" or
((p) (a b)) for "p and (a or b)"
*/
function satforHuman_formulagetExpanded(phi)
{
//return scheme.eval("(formulatocnf (formula-expand (make-conjunction " + "(quote (" + phi.replace(/\\n/, ' ') + ")))))");
return scheme.eval("(list* (map (lambda (x) (formula-expand x)) (quote (" + phi.replace(/\\n/, ' ') + "))))");
}
/*input: a string like "(phi1 phi2 phi3 ...)" where phii are formulas like "(bigand...)"
output: the CNF for the solver like ((p a)) for "p or a" or
((p) (a b)) for "p and (a or b)"
*/
function satforHuman_formulagetExpandedCNF(phi)
{
//return scheme.eval("(formulatocnf (formula-expand (make-conjunction " + "(quote (" + phi.replace(/\\n/, ' ') + ")))))");
return scheme.eval("(list* (map (lambda (x) (formulatocnf (formula-expand x))) (quote (" + phi.replace(/\\n/, ' ') + "))))");
}
//scheme.prettyprint(satforHuman_getValuation(satforHuman_formulagetExpandedCNF("((p or (not t)) and (q or t) and t)")))
//scheme.prettyprint(satforHuman_getValuation([[["not", "a"], "b"], "and", ["a"]]))
function satforHuman_getValuation(formulaCNFScheme, listOfChosenLiterals)
{
if(listOfChosenLiterals === undefined)
listOfChosenLiterals = [];
var clausesForSolver = getClausesForSolver(formulaCNFScheme);
var listOfChosenLiteralsForSolver = getListOfChosenLiteralsForSolver(listOfChosenLiterals);
//alert(clausesForSolver);
//alert(currentNumberOfPropositions-1);
//alert(listOfChosenLiteralsForSolver);
if(currentNumberOfPropositions >= 20)
{
UIDPLLoutputenabled = false;
UIpostMessage = function () {};
}
var result = satSolve(currentNumberOfPropositions-1, clausesForSolver, listOfChosenLiteralsForSolver);
if(result === false)
return false;
else
{
var valuation = [];
for(var i = 1; i <= currentNumberOfPropositions; i++)
{
if(result.vars[i] != undefined)
if(!result.vars[i].sign)
if((tableNumberToProposition[i] != undefined) && !(tableNumberToProposition[i][0] == '_'))
valuation.push(tableNumberToProposition[i]);
}
return valuation;
}
}