Search in sources :

Example 6 with RealInterval

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

the class AssignmentsSamplingIteratorTest method testSampleOverRealInterval.

@Test
public void testSampleOverRealInterval() {
    updateContextWithIndexAndType("R", new RealInterval("[1;10]"));
    // Sub-Interval
    Assert.assertEquals("{R=5.30333}:{R=6.588824}:{R=5.856368}", join(":", newSamplingIterator("R", 3, "R > 2 and R < 8")));
    // Singleton
    Assert.assertEquals("{R=2}:{R=2}:{R=2}", join(":", newSamplingIterator("R", 3, "R = 2")));
    // Empty Set
    Assert.assertEquals("", join(":", newSamplingIterator("R", 3, "R = 11")));
    // Broken Interval
    Assert.assertEquals("{R=5.53348}:{R=9.146728}:{R=3.1411}", join(":", newSamplingIterator("R", 3, "R != 1 and R != 3 and R !=4 and R != 5 and R != 6 and R !=7 and R != 8 and R != 10")));
}
Also used : RealInterval(com.sri.ai.expresso.type.RealInterval) Test(org.junit.Test)

Example 7 with RealInterval

use of com.sri.ai.expresso.type.RealInterval 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 8 with RealInterval

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

the class SingleVariableLinearRealArithmeticConstraint method getImplicitPositiveNormalizedAtomsIterator.

@Override
protected /**
 * Returns iterator ranging over implicit positive normalized atoms representing variable bounds.
 */
Iterator<Expression> getImplicitPositiveNormalizedAtomsIterator(Context context) {
    if (cachedImplicitPositiveNormalizedAtoms == null) {
        RealInterval interval = getType(context);
        Expression lowerBound = interval.getLowerBound();
        cachedImplicitPositiveNormalizedAtoms = list();
        if (interval.lowerBoundIsOpen() && !lowerBound.equals("unknown") && !lowerBound.equals(UnaryMinus.make(INFINITY))) {
            cachedImplicitPositiveNormalizedAtoms.add(apply(GREATER_THAN, getVariable(), lowerBound));
        }
        Expression upperBound = interval.getUpperBound();
        if (interval.upperBoundIsOpen() && !upperBound.equals("unknown") && !upperBound.equals(INFINITY)) {
            cachedImplicitPositiveNormalizedAtoms.add(apply(LESS_THAN, getVariable(), upperBound));
        }
    }
    return cachedImplicitPositiveNormalizedAtoms.iterator();
}
Also used : Expression(com.sri.ai.expresso.api.Expression) RealInterval(com.sri.ai.expresso.type.RealInterval)

Example 9 with RealInterval

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

the class SingleVariableLinearRealArithmeticConstraint method getImplicitNegativeNormalizedAtomsIterator.

@Override
protected /**
 * Returns iterator ranging over implicit negative normalized atoms representing variable bounds.
 */
Iterator<Expression> getImplicitNegativeNormalizedAtomsIterator(Context context) {
    if (cachedImplicitNegativeNormalizedAtoms == null) {
        RealInterval interval = getType(context);
        Expression lowerBound = interval.getLowerBound();
        cachedImplicitNegativeNormalizedAtoms = list();
        if (!interval.lowerBoundIsOpen() && !lowerBound.equals("unknown") && !lowerBound.equals(UnaryMinus.make(INFINITY))) {
            cachedImplicitNegativeNormalizedAtoms.add(apply(LESS_THAN, getVariable(), lowerBound));
        // this is the negation of variable >= nonStrictLowerBound. We need to use a negative normalized atom because applications of >= are not considered normalized atoms
        }
        Expression upperBound = interval.getUpperBound();
        if (!interval.upperBoundIsOpen() && !upperBound.equals("unknown") && !upperBound.equals(INFINITY)) {
            cachedImplicitNegativeNormalizedAtoms.add(apply(GREATER_THAN, getVariable(), upperBound));
        // this is the negation of variable <= nonStrictUpperBound. We need to use a negative normalized atom because applications of <= are not considered normalized atoms
        }
    }
    return cachedImplicitNegativeNormalizedAtoms.iterator();
}
Also used : Expression(com.sri.ai.expresso.api.Expression) RealInterval(com.sri.ai.expresso.type.RealInterval)

Example 10 with RealInterval

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

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