Search in sources :

Example 96 with Context

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

the class BasicTest method isLiteralTests.

@Test
public void isLiteralTests() {
    Expression expression;
    boolean expected;
    Context context = new TrueContext();
    context = context.makeCloneWithAdditionalRegisteredSymbolsAndTypes(map(parse("X"), parse("Real"), parse("Y"), parse("Real"), parse("Z"), parse("Real"), parse("W"), parse("Integer")));
    expression = parse("1 >= 0");
    expected = true;
    runIsLiteralTest(expression, expected, context);
    expression = parse("X >= 0");
    expected = true;
    runIsLiteralTest(expression, expected, context);
    expression = parse("-X >= 0");
    expected = true;
    runIsLiteralTest(expression, expected, context);
    expression = parse("X - Y >= 0");
    expected = true;
    runIsLiteralTest(expression, expected, context);
    expression = parse("X + Y >= 0");
    expected = true;
    runIsLiteralTest(expression, expected, context);
    expression = parse("X - Y + 3 >= 0");
    expected = true;
    runIsLiteralTest(expression, expected, context);
    expression = parse("X - Y + 3 + Z >= Z");
    expected = true;
    runIsLiteralTest(expression, expected, context);
    expression = parse("X - X + X - Y + Y - Z + 1 - 5 >= 0");
    expected = true;
    runIsLiteralTest(expression, expected, context);
    // Must be linear
    expression = parse("X^2 >= 0");
    expected = false;
    runIsLiteralTest(expression, expected, context);
    expression = parse("X*Y >= 0");
    expected = false;
    runIsLiteralTest(expression, expected, context);
    expression = parse("X*X - Y >= 0");
    expected = false;
    runIsLiteralTest(expression, expected, context);
    expression = parse("2*X -3*Y + 3 >= 0");
    expected = true;
    runIsLiteralTest(expression, expected, context);
    // Variables must be real
    expression = parse("2*X - 2*X + X - 2*W + 2*W - 2*Z + 1 - 5 >= -Z");
    expected = true;
    runIsLiteralTest(expression, expected, context);
    expression = parse("W - X >= 0");
    expected = false;
    runIsLiteralTest(expression, expected, context);
    // Sides should be polynomials
    expression = parse("2*f() > 0");
    expected = false;
    runIsLiteralTest(expression, expected, context);
}
Also used : TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) Expression(com.sri.ai.expresso.api.Expression) TrueContext(com.sri.ai.grinder.core.TrueContext) Test(org.junit.Test)

Example 97 with Context

use of com.sri.ai.grinder.api.Context 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<SingleVariableConstraint, Expression, ExpressionStepSolver> stepSolverMaker, Context context) {
    Expression body = parse(bodyString);
    Function<SingleVariableConstraint, ExpressionStepSolver> stepSolverMakerFromConstraint = c -> stepSolverMaker.apply(c, body);
    runTest(variable, constraintString, expected, computedFunction, stepSolverMakerFromConstraint, context);
}
Also used : BinaryFunction(com.sri.ai.util.base.BinaryFunction) Function(com.google.common.base.Function) DefaultSingleQuantifierEliminationProblem(com.sri.ai.grinder.core.solver.DefaultSingleQuantifierEliminationProblem) SatisfiabilityOfSingleVariableLinearRealArithmeticConstraintStepSolver(com.sri.ai.grinder.theory.linearrealarithmetic.SatisfiabilityOfSingleVariableLinearRealArithmeticConstraintStepSolver) SingleVariableLinearRealArithmeticConstraint(com.sri.ai.grinder.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint) TheoryTestingSupport(com.sri.ai.grinder.tester.TheoryTestingSupport) Random(java.util.Random) Test(org.junit.Test) Expression(com.sri.ai.expresso.api.Expression) Beta(com.google.common.annotations.Beta) LinearRealArithmeticTheory(com.sri.ai.grinder.theory.linearrealarithmetic.LinearRealArithmeticTheory) SummationOnLinearRealArithmeticAndPolynomialStepSolver(com.sri.ai.grinder.theory.linearrealarithmetic.SummationOnLinearRealArithmeticAndPolynomialStepSolver) Expressions.parse(com.sri.ai.expresso.helper.Expressions.parse) ExpressionStepSolver(com.sri.ai.grinder.api.ExpressionStepSolver) Sum(com.sri.ai.grinder.group.Sum) SingleVariableConstraint(com.sri.ai.grinder.api.SingleVariableConstraint) Simplifier(com.sri.ai.grinder.rewriter.api.Simplifier) Context(com.sri.ai.grinder.api.Context) IntervalWithMeasureEquivalentToSingleVariableLinearRealArithmeticConstraintStepSolver(com.sri.ai.grinder.theory.linearrealarithmetic.IntervalWithMeasureEquivalentToSingleVariableLinearRealArithmeticConstraintStepSolver) Assert.assertEquals(org.junit.Assert.assertEquals) MeasureOfSingleVariableLinearRealArithmeticConstraintStepSolver(com.sri.ai.grinder.theory.linearrealarithmetic.MeasureOfSingleVariableLinearRealArithmeticConstraintStepSolver) SingleVariableConstraint(com.sri.ai.grinder.api.SingleVariableConstraint) Expression(com.sri.ai.expresso.api.Expression) ExpressionStepSolver(com.sri.ai.grinder.api.ExpressionStepSolver)

Example 98 with Context

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

the class TupleRewriterTest method testTupleValuedFreeVariablesSimplifier.

