Search in sources :

Example 41 with IntensionalSet

use of com.sri.ai.expresso.api.IntensionalSet 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.getTypeOfExpression(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 : TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) 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.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint) RealInterval(com.sri.ai.expresso.type.RealInterval)

Example 42 with IntensionalSet

use of com.sri.ai.expresso.api.IntensionalSet in project aic-expresso by aic-sri-international.

the class DefaultIntensionalBound method applyFunctionToBound.

protected DefaultIntensionalBound applyFunctionToBound(Expression f, Expression variableName, Bound bound, Theory theory, Context context) {
    if (!bound.isIntensionalBound()) {
        return null;
    }
    IntensionalSet intensionalBound = (IntensionalSet) bound;
    IndexExpressionsSet indexExpressions = intensionalBound.getIndexExpressions();
    Expression Head = intensionalBound.getHead();
    Expression condition = intensionalBound.getCondition();
    Expression fOfHead = f.replaceAllOccurrences(variableName, Head, context);
    Expression evaluation = theory.evaluate(fOfHead, context);
    DefaultIntensionalBound result = new DefaultIntensionalBound(indexExpressions, evaluation, condition);
    return result;
}
Also used : IntensionalSet(com.sri.ai.expresso.api.IntensionalSet) Expression(com.sri.ai.expresso.api.Expression) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet)

Example 43 with IntensionalSet

use of com.sri.ai.expresso.api.IntensionalSet in project aic-expresso by aic-sri-international.

the class IntensionalSetToConditionalSimplifier method simplify.

public static Expression simplify(Expression expression, Context context) {
    Expression result = expression;
    if (Sets.isIntensionalSet(expression)) {
        IntensionalSet intensionalSet = (IntensionalSet) expression;
        Expression thereExistsCondition = ThereExists.make(intensionalSet.getIndexExpressions(), intensionalSet.getCondition());
        Expression condition = context.getTheory().evaluate(thereExistsCondition, context);
        Expression thenBranch = expression;
        Expression elseBranch = Sets.EMPTY_SET;
        result = IfThenElse.make(condition, thenBranch, elseBranch, true);
    }
    return result;
}
Also used : IntensionalSet(com.sri.ai.expresso.api.IntensionalSet) Expression(com.sri.ai.expresso.api.Expression)

Example 44 with IntensionalSet

use of com.sri.ai.expresso.api.IntensionalSet in project aic-expresso by aic-sri-international.

the class IntensionalUnionEqualToEmptySetSimplifier method simplify.

public static Expression simplify(Expression expression, Context context) {
    Expression result = expression;
    if (Equality.isEquality(expression) && expression.numberOfArguments() == 2) {
        Expression intensionalUnion = null;
        Expression emptySet = null;
        for (Expression arg : expression.getArguments()) {
            if (Sets.isIntensionalUnion(arg)) {
                intensionalUnion = arg;
            } else if (Sets.isEmptySet(arg)) {
                emptySet = arg;
            }
        }
        // (&cup;<sub>i &isin; D:C</sub>&Phi;) = &empty;
        if (intensionalUnion != null && emptySet != null) {
            IntensionalSet intensionalSet = (IntensionalSet) intensionalUnion.get(0);
            Expression C = intensionalSet.getCondition();
            Expression notC = Not.make(C);
            Expression Phi = intensionalSet.getHead();
            Expression notCorPhi = Or.make(notC, Phi);
            Expression notCorPhiEqualEmptySet = Equality.make(notCorPhi, emptySet);
            // &forall; i &isin; D : (&not;C &or; &Phi; = &empty;)
            result = ForAll.make(intensionalSet.getIndexExpressions(), notCorPhiEqualEmptySet);
        }
    }
    return result;
}
Also used : IntensionalSet(com.sri.ai.expresso.api.IntensionalSet) Expression(com.sri.ai.expresso.api.Expression)

Example 45 with IntensionalSet

use of com.sri.ai.expresso.api.IntensionalSet in project aic-expresso by aic-sri-international.

the class IntersectionIntensionalSetsSimplifier method standardizeApartIntensionalSets.

public static IntensionalSet standardizeApartIntensionalSets(IntensionalSet intensionalSet, IntensionalSet fromOtherIntensionalSet, Context context) {
    IntensionalSet result = intensionalSet;
    IndexExpressionsSet intensionalSetIndexes = intensionalSet.getIndexExpressions();
    IndexExpressionsSet fromOtherIntensionalSetIn = fromOtherIntensionalSet.getIndexExpressions();
    List<Expression> overlappingIndexNames = new ArrayList<>();
    for (Expression intensionalSetIndex : IndexExpressions.getIndices(intensionalSetIndexes)) {
        if (IndexExpressions.indexExpressionsContainIndex(fromOtherIntensionalSetIn, intensionalSetIndex)) {
            overlappingIndexNames.add(intensionalSetIndex);
        }
    }
    if (overlappingIndexNames.size() > 0) {
        Expression combinedExpression = And.make(intensionalSet, fromOtherIntensionalSet);
        List<Expression> newIndexNames = new ArrayList<>();
        for (Expression overlappingIndex : overlappingIndexNames) {
            Expression newIndexName = Expressions.makeUniqueVariable(overlappingIndex.toString(), combinedExpression, context);
            newIndexNames.add(newIndexName);
        }
        SyntaxTree resultSyntaxTree = result.getSyntaxTree();
        for (int i = 0; i < newIndexNames.size(); i++) {
            Expression replaced = overlappingIndexNames.get(i);
            Expression replacement = newIndexNames.get(i);
            resultSyntaxTree = resultSyntaxTree.replaceSubTreesAllOccurrences(replaced.getSyntaxTree(), replacement.getSyntaxTree());
        }
        result = (IntensionalSet) Expressions.makeFromSyntaxTree(resultSyntaxTree);
    }
    return result;
}
Also used : IntensionalSet(com.sri.ai.expresso.api.IntensionalSet) SyntaxTree(com.sri.ai.expresso.api.SyntaxTree) Expression(com.sri.ai.expresso.api.Expression) ArrayList(java.util.ArrayList) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet)

Aggregations

Expression (com.sri.ai.expresso.api.Expression)46 IntensionalSet (com.sri.ai.expresso.api.IntensionalSet)46 IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)24 ExtensionalIndexExpressionsSet (com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet)15 ArrayList (java.util.ArrayList)12 Type (com.sri.ai.expresso.api.Type)11 Context (com.sri.ai.grinder.api.Context)9 Context (com.sri.ai.grinder.sgdpllt.api.Context)9 RealExpressoType (com.sri.ai.expresso.type.RealExpressoType)8 RealInterval (com.sri.ai.expresso.type.RealInterval)8 Rational (com.sri.ai.util.math.Rational)8 TrueContext (com.sri.ai.grinder.core.TrueContext)5 List (java.util.List)5 CountingFormula (com.sri.ai.expresso.api.CountingFormula)4 FunctionType (com.sri.ai.expresso.type.FunctionType)4 IntegerExpressoType (com.sri.ai.expresso.type.IntegerExpressoType)4 IntegerInterval (com.sri.ai.expresso.type.IntegerInterval)4 TupleType (com.sri.ai.expresso.type.TupleType)4 TrueContext (com.sri.ai.grinder.sgdpllt.core.TrueContext)3 Beta (com.google.common.annotations.Beta)2