Search in sources :

Example 11 with RealInterval

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

the class Measure method constructComponentIntensionalSet.

private static Expression constructComponentIntensionalSet(Type indexType, IntensionalSet intensionalSet, Expression additiveIdentityElement, Context intensionalSetContext) {
    Expression conditionedBody = IfThenElse.make(intensionalSet.getCondition(), intensionalSet.getHead(), additiveIdentityElement);
    Expression componentIndex = Expressions.makeUniqueVariable("C", conditionedBody, intensionalSetContext);
    Expression indexExpression = IndexExpressions.makeIndexExpression(componentIndex, Expressions.parse(indexType.getName()));
    Expression intensionalCondition = Expressions.TRUE;
    // NOTE: handle the REAL cases where an SingleVariableLinearRealArithmeticConstraint is expected.
    if (indexType instanceof RealExpressoType || indexType instanceof RealInterval) {
        SingleVariableLinearRealArithmeticConstraint svlraConstraint = new SingleVariableLinearRealArithmeticConstraint(componentIndex, true, intensionalSetContext.getTheory());
        intensionalCondition = svlraConstraint;
    }
    Expression result = IntensionalSet.make(Sets.isMultiSet(intensionalSet) ? IntensionalSet.MULTI_SET_LABEL : IntensionalSet.UNI_SET_LABEL, new ExtensionalIndexExpressionsSet(Arrays.asList(indexExpression)), componentIndex, intensionalCondition);
    return result;
}
Also used : ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) 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 12 with RealInterval

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

the class Measure method get.

