Search in sources :

Example 11 with RealExpressoType

use of com.sri.ai.expresso.type.RealExpressoType in project aic-expresso by aic-sri-international.

the class SampleCommonInterpreterTest method run.

private Expression run(int sampleSizeN, boolean alwaysSample, String expressionString) {
    SamplingCommonInterpreter interpreter = new SamplingCommonInterpreter(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.getTypeOfExpression(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 : TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) SingleVariableConstraint(com.sri.ai.grinder.api.SingleVariableConstraint) IntegerInterval(com.sri.ai.expresso.type.IntegerInterval) 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) SamplingCommonInterpreter(com.sri.ai.grinder.interpreter.SamplingCommonInterpreter) 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.theory.differencearithmetic.SingleVariableDifferenceArithmeticConstraint) SingleVariableLinearRealArithmeticConstraint(com.sri.ai.grinder.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint)

Example 12 with RealExpressoType

use of com.sri.ai.expresso.type.RealExpressoType in project aic-expresso by aic-sri-international.

the class MeasureTest method measure.

private Rational measure(String testIntensionalSetString) {
    Expression testIntensionalSetExpression = parse(testIntensionalSetString);
    Expression properlyConditionedIntensionalSetExpression = testIntensionalSetExpression;
    if (Sets.isIntensionalSet(testIntensionalSetExpression)) {
        IntensionalSet intensionalSet = (IntensionalSet) testIntensionalSetExpression;
        List<Expression> indices = IndexExpressions.getIndices(intensionalSet.getIndexExpressions());
        if (indices.size() == 1) {
            Expression index = indices.get(0);
            Context intensionalSetContext = context.extendWith(intensionalSet.getIndexExpressions());
            Type type = GrinderUtil.getTypeOfExpression(index, intensionalSetContext);
            if (type instanceof RealExpressoType || type instanceof RealInterval) {
                SingleVariableLinearRealArithmeticConstraint singleVariableConstraint = new SingleVariableLinearRealArithmeticConstraint(index, true, context.getTheory());
                singleVariableConstraint = (SingleVariableLinearRealArithmeticConstraint) singleVariableConstraint.conjoin(intensionalSet.getCondition(), intensionalSetContext);
                properlyConditionedIntensionalSetExpression = IntensionalSet.make(Sets.isMultiSet(intensionalSet) ? IntensionalSet.MULTI_SET_LABEL : IntensionalSet.UNI_SET_LABEL, intensionalSet.getIndexExpressions(), intensionalSet.getHead(), singleVariableConstraint);
            }
        }
    }
    Rational result = Measure.get(properlyConditionedIntensionalSetExpression, context);
    return result;
}
Also used : TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) Type(com.sri.ai.expresso.api.Type) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) IntensionalSet(com.sri.ai.expresso.api.IntensionalSet) Rational(com.sri.ai.util.math.Rational) Expression(com.sri.ai.expresso.api.Expression) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) SingleVariableLinearRealArithmeticConstraint(com.sri.ai.grinder.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint) RealInterval(com.sri.ai.expresso.type.RealInterval)

Example 13 with RealExpressoType

use of com.sri.ai.expresso.type.RealExpressoType in project aic-expresso by aic-sri-international.

the class TypeTest method testIsFinite.

