use of com.sri.ai.grinder.sgdpllt.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.sgdpllt.api.Constraint in project aic-expresso by aic-sri-international.
the class ValuesOfSingleVariableDifferenceArithmeticConstraintStepSolverTest method runTest.
private void runTest(Expression variable, String constraintString, Expression expected, Context context) {
Constraint constraint = new SingleVariableDifferenceArithmeticConstraint(variable, true, context.getTheory());
constraint = constraint.conjoin(parse(constraintString), context);
ExpressionLiteralSplitterStepSolver stepSolver = new ValuesOfSingleVariableDifferenceArithmeticConstraintStepSolver((SingleVariableDifferenceArithmeticConstraint) constraint);
Expression actual = stepSolver.solve(context);
System.out.println("Variable " + variable + "\nhas possible values:\n" + actual + "\nsatisfying constraint:\n" + constraintString + "\n");
assertEquals(expected, actual);
}
use of com.sri.ai.grinder.sgdpllt.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.sgdpllt.api.Constraint in project aic-expresso by aic-sri-international.
the class AbstractSingleVariableConstraintWithBinaryAtomsIncludingEquality method conjoinNonTrivialNormalizedEqualityToConstraintWithNonBoundVariable.
protected AbstractSingleVariableConstraintWithDependentNormalizedAtoms conjoinNonTrivialNormalizedEqualityToConstraintWithNonBoundVariable(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 {
Expression binding = normalizedAtom;
Expression valueVariableIsBoundTo = binding.get(1);
// create a fresh constraint with the binding only and external literals
result = makeSimplification(arrayList(binding), arrayList(), getExternalLiterals());
// convert all other normalized atoms to external literals with valueVariableIsBoundTo standing for constraint's variable
result = conjoinWithSignAndNormalizedAtomsOnValueVariableIsBoundTo(result, true, in(getPositiveNormalizedAtomsIncludingImplicitOnes(context)), valueVariableIsBoundTo, context);
if (result != null) {
result = conjoinWithSignAndNormalizedAtomsOnValueVariableIsBoundTo(result, false, in(getNegativeNormalizedAtomsIncludingImplicitOnes(context)), valueVariableIsBoundTo, context);
}
}
return (AbstractSingleVariableConstraintWithDependentNormalizedAtoms) result;
}
use of com.sri.ai.grinder.sgdpllt.api.Constraint in project aic-expresso by aic-sri-international.
the class SGDPLLTTester method runGroupProblemSolvingTestGivenConstraintAndProblem.
/**
* @param problem
* @param indices
* @param constraint
* @param body
* @param testAgainstBruteForce
* @param theoryTestingSupport
* @param context
* @throws Error
*/
public static void runGroupProblemSolvingTestGivenConstraintAndProblem(Expression problem, Collection<Expression> indices, Constraint constraint, Expression body, boolean testAgainstBruteForce, TheoryTestingSupport theoryTestingSupport, Context context) throws Error {
Theory theory = theoryTestingSupport.getTheory();
Collection<Expression> freeVariables = getFreeVariableMinusIndices(indices, constraint, body, context);
String problemDescription = problem.toString();
output(problemDescription);
Simplifier symbolicInterpreter = (e, c) -> theory.evaluate(e, c);
long start = System.currentTimeMillis();
Expression symbolicSolution = symbolicInterpreter.apply(problem, context);
long time = System.currentTimeMillis() - start;
output("Symbolic solution: " + symbolicSolution);
output("Computed in " + time + " ms");
if (Util.thereExists(new SubExpressionsDepthFirstIterator(symbolicSolution), e -> e instanceof QuantifiedExpression || Sets.isIntensionalSet(e))) {
throw new Error("Symbolic solution is not quantifier-free: " + symbolicSolution);
}
if (testAgainstBruteForce) {
BinaryFunction<BruteForceCommonInterpreter, Context, Expression> bruteForceSolutionGivenInterpreterAndContextWithAssignmentToOtherVariables = (i, c) -> i.apply(problem, c);
testSymbolicVsBruteForceComputationForEachAssignment(theory, problemDescription, freeVariables, symbolicSolution, bruteForceSolutionGivenInterpreterAndContextWithAssignmentToOtherVariables, context);
// A more elegant approach would be to create a "for all free variables : symbolic = problem" expression
// and solve it by brute force instead of using testSymbolicVsBruteForceComputation
// which replicates the brute force interpreter to some extent.
// The reason we do not do this is simply due to the fact that the brute force interpreter would return "false"
// in case of failure, without indicating which assignment failed, which is very useful for debugging.
// If interpreters, and in fact the whole framework, provided proofs of its calculations,
// then we could simply use the more elegant approach.
} else {
output("Skipping test againt brute-force.");
}
}
Aggregations