Search in sources :

Example 76 with Context

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

the class SampleCommonInterpreterTest method run.

private Expression run(int sampleSizeN, boolean alwaysSample, String expressionString) {
    SampleCommonInterpreter interpreter = new SampleCommonInterpreter(sampleSizeN, alwaysSample, random);
    Expression expression = parse(expressionString);
    if (expression.numberOfArguments() == 1 && Sets.isIntensionalSet(expression.get(0))) {
        IntensionalSet intensionalSet = (IntensionalSet) expression.get(0);
        IndexExpressionsSet indexExpressions = intensionalSet.getIndexExpressions();
        List<Expression> indices = IndexExpressions.getIndices(indexExpressions);
        if (indices.size() == 1) {
            Expression index = indices.get(0);
            Context intensionalSetContext = context.extendWith(indexExpressions);
            // Ensure condition of correct type is created
            Type indexType = GrinderUtil.getType(index, intensionalSetContext);
            SingleVariableConstraint singleVariableConstraint = null;
            if (indexType instanceof RealExpressoType || indexType instanceof RealInterval) {
                singleVariableConstraint = new SingleVariableLinearRealArithmeticConstraint(index, true, intensionalSetContext.getTheory());
            } else if (indexType instanceof IntegerExpressoType || indexType instanceof IntegerInterval) {
                singleVariableConstraint = new SingleVariableDifferenceArithmeticConstraint(index, true, intensionalSetContext.getTheory());
            }
            if (singleVariableConstraint != null) {
                singleVariableConstraint = singleVariableConstraint.conjoin(intensionalSet.getCondition(), intensionalSetContext);
                intensionalSet = intensionalSet.setCondition(singleVariableConstraint);
                expression = expression.set(0, intensionalSet);
            }
        }
    }
    Expression result = interpreter.apply(expression, context);
    System.out.println("Evaluation with " + sampleSizeN + " samples of " + expressionString + " = " + result.doubleValue() + " (as rational=" + toString(result) + ")");
    return result;
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext) SingleVariableConstraint(com.sri.ai.grinder.sgdpllt.api.SingleVariableConstraint) IntegerInterval(com.sri.ai.expresso.type.IntegerInterval) SampleCommonInterpreter(com.sri.ai.grinder.sgdpllt.interpreter.SampleCommonInterpreter) RealInterval(com.sri.ai.expresso.type.RealInterval) Type(com.sri.ai.expresso.api.Type) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) IntegerExpressoType(com.sri.ai.expresso.type.IntegerExpressoType) IntensionalSet(com.sri.ai.expresso.api.IntensionalSet) Expression(com.sri.ai.expresso.api.Expression) IntegerExpressoType(com.sri.ai.expresso.type.IntegerExpressoType) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet) SingleVariableDifferenceArithmeticConstraint(com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.SingleVariableDifferenceArithmeticConstraint) SingleVariableLinearRealArithmeticConstraint(com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint)

Example 77 with Context

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

the class BoundTest method testNormalize.