@Test
public void testIsFinite() {
    // 
    // Categorical type tests
    Assert.assertFalse(new Categorical("UnknownCardCatType", -1).isFinite());
    Assert.assertFalse(new Categorical("InfiniteCardCatType", -2).isFinite());
    Assert.assertTrue(new Categorical("CardCatType", 0).isFinite());
    Assert.assertTrue(new Categorical("CardCatType", 100).isFinite());
    // 
    // Integer type tests
    Assert.assertFalse(new IntegerExpressoType().isFinite());
    // 
    // Real type tests
    Assert.assertFalse(new RealExpressoType().isFinite());
    // 
    // Integer Interval type tests
    Assert.assertFalse(new IntegerInterval("Integer").isFinite());
    Assert.assertFalse(new IntegerInterval("integer_Interval(-infinity, inifinity)").isFinite());
    Assert.assertFalse(new IntegerInterval("integer_Interval(-10, inifinity)").isFinite());
    Assert.assertFalse(new IntegerInterval("integer_Interval(-infinity, 10)").isFinite());
    Assert.assertTrue(new IntegerInterval("integer_Interval(-10, 10)").isFinite());
    // 
    // Real Interval type tests
    Assert.assertFalse(new RealInterval("Real").isFinite());
    Assert.assertFalse(new RealInterval("[-infinity;infinity]").isFinite());
    Assert.assertFalse(new RealInterval("[-10;infinity]").isFinite());
    Assert.assertFalse(new RealInterval("[-infinity;10]").isFinite());
    Assert.assertFalse(new RealInterval("[0;1]").isFinite());
    // 
    // Function Type
    Assert.assertFalse(new FunctionType(new IntegerExpressoType()).isFinite());
    Assert.assertFalse(new FunctionType(new RealExpressoType()).isFinite());
    Assert.assertTrue(new FunctionType(new Categorical("Cat", 10)).isFinite());
    Assert.assertTrue(new FunctionType(new IntegerInterval(1, 3)).isFinite());
    Assert.assertFalse(new FunctionType(new IntegerInterval("Integer")).isFinite());
    Assert.assertFalse(new FunctionType(new RealInterval("Real")).isFinite());
    Assert.assertTrue(new FunctionType(new TupleType(new IntegerInterval(1, 3))).isFinite());
    Assert.assertFalse(new FunctionType(new TupleType(new RealInterval("Real"))).isFinite());
    // 
    Assert.assertFalse(new FunctionType(new IntegerExpressoType(), new Categorical("Cat", 10)).isFinite());
    Assert.assertFalse(new FunctionType(new IntegerExpressoType(), new RealExpressoType()).isFinite());
    Assert.assertFalse(new FunctionType(new RealExpressoType(), new IntegerExpressoType()).isFinite());
    Assert.assertFalse(new FunctionType(new Categorical("Cat", 10), new IntegerExpressoType()).isFinite());
    Assert.assertFalse(new FunctionType(new Categorical("Cat", 10), new RealExpressoType()).isFinite());
    Assert.assertFalse(new FunctionType(new IntegerInterval("Integer"), new IntegerExpressoType()).isFinite());
    Assert.assertTrue(new FunctionType(new IntegerInterval(1, 2), new IntegerInterval(3, 5)).isFinite());
    Assert.assertFalse(new FunctionType(new IntegerInterval("Integer"), new RealExpressoType()).isFinite());
    Assert.assertFalse(new FunctionType(new RealInterval("Real")).isFinite());
    Assert.assertFalse(new FunctionType(new RealInterval("Real"), new IntegerExpressoType()).isFinite());
    // 
    // Tuple Type
    Assert.assertFalse(new TupleType(new IntegerExpressoType()).isFinite());
    Assert.assertFalse(new TupleType(new RealExpressoType()).isFinite());
    Assert.assertTrue(new TupleType(new Categorical("Cat", 10)).isFinite());
    Assert.assertTrue(new TupleType(new IntegerInterval(1, 3)).isFinite());
    Assert.assertFalse(new TupleType(new IntegerInterval("Integer")).isFinite());
    Assert.assertFalse(new TupleType(new RealInterval("Real")).isFinite());
    // 
    Assert.assertFalse(new TupleType(new IntegerExpressoType(), new Categorical("Cat", 10)).isFinite());
    Assert.assertFalse(new TupleType(new IntegerExpressoType(), new RealExpressoType()).isFinite());
    Assert.assertFalse(new TupleType(new RealExpressoType(), new IntegerExpressoType()).isFinite());
    Assert.assertFalse(new TupleType(new Categorical("Cat", 10), new IntegerExpressoType()).isFinite());
    Assert.assertFalse(new TupleType(new Categorical("Cat", 10), new RealExpressoType()).isFinite());
    Assert.assertFalse(new TupleType(new IntegerInterval("Integer"), new IntegerExpressoType()).isFinite());
    Assert.assertTrue(new TupleType(new IntegerInterval(1, 2), new IntegerInterval(3, 5)).isFinite());
    Assert.assertFalse(new TupleType(new IntegerInterval("Integer"), new RealExpressoType()).isFinite());
    Assert.assertFalse(new TupleType(new RealInterval("Real")).isFinite());
    Assert.assertFalse(new TupleType(new RealInterval("Real"), new IntegerExpressoType()).isFinite());
}
Also used : IntegerExpressoType(com.sri.ai.expresso.type.IntegerExpressoType) IntegerInterval(com.sri.ai.expresso.type.IntegerInterval) FunctionType(com.sri.ai.expresso.type.FunctionType) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) TupleType(com.sri.ai.expresso.type.TupleType) Categorical(com.sri.ai.expresso.type.Categorical) RealInterval(com.sri.ai.expresso.type.RealInterval) Test(org.junit.Test)

