Search in sources :

Example 26 with TrueContext

use of com.sri.ai.grinder.core.TrueContext in project aic-expresso by aic-sri-international.

the class CompoundTheoryWithDifferenceArithmeticTest method basicTests.

@Test
public void basicTests() {
    TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(makeRandom(), new CompoundTheory(new EqualityTheory(false, true), new DifferenceArithmeticTheory(false, true), new PropositionalTheory()));
    Expression condition = parse("X = Y and Y = X and P and not Q and P and X = a and X != b");
    Context context = theoryTestingSupport.makeContextWithTestingInformation();
    Constraint constraint = new CompleteMultiVariableContext(theoryTestingSupport.getTheory(), context);
    constraint = constraint.conjoin(condition, context);
    Expression expected = parse("(Y = a) and not Q and P and (X = Y)");
    assertEquals(expected, constraint);
    // nested indices
    Expression expression = parse("sum({{(on I in 1..2, J in 2..3) sum({{ (on I in 1..10, J in 1..2) I + J : I != J }}) }})");
    context = new TrueContext(theoryTestingSupport.getTheory());
    expected = parse("536");
    Expression actual = theoryTestingSupport.getTheory().evaluate(expression, context);
    println(actual);
    assertEquals(expected, actual);
}
Also used : TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) CompleteMultiVariableContext(com.sri.ai.grinder.core.constraint.CompleteMultiVariableContext) CompleteMultiVariableContext(com.sri.ai.grinder.core.constraint.CompleteMultiVariableContext) EqualityTheory(com.sri.ai.grinder.theory.equality.EqualityTheory) Expression(com.sri.ai.expresso.api.Expression) Constraint(com.sri.ai.grinder.api.Constraint) AbstractTheoryTestingSupport(com.sri.ai.grinder.core.constraint.AbstractTheoryTestingSupport) TheoryTestingSupport(com.sri.ai.grinder.tester.TheoryTestingSupport) DifferenceArithmeticTheory(com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory) PropositionalTheory(com.sri.ai.grinder.theory.propositional.PropositionalTheory) CompoundTheory(com.sri.ai.grinder.theory.compound.CompoundTheory) TrueContext(com.sri.ai.grinder.core.TrueContext) AbstractTheoryTest(com.sri.ai.test.grinder.theory.base.AbstractTheoryTest) Test(org.junit.Test)

Example 27 with TrueContext

use of com.sri.ai.grinder.core.TrueContext 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 28 with TrueContext

use of com.sri.ai.grinder.core.TrueContext 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 29 with TrueContext

use of com.sri.ai.grinder.core.TrueContext in project aic-expresso by aic-sri-international.

the class AbstractSingleVariableNumericConstraintFeasibilityRegionStepSolver method applyAndSimplifyWithoutConsideringContextualConstraint.

/**
 * A method for simplifying an expression according to the context's theory, types and global objects,
 * but <i>without</i> considering the contextual constraint,
 * for the purpose of computing information about the step solver that is constant across contexts
 * (that share the same basic information).
 * Note that the context is still an argument, for the sake of providing the basic information,
 * but the contextual constraint is ignored.
 * @param comparison
 * @param arguments
 * @param context
 * @return
 */
protected Expression applyAndSimplifyWithoutConsideringContextualConstraint(String comparison, ArrayList<Expression> arguments, Context context) {
    Expression unsimplifiedAtom = apply(comparison, arguments);
    TrueContext typeContext = new TrueContext(context);
    Expression result = constraint.getTheory().simplify(unsimplifiedAtom, typeContext);
    return result;
}
Also used : Expression(com.sri.ai.expresso.api.Expression) TrueContext(com.sri.ai.grinder.core.TrueContext)

Example 30 with TrueContext

use of com.sri.ai.grinder.core.TrueContext in project aic-expresso by aic-sri-international.

the class GrinderUtil method makeContext.

public static Context makeContext(Map<String, String> mapFromSymbolNameToTypeName, Map<String, String> mapFromCategoricalTypeNameToSizeString, Collection<Type> additionalTypes, Predicate<Expression> isUniquelyNamedConstantPredicate, Theory theory) {
    Context result = (Context) extendRegistryWith(mapFromSymbolNameToTypeName, additionalTypes, mapFromCategoricalTypeNameToSizeString, isUniquelyNamedConstantPredicate, new TrueContext(theory));
    result = result.setIsUniquelyNamedConstantPredicate(isUniquelyNamedConstantPredicate);
    return result;
}
Also used : TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) TrueContext(com.sri.ai.grinder.core.TrueContext)

Aggregations

TrueContext (com.sri.ai.grinder.core.TrueContext)39 DifferenceArithmeticTheory (com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory)28 CompoundTheory (com.sri.ai.grinder.theory.compound.CompoundTheory)25 Context (com.sri.ai.grinder.api.Context)24 Expression (com.sri.ai.expresso.api.Expression)20 TupleTheory (com.sri.ai.grinder.theory.tuple.TupleTheory)18 PropositionalTheory (com.sri.ai.grinder.theory.propositional.PropositionalTheory)15 Before (org.junit.Before)12 EqualityTheory (com.sri.ai.grinder.theory.equality.EqualityTheory)11 IntegerInterval (com.sri.ai.expresso.type.IntegerInterval)10 Test (org.junit.Test)10 LinearRealArithmeticTheory (com.sri.ai.grinder.theory.linearrealarithmetic.LinearRealArithmeticTheory)9 Theory (com.sri.ai.grinder.api.Theory)6 Rewriter (com.sri.ai.grinder.rewriter.api.Rewriter)6 Type (com.sri.ai.expresso.api.Type)3 ExtensionalIndexExpressionsSet (com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet)3 CombiningTopRewriter (com.sri.ai.grinder.rewriter.core.CombiningTopRewriter)3 Exhaustive (com.sri.ai.grinder.rewriter.core.Exhaustive)3 Recursive (com.sri.ai.grinder.rewriter.core.Recursive)3 Random (java.util.Random)3