use of com.sri.ai.grinder.sgdpllt.rewriter.api.Simplifier in project aic-expresso by aic-sri-international.
the class LinearRealArithmeticTheoryTest method testSummation.
@Test
public void testSummation() {
TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(makeRandom(), new LinearRealArithmeticTheory(true, true));
Context context = theoryTestingSupport.makeContextWithTestingInformation();
Simplifier simplifier = (e, c) -> theoryTestingSupport.getTheory().simplify(e, c);
Expression variable;
String constraintString;
String bodyString;
Expression expected;
variable = parse("X");
constraintString = "true";
bodyString = "1";
expected = parse("4");
runSummationTest(variable, constraintString, bodyString, expected, simplifier, context);
variable = parse("X");
constraintString = "X < 3 and X != 2";
bodyString = "1";
expected = parse("3");
runSummationTest(variable, constraintString, bodyString, expected, simplifier, context);
variable = parse("X");
constraintString = "X < Y and X != 2";
bodyString = "1";
expected = parse("if Y > 0 then Y else 0");
runSummationTest(variable, constraintString, bodyString, expected, simplifier, context);
variable = parse("X");
constraintString = "X < 3 and X != 2";
bodyString = "Y";
expected = parse("3*Y");
runSummationTest(variable, constraintString, bodyString, expected, simplifier, context);
variable = parse("X");
constraintString = "X < 3 and X != 2";
bodyString = "X";
expected = parse("4.5");
runSummationTest(variable, constraintString, bodyString, expected, simplifier, context);
variable = parse("X");
constraintString = "X < 3 and X != 2 and X = 2";
bodyString = "Y";
expected = parse("0");
runSummationTest(variable, constraintString, bodyString, expected, simplifier, context);
variable = parse("X");
constraintString = "X < Y and X != 2";
bodyString = "X";
expected = parse("if Y > 0 then 0.5*Y^2 else 0");
runSummationTest(variable, constraintString, bodyString, expected, simplifier, context);
variable = parse("X");
constraintString = "X < Y and X != 2";
bodyString = "Y";
expected = parse("if Y > 0 then Y^2 else 0");
runSummationTest(variable, constraintString, bodyString, expected, simplifier, context);
variable = parse("X");
constraintString = "X < Y and X != 2";
bodyString = "X + Y";
expected = parse("if Y > 0 then 1.5*Y^2 else 0");
runSummationTest(variable, constraintString, bodyString, expected, simplifier, context);
// variable = parse("X");
// constraintString = "X < Y and X != 2";
// bodyString = "X^2 + Y";
// expected = parse("if Y > 0 then 0.333333333*Y^3 + Y^2 else 0");
// runSummationTest(variable, constraintString, bodyString, expected, simplifier, context);
}
use of com.sri.ai.grinder.sgdpllt.rewriter.api.Simplifier in project aic-expresso by aic-sri-international.
the class TupleRewriterTest method testTupleDisEqualitySimplification.
@Test
public void testTupleDisEqualitySimplification() {
Simplifier tupleDisequalitySimplifier = new TupleDisequalitySimplifier();
Assert.assertEquals(parse("A1 != B1 or A2 != B2 or A3 != B3"), tupleDisequalitySimplifier.apply(parse("(A1, A2, A3) != (B1, B2, B3)"), context));
Expression expr = parse("(A1, A2) != (B1, B2, B3)");
Assert.assertTrue(expr == tupleDisequalitySimplifier.apply(expr, context));
expr = parse("A1 != B1");
Assert.assertTrue(expr == tupleDisequalitySimplifier.apply(expr, context));
}
use of com.sri.ai.grinder.sgdpllt.rewriter.api.Simplifier in project aic-expresso by aic-sri-international.
the class TupleRewriterTest method testTupleQuantifierSimplification.
@Test
public void testTupleQuantifierSimplification() {
Simplifier tupleQuantifierSimplifier = new TupleQuantifierSimplifier();
Assert.assertEquals(parse("for all X_1 in Boolean : for all X_2 in Integer : (X_1, X_2) = (true, 1)"), tupleQuantifierSimplifier.apply(parse("for all X in (Boolean x Integer) : X = (true, 1)"), context));
Assert.assertEquals(parse("there exists X_1 in Boolean : there exists X_2 in Integer : (X_1, X_2) = (true, 1)"), tupleQuantifierSimplifier.apply(parse("there exists X in (Boolean x Integer) : X = (true, 1)"), context));
Assert.assertEquals(parse("| X_1 in Boolean, X_2 in Integer : (X_1, X_2) = (true, 1) |"), tupleQuantifierSimplifier.apply(parse("| X in (Boolean x Integer) : X = (true, 1) |"), context));
Assert.assertEquals(parse("lambda X_1 in Boolean, X_2 in Integer : (X_1, X_2) = (true, 1)"), tupleQuantifierSimplifier.apply(parse("lambda X in (Boolean x Integer) : X = (true, 1)"), context));
Assert.assertEquals(parse("{ (on X_1 in Boolean, X_2 in Integer) (X_1, X_2) }"), tupleQuantifierSimplifier.apply(parse("{ (on X in (Boolean x Integer)) X }"), context));
Assert.assertEquals(parse("{ (on X_1 in Boolean, X_2 in Integer) (X_1, X_2) : (X_1, X_2) != (true, 1) }"), tupleQuantifierSimplifier.apply(parse("{ (on X in (Boolean x Integer)) X : X != (true, 1) }"), context));
Assert.assertEquals(parse("{{ (on X_1 in Boolean, X_2 in Integer) (X_1, X_2) }}"), tupleQuantifierSimplifier.apply(parse("{{ (on X in (Boolean x Integer)) X }}"), context));
Assert.assertEquals(parse("{{ (on X_1 in Boolean, X_2 in Integer) (X_1, X_2) : (X_1, X_2) != (true, 1) }}"), tupleQuantifierSimplifier.apply(parse("{{ (on X in (Boolean x Integer)) X : X != (true, 1) }}"), context));
//
// Ensure Introduced Variables are Unique
Assert.assertEquals(parse("lambda X_1' in Boolean, X_2' in Integer, X_1 in Boolean, X_2 in Boolean : (X_1', X_2') = (true, 1) and X_1 = X_2"), tupleQuantifierSimplifier.apply(parse("lambda X in (Boolean x Integer), X_1 in Boolean, X_2 in Boolean : X = (true, 1) and X_1 = X_2"), context));
//
// Ensure we don't introduce undeclared types
Assert.assertEquals(parse("lambda X_1 in Boolean, X_2 in Integer, Y, Z : (X_1, X_2) = (true, 1) and Y = Z"), tupleQuantifierSimplifier.apply(parse("lambda X in (Boolean x Integer), Y, Z : X = (true, 1) and Y = Z"), context));
}
use of com.sri.ai.grinder.sgdpllt.rewriter.api.Simplifier 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.");
}
}
Aggregations