Search in sources :

Example 6 with Simplifier

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);
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) IntervalWithMeasureEquivalentToSingleVariableLinearRealArithmeticConstraintStepSolver(com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.IntervalWithMeasureEquivalentToSingleVariableLinearRealArithmeticConstraintStepSolver) BinaryFunction(com.sri.ai.util.base.BinaryFunction) ExpressionStepSolver(com.sri.ai.grinder.sgdpllt.api.ExpressionStepSolver) Function(com.google.common.base.Function) Simplifier(com.sri.ai.grinder.sgdpllt.rewriter.api.Simplifier) Random(java.util.Random) Test(org.junit.Test) Expression(com.sri.ai.expresso.api.Expression) Constraint(com.sri.ai.grinder.sgdpllt.api.Constraint) Context(com.sri.ai.grinder.sgdpllt.api.Context) LinearRealArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.LinearRealArithmeticTheory) Beta(com.google.common.annotations.Beta) Expressions.parse(com.sri.ai.expresso.helper.Expressions.parse) SingleVariableLinearRealArithmeticConstraint(com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint) MeasureOfSingleVariableLinearRealArithmeticConstraintStepSolver(com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.MeasureOfSingleVariableLinearRealArithmeticConstraintStepSolver) TheoryTestingSupport(com.sri.ai.grinder.sgdpllt.tester.TheoryTestingSupport) SummationOnLinearRealArithmeticAndPolynomialStepSolver(com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.SummationOnLinearRealArithmeticAndPolynomialStepSolver) Assert.assertEquals(org.junit.Assert.assertEquals) SatisfiabilityOfSingleVariableLinearRealArithmeticConstraintStepSolver(com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.SatisfiabilityOfSingleVariableLinearRealArithmeticConstraintStepSolver) Expression(com.sri.ai.expresso.api.Expression) TheoryTestingSupport(com.sri.ai.grinder.sgdpllt.tester.TheoryTestingSupport) LinearRealArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.LinearRealArithmeticTheory) Simplifier(com.sri.ai.grinder.sgdpllt.rewriter.api.Simplifier) Test(org.junit.Test)

Example 7 with Simplifier

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));
}
Also used : TupleDisequalitySimplifier(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleDisequalitySimplifier) Expression(com.sri.ai.expresso.api.Expression) Simplifier(com.sri.ai.grinder.sgdpllt.rewriter.api.Simplifier) TupleEqualitySimplifier(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleEqualitySimplifier) TupleGetSimplifier(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleGetSimplifier) TupleQuantifierSimplifier(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleQuantifierSimplifier) TupleDisequalitySimplifier(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleDisequalitySimplifier) TupleValuedFreeVariablesSimplifier(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleValuedFreeVariablesSimplifier) TupleSetSimplifier(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleSetSimplifier) Test(org.junit.Test)

Example 8 with Simplifier

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));
}
Also used : Simplifier(com.sri.ai.grinder.sgdpllt.rewriter.api.Simplifier) TupleEqualitySimplifier(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleEqualitySimplifier) TupleGetSimplifier(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleGetSimplifier) TupleQuantifierSimplifier(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleQuantifierSimplifier) TupleDisequalitySimplifier(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleDisequalitySimplifier) TupleValuedFreeVariablesSimplifier(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleValuedFreeVariablesSimplifier) TupleSetSimplifier(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleSetSimplifier) TupleQuantifierSimplifier(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleQuantifierSimplifier) Test(org.junit.Test)