public static Rational get(Expression intensionalSetExpression, Context context) {
    Rational result;
    if (Sets.isIntensionalSet(intensionalSetExpression)) {
        IntensionalSet intensionalSet = (IntensionalSet) intensionalSetExpression;
        IndexExpressionsSet indexExpressionsSet = intensionalSet.getIndexExpressions();
        List<Expression> indices = IndexExpressions.getIndices(indexExpressionsSet);
        if (indices.size() == 1) {
            Expression evaluatedResult;
            Expression intensionalSetIndex = indices.get(0);
            Expression intensionalSetHead = intensionalSet.getHead();
            if (!intensionalSetHead.equals(intensionalSetIndex)) {
                throw new UnsupportedOperationException("Index and Head must be the same to calculate the meaure of an Intensional : " + intensionalSet);
            }
            Expression intensionalSetCondition = intensionalSet.getCondition();
            Context intensionalSetContext = context.extendWith(indexExpressionsSet);
            Type indexType = GrinderUtil.getTypeOfExpression(intensionalSetIndex, intensionalSetContext);
            if (intensionalSetCondition.equals(false)) {
                // short circuit known empty sets up front.
                evaluatedResult = Expressions.ZERO;
            } else if (indexType instanceof RealExpressoType || indexType instanceof RealInterval) {
                // NOTE : For Reals can always assume the condition is of this type.
                SingleVariableLinearRealArithmeticConstraint svConstraint = (SingleVariableLinearRealArithmeticConstraint) intensionalSetCondition;
                MeasureOfSingleVariableLinearRealArithmeticConstraintStepSolver realSolver = new MeasureOfSingleVariableLinearRealArithmeticConstraintStepSolver(svConstraint);
                evaluatedResult = realSolver.solve(intensionalSetContext);
            } else if (indexType instanceof FunctionType) {
                if (!intensionalSetCondition.equals(true)) {
                    throw new UnsupportedOperationException("Measure of intensional set with a function type domain currently do not support conditions: " + intensionalSet);
                }
                // measure(co-domain)^measure(domain)
                FunctionType indexFunctionType = (FunctionType) indexType;
                Expression condomainIntensionalSet = constructComponentIntensionalSet(indexFunctionType.getCodomain(), intensionalSet, ZERO, intensionalSetContext);
                Rational codomainMeasure = get(condomainIntensionalSet, intensionalSetContext);
                Rational domainMeasure = Rational.ONE;
                for (Type argDomainType : indexFunctionType.getArgumentTypes()) {
                    Expression argDomainIntensionalSet = constructComponentIntensionalSet(argDomainType, intensionalSet, ZERO, intensionalSetContext);
                    Rational argMeasure = get(argDomainIntensionalSet, intensionalSetContext);
                    domainMeasure = domainMeasure.multiply(argMeasure);
                }
                evaluatedResult = Expressions.makeSymbol(codomainMeasure.pow(domainMeasure.intValueExact()));
            } else if (indexType instanceof TupleType) {
                if (!intensionalSetCondition.equals(true)) {
                    throw new UnsupportedOperationException("Measure of intensional set with a tuple type domain currently do not support conditions: " + intensionalSet);
                }
                // (element_1, ..., element_n) = measure(element_1) * ... * measure(element_n)
                TupleType indexTupleType = (TupleType) indexType;
                Rational elementMeasuresProduct = Rational.ONE;
                for (Type elementType : indexTupleType.getElementTypes()) {
                    Expression elementDomainIntensionalSet = constructComponentIntensionalSet(elementType, intensionalSet, ZERO, intensionalSetContext);
                    Rational elementMeasure = get(elementDomainIntensionalSet, intensionalSetContext);
                    elementMeasuresProduct = elementMeasuresProduct.multiply(elementMeasure);
                }
                evaluatedResult = Expressions.makeSymbol(elementMeasuresProduct);
            } else {
                Expression countingFormula = new DefaultCountingFormula(indexExpressionsSet, intensionalSet.getCondition());
                evaluatedResult = context.getTheory().evaluate(countingFormula, context);
            }
            if (Expressions.isNumber(evaluatedResult)) {
                result = evaluatedResult.rationalValue();
            } else {
                throw new UnsupportedOperationException("Unable to compute a finite measure for: " + intensionalSet + ", got : " + evaluatedResult);
            }
        } else {
            throw new UnsupportedOperationException("Currently only support the measure of single indexed intensional sets: " + intensionalSet);
        }
    } else {
        throw new IllegalArgumentException("Not an intensional set: " + intensionalSetExpression);
    }
    return result;
}
Also used : Context(com.sri.ai.grinder.api.Context) Rational(com.sri.ai.util.math.Rational) FunctionType(com.sri.ai.expresso.type.FunctionType) DefaultCountingFormula(com.sri.ai.expresso.core.DefaultCountingFormula) RealInterval(com.sri.ai.expresso.type.RealInterval) Type(com.sri.ai.expresso.api.Type) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) FunctionType(com.sri.ai.expresso.type.FunctionType) TupleType(com.sri.ai.expresso.type.TupleType) IntensionalSet(com.sri.ai.expresso.api.IntensionalSet) Expression(com.sri.ai.expresso.api.Expression) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) TupleType(com.sri.ai.expresso.type.TupleType) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet) SingleVariableLinearRealArithmeticConstraint(com.sri.ai.grinder.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint) MeasureOfSingleVariableLinearRealArithmeticConstraintStepSolver(com.sri.ai.grinder.theory.linearrealarithmetic.MeasureOfSingleVariableLinearRealArithmeticConstraintStepSolver)

Example 13 with RealInterval

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

the class RealIntervalTest method testContains.