@Test
public void testNormalize() {
    Theory theory = new CompoundTheory(new EqualityTheory(false, true), new DifferenceArithmeticTheory(false, false), new LinearRealArithmeticTheory(false, false), new TupleTheory(), new PropositionalTheory());
    Context context = new TrueContext(theory);
    context = context.extendWithSymbolsAndTypes("X", "Boolean");
    context = context.extendWithSymbolsAndTypes("Y", "Boolean");
    //Set of numbers
    Expression one = DefaultSymbol.createSymbol(1);
    Expression two = DefaultSymbol.createSymbol(2);
    Expression three = DefaultSymbol.createSymbol(3);
    Expression setOFNumbers = ExtensionalSets.makeUniSet(one, two, three);
    //Set of functions
    Expression phi1 = parse("if X = true then 1 else if Y = true then 2 else 3");
    Expression phi2 = parse("if X = true then if Y = true then 4 else 5 else 6");
    Expression phi3 = parse("if X = true then 7 else if Y = true then 8 else 9");
    Expression phi4 = parse("if X = true then 10 else if Y = true then 11 else 12");
    Expression setOfFactors = ExtensionalSets.makeUniSet(phi1, phi2, phi3, phi4);
    assertEquals(parse("{if X then 1/7 else if Y then 2/7 else 3/7," + " if X then if Y then 4/21 else 5/21 else 2/7, " + "if X then 7/31 else if Y then 8/31 else 9/31, " + "if X then 10/43 else if Y then 11/43 else 12/43 }"), Bounds.normalize(setOfFactors, theory, context));
    assertEquals(parse("{ 1, 1, 1 }"), Bounds.normalize(setOFNumbers, theory, context));
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext) EqualityTheory(com.sri.ai.grinder.sgdpllt.theory.equality.EqualityTheory) PropositionalTheory(com.sri.ai.grinder.sgdpllt.theory.propositional.PropositionalTheory) DifferenceArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory) EqualityTheory(com.sri.ai.grinder.sgdpllt.theory.equality.EqualityTheory) LinearRealArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.LinearRealArithmeticTheory) Theory(com.sri.ai.grinder.sgdpllt.api.Theory) CompoundTheory(com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory) TupleTheory(com.sri.ai.grinder.sgdpllt.theory.tuple.TupleTheory) Expression(com.sri.ai.expresso.api.Expression) DifferenceArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory) LinearRealArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.LinearRealArithmeticTheory) PropositionalTheory(com.sri.ai.grinder.sgdpllt.theory.propositional.PropositionalTheory) CompoundTheory(com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory) TupleTheory(com.sri.ai.grinder.sgdpllt.theory.tuple.TupleTheory) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext) Test(org.junit.Test)

Example 78 with Context

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

the class BoundTest method testBoundProduct.

@Test
public void testBoundProduct() {
    Theory theory = new CompoundTheory(new EqualityTheory(false, true), new DifferenceArithmeticTheory(false, false), new LinearRealArithmeticTheory(false, false), new TupleTheory(), new PropositionalTheory());
    Context context = new TrueContext(theory);
    context = context.extendWithSymbolsAndTypes("X", "Boolean");
    context = context.extendWithSymbolsAndTypes("Y", "Boolean");
    //Set of numbers
    Expression one = DefaultSymbol.createSymbol(1);
    Expression two = DefaultSymbol.createSymbol(2);
    Expression three = DefaultSymbol.createSymbol(3);
    Expression setOFNumbers = ExtensionalSets.makeUniSet(one, two, three);
    //Set of functions
    Expression phi1 = parse("if X = true then 1 else if Y = true then 2 else 3");
    Expression phi2 = parse("if X = true then if Y = true then 4 else 5 else 6");
    Expression phi3 = parse("if X = true then 7 else if Y = true then 8 else 9");
    Expression phi4 = parse("if X = true then 10 else if Y = true then 11 else 12");
    Expression setOfFactors = ExtensionalSets.makeUniSet(phi1, phi2, phi3, phi4);
    assertEquals(parse("{ if X then 1 else if Y then 4 else 9," + " if X then 2 else if Y then 8 else 18," + " if X then 3 else if Y then 12 else 27," + " if X then if Y then 4 else 5 else if Y then 12 else 18, " + "if X then if Y then 8 else 10 else if Y then 24 else 36," + " if X then if Y then 12 else 15 else if Y then 36 else 54," + " if X then 7 else if Y then 16 else 27," + " if X then 14 else if Y then 32 else 54, " + "if X then 21 else if Y then 48 else 81, " + "if X then 10 else if Y then 22 else 36, " + "if X then 20 else if Y then 44 else 72, " + "if X then 30 else if Y then 66 else 108," + " if X then if Y then 4 else 5 else if Y then 12 else 18, " + "if X then if Y then 8 else 10 else if Y then 24 else 36, " + "if X then if Y then 12 else 15 else if Y then 36 else 54, " + "if X then if Y then 16 else 25 else 36, " + "if X then if Y then 32 else 50 else 72, " + "if X then if Y then 48 else 75 else 108, " + "if X then if Y then 28 else 35 else if Y then 48 else 54, " + "if X then if Y then 56 else 70 else if Y then 96 else 108, " + "if X then if Y then 84 else 105 else if Y then 144 else 162, " + "if X then if Y then 40 else 50 else if Y then 66 else 72, " + "if X then if Y then 80 else 100 else if Y then 132 else 144, " + "if X then if Y then 120 else 150 else if Y then 198 else 216, " + "if X then 7 else if Y then 16 else 27, " + "if X then 14 else if Y then 32 else 54, " + "if X then 21 else if Y then 48 else 81, " + "if X then if Y then 28 else 35 else if Y then 48 else 54, " + "if X then if Y then 56 else 70 else if Y then 96 else 108, " + "if X then if Y then 84 else 105 else if Y then 144 else 162, " + "if X then 49 else if Y then 64 else 81, " + "if X then 98 else if Y then 128 else 162, " + "if X then 147 else if Y then 192 else 243, " + "if X then 70 else if Y then 88 else 108, " + "if X then 140 else if Y then 176 else 216, " + "if X then 210 else if Y then 264 else 324, " + "if X then 10 else if Y then 22 else 36, " + "if X then 20 else if Y then 44 else 72, " + "if X then 30 else if Y then 66 else 108, " + "if X then if Y then 40 else 50 else if Y then 66 else 72, " + "if X then if Y then 80 else 100 else if Y then 132 else 144, " + "if X then if Y then 120 else 150 else if Y then 198 else 216, " + "if X then 70 else if Y then 88 else 108, " + "if X then 140 else if Y then 176 else 216," + " if X then 210 else if Y then 264 else 324, " + "if X then 100 else if Y then 121 else 144, " + "if X then 200 else if Y then 242 else 288, " + "if X then 300 else if Y then 363 else 432 }"), Bounds.boundProduct(theory, context, setOfFactors, setOfFactors, setOFNumbers));
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext) EqualityTheory(com.sri.ai.grinder.sgdpllt.theory.equality.EqualityTheory) PropositionalTheory(com.sri.ai.grinder.sgdpllt.theory.propositional.PropositionalTheory) DifferenceArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory) EqualityTheory(com.sri.ai.grinder.sgdpllt.theory.equality.EqualityTheory) LinearRealArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.LinearRealArithmeticTheory) Theory(com.sri.ai.grinder.sgdpllt.api.Theory) CompoundTheory(com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory) TupleTheory(com.sri.ai.grinder.sgdpllt.theory.tuple.TupleTheory) Expression(com.sri.ai.expresso.api.Expression) DifferenceArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory) LinearRealArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.LinearRealArithmeticTheory) PropositionalTheory(com.sri.ai.grinder.sgdpllt.theory.propositional.PropositionalTheory) CompoundTheory(com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory) TupleTheory(com.sri.ai.grinder.sgdpllt.theory.tuple.TupleTheory) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext) Test(org.junit.Test)

