Search in sources :

Example 1 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.getType(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 : Context(com.sri.ai.grinder.sgdpllt.api.Context) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext) 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.sgdpllt.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint) RealInterval(com.sri.ai.expresso.type.RealInterval)

Example 2 with RealExpressoType

use of com.sri.ai.expresso.type.RealExpressoType 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.sgdpllt.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint) RealInterval(com.sri.ai.expresso.type.RealInterval)

Example 3 with RealExpressoType

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

the class AssignmentsSamplingIterator method getTypeToSampleFrom.

public static Type getTypeToSampleFrom(Expression variable, Expression condition, Context context) {
    Type result = GrinderUtil.getTypeOfExpression(variable, context);
    if (result instanceof FunctionType) {
        FunctionType functionType = (FunctionType) result;
        result = new LazySampledFunctionType(functionType.getCodomain(), functionType.getArgumentTypes().toArray(new Type[functionType.getArity()]));
    } else {
        if (condition.equals(false)) {
            result = null;
        } else if (condition.equals(true)) {
        // we leave as is.
        } else if (result instanceof RealExpressoType || result instanceof RealInterval) {
            LinearRealArithmeticTheory theory = new LinearRealArithmeticTheory(true, true);
            SingleVariableLinearRealArithmeticConstraint constraint = (SingleVariableLinearRealArithmeticConstraint) theory.makeSingleVariableConstraint(variable, context);
            constraint = (SingleVariableLinearRealArithmeticConstraint) constraint.conjoin(condition, context);
            IntervalWithMeasureEquivalentToSingleVariableLinearRealArithmeticConstraintStepSolver solver = new IntervalWithMeasureEquivalentToSingleVariableLinearRealArithmeticConstraintStepSolver(constraint);
            Expression realInterval = solver.solve(context);
            if (Sets.isEmptySet(realInterval)) {
                // used to indicate an empty set.
                result = null;
            } else if (ExtensionalSets.isExtensionalSet(realInterval) && ExtensionalSets.isSingleton(realInterval)) {
                String singletonValue = realInterval.get(0).toString();
                result = new RealInterval("[" + singletonValue + ";" + singletonValue + "]");
            } else {
                result = new RealInterval(realInterval.toString());
            }
        } else if (result instanceof IntegerExpressoType || result instanceof IntegerInterval) {
            DifferenceArithmeticTheory theory = new DifferenceArithmeticTheory(true, true);
            SingleVariableDifferenceArithmeticConstraint constraint = (SingleVariableDifferenceArithmeticConstraint) theory.makeSingleVariableConstraint(variable, context);
            constraint = (SingleVariableDifferenceArithmeticConstraint) constraint.conjoin(condition, context);
            ValuesOfSingleVariableDifferenceArithmeticConstraintStepSolver solver = new ValuesOfSingleVariableDifferenceArithmeticConstraintStepSolver(constraint);
            // NOTE: the exceptions set returned here is implicit in the condition so no need to use it here.
            RangeAndExceptionsSet rangeAndExceptionsSet = (RangeAndExceptionsSet) solver.solve(context);
            if (rangeAndExceptionsSet.isEmpty()) {
                // used to indicate an empty set.
                result = null;
            } else if (rangeAndExceptionsSet.isSingleton()) {
                result = new IntegerInterval(rangeAndExceptionsSet.getSingleValue().intValueExact(), rangeAndExceptionsSet.getSingleValue().intValueExact());
            } else {
                result = new IntegerInterval(rangeAndExceptionsSet.getStrictLowerBound().intValueExact() + 1, rangeAndExceptionsSet.getNonStrictUpperBound().intValueExact());
            }
        }
    }
    if (result != null && !result.isSampleUniquelyNamedConstantSupported()) {
        throw new IllegalArgumentException("Unable to sample " + variable + " from " + result);
    }
    return result;
}
Also used : RangeAndExceptionsSet(com.sri.ai.grinder.theory.differencearithmetic.RangeAndExceptionsSet) FunctionType(com.sri.ai.expresso.type.FunctionType) IntegerInterval(com.sri.ai.expresso.type.IntegerInterval) DifferenceArithmeticTheory(com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory) LinearRealArithmeticTheory(com.sri.ai.grinder.theory.linearrealarithmetic.LinearRealArithmeticTheory) IntervalWithMeasureEquivalentToSingleVariableLinearRealArithmeticConstraintStepSolver(com.sri.ai.grinder.theory.linearrealarithmetic.IntervalWithMeasureEquivalentToSingleVariableLinearRealArithmeticConstraintStepSolver) 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) FunctionType(com.sri.ai.expresso.type.FunctionType) ValuesOfSingleVariableDifferenceArithmeticConstraintStepSolver(com.sri.ai.grinder.theory.differencearithmetic.ValuesOfSingleVariableDifferenceArithmeticConstraintStepSolver) Expression(com.sri.ai.expresso.api.Expression) IntegerExpressoType(com.sri.ai.expresso.type.IntegerExpressoType) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) SingleVariableDifferenceArithmeticConstraint(com.sri.ai.grinder.theory.differencearithmetic.SingleVariableDifferenceArithmeticConstraint) SingleVariableLinearRealArithmeticConstraint(com.sri.ai.grinder.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint)

Example 4 with RealExpressoType

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

the class LinearRealArithmeticTheory method symbolIsRealTyped.

private static boolean symbolIsRealTyped(Expression variable, Context context) {
    Type variableType = context.getTypeOfRegisteredSymbol(variable);
    boolean variableIsInteger = variableType instanceof RealExpressoType || variableType instanceof RealInterval;
    return variableIsInteger;
}
Also used : Type(com.sri.ai.expresso.api.Type) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) RealInterval(com.sri.ai.expresso.type.RealInterval)

Example 5 with RealExpressoType

use of com.sri.ai.expresso.type.RealExpressoType 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)

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