use of com.sri.ai.grinder.sgdpllt.api.Context in project aic-expresso by aic-sri-international.
the class SGDPLLTTester method testMultiVariableConstraints.
/**
* Given a theory and a number <code>n</code> of multi-variable constraint tests,
* generates <code>n</code> formulas in the theory
* and see if those detected as unsatisfiable by the corresponding solver
* are indeed unsatisfiable (checked by brute force).
* Throws an {@link Error} with the failure description if a test fails.
* @param theoryTestingSupport
* @param numberOfTests
* @param maxNumberOfLiterals
* @param outputCount
*/
public static void testMultiVariableConstraints(boolean testAgainstBruteForce, TheoryTestingSupport theoryTestingSupport, long numberOfTests, int maxNumberOfLiterals, boolean outputCount) {
NullaryFunction<Constraint> makeInitialConstraint = () -> new DefaultMultiVariableConstraint(theoryTestingSupport.getTheory());
Context context = theoryTestingSupport.makeContextWithTestingInformation();
Function<Constraint, Expression> makeRandomLiteral = c -> theoryTestingSupport.makeRandomLiteral(context);
// DefaultMultiVariableConstraint is incomplete
TestRunner tester = SGDPLLTTester::testIncompleteSatisfiability;
runTesterGivenOnSuccessiveConjunctionsOfLiterals("incomplete satisfiability", tester, numberOfTests, maxNumberOfLiterals, testAgainstBruteForce, theoryTestingSupport, makeInitialConstraint, makeRandomLiteral, outputCount, context);
}
use of com.sri.ai.grinder.sgdpllt.api.Context in project aic-expresso by aic-sri-international.
the class SGDPLLTTester method runModelCountingTestForSingleVariableConstraint.
private static void runModelCountingTestForSingleVariableConstraint(Expression variable, Collection<Expression> literals, Constraint constraint, boolean testAgainstBruteForce, Theory theory, Context context) {
Expression literalsConjunction = And.make(literals);
String problemDescription = "model counting of " + literalsConjunction + " for variable " + variable;
output("Problem: " + problemDescription);
Simplifier symbolicSolver = (e, p) -> computeModelCountBySolver((SingleVariableConstraint) e, p);
SingleVariableConstraint singleVariableConstraint = (SingleVariableConstraint) constraint;
Expression symbolicSolution = symbolicSolver.apply(singleVariableConstraint, context);
if (Util.thereExists(new SubExpressionsDepthFirstIterator(symbolicSolution), e -> e instanceof QuantifiedExpression || Sets.isIntensionalSet(e))) {
throw new Error("Symbolic solution is not quantifier-free: " + symbolicSolution);
}
output("Symbolic result: " + symbolicSolution);
if (testAgainstBruteForce) {
if (singleVariableConstraint.isContradiction()) {
if (!symbolicSolution.equals(ZERO)) {
throw new Error("Constraint is contradiction, but symbolic solver does not produce 0, but instead " + symbolicSolution);
}
} else {
Expression testingVariable = singleVariableConstraint.getVariable();
Set<Expression> allVariables = getVariableReferences(singleVariableConstraint, context);
Collection<Expression> otherVariables = removeFromSetNonDestructively(allVariables, v -> v.equals(testingVariable));
BinaryFunction<BruteForceCommonInterpreter, Context, Expression> fromInterpreterAndContextWithAssignmentToOtherVariablesToBruteForceSolution = (interpreter, contextWithAssignmentToOtherVariables) -> bruteForceModelCounterForVariableGivenInterpreterAndAssignmentToOtherVariables(variable, literalsConjunction, interpreter, theory, contextWithAssignmentToOtherVariables);
testSymbolicVsBruteForceComputationForEachAssignment(theory, problemDescription, otherVariables, symbolicSolution, fromInterpreterAndContextWithAssignmentToOtherVariablesToBruteForceSolution, context);
}
} else {
output("Skipping test againt brute-force.");
}
}
use of com.sri.ai.grinder.sgdpllt.api.Context in project aic-expresso by aic-sri-international.
the class SGDPLLTTester method bruteForceModelCounterForVariableGivenInterpreterAndAssignmentToOtherVariables.
private static Expression bruteForceModelCounterForVariableGivenInterpreterAndAssignmentToOtherVariables(Expression variable, Expression conjunction, BruteForceCommonInterpreter interpreter, Theory theory, Context context) {
output("Computing model count by brute force of: " + conjunction);
int modelCount = 0;
Expression testingVariable = variable;
AssignmentsIterator testingVariableAssignmentsIterator = new AssignmentsIterator(list(testingVariable), context);
for (Map<Expression, Expression> testingVariableAssignment : in(testingVariableAssignmentsIterator)) {
Context extendedContext = AbstractIterativeMultiIndexQuantifierElimination.extendAssignments(testingVariableAssignment, context);
Expression value = interpreter.apply(conjunction, extendedContext);
if (value.equals(TRUE)) {
modelCount++;
}
// output("For " + completeInterpreter.getAssignment() + ",");
// output("value is " + value);
// output("Model count is " + modelCount + "\n");
}
return makeSymbol(modelCount);
}
use of com.sri.ai.grinder.sgdpllt.api.Context in project aic-expresso by aic-sri-international.
the class SGDPLLTTester method testSingleVariableConstraints.
/**
* Given a theory and a number <code>n</code> of single-variable constraint tests,
* generates <code>n</code> formulas in the theory
* and see if those detected as unsatisfiable by the corresponding solver
* are indeed unsatisfiable (checked by brute force).
* Throws an {@link Error} with the failure description if a test fails.
* @param theoryTestingSupport
* @param numberOfTests
* @param maxNumberOfLiterals
* @param outputCount
*/
public static void testSingleVariableConstraints(boolean testAgainstBruteForce, TheoryTestingSupport theoryTestingSupport, long numberOfTests, int maxNumberOfLiterals, boolean outputCount) {
Context context = theoryTestingSupport.makeContextWithTestingInformation();
NullaryFunction<Constraint> makeInitialConstraint = () -> theoryTestingSupport.getTheory().makeSingleVariableConstraint(parse(theoryTestingSupport.pickTestingVariableAtRandom()), theoryTestingSupport.getTheory(), context);
Function<Constraint, Expression> makeRandomLiteral = c -> theoryTestingSupport.makeRandomLiteralOn(((SingleVariableConstraint) c).getVariable().toString(), context);
boolean isComplete = theoryTestingSupport.getTheory().singleVariableConstraintIsCompleteWithRespectToItsVariable();
TestRunner tester = isComplete ? SGDPLLTTester::testCompleteSatisfiability : SGDPLLTTester::testIncompleteSatisfiability;
String problemName = (isComplete ? "complete" : "incomplete") + " satisfiability for single-variable constraints";
runTesterGivenOnSuccessiveConjunctionsOfLiterals(problemName, tester, numberOfTests, maxNumberOfLiterals, testAgainstBruteForce, theoryTestingSupport, makeInitialConstraint, makeRandomLiteral, outputCount, context);
}
use of com.sri.ai.grinder.sgdpllt.api.Context in project aic-expresso by aic-sri-international.
the class IntersectionIntensionalSetsSimplifier method simplify.
public static Expression simplify(Expression expression, Context context) {
Expression result = expression;
if (expression.hasFunctor(FunctorConstants.INTERSECTION)) {
List<Expression> intensionalMultiSetArgs = new ArrayList<>();
List<Expression> nonIntensionalMultiSetArgs = new ArrayList<>();
for (Expression arg : expression.getArguments()) {
if (Sets.isIntensionalMultiSet(arg)) {
intensionalMultiSetArgs.add(arg);
} else {
nonIntensionalMultiSetArgs.add(arg);
}
}
if (intensionalMultiSetArgs.size() > 1) {
boolean resultIsEmptySet = false;
IntensionalSet intersectedMultiSet = (IntensionalSet) intensionalMultiSetArgs.get(0);
for (int i = 1; i < intensionalMultiSetArgs.size() && !resultIsEmptySet; i++) {
IntensionalSet otherMultiSet = standardizeApartIntensionalSets((IntensionalSet) intensionalMultiSetArgs.get(i), intersectedMultiSet, context);
// {{ (on I1) H1 : C1 }} intersection {{ (on I2) H2 : C2 }}
// ---->
// {{ (on I1) H1 : C1 and evaluate(there exists I2 : C2 and H2 = H1) }}
IndexExpressionsSet i1 = intersectedMultiSet.getIndexExpressions();
IndexExpressionsSet i2 = otherMultiSet.getIndexExpressions();
Expression h1 = intersectedMultiSet.getHead();
Expression h2 = otherMultiSet.getHead();
Expression c1 = intersectedMultiSet.getCondition();
Expression c2 = otherMultiSet.getCondition();
Expression thereExists = ThereExists.make(i2, And.make(c2, Equality.make(h2, h1)));
Context i1ExtendedContext = context.extendWith(i1);
Expression thereExistsEvaluated = context.getTheory().evaluate(thereExists, i1ExtendedContext);
if (thereExistsEvaluated.equals(false)) {
// They don't intersect, which means you have an empty
// set in the intersection, which means the whole thing
// results in the empty set.
resultIsEmptySet = true;
} else if (!thereExistsEvaluated.equals(true)) {
// If we have a condition, other than false and true
// we will want to extend the current result by the condition
Expression extendedCondition = And.make(c1, thereExistsEvaluated);
intersectedMultiSet = (IntensionalSet) IntensionalSet.intensionalMultiSet(i1, h1, extendedCondition);
// Ensure we don't have a false condition.
Expression simplifiedIntersectedMultiSet = context.getTheory().evaluate(intersectedMultiSet, context);
if (Sets.isEmptySet(simplifiedIntersectedMultiSet)) {
resultIsEmptySet = true;
}
}
}
if (resultIsEmptySet) {
result = Sets.EMPTY_SET;
} else if (nonIntensionalMultiSetArgs.size() > 0) {
List<Expression> intersectedArgs = new ArrayList<>();
intersectedArgs.add(intersectedMultiSet);
intersectedArgs.addAll(nonIntensionalMultiSetArgs);
result = Sets.makeIntersection(intersectedArgs.toArray(new Expression[intersectedArgs.size()]));
} else {
result = intersectedMultiSet;
}
}
}
return result;
}
Aggregations