Search in sources :

Example 1 with BinaryFunction

use of com.sri.ai.util.base.BinaryFunction 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.");
    }
}
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) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) Theory(com.sri.ai.grinder.sgdpllt.api.Theory) 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)

Example 2 with BinaryFunction

use of com.sri.ai.util.base.BinaryFunction in project aic-expresso by aic-sri-international.

the class LinearRealArithmeticTheoryTest method runQuantifierTest.

private void runQuantifierTest(Expression variable, String constraintString, String bodyString, Expression expected, String computedFunction, BinaryFunction<Constraint, Expression, ExpressionStepSolver> stepSolverMaker, Context context) {
    Expression body = parse(bodyString);
    Function<Constraint, ExpressionStepSolver> stepSolverMakerFromConstraint = c -> stepSolverMaker.apply(c, body);
    runTest(variable, constraintString, expected, computedFunction, stepSolverMakerFromConstraint, context);
}
Also used : 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) Constraint(com.sri.ai.grinder.sgdpllt.api.Constraint) SingleVariableLinearRealArithmeticConstraint(com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint) ExpressionStepSolver(com.sri.ai.grinder.sgdpllt.api.ExpressionStepSolver)

Example 3 with BinaryFunction

use of com.sri.ai.util.base.BinaryFunction 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

Beta (com.google.common.annotations.Beta)3 Expression (com.sri.ai.expresso.api.Expression)3 Expressions.parse (com.sri.ai.expresso.helper.Expressions.parse)3 Constraint (com.sri.ai.grinder.sgdpllt.api.Constraint)3 Context (com.sri.ai.grinder.sgdpllt.api.Context)3 Simplifier (com.sri.ai.grinder.sgdpllt.rewriter.api.Simplifier)3 BinaryFunction (com.sri.ai.util.base.BinaryFunction)3 QuantifiedExpression (com.sri.ai.expresso.api.QuantifiedExpression)2 Type (com.sri.ai.expresso.api.Type)2 TRUE (com.sri.ai.expresso.helper.Expressions.TRUE)2 ZERO (com.sri.ai.expresso.helper.Expressions.ZERO)2 Expressions.getVariableReferences (com.sri.ai.expresso.helper.Expressions.getVariableReferences)2 Expressions.makeSymbol (com.sri.ai.expresso.helper.Expressions.makeSymbol)2 SubExpressionsDepthFirstIterator (com.sri.ai.expresso.helper.SubExpressionsDepthFirstIterator)2 AssignmentsIterator (com.sri.ai.grinder.helper.AssignmentsIterator)2 GrinderUtil (com.sri.ai.grinder.helper.GrinderUtil)2 SingleVariableConstraint (com.sri.ai.grinder.sgdpllt.api.SingleVariableConstraint)2 Theory (com.sri.ai.grinder.sgdpllt.api.Theory)2 CompleteMultiVariableContext (com.sri.ai.grinder.sgdpllt.core.constraint.CompleteMultiVariableContext)2 DefaultMultiVariableConstraint (com.sri.ai.grinder.sgdpllt.core.constraint.DefaultMultiVariableConstraint)2