Example 79 with Context

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

the class BoundTest method testApplyFunctionToBound.

@Test
public void testApplyFunctionToBound() {
    Theory theory = new CompoundTheory(new EqualityTheory(false, true), new DifferenceArithmeticTheory(false, false), new LinearRealArithmeticTheory(false, false), new TupleTheory(), new PropositionalTheory());
    Context context = new TrueContext(theory);
    context = context.extendWithSymbolsAndTypes("X", "Boolean");
    context = context.extendWithSymbolsAndTypes("Y", "Boolean");
    //Set of numbers
    Expression one = DefaultSymbol.createSymbol(1);
    Expression two = DefaultSymbol.createSymbol(2);
    Expression three = DefaultSymbol.createSymbol(3);
    Expression setOFNumbers = ExtensionalSets.makeUniSet(one, two, three);
    //Set of functions
    Expression phi1 = parse("if X = true then 1 else if Y = true then 2 else 3");
    Expression phi2 = parse("if X = true then if Y = true then 4 else 5 else 6");
    Expression phi3 = parse("if X = true then 7 else if Y = true then 8 else 9");
    Expression phi4 = parse("if X = true then 10 else if Y = true then 11 else 12");
    Expression setOfFactors = ExtensionalSets.makeUniSet(phi1, phi2, phi3, phi4);
    Expression phi = DefaultSymbol.createSymbol("phi");
    Expression f = parse("13*phi");
    println("f: " + f);
    assertEquals(parse("{ 13, 26, 39 }"), Bounds.applyFunctionToBound(f, phi, setOFNumbers, theory, context));
    assertEquals(parse("{ if X then 13 else if Y then 26 else 39, " + "if X then if Y then 52 else 65 else 78, " + "if X then 91 else if Y then 104 else 117, " + "if X then 130 else if Y then 143 else 156 }"), Bounds.applyFunctionToBound(f, phi, setOfFactors, theory, context));
    println("f(setfac): " + Bounds.applyFunctionToBound(f, phi, setOfFactors, theory, context));
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext) EqualityTheory(com.sri.ai.grinder.sgdpllt.theory.equality.EqualityTheory) PropositionalTheory(com.sri.ai.grinder.sgdpllt.theory.propositional.PropositionalTheory) DifferenceArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory) EqualityTheory(com.sri.ai.grinder.sgdpllt.theory.equality.EqualityTheory) LinearRealArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.LinearRealArithmeticTheory) Theory(com.sri.ai.grinder.sgdpllt.api.Theory) CompoundTheory(com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory) TupleTheory(com.sri.ai.grinder.sgdpllt.theory.tuple.TupleTheory) Expression(com.sri.ai.expresso.api.Expression) DifferenceArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory) LinearRealArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.LinearRealArithmeticTheory) PropositionalTheory(com.sri.ai.grinder.sgdpllt.theory.propositional.PropositionalTheory) CompoundTheory(com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory) TupleTheory(com.sri.ai.grinder.sgdpllt.theory.tuple.TupleTheory) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext) Test(org.junit.Test)

