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