Search in sources :

Example 41 with IntegerInterval

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

the class FromTableToExpressionFactorConverter method makeTypeExpression.

Expression makeTypeExpression(TableVariable tableVariable) {
    int cardinality = tableVariable.getCardinality();
    Expression typeExpression = new IntegerInterval(0, cardinality - 1).toExpression();
    return typeExpression;
}
Also used : Expression(com.sri.ai.expresso.api.Expression) IntegerInterval(com.sri.ai.expresso.type.IntegerInterval)

Example 42 with IntegerInterval

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

the class AssignmentsSamplingIteratorTest method newSamplingIterator.

private Iterator<Assignment> 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.getTypeOfExpression(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;
    }
    AssignmentsSamplingIterator samplingIterator = new AssignmentsSamplingIterator(Arrays.asList(index), condition, conditionRewriter, random, context);
    Iterator<Assignment> result = nIterator(sampleSize, samplingIterator);
    return result;
}
Also used : Assignment(com.sri.ai.grinder.interpreter.Assignment) TupleType(com.sri.ai.expresso.type.TupleType) 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) Expression(com.sri.ai.expresso.api.Expression) IntegerExpressoType(com.sri.ai.expresso.type.IntegerExpressoType) IntegerInterval(com.sri.ai.expresso.type.IntegerInterval) AssignmentsSamplingIterator(com.sri.ai.grinder.helper.AssignmentsSamplingIterator) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) SingleVariableDifferenceArithmeticConstraint(com.sri.ai.grinder.theory.differencearithmetic.SingleVariableDifferenceArithmeticConstraint) SingleVariableLinearRealArithmeticConstraint(com.sri.ai.grinder.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint) RealInterval(com.sri.ai.expresso.type.RealInterval)

Example 43 with IntegerInterval

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

the class SampleCommonInterpreterTest method run.

private Expression run(int sampleSizeN, boolean alwaysSample, String expressionString) {
    SamplingCommonInterpreter interpreter = new SamplingCommonInterpreter(sampleSizeN, alwaysSample, random);
    Expression expression = parse(expressionString);
    if (expression.numberOfArguments() == 1 && Sets.isIntensionalSet(expression.get(0))) {
        IntensionalSet intensionalSet = (IntensionalSet) expression.get(0);
        IndexExpressionsSet indexExpressions = intensionalSet.getIndexExpressions();
        List<Expression> indices = IndexExpressions.getIndices(indexExpressions);
        if (indices.size() == 1) {
            Expression index = indices.get(0);
            Context intensionalSetContext = context.extendWith(indexExpressions);
            // Ensure condition of correct type is created
            Type indexType = GrinderUtil.getTypeOfExpression(index, intensionalSetContext);
            SingleVariableConstraint singleVariableConstraint = null;
            if (indexType instanceof RealExpressoType || indexType instanceof RealInterval) {
                singleVariableConstraint = new SingleVariableLinearRealArithmeticConstraint(index, true, intensionalSetContext.getTheory());
            } else if (indexType instanceof IntegerExpressoType || indexType instanceof IntegerInterval) {
                singleVariableConstraint = new SingleVariableDifferenceArithmeticConstraint(index, true, intensionalSetContext.getTheory());
            }
            if (singleVariableConstraint != null) {
                singleVariableConstraint = singleVariableConstraint.conjoin(intensionalSet.getCondition(), intensionalSetContext);
                intensionalSet = intensionalSet.setCondition(singleVariableConstraint);
                expression = expression.set(0, intensionalSet);
            }
        }
    }
    Expression result = interpreter.apply(expression, context);
    System.out.println("Evaluation with " + sampleSizeN + " samples of " + expressionString + " = " + result.doubleValue() + " (as rational=" + toString(result) + ")");
    return result;
}
Also used : TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) SingleVariableConstraint(com.sri.ai.grinder.api.SingleVariableConstraint) IntegerInterval(com.sri.ai.expresso.type.IntegerInterval) 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) IntensionalSet(com.sri.ai.expresso.api.IntensionalSet) Expression(com.sri.ai.expresso.api.Expression) SamplingCommonInterpreter(com.sri.ai.grinder.interpreter.SamplingCommonInterpreter) IntegerExpressoType(com.sri.ai.expresso.type.IntegerExpressoType) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet) SingleVariableDifferenceArithmeticConstraint(com.sri.ai.grinder.theory.differencearithmetic.SingleVariableDifferenceArithmeticConstraint) SingleVariableLinearRealArithmeticConstraint(com.sri.ai.grinder.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint)