Example 80 with Context

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

the class LambdaBetaReductionSimplifierTest method testSimpleReduction.

@Test
public void testSimpleReduction() {
    Type peopleType = new Categorical("People", 4, arrayList(makeSymbol("ann"), makeSymbol("bob")));
    Context context = new TrueContext();
    context = context.add(peopleType);
    Assert.assertEquals(parse("if X = ann then 0 else if X = bob then 0 else 0"), simplifier.apply(parse("(lambda : if X = ann then 0 else if X = bob then 0 else 0)()"), context));
    Assert.assertEquals(parse("if ann = ann then 0 else if ann = bob then 0 else 0"), simplifier.apply(parse("(lambda X in People : if X = ann then 0 else if X = bob then 0 else 0)(ann)"), context));
    Assert.assertEquals(parse("if ann = ann then 0 else if bob = bob then 0 else 0"), simplifier.apply(parse("(lambda X in People, Y in People : if X = ann then 0 else if Y = bob then 0 else 0)(ann, bob)"), context));
}
Also used : TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext) Context(com.sri.ai.grinder.sgdpllt.api.Context) Type(com.sri.ai.expresso.api.Type) Categorical(com.sri.ai.expresso.type.Categorical) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext) Test(org.junit.Test)

Aggregations

Context (com.sri.ai.grinder.sgdpllt.api.Context)105 Expression (com.sri.ai.expresso.api.Expression)80 Test (org.junit.Test)48 TheoryTestingSupport (com.sri.ai.grinder.sgdpllt.tester.TheoryTestingSupport)36 TrueContext (com.sri.ai.grinder.sgdpllt.core.TrueContext)30 Type (com.sri.ai.expresso.api.Type)28 DifferenceArithmeticTheory (com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory)28 PropositionalTheory (com.sri.ai.grinder.sgdpllt.theory.propositional.PropositionalTheory)22 EqualityTheory (com.sri.ai.grinder.sgdpllt.theory.equality.EqualityTheory)20 CompoundTheory (com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory)18 Constraint (com.sri.ai.grinder.sgdpllt.api.Constraint)17 Map (java.util.Map)17 Beta (com.google.common.annotations.Beta)16 CompleteMultiVariableContext (com.sri.ai.grinder.sgdpllt.core.constraint.CompleteMultiVariableContext)16 Expressions.parse (com.sri.ai.expresso.helper.Expressions.parse)14 Theory (com.sri.ai.grinder.sgdpllt.api.Theory)14 LinearRealArithmeticTheory (com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.LinearRealArithmeticTheory)14 FunctionType (com.sri.ai.expresso.type.FunctionType)13 Util (com.sri.ai.util.Util)13 SingleVariableConstraint (com.sri.ai.grinder.sgdpllt.api.SingleVariableConstraint)12