Search in sources :

Example 1 with RealInterval

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;
}
Also used : ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) Expression(com.sri.ai.expresso.api.Expression) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) SingleVariableLinearRealArithmeticConstraint(com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint) RealInterval(com.sri.ai.expresso.type.RealInterval)

Example 2 with RealInterval

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

the class GrinderUtilTest method testIsRealIntervalSubtypeOf.

@Test
public void testIsRealIntervalSubtypeOf() {
    RealInterval realInterval = new RealInterval("Real");
    Assert.assertFalse(isTypeSubtypeOf(realInterval, BOOLEAN_TYPE));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, INTEGER_TYPE));
    Assert.assertTrue(isTypeSubtypeOf(realInterval, REAL_TYPE));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new IntegerInterval("Integer")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new IntegerInterval("0..4")));
    Assert.assertTrue(isTypeSubtypeOf(realInterval, new RealInterval("Real")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new RealInterval("[-0.5;4.5]")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new FunctionType(realInterval)));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new FunctionType(realInterval, realInterval)));
    realInterval = new RealInterval("[0;4]");
    Assert.assertFalse(isTypeSubtypeOf(realInterval, INTEGER_TYPE));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new IntegerInterval("0..4")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new IntegerInterval("1..4")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new IntegerInterval("0..3")));
    Assert.assertTrue(isTypeSubtypeOf(realInterval, REAL_TYPE));
    Assert.assertTrue(isTypeSubtypeOf(realInterval, new RealInterval("Real")));
    Assert.assertTrue(isTypeSubtypeOf(realInterval, new RealInterval("[0;4]")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new RealInterval("]0;4]")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new RealInterval("[0;4[")));
    realInterval = new RealInterval("[-infinity;4]");
    Assert.assertFalse(isTypeSubtypeOf(realInterval, INTEGER_TYPE));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new IntegerInterval("0..4")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new IntegerInterval("1..4")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new IntegerInterval("0..3")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new IntegerInterval("-infinity..5")));
    Assert.assertTrue(isTypeSubtypeOf(realInterval, REAL_TYPE));
    Assert.assertTrue(isTypeSubtypeOf(realInterval, new RealInterval("Real")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new RealInterval("[0;4]")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new RealInterval("]0;4]")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new RealInterval("[0;4[")));
    Assert.assertTrue(isTypeSubtypeOf(realInterval, new RealInterval("[-infinity;4]")));
    Assert.assertTrue(isTypeSubtypeOf(realInterval, new RealInterval("]-infinity;4]")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new RealInterval("[-infinity;4[")));
    realInterval = new RealInterval("[0;infinity]");
    Assert.assertFalse(isTypeSubtypeOf(realInterval, INTEGER_TYPE));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new IntegerInterval("0..4")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new IntegerInterval("1..4")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new IntegerInterval("0..3")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new IntegerInterval("-1..infinity")));
    Assert.assertTrue(isTypeSubtypeOf(realInterval, REAL_TYPE));
    Assert.assertTrue(isTypeSubtypeOf(realInterval, new RealInterval("Real")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new RealInterval("[0;4]")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new RealInterval("]0;4]")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new RealInterval("[0;4[")));
    Assert.assertTrue(isTypeSubtypeOf(realInterval, new RealInterval("[0;infinity]")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new RealInterval("]0;infinity]")));
    Assert.assertTrue(isTypeSubtypeOf(realInterval, new RealInterval("[0;infinity[")));
    realInterval = new RealInterval("[-infinity;infinity]");
    Assert.assertFalse(isTypeSubtypeOf(realInterval, INTEGER_TYPE));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new IntegerInterval("0..4")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new IntegerInterval("1..4")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new IntegerInterval("0..3")));
    Assert.assertTrue(isTypeSubtypeOf(realInterval, REAL_TYPE));
    Assert.assertTrue(isTypeSubtypeOf(realInterval, new RealInterval("Real")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new RealInterval("[0;4]")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new RealInterval("]0;4]")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new RealInterval("[0;4[")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new RealInterval("[-infinity;4]")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new RealInterval("]-infinity;4]")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new RealInterval("[-infinity;4[")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new RealInterval("[0;infinity]")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new RealInterval("]0;infinity]")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new RealInterval("[0;infinity[")));
    Assert.assertTrue(isTypeSubtypeOf(realInterval, new RealInterval("[-infinity;infinity]")));
    Assert.assertTrue(isTypeSubtypeOf(realInterval, new RealInterval("]-infinity;infinity]")));
    Assert.assertTrue(isTypeSubtypeOf(realInterval, new RealInterval("[-infinity;infinity[")));
    Assert.assertTrue(isTypeSubtypeOf(realInterval, new RealInterval("]-infinity;infinity[")));
}
Also used : IntegerInterval(com.sri.ai.expresso.type.IntegerInterval) FunctionType(com.sri.ai.expresso.type.FunctionType) RealInterval(com.sri.ai.expresso.type.RealInterval) Test(org.junit.Test)