@Test
public void testContains() {
    RealInterval realInterval = new RealInterval("Real");
    Assert.assertTrue(realInterval.contains(parse("0")));
    realInterval = new RealInterval("[0;4]");
    Assert.assertFalse(realInterval.contains(parse("-1")));
    Assert.assertTrue(realInterval.contains(parse("0")));
    Assert.assertTrue(realInterval.contains(parse("1")));
    Assert.assertTrue(realInterval.contains(parse("2")));
    Assert.assertTrue(realInterval.contains(parse("3")));
    Assert.assertTrue(realInterval.contains(parse("4")));
    Assert.assertFalse(realInterval.contains(parse("5")));
    Assert.assertTrue(realInterval.contains(parse("2.5")));
    realInterval = new RealInterval("]0;4[");
    Assert.assertFalse(realInterval.contains(parse("-1")));
    Assert.assertFalse(realInterval.contains(parse("0")));
    Assert.assertTrue(realInterval.contains(parse("1")));
    Assert.assertTrue(realInterval.contains(parse("2")));
    Assert.assertTrue(realInterval.contains(parse("3")));
    Assert.assertFalse(realInterval.contains(parse("4")));
    Assert.assertFalse(realInterval.contains(parse("5")));
    Assert.assertTrue(realInterval.contains(parse("2.5")));
    realInterval = new RealInterval("[-infinity;4]");
    Assert.assertFalse(realInterval.contains(parse("-infinity")));
    Assert.assertTrue(realInterval.contains(parse("-100")));
    Assert.assertTrue(realInterval.contains(parse("4")));
    Assert.assertFalse(realInterval.contains(parse("5")));
    realInterval = new RealInterval("[0;infinity]");
    Assert.assertFalse(realInterval.contains(parse("infinity")));
    Assert.assertFalse(realInterval.contains(parse("-1")));
    Assert.assertTrue(realInterval.contains(parse("0")));
    Assert.assertTrue(realInterval.contains(parse("4")));
    Assert.assertTrue(realInterval.contains(parse("5")));
    Assert.assertTrue(realInterval.contains(parse("500")));
}
Also used : RealInterval(com.sri.ai.expresso.type.RealInterval) Test(org.junit.Test)

Example 14 with RealInterval

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

the class GrinderUtilTest method testIsCategoricalTypeSubtypeOf.

@Test
public void testIsCategoricalTypeSubtypeOf() {
    Categorical categoricalType = new Categorical("TestCategorical1", 10, parse("a"), parse("b"), parse("c"));
    Assert.assertFalse(isTypeSubtypeOf(categoricalType, BOOLEAN_TYPE));
    Assert.assertFalse(isTypeSubtypeOf(categoricalType, INTEGER_TYPE));
    Assert.assertFalse(isTypeSubtypeOf(categoricalType, REAL_TYPE));
    Assert.assertFalse(isTypeSubtypeOf(categoricalType, new IntegerInterval("Integer")));
    Assert.assertFalse(isTypeSubtypeOf(categoricalType, new IntegerInterval("0..4")));
    Assert.assertFalse(isTypeSubtypeOf(categoricalType, new RealInterval("Real")));
    Assert.assertFalse(isTypeSubtypeOf(categoricalType, new RealInterval("[-0.5;4.5]")));
    Assert.assertFalse(isTypeSubtypeOf(categoricalType, new FunctionType(categoricalType)));
    Assert.assertFalse(isTypeSubtypeOf(categoricalType, new FunctionType(categoricalType, categoricalType)));
    // NOTE: Categorical types equality is based on name.
    Assert.assertTrue(isTypeSubtypeOf(categoricalType, new Categorical("TestCategorical1", 10, parse("a"), parse("b"), parse("c"))));
    Assert.assertTrue(isTypeSubtypeOf(categoricalType, new Categorical("TestCategorical1", 5, parse("x"), parse("y"), parse("z"))));
    Assert.assertFalse(isTypeSubtypeOf(categoricalType, new Categorical("TestCategorical2", 10, parse("a"), parse("b"), parse("c"))));
}
Also used : IntegerInterval(com.sri.ai.expresso.type.IntegerInterval) FunctionType(com.sri.ai.expresso.type.FunctionType) Categorical(com.sri.ai.expresso.type.Categorical) RealInterval(com.sri.ai.expresso.type.RealInterval) Test(org.junit.Test)

Example 15 with RealInterval

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

the class GrinderUtilTest method testIsIntegerIntervalSubtypeOf.

