Search in sources :

Example 6 with Simplifier

use of com.sri.ai.grinder.rewriter.api.Simplifier in project aic-expresso by aic-sri-international.

the class SamplingProceduralAttachmentSingleQuantifierEliminator method makeAndRegisterProceduralAttachment.

private Symbol makeAndRegisterProceduralAttachment(int arity, Context context) {
    Integer counter = context.updateInplaceGlobalObject(COUNTER_KEY, () -> 0, c -> c.intValue() + 1);
    Simplifier samplerRewriter = (Simplifier) (e, c) -> {
        sampler.setContext(c);
        Expression result = sampler.next();
        return result;
    };
    Symbol samplerFunctor = makeSymbol("sampler" + counter);
    registerProceduralAttachment(samplerFunctor, arity, samplerRewriter, context);
    return samplerFunctor;
}
Also used : Expression(com.sri.ai.expresso.api.Expression) Expressions.makeSymbol(com.sri.ai.expresso.helper.Expressions.makeSymbol) Symbol(com.sri.ai.expresso.api.Symbol) Simplifier(com.sri.ai.grinder.rewriter.api.Simplifier)

Example 7 with Simplifier

use of com.sri.ai.grinder.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 = getVariablesBeingReferenced(singleVariableConstraint, context);
            Collection<? extends 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) Sets(com.sri.ai.grinder.library.set.Sets) Function(java.util.function.Function) GrinderUtil(com.sri.ai.grinder.helper.GrinderUtil) And(com.sri.ai.grinder.library.boole.And) Util.in(com.sri.ai.util.Util.in) AssociativeCommutativeGroup(com.sri.ai.grinder.group.AssociativeCommutativeGroup) BruteForceCommonInterpreter(com.sri.ai.grinder.interpreter.BruteForceCommonInterpreter) IndexExpressions(com.sri.ai.grinder.library.indexexpression.IndexExpressions) Expressions.parse(com.sri.ai.expresso.helper.Expressions.parse) Map(java.util.Map) ThereExists(com.sri.ai.grinder.library.boole.ThereExists) Context(com.sri.ai.grinder.api.Context) ZERO(com.sri.ai.expresso.helper.Expressions.ZERO) SingleQuantifierEliminationProblem(com.sri.ai.grinder.api.SingleQuantifierEliminationProblem) LinkedHashSet(java.util.LinkedHashSet) Type(com.sri.ai.expresso.api.Type) DefaultSingleQuantifierEliminationProblem(com.sri.ai.grinder.core.solver.DefaultSingleQuantifierEliminationProblem) NullaryFunction(com.sri.ai.util.base.NullaryFunction) Util.join(com.sri.ai.util.Util.join) Expressions.getVariablesBeingReferenced(com.sri.ai.expresso.helper.Expressions.getVariablesBeingReferenced) Collection(java.util.Collection) Util.list(com.sri.ai.util.Util.list) Set(java.util.Set) IncompleteMultiVariableConstraint(com.sri.ai.grinder.core.constraint.IncompleteMultiVariableConstraint) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) AssignmentMapsIterator(com.sri.ai.grinder.helper.AssignmentMapsIterator) Assignment(com.sri.ai.grinder.interpreter.Assignment) Beta(com.google.common.annotations.Beta) Theory(com.sri.ai.grinder.api.Theory) Expressions.makeSymbol(com.sri.ai.expresso.helper.Expressions.makeSymbol) SingleVariableConstraint(com.sri.ai.grinder.api.SingleVariableConstraint) Simplifier(com.sri.ai.grinder.rewriter.api.Simplifier) Util(com.sri.ai.util.Util) Util.pickKElementsWithoutReplacement(com.sri.ai.util.Util.pickKElementsWithoutReplacement) TRUE(com.sri.ai.expresso.helper.Expressions.TRUE) Constraint(com.sri.ai.grinder.api.Constraint) Context(com.sri.ai.grinder.api.Context) BruteForceCommonInterpreter(com.sri.ai.grinder.interpreter.BruteForceCommonInterpreter) SingleVariableConstraint(com.sri.ai.grinder.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.rewriter.api.Simplifier) SubExpressionsDepthFirstIterator(com.sri.ai.expresso.helper.SubExpressionsDepthFirstIterator)

Example 8 with Simplifier