Example 3 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 4 with RealInterval

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

the class SymbolicShell method main.

public static void main(String[] args) {
    CompoundTheory theory = new CompoundTheory(new EqualityTheory(false, true), new DifferenceArithmeticTheory(false, false), new LinearRealArithmeticTheory(false, false), new TupleTheory(), new PropositionalTheory());
    Context context = new TrueContext(theory);
    context = context.add(BOOLEAN_TYPE);
    context = context.add(new Categorical("People", 1000000, makeSymbol("ann"), makeSymbol("bob"), makeSymbol("ciaran")));
    context = context.add(new IntegerInterval("Integer"));
    context = context.add(new RealInterval("Real"));
    context = context.registerAdditionalSymbolsAndTypes(map(makeSymbol("P"), makeSymbol("Boolean")));
    context = context.registerAdditionalSymbolsAndTypes(map(makeSymbol("Q"), makeSymbol("Boolean")));
    context = context.registerAdditionalSymbolsAndTypes(map(makeSymbol("R"), makeSymbol("Boolean")));
    context = context.registerAdditionalSymbolsAndTypes(map(makeSymbol("S"), makeSymbol("Boolean")));
    context = context.registerAdditionalSymbolsAndTypes(map(makeSymbol("C"), makeSymbol("People")));
    context = context.registerAdditionalSymbolsAndTypes(map(makeSymbol("D"), makeSymbol("People")));
    context = context.registerAdditionalSymbolsAndTypes(map(makeSymbol("E"), makeSymbol("People")));
    context = context.registerAdditionalSymbolsAndTypes(map(makeSymbol("I"), makeSymbol("Integer")));
    context = context.registerAdditionalSymbolsAndTypes(map(makeSymbol("J"), makeSymbol("Integer")));
    context = context.registerAdditionalSymbolsAndTypes(map(makeSymbol("K"), makeSymbol("Integer")));
    context = context.registerAdditionalSymbolsAndTypes(map(makeSymbol("X"), makeSymbol("Real")));
    context = context.registerAdditionalSymbolsAndTypes(map(makeSymbol("Y"), makeSymbol("Real")));
    context = context.registerAdditionalSymbolsAndTypes(map(makeSymbol("Z"), makeSymbol("Real")));
    context = context.registerAdditionalSymbolsAndTypes(map(makeSymbol("T"), parse("(1..5 x 1..5)")));
    ConsoleIterator consoleIterator = getConsole(args);
    help(consoleIterator);
    Collection<String> examples = list("sum({{ (on C in People)  3 }})", "sum({{ (on C in People)  3 :  C != D }})", "product({{ (on C in People)  3 :  C != D }})", "| {{ (on C in People)  3 :  C != D }} |", "| { (on C in People)  tuple(C) :  C != D } |", "max({{ (on C in People)  3 :  C != D }})", "sum({{ (on C in People, D in People)  3 :  C != D }})", "sum({{ (on C in People)  3 :  C != D and C != ann }})", "sum({{ (on C in People, P in Boolean)  3 :  C != ann }})", "sum({{ (on C in People, P in Boolean)  3 :  C != ann and not P }})", "sum({{ (on C in People, D in People)  if C = ann and D != bob then 2 else 0  :  for all E in People : E = ann => C = E }})", "sum({{ (on I in 1..100)  I }})", "sum({{ (on I in 1..100)  I : I != 3 and I != 5 and I != 500 }})", "sum({{ (on I in 1..100)  I : I != J and I != 5 and I != 500 }})", "sum({{ (on I in 1..100)  (I - J)^2 }})", "sum({{ (on I in 1..100)  if I != K then (I - J)^2 else 0 }})", "sum({{ (on I in 1..100)  I : I >= 3 and I < 21 }})", "sum({{ (on I in 1..100)  I : I > J and I < 5 and I < 500 }})", "sum({{ (on I in 1..100)  (I - J)^2 : I < 50 }})", "sum({{ (on X in [0;100])  1 }})", "sum({{ (on X in [0;100[)  1 }})", "sum({{ (on X in ]0;100])  1 }})", "sum({{ (on X in [0;100])  Y }})", "sum({{ (on X in [0;100])  X }})", "sum({{ (on X in [0;100])  X^2 }})", "sum({{ (on X in [0;100])  X + Y }})", "sum({{ (on X in [0;100])  1 : Y < X and X < Z}})", "sum({{ (on X in Real)  1 : 0 <= X and X <= 100 and Y < X and X < Z}})", "for all X in Real : X > 0 or X <= 0", "for all X in ]0;10] : X > 0", "for all X in [0;10] : X > 0", "| X in 1..10 : X < 4 or X > 8 |", "| X in 1..10, Y in 3..5 : (X < 4 or X > 8) and Y != 5 |", "sum( {{ (on T in (1..4 x 1..4)) 10 }})", "sum( {{ (on T in (1..4 x 1..4)) 10 : T != (2, 3) }})", "sum( {{ (on T in (1..4 x 1..4)) 10 : T != (I, J) }})", "sum( {{ (on T in (1..4 x 1..4)) 10 : get(T, 1) != 2 }})");
    for (String example : examples) {
        consoleIterator.getOutputWriter().println(consoleIterator.getPrompt() + example);
        interpretedInputParsedAsExpression(consoleIterator, theory, example, context);
        consoleIterator.getOutputWriter().println("\n");
    }
    while (consoleIterator.hasNext()) {
        String input = consoleIterator.next();
        if (input.equals("")) {
            consoleIterator.getOutputWriter().println();
        } else if (input.startsWith("show")) {
            consoleIterator.getOutputWriter().println("\n" + join(mapIntoList(context.getSymbolsAndTypes().entrySet(), e -> e.getKey() + ": " + e.getValue()), ", ") + "\n");
        } else if (input.equals("debug")) {
            debug = !debug;
            consoleIterator.getOutputWriter().println("\nDebug toggled to " + debug + "\n");
        } else if (input.equals("help")) {
            help(consoleIterator);
        } else {
            context = interpretedInputParsedAsExpression(consoleIterator, theory, input, context);
        }
    }
    consoleIterator.getOutputWriter().println("\nGoodbye.");
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext) Arrays(java.util.Arrays) PropositionalTheory(com.sri.ai.grinder.sgdpllt.theory.propositional.PropositionalTheory) Categorical(com.sri.ai.expresso.type.Categorical) DifferenceArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory) Expression(com.sri.ai.expresso.api.Expression) IntegerInterval(com.sri.ai.expresso.type.IntegerInterval) Util.map(com.sri.ai.util.Util.map) BOOLEAN_TYPE(com.sri.ai.grinder.helper.GrinderUtil.BOOLEAN_TYPE) Expressions.parse(com.sri.ai.expresso.helper.Expressions.parse) OptionParser(joptsimple.OptionParser) GUIConsoleIterator(com.sri.ai.util.console.gui.GUIConsoleIterator) OptionSet(joptsimple.OptionSet) ConsoleIterator(com.sri.ai.util.console.ConsoleIterator) OptionSpec(joptsimple.OptionSpec) Util.join(com.sri.ai.util.Util.join) Collection(java.util.Collection) Util.list(com.sri.ai.util.Util.list) RealInterval(com.sri.ai.expresso.type.RealInterval) Util.mapIntoList(com.sri.ai.util.Util.mapIntoList) EqualityTheory(com.sri.ai.grinder.sgdpllt.theory.equality.EqualityTheory) Context(com.sri.ai.grinder.sgdpllt.api.Context) LinearRealArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.LinearRealArithmeticTheory) Theory(com.sri.ai.grinder.sgdpllt.api.Theory) Beta(com.google.common.annotations.Beta) List(java.util.List) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext) CompoundTheory(com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory) Expressions.makeSymbol(com.sri.ai.expresso.helper.Expressions.makeSymbol) DefaultConsoleIterator(com.sri.ai.util.console.DefaultConsoleIterator) TupleTheory(com.sri.ai.grinder.sgdpllt.theory.tuple.TupleTheory) EqualityTheory(com.sri.ai.grinder.sgdpllt.theory.equality.EqualityTheory) DifferenceArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory) IntegerInterval(com.sri.ai.expresso.type.IntegerInterval) LinearRealArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.LinearRealArithmeticTheory) PropositionalTheory(com.sri.ai.grinder.sgdpllt.theory.propositional.PropositionalTheory) GUIConsoleIterator(com.sri.ai.util.console.gui.GUIConsoleIterator) ConsoleIterator(com.sri.ai.util.console.ConsoleIterator) DefaultConsoleIterator(com.sri.ai.util.console.DefaultConsoleIterator) Categorical(com.sri.ai.expresso.type.Categorical) CompoundTheory(com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory) TupleTheory(com.sri.ai.grinder.sgdpllt.theory.tuple.TupleTheory) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext) RealInterval(com.sri.ai.expresso.type.RealInterval)

