-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSATEvaluation.cpp
More file actions
78 lines (70 loc) · 2.31 KB
/
SATEvaluation.cpp
File metadata and controls
78 lines (70 loc) · 2.31 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
//
// Created by Dominik Plíšek on 22/01/16.
//
#include "SATEvaluation.h"
SATEvaluation::SATEvaluation(SATInstance *instance) : instance(instance) {
weight = 0;
satisfiedClauseCount = 0;
values = new bool[instance->variableCount];
clausesTrue = new bool[instance->clauseCount];
}
SATEvaluation::~SATEvaluation() {
delete [] values;
delete [] clausesTrue;
}
void SATEvaluation::randomize() {
satisfiedClauseCount = 0;
weight = 0;
for (int i = 0; i < instance->variableCount; ++i) {
values[i] = (bool) (rand() % 2);
if (values[i]) weight += instance->weights[i];
}
for (int j = 0; j < instance->clauseCount; ++j) {
SATClause *clause = instance->clauses[j];
bool clauseTrue = isClauseTrue(clause);
clausesTrue[j] = clauseTrue;
if (clauseTrue) satisfiedClauseCount++;
}
}
bool SATEvaluation::isClauseTrue(const SATClause *clause) const {
bool clauseTrue = false;
for (int i = 0; i < instance->clauseSize; ++i) {
bool variableValue = values[clause->variables[i]];
clauseTrue = clauseTrue || clause->positive[i] == variableValue;
}
return clauseTrue;
}
void SATEvaluation::toggleVariable(int index) {
if (values[index]) {
weight -= instance->weights[index];
} else {
weight += instance->weights[index];
}
values[index] = !values[index];
for (vector<SATClause *>::iterator it = instance->clausePresence[index].begin(); it != instance->clausePresence[index].end(); ++it) {
SATClause *clause = *it;
bool clauseTrue = isClauseTrue(clause);
if (clausesTrue[clause->index] != clauseTrue) {
clausesTrue[clause->index] = clauseTrue;
if (clauseTrue) {
satisfiedClauseCount++;
} else {
satisfiedClauseCount--;
}
}
}
}
SATEvaluation &SATEvaluation::operator=(const SATEvaluation &other) {
for (int i = 0; i < instance->variableCount; ++i) {
values[i] = other.values[i];
}
for (int j = 0; j < instance->clauseCount; ++j) {
clausesTrue[j] = other.clausesTrue[j];
}
satisfiedClauseCount = other.satisfiedClauseCount;
weight = other.weight;
return *this;
}
bool SATEvaluation::isSatisfied() {
return satisfiedClauseCount == instance->clauseCount;
}