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")));
}
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;
}
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();
}
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();
}
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;
}
Aggregations