Example 5 with RealInterval

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

the class MeasureTest method measure.

private Rational measure(String testIntensionalSetString) {
    Expression testIntensionalSetExpression = parse(testIntensionalSetString);
    Expression properlyConditionedIntensionalSetExpression = testIntensionalSetExpression;
    if (Sets.isIntensionalSet(testIntensionalSetExpression)) {
        IntensionalSet intensionalSet = (IntensionalSet) testIntensionalSetExpression;
        List<Expression> indices = IndexExpressions.getIndices(intensionalSet.getIndexExpressions());
        if (indices.size() == 1) {
            Expression index = indices.get(0);
            Context intensionalSetContext = context.extendWith(intensionalSet.getIndexExpressions());
            Type type = GrinderUtil.getType(index, intensionalSetContext);
            if (type instanceof RealExpressoType || type instanceof RealInterval) {
                SingleVariableLinearRealArithmeticConstraint singleVariableConstraint = new SingleVariableLinearRealArithmeticConstraint(index, true, context.getTheory());
                singleVariableConstraint = (SingleVariableLinearRealArithmeticConstraint) singleVariableConstraint.conjoin(intensionalSet.getCondition(), intensionalSetContext);
                properlyConditionedIntensionalSetExpression = IntensionalSet.make(Sets.isMultiSet(intensionalSet) ? IntensionalSet.MULTI_SET_LABEL : IntensionalSet.UNI_SET_LABEL, intensionalSet.getIndexExpressions(), intensionalSet.getHead(), singleVariableConstraint);
            }
        }
    }
    Rational result = Measure.get(properlyConditionedIntensionalSetExpression, context);
    return result;
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext) Type(com.sri.ai.expresso.api.Type) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) IntensionalSet(com.sri.ai.expresso.api.IntensionalSet) Rational(com.sri.ai.util.math.Rational) Expression(com.sri.ai.expresso.api.Expression) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) SingleVariableLinearRealArithmeticConstraint(com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint) RealInterval(com.sri.ai.expresso.type.RealInterval)