@Test
public void testIsIntegerIntervalSubtypeOf() {
    IntegerInterval intInterval = new IntegerInterval("Integer");
    Assert.assertFalse(isTypeSubtypeOf(intInterval, BOOLEAN_TYPE));
    Assert.assertTrue(isTypeSubtypeOf(intInterval, INTEGER_TYPE));
    Assert.assertTrue(isTypeSubtypeOf(intInterval, REAL_TYPE));
    Assert.assertTrue(isTypeSubtypeOf(intInterval, new IntegerInterval("Integer")));
    Assert.assertFalse(isTypeSubtypeOf(intInterval, new IntegerInterval("0..4")));
    Assert.assertTrue(isTypeSubtypeOf(intInterval, new RealInterval("Real")));
    Assert.assertFalse(isTypeSubtypeOf(intInterval, new RealInterval("[-0.5;4.5]")));
    Assert.assertFalse(isTypeSubtypeOf(intInterval, new FunctionType(intInterval)));
    Assert.assertFalse(isTypeSubtypeOf(intInterval, new FunctionType(intInterval, intInterval)));
    intInterval = new IntegerInterval("0..4");
    Assert.assertTrue(isTypeSubtypeOf(intInterval, INTEGER_TYPE));
    Assert.assertTrue(isTypeSubtypeOf(intInterval, new IntegerInterval("0..4")));
    Assert.assertFalse(isTypeSubtypeOf(intInterval, new IntegerInterval("1..4")));
    Assert.assertFalse(isTypeSubtypeOf(intInterval, new IntegerInterval("0..3")));
    Assert.assertTrue(isTypeSubtypeOf(intInterval, REAL_TYPE));
    Assert.assertTrue(isTypeSubtypeOf(intInterval, new RealInterval("Real")));
    Assert.assertTrue(isTypeSubtypeOf(intInterval, new RealInterval("[0;4]")));
    Assert.assertFalse(isTypeSubtypeOf(intInterval, new RealInterval("]0;4]")));
    Assert.assertFalse(isTypeSubtypeOf(intInterval, new RealInterval("[0;4[")));
    intInterval = new IntegerInterval("-infinity..4");
    Assert.assertTrue(isTypeSubtypeOf(intInterval, INTEGER_TYPE));
    Assert.assertFalse(isTypeSubtypeOf(intInterval, new IntegerInterval("0..4")));
    Assert.assertFalse(isTypeSubtypeOf(intInterval, new IntegerInterval("1..4")));
    Assert.assertFalse(isTypeSubtypeOf(intInterval, new IntegerInterval("0..3")));
    Assert.assertTrue(isTypeSubtypeOf(intInterval, new IntegerInterval("-infinity..5")));
    Assert.assertTrue(isTypeSubtypeOf(intInterval, REAL_TYPE));
    Assert.assertTrue(isTypeSubtypeOf(intInterval, new RealInterval("Real")));
    Assert.assertFalse(isTypeSubtypeOf(intInterval, new RealInterval("[0;4]")));
    Assert.assertFalse(isTypeSubtypeOf(intInterval, new RealInterval("]0;4]")));
    Assert.assertFalse(isTypeSubtypeOf(intInterval, new RealInterval("[0;4[")));
    Assert.assertTrue(isTypeSubtypeOf(intInterval, new RealInterval("[-infinity;4]")));
    Assert.assertTrue(isTypeSubtypeOf(intInterval, new RealInterval("]-infinity;4]")));
    Assert.assertFalse(isTypeSubtypeOf(intInterval, new RealInterval("[-infinity;4[")));
    intInterval = new IntegerInterval("0..infinity");
    Assert.assertTrue(isTypeSubtypeOf(intInterval, INTEGER_TYPE));
    Assert.assertFalse(isTypeSubtypeOf(intInterval, new IntegerInterval("0..4")));
    Assert.assertFalse(isTypeSubtypeOf(intInterval, new IntegerInterval("1..4")));
    Assert.assertFalse(isTypeSubtypeOf(intInterval, new IntegerInterval("0..3")));
    Assert.assertTrue(isTypeSubtypeOf(intInterval, new IntegerInterval("-1..infinity")));
    Assert.assertTrue(isTypeSubtypeOf(intInterval, REAL_TYPE));
    Assert.assertTrue(isTypeSubtypeOf(intInterval, new RealInterval("Real")));
    Assert.assertFalse(isTypeSubtypeOf(intInterval, new RealInterval("[0;4]")));
    Assert.assertFalse(isTypeSubtypeOf(intInterval, new RealInterval("]0;4]")));
    Assert.assertFalse(isTypeSubtypeOf(intInterval, new RealInterval("[0;4[")));
    Assert.assertTrue(isTypeSubtypeOf(intInterval, new RealInterval("[0;infinity]")));
    Assert.assertFalse(isTypeSubtypeOf(intInterval, new RealInterval("]0;infinity]")));
    Assert.assertTrue(isTypeSubtypeOf(intInterval, new RealInterval("[0;infinity[")));
    intInterval = new IntegerInterval("-infinity..infinity");
    Assert.assertTrue(isTypeSubtypeOf(intInterval, INTEGER_TYPE));
    Assert.assertFalse(isTypeSubtypeOf(intInterval, new IntegerInterval("0..4")));
    Assert.assertFalse(isTypeSubtypeOf(intInterval, new IntegerInterval("1..4")));
    Assert.assertFalse(isTypeSubtypeOf(intInterval, new IntegerInterval("0..3")));
    Assert.assertTrue(isTypeSubtypeOf(intInterval, REAL_TYPE));
    Assert.assertTrue(isTypeSubtypeOf(intInterval, new RealInterval("Real")));
    Assert.assertFalse(isTypeSubtypeOf(intInterval, new RealInterval("[0;4]")));
    Assert.assertFalse(isTypeSubtypeOf(intInterval, new RealInterval("]0;4]")));
    Assert.assertFalse(isTypeSubtypeOf(intInterval, new RealInterval("[0;4[")));
    Assert.assertFalse(isTypeSubtypeOf(intInterval, new RealInterval("[-infinity;4]")));
    Assert.assertFalse(isTypeSubtypeOf(intInterval, new RealInterval("]-infinity;4]")));
    Assert.assertFalse(isTypeSubtypeOf(intInterval, new RealInterval("[-infinity;4[")));
    Assert.assertFalse(isTypeSubtypeOf(intInterval, new RealInterval("[0;infinity]")));
    Assert.assertFalse(isTypeSubtypeOf(intInterval, new RealInterval("]0;infinity]")));
    Assert.assertFalse(isTypeSubtypeOf(intInterval, new RealInterval("[0;infinity[")));
    Assert.assertTrue(isTypeSubtypeOf(intInterval, new RealInterval("[-infinity;infinity]")));
    Assert.assertTrue(isTypeSubtypeOf(intInterval, new RealInterval("]-infinity;infinity]")));
    Assert.assertTrue(isTypeSubtypeOf(intInterval, new RealInterval("[-infinity;infinity[")));
    Assert.assertTrue(isTypeSubtypeOf(intInterval, new RealInterval("]-infinity;infinity[")));
}
Also used : IntegerInterval(com.sri.ai.expresso.type.IntegerInterval) FunctionType(com.sri.ai.expresso.type.FunctionType) RealInterval(com.sri.ai.expresso.type.RealInterval) Test(org.junit.Test)

