Search in sources :

Example 1 with BruteForceCommonInterpreter

use of com.sri.ai.grinder.sgdpllt.interpreter.BruteForceCommonInterpreter in project aic-expresso by aic-sri-international.

the class BruteForceCommonInterpreterTest method test.

@Test
public void test() {
    LinkedHashMap<Expression, Expression> assignment = map(parse("Two"), Expressions.TWO);
    BruteForceCommonInterpreter interpreter = new BruteForceCommonInterpreter();
    Context context = new TrueContext();
    context = AbstractIterativeMultiIndexQuantifierElimination.extendAssignments(assignment, context);
    String expression;
    String expected;
    expression = "max({{ (on I in 0..3) max( {{ (on J in 0..3) I + J : I != J }} ) }})";
    expected = "5";
    runTest(expression, expected, interpreter, context);
    expression = "3 * (Two + 5 - 3)*(-10)";
    expected = "-120";
    runTest(expression, expected, interpreter, context);
    expression = "3 * (2 + 5 - 3)*(-10)";
    expected = "-120";
    runTest(expression, expected, interpreter, context);
    expression = "sum({{ (on x in 0..2)  x }} )";
    expected = "3";
    runTest(expression, expected, interpreter, context);
    expression = "sum({{ (on f in 0..2 -> Boolean)  if f(0) then 2 else 3 }} )";
    expected = "20";
    runTest(expression, expected, interpreter, context);
    expression = "sum({{ (on f in 0..2 -> Boolean)  if f(0) and f(1) then 2 else 3  :  f(2) }} )";
    expected = "11";
    runTest(expression, expected, interpreter, context);
    expression = "sum({{ (on f in 0..2 -> Boolean)  " + "if f(0) and f(1) then 2 else | f in 0..2 x 0..2 -> Boolean : f(0, 0) |  " + ":  f(2) }} )";
    expected = "770";
    runTest(expression, expected, interpreter, context);
    expression = "sum({{ (on f in '->'(x(1..2), Boolean), g in '->'(x(1..2), Boolean))  if f(1) and g(2) then 2 else 3  :  f(2) }} )";
    expected = "22";
    runTest(expression, expected, interpreter, context);
    expression = "| f in '->'(x(0..2, 0..2), Boolean) : f(0, 0) |";
    expected = "256";
    runTest(expression, expected, interpreter, context);
    expression = "| f in 0..2 x 0..2 -> Boolean : f(0, 0) |";
    expected = "256";
    runTest(expression, expected, interpreter, context);
    expression = "| f in 0..2 x 0..2 -> Boolean, g in 0..2 -> Boolean : f(0, 0) |";
    expected = "2048";
    runTest(expression, expected, interpreter, context);
    expression = "sum( {{ (on p in Boolean) if p then 1 else 2 }} )";
    expected = "3";
    runTest(expression, expected, interpreter, context);
}
Also used : TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext) Context(com.sri.ai.grinder.sgdpllt.api.Context) BruteForceCommonInterpreter(com.sri.ai.grinder.sgdpllt.interpreter.BruteForceCommonInterpreter) Expression(com.sri.ai.expresso.api.Expression) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext) Test(org.junit.Test)

Example 2 with BruteForceCommonInterpreter

use of com.sri.ai.grinder.sgdpllt.interpreter.BruteForceCommonInterpreter in project aic-expresso by aic-sri-international.

the class SampleCommonInterpreterTest method bruteForceResult.

private Expression bruteForceResult(String expression) {
    BruteForceCommonInterpreter bruteForceInterpreter = new BruteForceCommonInterpreter();
    Expression result = bruteForceInterpreter.apply(Expressions.parse(expression), context);
    return result;
}
Also used : BruteForceCommonInterpreter(com.sri.ai.grinder.sgdpllt.interpreter.BruteForceCommonInterpreter) Expression(com.sri.ai.expresso.api.Expression)

Example 3 with BruteForceCommonInterpreter

use of com.sri.ai.grinder.sgdpllt.interpreter.BruteForceCommonInterpreter 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 4 with BruteForceCommonInterpreter

use of com.sri.ai.grinder.sgdpllt.interpreter.BruteForceCommonInterpreter in project aic-expresso by aic-sri-international.

