use of com.sri.ai.expresso.type.IntegerExpressoType in project aic-expresso by aic-sri-international.
the class SingleVariableDifferenceArithmeticConstraint method getType.
/**
* Returns the {@link IntegerInterval} type of the constraint's variable.
* @param context
* @return
*/
public IntegerInterval getType(Context context) {
if (cachedType == null) {
Expression variableTypeExpression = getVariableTypeExpression(context);
Type type = context.getType(variableTypeExpression);
if (type instanceof IntegerExpressoType) {
cachedType = new IntegerInterval("-infinity..infinity");
// represents Integer as integer interval for uniformity
} else {
cachedType = (IntegerInterval) type;
}
}
return cachedType;
}
use of com.sri.ai.expresso.type.IntegerExpressoType 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.getType(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) {
IntervalWithMeasureEquivalentToSingleVariableLinearRealArithmeticConstraintStepSolver solver = new IntervalWithMeasureEquivalentToSingleVariableLinearRealArithmeticConstraintStepSolver((SingleVariableLinearRealArithmeticConstraint) condition);
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) {
ValuesOfSingleVariableDifferenceArithmeticConstraintStepSolver solver = new ValuesOfSingleVariableDifferenceArithmeticConstraintStepSolver((SingleVariableDifferenceArithmeticConstraint) condition);
// 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;
}
use of com.sri.ai.expresso.type.IntegerExpressoType in project aic-expresso by aic-sri-international.
the class AssignmentsSamplingIteratorTest method newSamplingIterator.
private Iterator<Map<Expression, Expression>> newSamplingIterator(String indexString, int sampleSize, String conditionString) {
Expression index = parse(indexString);
Expression condition = parse(conditionString);
// Ensure condition of correct type is created
Type indexType = GrinderUtil.getType(index, context);
if (indexType instanceof RealExpressoType || indexType instanceof RealInterval) {
SingleVariableLinearRealArithmeticConstraint svlraConstraint = new SingleVariableLinearRealArithmeticConstraint(index, true, context.getTheory());
svlraConstraint = (SingleVariableLinearRealArithmeticConstraint) svlraConstraint.conjoin(condition, context);
condition = svlraConstraint;
} else if (indexType instanceof IntegerExpressoType || indexType instanceof IntegerInterval) {
SingleVariableDifferenceArithmeticConstraint svdaConstraint = new SingleVariableDifferenceArithmeticConstraint(index, true, context.getTheory());
svdaConstraint = (SingleVariableDifferenceArithmeticConstraint) svdaConstraint.conjoin(condition, context);
condition = svdaConstraint;
}
Iterator<Map<Expression, Expression>> result = new AssignmentsSamplingIterator(Arrays.asList(index), sampleSize, condition, conditionRewriter, random, context);
return result;
}
use of com.sri.ai.expresso.type.IntegerExpressoType 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());
}
use of com.sri.ai.expresso.type.IntegerExpressoType 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());
}
Aggregations