Example 44 with IntegerInterval

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

the class IntensionalSetConditionSimplifierTest method setUp.

@Before
public void setUp() {
    context = new TrueContext(new CompoundTheory(new DifferenceArithmeticTheory(false, false), new TupleTheory()));
    IntegerInterval nType = new IntegerInterval(1, 10);
    context = (Context) GrinderUtil.extendRegistryWith(map("N", nType.toString()), Arrays.asList(nType), context);
    simplifier = new IntensionalSetConditionSimplifier();
}
Also used : IntensionalSetConditionSimplifier(com.sri.ai.grinder.library.set.IntensionalSetConditionSimplifier) DifferenceArithmeticTheory(com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory) IntegerInterval(com.sri.ai.expresso.type.IntegerInterval) CompoundTheory(com.sri.ai.grinder.theory.compound.CompoundTheory) TrueContext(com.sri.ai.grinder.core.TrueContext) TupleTheory(com.sri.ai.grinder.theory.tuple.TupleTheory) Before(org.junit.Before)

Example 45 with IntegerInterval

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

the class IntegerIntervalTest method testContains.

@Test
public void testContains() {
    IntegerInterval intInterval = new IntegerInterval("Integer");
    Assert.assertTrue(intInterval.contains(parse("0")));
    intInterval = new IntegerInterval("0..4");
    Assert.assertFalse(intInterval.contains(parse("-1")));
    Assert.assertTrue(intInterval.contains(parse("0")));
    Assert.assertTrue(intInterval.contains(parse("1")));
    Assert.assertTrue(intInterval.contains(parse("2")));
    Assert.assertTrue(intInterval.contains(parse("3")));
    Assert.assertTrue(intInterval.contains(parse("4")));
    Assert.assertFalse(intInterval.contains(parse("5")));
    Assert.assertFalse(intInterval.contains(parse("2.5")));
    intInterval = new IntegerInterval("-infinity..4");
    Assert.assertFalse(intInterval.contains(parse("-infinity")));
    Assert.assertTrue(intInterval.contains(parse("-100")));
    Assert.assertTrue(intInterval.contains(parse("4")));
    Assert.assertFalse(intInterval.contains(parse("5")));
    intInterval = new IntegerInterval("0..infinity");
    Assert.assertFalse(intInterval.contains(parse("infinity")));
    Assert.assertFalse(intInterval.contains(parse("-1")));
    Assert.assertTrue(intInterval.contains(parse("0")));
    Assert.assertTrue(intInterval.contains(parse("4")));
    Assert.assertTrue(intInterval.contains(parse("5")));
    Assert.assertTrue(intInterval.contains(parse("500")));
}
Also used : IntegerInterval(com.sri.ai.expresso.type.IntegerInterval) Test(org.junit.Test)

Aggregations

IntegerInterval (com.sri.ai.expresso.type.IntegerInterval)62 Expression (com.sri.ai.expresso.api.Expression)24 Before (org.junit.Before)20 FunctionType (com.sri.ai.expresso.type.FunctionType)18 Test (org.junit.Test)18 Type (com.sri.ai.expresso.api.Type)16 DifferenceArithmeticTheory (com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory)15 DifferenceArithmeticTheory (com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory)15 RealInterval (com.sri.ai.expresso.type.RealInterval)14 TrueContext (com.sri.ai.grinder.core.TrueContext)14 IntegerExpressoType (com.sri.ai.expresso.type.IntegerExpressoType)13 CompoundTheory (com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory)13 Categorical (com.sri.ai.expresso.type.Categorical)12 TupleType (com.sri.ai.expresso.type.TupleType)12 TrueContext (com.sri.ai.grinder.sgdpllt.core.TrueContext)12 CompoundTheory (com.sri.ai.grinder.theory.compound.CompoundTheory)12 RealExpressoType (com.sri.ai.expresso.type.RealExpressoType)10 TupleTheory (com.sri.ai.grinder.theory.tuple.TupleTheory)10 TupleTheory (com.sri.ai.grinder.sgdpllt.theory.tuple.TupleTheory)8 Context (com.sri.ai.grinder.api.Context)7