Example 14 with RealExpressoType

use of com.sri.ai.expresso.type.RealExpressoType in project aic-expresso by aic-sri-international.

the class TypeTest method testIsDiscrete.

@Test
public void testIsDiscrete() {
    // 
    // Categorical type tests
    Assert.assertTrue(new Categorical("UnknownCardCatType", -1).isDiscrete());
    Assert.assertTrue(new Categorical("InfiniteCardCatType", -2).isDiscrete());
    Assert.assertTrue(new Categorical("CardCatType", 0).isDiscrete());
    Assert.assertTrue(new Categorical("CardCatType", 100).isDiscrete());
    // 
    // Integer type tests
    Assert.assertTrue(new IntegerExpressoType().isDiscrete());
    // 
    // Real type tests
    Assert.assertFalse(new RealExpressoType().isDiscrete());
    // 
    // Integer Interval type tests
    Assert.assertTrue(new IntegerInterval("Integer").isDiscrete());
    Assert.assertTrue(new IntegerInterval("integer_Interval(-infinity, inifinity)").isDiscrete());
    Assert.assertTrue(new IntegerInterval("integer_Interval(-10, inifinity)").isDiscrete());
    Assert.assertTrue(new IntegerInterval("integer_Interval(-infinity, 10)").isDiscrete());
    Assert.assertTrue(new IntegerInterval("integer_Interval(-10, 10)").isDiscrete());
    // 
    // Real Interval type tests
    Assert.assertFalse(new RealInterval("Real").isDiscrete());
    Assert.assertFalse(new RealInterval("[-infinity;infinity]").isDiscrete());
    Assert.assertFalse(new RealInterval("[-10;infinity]").isDiscrete());
    Assert.assertFalse(new RealInterval("[-infinity;10]").isDiscrete());
    Assert.assertFalse(new RealInterval("[0;1]").isDiscrete());
    // 
    // Function Type
    Assert.assertTrue(new FunctionType(new IntegerExpressoType()).isDiscrete());
    Assert.assertFalse(new FunctionType(new RealExpressoType()).isDiscrete());
    Assert.assertTrue(new FunctionType(new Categorical("Cat", 10)).isDiscrete());
    Assert.assertTrue(new FunctionType(new IntegerInterval("Integer")).isDiscrete());
    Assert.assertFalse(new FunctionType(new RealInterval("Real")).isDiscrete());
    Assert.assertTrue(new FunctionType(new TupleType(new IntegerExpressoType())).isDiscrete());
    Assert.assertFalse(new FunctionType(new TupleType(new RealInterval("Real"))).isDiscrete());
    // 
    Assert.assertTrue(new FunctionType(new IntegerExpressoType(), new Categorical("Cat", 10)).isDiscrete());
    Assert.assertFalse(new FunctionType(new IntegerExpressoType(), new RealExpressoType()).isDiscrete());
    Assert.assertFalse(new FunctionType(new RealExpressoType(), new IntegerExpressoType()).isDiscrete());
    Assert.assertTrue(new FunctionType(new Categorical("Cat", 10), new IntegerExpressoType()).isDiscrete());
    Assert.assertFalse(new FunctionType(new Categorical("Cat", 10), new RealExpressoType()).isDiscrete());
    Assert.assertTrue(new FunctionType(new IntegerInterval("Integer"), new IntegerExpressoType()).isDiscrete());
    Assert.assertFalse(new FunctionType(new IntegerInterval("Integer"), new RealExpressoType()).isDiscrete());
    Assert.assertFalse(new FunctionType(new RealInterval("Real")).isDiscrete());
    Assert.assertFalse(new FunctionType(new RealInterval("Real"), new IntegerExpressoType()).isDiscrete());
    // 
    // Tuple Type
    Assert.assertTrue(new TupleType().isDiscrete());
    Assert.assertTrue(new TupleType(new IntegerExpressoType()).isDiscrete());
    Assert.assertFalse(new TupleType(new RealExpressoType()).isDiscrete());
    Assert.assertTrue(new TupleType(new Categorical("Cat", 10)).isDiscrete());
    Assert.assertTrue(new TupleType(new IntegerInterval("Integer")).isDiscrete());
    Assert.assertFalse(new TupleType(new RealInterval("Real")).isDiscrete());
    // 
    Assert.assertTrue(new TupleType(new IntegerExpressoType(), new Categorical("Cat", 10)).isDiscrete());
    Assert.assertFalse(new TupleType(new IntegerExpressoType(), new RealExpressoType()).isDiscrete());
    Assert.assertFalse(new TupleType(new RealExpressoType(), new IntegerExpressoType()).isDiscrete());
    Assert.assertTrue(new TupleType(new Categorical("Cat", 10), new IntegerExpressoType()).isDiscrete());
    Assert.assertFalse(new TupleType(new Categorical("Cat", 10), new RealExpressoType()).isDiscrete());
    Assert.assertTrue(new TupleType(new IntegerInterval("Integer"), new IntegerExpressoType()).isDiscrete());
    Assert.assertFalse(new TupleType(new IntegerInterval("Integer"), new RealExpressoType()).isDiscrete());
    Assert.assertFalse(new TupleType(new RealInterval("Real")).isDiscrete());
    Assert.assertFalse(new TupleType(new RealInterval("Real"), new IntegerExpressoType()).isDiscrete());
}
Also used : IntegerExpressoType(com.sri.ai.expresso.type.IntegerExpressoType) IntegerInterval(com.sri.ai.expresso.type.IntegerInterval) FunctionType(com.sri.ai.expresso.type.FunctionType) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) TupleType(com.sri.ai.expresso.type.TupleType) Categorical(com.sri.ai.expresso.type.Categorical) RealInterval(com.sri.ai.expresso.type.RealInterval) Test(org.junit.Test)

