use of com.sri.ai.grinder.api.Constraint in project aic-expresso by aic-sri-international.
the class AbstractSingleVariableConstraintWithBinaryAtomsIncludingEquality method conjoinNonTrivialSignAndNormalizedAtomToConstraintWithBoundVariable.
private AbstractSingleVariableConstraintWithDependentNormalizedAtoms conjoinNonTrivialSignAndNormalizedAtomToConstraintWithBoundVariable(boolean sign, Expression normalizedAtom, Context context) {
Constraint result;
// first, use super's implementation to detect inconsistencies
AbstractSingleVariableConstraintWithDependentNormalizedAtoms conjunctionWithSignAndNormalizedAtom = super.conjoinNonTrivialSignAndNormalizedAtom(sign, normalizedAtom, context);
if (conjunctionWithSignAndNormalizedAtom == null) {
result = makeContradiction();
} else {
// this assumes the original single positive normalized atom stays as the first one in the conjoined constraint
Expression binding = getPositiveNormalizedAtoms().get(0);
// create a fresh constraint with the binding and external literals only
result = makeSimplification(arrayList(binding), arrayList(), getExternalLiterals());
// apply new normalized atom after replacing constraint's variable by its value (making it an external literal)
Expression newExternalLiteral = rewriteSignAndNormalizedAtomForValueVariableIsBoundTo(sign, normalizedAtom, binding.get(1), context);
result = result.conjoinWithLiteral(newExternalLiteral, context);
}
return (AbstractSingleVariableConstraintWithDependentNormalizedAtoms) result;
// Note: a simpler, more expensive version of this method could create an empty constraint,
// conjoin it with the binding, with each external literal, and the new normalized atom, converted to external literal,
// as opposed to using makeRefinementWith with all external literals at once.
// That solution would require the application of external literals one by one, however, whereas the above just copies them all at once.
}
use of com.sri.ai.grinder.api.Constraint in project aic-expresso by aic-sri-international.
the class CompoundTheoryWithDifferenceArithmeticTest method basicTests.
@Test
public void basicTests() {
TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(makeRandom(), new CompoundTheory(new EqualityTheory(false, true), new DifferenceArithmeticTheory(false, true), new PropositionalTheory()));
Expression condition = parse("X = Y and Y = X and P and not Q and P and X = a and X != b");
Context context = theoryTestingSupport.makeContextWithTestingInformation();
Constraint constraint = new CompleteMultiVariableContext(theoryTestingSupport.getTheory(), context);
constraint = constraint.conjoin(condition, context);
Expression expected = parse("(Y = a) and not Q and P and (X = Y)");
assertEquals(expected, constraint);
// nested indices
Expression expression = parse("sum({{(on I in 1..2, J in 2..3) sum({{ (on I in 1..10, J in 1..2) I + J : I != J }}) }})");
context = new TrueContext(theoryTestingSupport.getTheory());
expected = parse("536");
Expression actual = theoryTestingSupport.getTheory().evaluate(expression, context);
println(actual);
assertEquals(expected, actual);
}
use of com.sri.ai.grinder.api.Constraint 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.api.Constraint in project aic-expresso by aic-sri-international.
the class AbstractSingleQuantifierEliminationStepSolver method resultIfLiteralContainsIndex.
protected Step resultIfLiteralContainsIndex(Expression literal, ExpressionLiteralSplitterStepSolver.Step bodyStep, Context contextForBody, Context context) {
// if the splitter contains the index, we must split the quantifier:
// Quant_x:C Body ---> (Quant_{x:C and L} Body) op (Quant_{x:C and not L} Body)
Step result;
Expression solutionValue;
// Here, we need to obtain the new index constraints, for the case in which the splitter literal is true and false,
// to create the corresponding sub-problems, solve them, and combine them.
// However, it is important to remember that bodyStep.getContextSplittingWhenSplitterIsLiteral()
// contains the splitting of contextForBody with the splitter literal,
// so the information on the new index constraints is already there in some form.
// TODO: We current don't have a Constraint-generic way to extract it, but expect to do it in the future.
// For now, we split the index constraint separately
ConstraintSplitting indexConstraintSplitting = new ConstraintSplitting(literal, getIndexConstraint(), context);
Constraint indexConstraintAndLiteral = indexConstraintSplitting.getConstraintAndLiteral();
Constraint indexConstraintAndLiteralNegation = indexConstraintSplitting.getConstraintAndLiteralNegation();
switch(indexConstraintSplitting.getResult()) {
case CONSTRAINT_IS_CONTRADICTORY:
solutionValue = null;
break;
case LITERAL_IS_UNDEFINED:
// (**) IF DELETING THIS MARKER, DELETE ALL THE REFERENCES TO IT IN THIS FILE
// This is where this step solver may return a Solution with literals in it:
// solveSubProblem uses an exhaustive solve.
solutionValue = solveSubProblems(makeSubProblem(true, bodyStep, indexConstraintAndLiteral), makeSubProblem(false, bodyStep, indexConstraintAndLiteralNegation), context);
break;
case LITERAL_IS_TRUE:
solutionValue = solveSubProblem(makeSubProblem(true, bodyStep, indexConstraintAndLiteral), context);
break;
case LITERAL_IS_FALSE:
solutionValue = solveSubProblem(makeSubProblem(false, bodyStep, indexConstraintAndLiteralNegation), context);
break;
default:
throw new Error("Unrecognized result for " + ConstraintSplitting.class + ": " + indexConstraintSplitting.getResult());
}
if (solutionValue == null) {
result = null;
} else {
result = new Solution(solutionValue);
}
return result;
}
use of com.sri.ai.grinder.api.Constraint in project aic-expresso by aic-sri-international.
the class AbstractSingleQuantifierEliminationStepSolver method splitOnDefinedSplitter.
private Step splitOnDefinedSplitter(ExpressionLiteralSplitterStepSolver.Step bodyStep, ConstraintSplitting indexConstraintSplitting, Context context) {
boolean splitterValue = indexConstraintSplitting.getResult() == ConstraintSplitting.Result.LITERAL_IS_TRUE;
explain("Index literal ", bodyStep.getSplitter(), " is always " + splitterValue + " under current context, so we will solve a single sub-problem");
Constraint indexConstrainedConjoinedWithSplitter = indexConstraintSplitting.getConstraintAndLiteralEqualTo(splitterValue);
Step step = stepOnSubProblemIfSplitterIs(splitterValue, bodyStep, indexConstrainedConjoinedWithSplitter, context);
return step;
}
Aggregations