Skip to content

Commit 83fc698

Browse files
committed
Started implementing MSS coverage verification
1 parent 9a48676 commit 83fc698

File tree

4 files changed

+147
-20
lines changed

4 files changed

+147
-20
lines changed

TrivialSpec.cc

+14
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@ void TrivialSpec::forEach(function<void(BVar, const CNFClause&)> visitor) const
1818
visitor(_defined[i], _negDefinitions[i]);
1919
}
2020

21+
Set<BVar> TrivialSpec::eval(const Set<BVar>& assignment) const
22+
{
23+
Set<BVar> outputAssignment;
24+
25+
forEach([&] (BVar var, const CNFClause& negDefinition)
26+
{
27+
/* Add output variable to the assignment if the negation of its definition evaluates to false */
28+
if (!negDefinition.eval(assignment))
29+
outputAssignment.insert(var);
30+
});
31+
32+
return outputAssignment;
33+
}
34+
2135
Graph<size_t> TrivialSpec::conflictGraph() const
2236
{
2337
/* Maps every x literal to the indices of the clauses where it appears. */

TrivialSpec.hpp

+20-15
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,29 @@
1313
*/
1414
class TrivialSpec
1515
{
16-
Vector<BVar> _defined; /**< z_1, z_2, ..., z_n */
17-
Vector<CNFClause> _negDefinitions; /**< X_1, X_2, ..., X_n */
16+
Vector<BVar> _defined; /**< z_1, z_2, ..., z_n */
17+
Vector<CNFClause> _negDefinitions; /**< X_1, X_2, ..., X_n */
1818

1919
public:
2020

21-
TrivialSpec(Vector<BVar> defined, Vector<CNFClause> negDefinitions);
21+
TrivialSpec(Vector<BVar> defined, Vector<CNFClause> negDefinitions);
2222

23-
/**
24-
* Iterates over definitions, executing the visitor function for each.
25-
* Example: For a definition of the form (z <-> ~(l_1 | ... | l_k)),
26-
* the inputs given to the visitor would be (z, (l_1, ..., l_k)).
27-
*/
28-
void forEach(std::function<void(BVar, const CNFClause&)> visitor) const;
23+
/**
24+
* Iterates over definitions, executing the visitor function for each.
25+
* Example: For a definition of the form (z <-> ~(l_1 | ... | l_k)),
26+
* the inputs given to the visitor would be (z, (l_1, ..., l_k)).
27+
*/
28+
void forEach(std::function<void(BVar, const CNFClause&)> visitor) const;
2929

30-
/**
31-
* Returns graph where:
32-
* - Vertex i represents clause X_i;
33-
* - There is an edge between two vertices iff the clauses have opposite literals.
34-
*/
35-
Graph<size_t> conflictGraph() const;
30+
/**
31+
* Returns graph where:
32+
* - Vertex i represents clause X_i;
33+
* - There is an edge between two vertices iff the clauses have opposite literals.
34+
*/
35+
Graph<size_t> conflictGraph() const;
36+
37+
/**
38+
* Evaluates the function given by the specification on an assignment to the input variables.
39+
*/
40+
Set<BVar> eval(const Set<BVar>& assignment) const;
3641
};

Verifier.cc

+110-2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,114 @@ bool Verifier::VerifyList()
6262
return true;
6363
}
6464

65-
//bool VerifyInputCover();
65+
bool forAllAssignments(Set<BVar>& fixed,
66+
Set<BVar>& remaining,
67+
function<bool(const Set<BVar>&) callback)
68+
{
69+
if (remaining.empty())
70+
{
71+
return callback(fixed);
72+
}
73+
else
74+
{
75+
BVar pivot = *remaining.begin();
76+
77+
remaining.erase(remaining.begin());
78+
79+
bool result = forAllAssignments(fixed, remaining, callback);
80+
81+
if (result)
82+
{
83+
fixed.insert(pivot);
84+
85+
result = forAllAssignments(fixed, remaining, callback);
86+
87+
fixed.erase(fixed.find(pivot));
88+
}
89+
90+
remaining.insert(pivot);
91+
92+
return result;
93+
}
94+
}
95+
96+
bool VerifyInputCover() const
97+
{
98+
Set<BVar> inputVars = f.inputVars();
99+
Set<BVar> potentialAssignment;
100+
101+
forAllAssignments(inputVars, potentialAssignment,
102+
[&f1] (const Set<BVar>& assignment) {
103+
104+
}
105+
106+
/**
107+
* Uses the given RNG to select a random subset of the given set of variables.
108+
*/
109+
Set<BVar> randomSubset(const Set<BVar>& vars,
110+
const mt19937& rng)
111+
{
112+
/* Generates booleans with equal probability */
113+
bernoulli_distribution dist(0.5);
114+
115+
Set<BVar> subset;
116+
117+
for (BVar var : vars)
118+
{
119+
if (dist(rng))
120+
subset.insert(var);
121+
}
66122

67-
//bool RandomVerifyInputCover();
123+
return subset;
124+
}
125+
126+
127+
128+
bool RandomVerifyInputCover() const
129+
{
130+
cout << "=== Verifying coverage ===" << endl;
131+
132+
/* Initialize random generation */
133+
random_device rd;
134+
mt19937 rng(rd());
135+
136+
size_t sampleSize = 100; // number of sample assignments taken
137+
bool ok = true; // is set to false when verification fails
138+
139+
for (size_t i = 0; i < sampleSize; i++)
140+
{
141+
/* Generates random assignment to the x variables */
142+
Set<BVar> inputAssignment = randomSubset(f.inputVars(), rng);
143+
144+
cout << "Random input: ";
145+
print(inputAssignment, "x");
146+
cout << endl;
147+
148+
/* Set of z variables activated by the given input */
149+
Set<BVar> outputAssignment = f1.eval(inputAssignment);
150+
151+
cout << "Activated variables: ";
152+
print(outputAssignment, "z");
153+
cout << endl;
154+
155+
bool inputIsCovered = false;
156+
157+
/* Look for an MSS that covers the assignment */
158+
for (const Set<BVar>& mss : mssList)
159+
{
160+
if (isSubset(outputAssignment, mss))
161+
{
162+
cout << "MSS covering this input: ";
163+
print(setDifference(mss, spec.outputVars()), "z"); // remove y variables from MSS for printing
164+
cout << endl;
165+
166+
inputIsCovered = true;
167+
break;
168+
}
169+
}
170+
171+
ok &= inputIsCovered;
172+
}
173+
174+
return ok;
175+
}

Verifier.hpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ Verifier(Vector<Set<BVar>> mssList, CNFSpec f);
5454
//The following method verify that each MSS is set to the correct assignment. It does it as follows: for every pair <M,a> of <MSS,assignment> in the list the method checks that the assignment satisfies the clauses in the MSS.
5555
bool VerifyList();
5656

57-
//bool VerifyInputCover();
57+
bool VerifyInputCover();
5858

59-
//bool RandomVerifyInputCover();
60-
};
59+
bool RandomVerifyInputCover();
60+
};

0 commit comments

Comments
 (0)