Aggregations

RealInterval (com.sri.ai.expresso.type.RealInterval)22 Expression (com.sri.ai.expresso.api.Expression)13 IntegerInterval (com.sri.ai.expresso.type.IntegerInterval)11 RealExpressoType (com.sri.ai.expresso.type.RealExpressoType)11 FunctionType (com.sri.ai.expresso.type.FunctionType)10 Test (org.junit.Test)8 Type (com.sri.ai.expresso.api.Type)7 IntegerExpressoType (com.sri.ai.expresso.type.IntegerExpressoType)7 TupleType (com.sri.ai.expresso.type.TupleType)6 Categorical (com.sri.ai.expresso.type.Categorical)5 Context (com.sri.ai.grinder.sgdpllt.api.Context)5 SingleVariableLinearRealArithmeticConstraint (com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint)5 IntensionalSet (com.sri.ai.expresso.api.IntensionalSet)4 IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)3 ExtensionalIndexExpressionsSet (com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet)3 SingleVariableDifferenceArithmeticConstraint (com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.SingleVariableDifferenceArithmeticConstraint)3 Rational (com.sri.ai.util.math.Rational)3 Beta (com.google.common.annotations.Beta)2 Expressions.makeSymbol (com.sri.ai.expresso.helper.Expressions.makeSymbol)2 TrueContext (com.sri.ai.grinder.sgdpllt.core.TrueContext)2