Example 15 with RealExpressoType

use of com.sri.ai.expresso.type.RealExpressoType in project aic-expresso by aic-sri-international.

the class SingleVariableLinearRealArithmeticConstraint method getType.

/**
 * Returns the {@link RealInterval} type of the constraint's variable.
 * @param context
 * @return
 */
public RealInterval getType(Context context) {
    if (cachedType == null) {
        Expression variableTypeExpression = getVariableTypeExpression(context);
        Type type = context.getTypeFromTypeExpression(variableTypeExpression);
        if (type instanceof RealExpressoType) {
            cachedType = new RealInterval("]-infinity;infinity[");
        // represents Real as real interval for uniformity
        } else {
            cachedType = (RealInterval) type;
        }
    }
    return cachedType;
}
Also used : Type(com.sri.ai.expresso.api.Type) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) Expression(com.sri.ai.expresso.api.Expression) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) RealInterval(com.sri.ai.expresso.type.RealInterval)

Aggregations

RealExpressoType (com.sri.ai.expresso.type.RealExpressoType)16 RealInterval (com.sri.ai.expresso.type.RealInterval)16 Expression (com.sri.ai.expresso.api.Expression)12 Type (com.sri.ai.expresso.api.Type)11 FunctionType (com.sri.ai.expresso.type.FunctionType)7 IntegerExpressoType (com.sri.ai.expresso.type.IntegerExpressoType)7 IntegerInterval (com.sri.ai.expresso.type.IntegerInterval)7 IntensionalSet (com.sri.ai.expresso.api.IntensionalSet)6 TupleType (com.sri.ai.expresso.type.TupleType)6 SingleVariableLinearRealArithmeticConstraint (com.sri.ai.grinder.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint)6 IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)4 ExtensionalIndexExpressionsSet (com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet)4 SingleVariableLinearRealArithmeticConstraint (com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint)4 Rational (com.sri.ai.util.math.Rational)4 Context (com.sri.ai.grinder.api.Context)3 Context (com.sri.ai.grinder.sgdpllt.api.Context)3 SingleVariableDifferenceArithmeticConstraint (com.sri.ai.grinder.theory.differencearithmetic.SingleVariableDifferenceArithmeticConstraint)3 DefaultCountingFormula (com.sri.ai.expresso.core.DefaultCountingFormula)2 Categorical (com.sri.ai.expresso.type.Categorical)2 TrueContext (com.sri.ai.grinder.core.TrueContext)2