use of com.sri.ai.grinder.rewriter.api.Simplifier 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<? extends 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.");
    }
}
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) Sets(com.sri.ai.grinder.library.set.Sets) Function(java.util.function.Function) GrinderUtil(com.sri.ai.grinder.helper.GrinderUtil) And(com.sri.ai.grinder.library.boole.And) Util.in(com.sri.ai.util.Util.in) AssociativeCommutativeGroup(com.sri.ai.grinder.group.AssociativeCommutativeGroup) BruteForceCommonInterpreter(com.sri.ai.grinder.interpreter.BruteForceCommonInterpreter) IndexExpressions(com.sri.ai.grinder.library.indexexpression.IndexExpressions) Expressions.parse(com.sri.ai.expresso.helper.Expressions.parse) Map(java.util.Map) ThereExists(com.sri.ai.grinder.library.boole.ThereExists) Context(com.sri.ai.grinder.api.Context) ZERO(com.sri.ai.expresso.helper.Expressions.ZERO) SingleQuantifierEliminationProblem(com.sri.ai.grinder.api.SingleQuantifierEliminationProblem) LinkedHashSet(java.util.LinkedHashSet) Type(com.sri.ai.expresso.api.Type) DefaultSingleQuantifierEliminationProblem(com.sri.ai.grinder.core.solver.DefaultSingleQuantifierEliminationProblem) NullaryFunction(com.sri.ai.util.base.NullaryFunction) Util.join(com.sri.ai.util.Util.join) Expressions.getVariablesBeingReferenced(com.sri.ai.expresso.helper.Expressions.getVariablesBeingReferenced) Collection(java.util.Collection) Util.list(com.sri.ai.util.Util.list) Set(java.util.Set) IncompleteMultiVariableConstraint(com.sri.ai.grinder.core.constraint.IncompleteMultiVariableConstraint) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) AssignmentMapsIterator(com.sri.ai.grinder.helper.AssignmentMapsIterator) Assignment(com.sri.ai.grinder.interpreter.Assignment) Beta(com.google.common.annotations.Beta) Theory(com.sri.ai.grinder.api.Theory) Expressions.makeSymbol(com.sri.ai.expresso.helper.Expressions.makeSymbol) SingleVariableConstraint(com.sri.ai.grinder.api.SingleVariableConstraint) Simplifier(com.sri.ai.grinder.rewriter.api.Simplifier) Util(com.sri.ai.util.Util) Util.pickKElementsWithoutReplacement(com.sri.ai.util.Util.pickKElementsWithoutReplacement) TRUE(com.sri.ai.expresso.helper.Expressions.TRUE) Constraint(com.sri.ai.grinder.api.Constraint) Context(com.sri.ai.grinder.api.Context) BruteForceCommonInterpreter(com.sri.ai.grinder.interpreter.BruteForceCommonInterpreter) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) Theory(com.sri.ai.grinder.api.Theory) Expression(com.sri.ai.expresso.api.Expression) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) Simplifier(com.sri.ai.grinder.rewriter.api.Simplifier) SubExpressionsDepthFirstIterator(com.sri.ai.expresso.helper.SubExpressionsDepthFirstIterator)

Example 9 with Simplifier

use of com.sri.ai.grinder.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.theory.tuple.rewriter.TupleDisequalitySimplifier) Expression(com.sri.ai.expresso.api.Expression) TupleGetSimplifier(com.sri.ai.grinder.theory.tuple.rewriter.TupleGetSimplifier) TupleQuantifierSimplifier(com.sri.ai.grinder.theory.tuple.rewriter.TupleQuantifierSimplifier) TupleEqualitySimplifier(com.sri.ai.grinder.theory.tuple.rewriter.TupleEqualitySimplifier) TupleSetSimplifier(com.sri.ai.grinder.theory.tuple.rewriter.TupleSetSimplifier) TupleDisequalitySimplifier(com.sri.ai.grinder.theory.tuple.rewriter.TupleDisequalitySimplifier) TupleValuedFreeVariablesSimplifier(com.sri.ai.grinder.theory.tuple.rewriter.TupleValuedFreeVariablesSimplifier) Simplifier(com.sri.ai.grinder.rewriter.api.Simplifier) Test(org.junit.Test)

Example 10 with Simplifier

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

Aggregations

Simplifier (com.sri.ai.grinder.rewriter.api.Simplifier)11 Expression (com.sri.ai.expresso.api.Expression)10 Test (org.junit.Test)8 Expressions.parse (com.sri.ai.expresso.helper.Expressions.parse)5 Context (com.sri.ai.grinder.api.Context)5 TupleDisequalitySimplifier (com.sri.ai.grinder.theory.tuple.rewriter.TupleDisequalitySimplifier)5 TupleEqualitySimplifier (com.sri.ai.grinder.theory.tuple.rewriter.TupleEqualitySimplifier)5 TupleGetSimplifier (com.sri.ai.grinder.theory.tuple.rewriter.TupleGetSimplifier)5 TupleQuantifierSimplifier (com.sri.ai.grinder.theory.tuple.rewriter.TupleQuantifierSimplifier)5 TupleSetSimplifier (com.sri.ai.grinder.theory.tuple.rewriter.TupleSetSimplifier)5 TupleValuedFreeVariablesSimplifier (com.sri.ai.grinder.theory.tuple.rewriter.TupleValuedFreeVariablesSimplifier)5 Beta (com.google.common.annotations.Beta)4 Expressions.makeSymbol (com.sri.ai.expresso.helper.Expressions.makeSymbol)4 Type (com.sri.ai.expresso.api.Type)3 Constraint (com.sri.ai.grinder.api.Constraint)3 SingleVariableConstraint (com.sri.ai.grinder.api.SingleVariableConstraint)3 Theory (com.sri.ai.grinder.api.Theory)3 DefaultSingleQuantifierEliminationProblem (com.sri.ai.grinder.core.solver.DefaultSingleQuantifierEliminationProblem)3 And (com.sri.ai.grinder.library.boole.And)3 BinaryFunction (com.sri.ai.util.base.BinaryFunction)3