Aggregations

RealInterval (com.sri.ai.expresso.type.RealInterval)32 Expression (com.sri.ai.expresso.api.Expression)22 RealExpressoType (com.sri.ai.expresso.type.RealExpressoType)17 Type (com.sri.ai.expresso.api.Type)12 IntegerInterval (com.sri.ai.expresso.type.IntegerInterval)12 FunctionType (com.sri.ai.expresso.type.FunctionType)11 IntegerExpressoType (com.sri.ai.expresso.type.IntegerExpressoType)8 Test (org.junit.Test)8 IntensionalSet (com.sri.ai.expresso.api.IntensionalSet)7 TupleType (com.sri.ai.expresso.type.TupleType)7 SingleVariableLinearRealArithmeticConstraint (com.sri.ai.grinder.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint)6 IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)5 ExtensionalIndexExpressionsSet (com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet)5 Categorical (com.sri.ai.expresso.type.Categorical)5 Rational (com.sri.ai.util.math.Rational)5 Context (com.sri.ai.grinder.api.Context)4 Context (com.sri.ai.grinder.sgdpllt.api.Context)4 SingleVariableLinearRealArithmeticConstraint (com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint)4 TrueContext (com.sri.ai.grinder.core.TrueContext)3 SingleVariableDifferenceArithmeticConstraint (com.sri.ai.grinder.theory.differencearithmetic.SingleVariableDifferenceArithmeticConstraint)3