Example 9 with Simplifier

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.");
    }
}
Also used : BinaryFunction(com.sri.ai.util.base.BinaryFunction) Util.removeFromSetNonDestructively(com.sri.ai.util.Util.removeFromSetNonDestructively) SubExpressionsDepthFirstIterator(com.sri.ai.expresso.helper.SubExpressionsDepthFirstIterator) Expression(com.sri.ai.expresso.api.Expression) Constraint(com.sri.ai.grinder.sgdpllt.api.Constraint) Function(java.util.function.Function) GrinderUtil(com.sri.ai.grinder.helper.GrinderUtil) Util.in(com.sri.ai.util.Util.in) ThereExists(com.sri.ai.grinder.sgdpllt.library.boole.ThereExists) AbstractIterativeMultiIndexQuantifierElimination(com.sri.ai.grinder.sgdpllt.interpreter.AbstractIterativeMultiIndexQuantifierElimination) Expressions.parse(com.sri.ai.expresso.helper.Expressions.parse) Map(java.util.Map) CompleteMultiVariableContext(com.sri.ai.grinder.sgdpllt.core.constraint.CompleteMultiVariableContext) And(com.sri.ai.grinder.sgdpllt.library.boole.And) DefaultMultiVariableConstraint(com.sri.ai.grinder.sgdpllt.core.constraint.DefaultMultiVariableConstraint) ZERO(com.sri.ai.expresso.helper.Expressions.ZERO) LinkedHashSet(java.util.LinkedHashSet) Sets(com.sri.ai.grinder.sgdpllt.library.set.Sets) Type(com.sri.ai.expresso.api.Type) NullaryFunction(com.sri.ai.util.base.NullaryFunction) Util.join(com.sri.ai.util.Util.join) SingleVariableConstraint(com.sri.ai.grinder.sgdpllt.api.SingleVariableConstraint) Simplifier(com.sri.ai.grinder.sgdpllt.rewriter.api.Simplifier) Collection(java.util.Collection) Util.list(com.sri.ai.util.Util.list) Set(java.util.Set) Expressions.getVariableReferences(com.sri.ai.expresso.helper.Expressions.getVariableReferences) Context(com.sri.ai.grinder.sgdpllt.api.Context) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) Theory(com.sri.ai.grinder.sgdpllt.api.Theory) Beta(com.google.common.annotations.Beta) IndexExpressions(com.sri.ai.grinder.sgdpllt.library.indexexpression.IndexExpressions) Expressions.makeSymbol(com.sri.ai.expresso.helper.Expressions.makeSymbol) Util(com.sri.ai.util.Util) AssociativeCommutativeGroup(com.sri.ai.grinder.sgdpllt.group.AssociativeCommutativeGroup) Util.pickKElementsWithoutReplacement(com.sri.ai.util.Util.pickKElementsWithoutReplacement) TRUE(com.sri.ai.expresso.helper.Expressions.TRUE) BruteForceCommonInterpreter(com.sri.ai.grinder.sgdpllt.interpreter.BruteForceCommonInterpreter) AssignmentsIterator(com.sri.ai.grinder.helper.AssignmentsIterator) CompleteMultiVariableContext(com.sri.ai.grinder.sgdpllt.core.constraint.CompleteMultiVariableContext) Context(com.sri.ai.grinder.sgdpllt.api.Context) BruteForceCommonInterpreter(com.sri.ai.grinder.sgdpllt.interpreter.BruteForceCommonInterpreter) SingleVariableConstraint(com.sri.ai.grinder.sgdpllt.api.SingleVariableConstraint) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) Expression(com.sri.ai.expresso.api.Expression) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) Simplifier(com.sri.ai.grinder.sgdpllt.rewriter.api.Simplifier) SubExpressionsDepthFirstIterator(com.sri.ai.expresso.helper.SubExpressionsDepthFirstIterator)

Aggregations

Simplifier (com.sri.ai.grinder.sgdpllt.rewriter.api.Simplifier)9 Expression (com.sri.ai.expresso.api.Expression)8 Test (org.junit.Test)7 TupleDisequalitySimplifier (com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleDisequalitySimplifier)5 TupleEqualitySimplifier (com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleEqualitySimplifier)5 TupleGetSimplifier (com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleGetSimplifier)5 TupleQuantifierSimplifier (com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleQuantifierSimplifier)5 TupleSetSimplifier (com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleSetSimplifier)5 TupleValuedFreeVariablesSimplifier (com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleValuedFreeVariablesSimplifier)5 Beta (com.google.common.annotations.Beta)4 Expressions.parse (com.sri.ai.expresso.helper.Expressions.parse)4 Constraint (com.sri.ai.grinder.sgdpllt.api.Constraint)4 Context (com.sri.ai.grinder.sgdpllt.api.Context)4 Type (com.sri.ai.expresso.api.Type)3 CompleteMultiVariableContext (com.sri.ai.grinder.sgdpllt.core.constraint.CompleteMultiVariableContext)3 And (com.sri.ai.grinder.sgdpllt.library.boole.And)3 BinaryFunction (com.sri.ai.util.base.BinaryFunction)3 QuantifiedExpression (com.sri.ai.expresso.api.QuantifiedExpression)2 TRUE (com.sri.ai.expresso.helper.Expressions.TRUE)2 ZERO (com.sri.ai.expresso.helper.Expressions.ZERO)2