the class SGDPLLTTester method testSymbolicVsBruteForceComputationForAssignment.

private static void testSymbolicVsBruteForceComputationForAssignment(Map<Expression, Expression> assignment, Theory theory, String problemDescription, Expression symbolicSolution, BinaryFunction<BruteForceCommonInterpreter, Context, Expression> fromInterpreterAndContextWithAssignmentToBruteForceSolution, Context context) throws Error {
    BruteForceCommonInterpreter interpreter = new BruteForceCommonInterpreter();
    Context extendedContext = AbstractIterativeMultiIndexQuantifierElimination.extendAssignments(assignment, context);
    Expression bruteForceResultUnderAssignment = fromInterpreterAndContextWithAssignmentToBruteForceSolution.apply(interpreter, extendedContext);
    Expression symbolicResultUnderAssignment = interpreter.apply(symbolicSolution, extendedContext);
    output("Under free variables assignment " + assignment);
    output("Symbolic    result becomes " + symbolicResultUnderAssignment);
    output("Brute force result becomes " + bruteForceResultUnderAssignment + "\n");
    if (!symbolicResultUnderAssignment.equals(bruteForceResultUnderAssignment)) {
        throw new Error("Failure in testing of " + problemDescription + "\n" + "Symbolic solution: " + symbolicSolution + "\n" + "Under assignment to free variables: " + assignment + "\n" + "Value of symbolic solution      : " + symbolicResultUnderAssignment + "\n" + "Value of brute force computation: " + bruteForceResultUnderAssignment + "\n" + "Context                         : " + extendedContext + "\n");
    }
}
Also used : 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) Expression(com.sri.ai.expresso.api.Expression) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression)

Example 5 with BruteForceCommonInterpreter

use of com.sri.ai.grinder.sgdpllt.interpreter.BruteForceCommonInterpreter in project aic-expresso by aic-sri-international.

the class SGDPLLTTester method isSatisfiableByBruteForce.

/**
	 * Determines whether a formula is satisfiable by adding existential quantifiers for each of its variables
	 * (according to the theory provided) and evaluating it.
	 * @param formula
	 * @param theoryTestingSupport
	 * @param context
	 * @return whether the formula is satisfiable.
	 */
public static boolean isSatisfiableByBruteForce(Expression formula, TheoryTestingSupport theoryTestingSupport, Context context) {
    Map<String, Type> variableNamesAndTypesForTesting = theoryTestingSupport.getVariableNamesAndTypesForTesting();
    Expression quantifiedFormula = formula;
    Collection<Expression> variables = theoryTestingSupport.getTheory().getVariablesIn(formula, context);
    for (Expression variable : variables) {
        Expression typeNameExpression = parse(variableNamesAndTypesForTesting.get(variable).toString());
        quantifiedFormula = ThereExists.make(IndexExpressions.makeIndexExpression(variable, typeNameExpression), quantifiedFormula);
    }
    Expression evaluation = new BruteForceCommonInterpreter().apply(quantifiedFormula, context);
    boolean result = evaluation.equals(TRUE);
    return result;
}
Also used : BruteForceCommonInterpreter(com.sri.ai.grinder.sgdpllt.interpreter.BruteForceCommonInterpreter) Type(com.sri.ai.expresso.api.Type) Expression(com.sri.ai.expresso.api.Expression) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression)

Aggregations

Expression (com.sri.ai.expresso.api.Expression)6 BruteForceCommonInterpreter (com.sri.ai.grinder.sgdpllt.interpreter.BruteForceCommonInterpreter)6 QuantifiedExpression (com.sri.ai.expresso.api.QuantifiedExpression)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 Beta (com.google.common.annotations.Beta)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 Expressions.parse (com.sri.ai.expresso.helper.Expressions.parse)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 Constraint (com.sri.ai.grinder.sgdpllt.api.Constraint)2 SingleVariableConstraint (com.sri.ai.grinder.sgdpllt.api.SingleVariableConstraint)2 Theory (com.sri.ai.grinder.sgdpllt.api.Theory)2 DefaultMultiVariableConstraint (com.sri.ai.grinder.sgdpllt.core.constraint.DefaultMultiVariableConstraint)2 AssociativeCommutativeGroup (com.sri.ai.grinder.sgdpllt.group.AssociativeCommutativeGroup)2