@Test
public void testTupleValuedFreeVariablesSimplifier() {
    Context tupleTheoryContext = new TrueContext(new CompoundTheory(new DifferenceArithmeticTheory(false, false), new TupleTheory()));
    TupleType nTupleType = new TupleType(new IntegerInterval(1, 10), new IntegerInterval(1, 10));
    tupleTheoryContext = (Context) GrinderUtil.extendRegistryWith(map("N", nTupleType.toString()), Arrays.asList(nTupleType), tupleTheoryContext);
    TupleValuedFreeVariablesSimplifier simplifier = new TupleValuedFreeVariablesSimplifier();
    Expression expression = parse("sum( {{ (on X in 1..10) if N = (2, X) then 2 else 3 }} )");
    Expression simplified = simplifier.apply(expression, tupleTheoryContext);
    Assert.assertEquals(parse("if get(N, 1) = 2 then 29 else 30"), simplified);
}
Also used : TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) Expression(com.sri.ai.expresso.api.Expression) DifferenceArithmeticTheory(com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory) IntegerInterval(com.sri.ai.expresso.type.IntegerInterval) TupleType(com.sri.ai.expresso.type.TupleType) TupleValuedFreeVariablesSimplifier(com.sri.ai.grinder.theory.tuple.rewriter.TupleValuedFreeVariablesSimplifier) CompoundTheory(com.sri.ai.grinder.theory.compound.CompoundTheory) TrueContext(com.sri.ai.grinder.core.TrueContext) TupleTheory(com.sri.ai.grinder.theory.tuple.TupleTheory) Test(org.junit.Test)

Example 99 with Context

use of com.sri.ai.grinder.api.Context 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 = Assignment.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 : Context(com.sri.ai.grinder.api.Context) CompleteMultiVariableContext(com.sri.ai.grinder.core.constraint.CompleteMultiVariableContext) BruteForceCommonInterpreter(com.sri.ai.grinder.interpreter.BruteForceCommonInterpreter) Expression(com.sri.ai.expresso.api.Expression) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression)

Example 100 with Context

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

the class SGDPLLTTester method testMultiVariableConstraints.

/**
 * Given a theory and a number <code>n</code> of multi-variable constraint tests,
 * generates <code>n</code> formulas in the theory
 * and see if those detected as unsatisfiable by the corresponding solver
 * are indeed unsatisfiable (checked by brute force).
 * Throws an {@link Error} with the failure description if a test fails.
 * @param theoryTestingSupport
 * @param numberOfTests
 * @param maxNumberOfLiterals
 * @param outputCount
 */
public static void testMultiVariableConstraints(boolean testAgainstBruteForce, TheoryTestingSupport theoryTestingSupport, long numberOfTests, int maxNumberOfLiterals, boolean outputCount) {
    NullaryFunction<Constraint> makeInitialConstraint = () -> new DefaultMultiVariableConstraint(theoryTestingSupport.getTheory());
    Context context = theoryTestingSupport.makeContextWithTestingInformation();
    Function<Constraint, Expression> makeRandomLiteral = c -> theoryTestingSupport.makeRandomLiteral(context);
    // DefaultMultiVariableConstraint is incomplete
    TestRunner tester = SGDPLLTTester::testIncompleteSatisfiability;
    runTesterGivenOnSuccessiveConjunctionsOfLiterals("incomplete satisfiability", tester, numberOfTests, maxNumberOfLiterals, testAgainstBruteForce, theoryTestingSupport, makeInitialConstraint, makeRandomLiteral, outputCount, context);
}
Also used : Context(com.sri.ai.grinder.api.Context) CompleteMultiVariableContext(com.sri.ai.grinder.core.constraint.CompleteMultiVariableContext) 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) DefaultMultiVariableConstraint(com.sri.ai.grinder.core.constraint.DefaultMultiVariableConstraint) Set(java.util.Set) CompleteMultiVariableContext(com.sri.ai.grinder.core.constraint.CompleteMultiVariableContext) 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) DefaultMultiVariableConstraint(com.sri.ai.grinder.core.constraint.DefaultMultiVariableConstraint) SingleVariableConstraint(com.sri.ai.grinder.api.SingleVariableConstraint) Constraint(com.sri.ai.grinder.api.Constraint) Expression(com.sri.ai.expresso.api.Expression) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) DefaultMultiVariableConstraint(com.sri.ai.grinder.core.constraint.DefaultMultiVariableConstraint)

Aggregations

Context (com.sri.ai.grinder.api.Context)132 Expression (com.sri.ai.expresso.api.Expression)100 Test (org.junit.Test)50 TrueContext (com.sri.ai.grinder.core.TrueContext)40 TheoryTestingSupport (com.sri.ai.grinder.tester.TheoryTestingSupport)36 DifferenceArithmeticTheory (com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory)32 Type (com.sri.ai.expresso.api.Type)31 Theory (com.sri.ai.grinder.api.Theory)24 PropositionalTheory (com.sri.ai.grinder.theory.propositional.PropositionalTheory)23 IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)22 EqualityTheory (com.sri.ai.grinder.theory.equality.EqualityTheory)21 Set (java.util.Set)20 IntensionalSet (com.sri.ai.expresso.api.IntensionalSet)19 CompoundTheory (com.sri.ai.grinder.theory.compound.CompoundTheory)19 CompleteMultiVariableContext (com.sri.ai.grinder.core.constraint.CompleteMultiVariableContext)16 Map (java.util.Map)16 Beta (com.google.common.annotations.Beta)15 Constraint (com.sri.ai.grinder.api.Constraint)15 Expressions.parse (com.sri.ai.expresso.helper.Expressions.parse)14 SingleVariableConstraint (com.sri.ai.grinder.api.SingleVariableConstraint)14