use of com.sri.ai.grinder.api.SingleVariableConstraint 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()), 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.api.SingleVariableConstraint in project aic-expresso by aic-sri-international.
the class SGDPLLTTester method runGroupProblemSolvingTesterForSuccessiveConstraints.
private static void runGroupProblemSolvingTesterForSuccessiveConstraints(String problemName, TestRunner tester, boolean testAgainstBruteForce, AssociativeCommutativeGroup group, TheoryTestingSupport theoryTestingSupport, long numberOfTests, int maxNumberOfLiterals, boolean outputCount) throws Error {
Context context = theoryTestingSupport.makeContextWithTestingInformation();
NullaryFunction<Constraint> makeInitialConstraint = () -> theoryTestingSupport.getTheory().makeSingleVariableConstraint(parse(theoryTestingSupport.pickTestingVariableAtRandom()), context);
Function<Constraint, Expression> makeRandomLiteral = c -> theoryTestingSupport.makeRandomLiteralOn(((SingleVariableConstraint) c).getVariable().toString(), context);
runTesterGivenOnSuccessiveConjunctionsOfLiterals(problemName, tester, numberOfTests, maxNumberOfLiterals, testAgainstBruteForce, theoryTestingSupport, makeInitialConstraint, makeRandomLiteral, outputCount, context);
}
use of com.sri.ai.grinder.api.SingleVariableConstraint in project aic-expresso by aic-sri-international.
the class MultiVariableContextWithCheckedProperty method conjoinWithLiteral.
@Override
public Context conjoinWithLiteral(Expression literal, Context context) {
Context result;
if (literal.equals(TRUE)) {
result = this;
} else if (literal.equals(FALSE)) {
result = makeContradiction();
} else {
Collection<Expression> variablesInLiteral = getTheory().getVariablesIn(literal, context);
if (variablesInLiteral.isEmpty()) {
Expression literalSimplifiedToConstant = getTheory().simplify(literal, context);
if (literalSimplifiedToConstant == literal) {
throw new Error("Literal " + literal + " should have been simplified to a boolean constant, but was not. Sometimes this is caused by using a symbol as a variable, but which has not been declared as a variable in the context, or has been declared as a uniquely named constant in the Context (for example by constructing the Context with the default PrologConstantPredicate as a default predicate for recognizing constants, which recognizes all non-capitalized identifiers as such)");
}
result = conjoinWithLiteral(literalSimplifiedToConstant, context);
} else if (head != null) {
SingleVariableConstraint newHead;
Context newTail;
if (variablesInLiteral.contains(head.getVariable())) {
newHead = head.conjoin(literal, context);
newTail = tail;
} else {
newHead = head;
newTail = tail.conjoin(literal, context);
}
// up the chain so they are integrated and simplified in the corresponding single-variable constraints
if (!newHead.isContradiction()) {
for (Expression externalLiteral : newHead.getExternalLiterals()) {
if (!newTail.isContradiction()) {
newTail = newTail.conjoin(externalLiteral, context);
}
}
newHead = newHead.makeSimplificationWithoutExternalLiterals();
}
if (newHead == head && newTail == tail) {
// in case nothing changed
result = this;
} else {
result = makeAndCheck(getTheory(), newHead, newTail, contextDependentProblemStepSolverMaker, context);
}
} else {
Expression firstVariable = getFirstOrNull(variablesInLiteral);
SingleVariableConstraint newSingleVariableConstraint = getTheory().makeSingleVariableConstraint(firstVariable, context);
newSingleVariableConstraint = newSingleVariableConstraint.conjoin(literal, context);
result = makeAndCheck(getTheory(), newSingleVariableConstraint, this, contextDependentProblemStepSolverMaker, context);
}
}
return result;
}
use of com.sri.ai.grinder.api.SingleVariableConstraint in project aic-expresso by aic-sri-international.
the class AbstractSingleQuantifierEliminationStepSolver method makeSubProblem.
protected AbstractSingleQuantifierEliminationStepSolver makeSubProblem(boolean valueForLiteral, ExpressionLiteralSplitterStepSolver.Step bodyStep, Constraint newIndexConstraint) {
SingleVariableConstraint newIndexConstraintAsSingleVariableConstraint = (SingleVariableConstraint) newIndexConstraint;
AbstractSingleQuantifierEliminationStepSolver result = makeWithNewIndexConstraint(newIndexConstraintAsSingleVariableConstraint);
result.initialBodyEvaluationStepSolver = valueForLiteral ? bodyStep.getStepSolverForWhenSplitterIsTrue() : bodyStep.getStepSolverForWhenSplitterIsFalse();
result.initialContextForBody = valueForLiteral ? bodyStep.getContextSplittingWhenSplitterIsLiteral().getConstraintAndLiteral() : bodyStep.getContextSplittingWhenSplitterIsLiteral().getConstraintAndLiteralNegation();
return result;
}
use of com.sri.ai.grinder.api.SingleVariableConstraint in project aic-expresso by aic-sri-international.
the class ConjoinedContext method conjoinTrueContextWithLiteralContainingVariables.
private static Context conjoinTrueContextWithLiteralContainingVariables(Expression literal, Collection<Expression> variablesInLiteral, TrueContext trueContext, ConjoinedContextPropertyCheckerStepSolverMaker contextDependentProblemStepSolverMaker, Theory theory) {
Context result;
SingleVariableConstraint head = theory.makeNewSingleVariableConstraintOnSomeVariableOfLiteral(literal, variablesInLiteral, trueContext);
if (head.isContradiction()) {
result = trueContext.makeContradiction();
} else {
result = makeAndCheck(theory, head, trueContext, contextDependentProblemStepSolverMaker, trueContext);
}
return result;
}
Aggregations