Search in sources :

Example 41 with IndexExpressionsSet

use of com.sri.ai.expresso.api.IndexExpressionsSet 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;
}
Also used : Context(com.sri.ai.grinder.api.Context) Rational(com.sri.ai.util.math.Rational) FunctionType(com.sri.ai.expresso.type.FunctionType) DefaultCountingFormula(com.sri.ai.expresso.core.DefaultCountingFormula) RealInterval(com.sri.ai.expresso.type.RealInterval) Type(com.sri.ai.expresso.api.Type) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) FunctionType(com.sri.ai.expresso.type.FunctionType) TupleType(com.sri.ai.expresso.type.TupleType) IntensionalSet(com.sri.ai.expresso.api.IntensionalSet) Expression(com.sri.ai.expresso.api.Expression) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) TupleType(com.sri.ai.expresso.type.TupleType) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet) SingleVariableLinearRealArithmeticConstraint(com.sri.ai.grinder.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint) MeasureOfSingleVariableLinearRealArithmeticConstraintStepSolver(com.sri.ai.grinder.theory.linearrealarithmetic.MeasureOfSingleVariableLinearRealArithmeticConstraintStepSolver)

Example 42 with IndexExpressionsSet

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

the class DefaultExtensionalBound method normalize.

private DefaultExtensionalBound normalize(Bound bound, Theory theory, Context context) {
    if (!bound.isExtensionalBound()) {
        return null;
    }
    List<Expression> listOfBound = getElements(bound);
    if (listOfBound.size() == 0) {
        return null;
    }
    int numberOfExtremes = listOfBound.size();
    ArrayList<Expression> elements = new ArrayList<>(numberOfExtremes);
    for (Expression element : listOfBound) {
        IndexExpressionsSet indices = getIndexExpressionsOfFreeVariablesIn(element, context);
        Expression setOfFactorInstantiations = IntensionalSet.makeMultiSet(indices, // head
        element, // No Condition
        makeSymbol(true));
        Expression sumOnPhi = apply(SUM, setOfFactorInstantiations);
        Expression f = apply("/", element, sumOnPhi);
        Expression evaluation = theory.evaluate(f, context);
        elements.add(evaluation);
    }
    DefaultExtensionalBound fOfBound = new DefaultExtensionalBound(elements);
    // Updating extreme points
    DefaultExtensionalBound result = updateExtremes(fOfBound, theory, context);
    return result;
}
Also used : Expression(com.sri.ai.expresso.api.Expression) Util.mapIntoArrayList(com.sri.ai.util.Util.mapIntoArrayList) ArrayList(java.util.ArrayList) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet)

Example 43 with IndexExpressionsSet

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

the class InversionSimplifier method getIndexAndType.

private static Pair<Expression, Expression> getIndexAndType(Expression functionOnIntensionalSet) {
    IndexExpressionsSet indexExpressionsSet = getIndexExpressions(functionOnIntensionalSet);
    List<Expression> indexExpressionsWithType = IndexExpressions.getIndexExpressionsWithType(indexExpressionsSet);
    if (indexExpressionsWithType.size() != 1) {
        throw new UnsupportedOperationException("Currently only support singular indices");
    }
    Pair<Expression, Expression> result = IndexExpressions.getIndexAndDomain(indexExpressionsWithType.get(0));
    return result;
}
Also used : Expression(com.sri.ai.expresso.api.Expression) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet)

Example 44 with IndexExpressionsSet

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

the class InversionSimplifier method isFunctionOnIntensionalSetWithSingleIndex.

private static boolean isFunctionOnIntensionalSetWithSingleIndex(Object functor, Expression expression) {
    boolean result = false;
    if (((functor == null && Expressions.isFunctionApplicationWithArguments(expression)) || expression.hasFunctor(functor)) && expression.numberOfArguments() == 1) {
        Expression expressionArg1 = expression.get(0);
        if (Sets.isIntensionalSet(expressionArg1)) {
            IntensionalSet intensionalSet = (IntensionalSet) expressionArg1;
            IndexExpressionsSet indexExpressionsSet = intensionalSet.getIndexExpressions();
            List<Expression> indices = IndexExpressions.getIndices(indexExpressionsSet);
            if (indices.size() == 1) {
                result = true;
            }
        }
    }
    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 45 with IndexExpressionsSet

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

the class IntensionalUnionIntersectionEqualToEmptySetSimplifier method standardizeApart.

private static IntensionalSet standardizeApart(IntensionalSet intensionalSet, List<Expression> forAllsSoFar, Context context) {
    IntensionalSet result = intensionalSet;
    if (forAllsSoFar.size() > 0) {
        // May need to standardize apart
        List<Expression> tupleArgs = new ArrayList<>();
        for (Expression forAll : forAllsSoFar) {
            tupleArgs.add(ForAll.getIndexExpression(forAll));
            tupleArgs.add(ForAll.getBody(forAll));
        }
        Expression tupleOfForAlls = Expressions.makeTuple(tupleArgs);
        List<Expression> originalIndexes = IndexExpressions.getIndices(intensionalSet.getIndexExpressions());
        List<Expression> saIndexes = new ArrayList<>();
        for (Expression index : originalIndexes) {
            Expression saIndex = Expressions.primedUntilUnique(index, tupleOfForAlls, context);
            saIndexes.add(saIndex);
        }
        if (!originalIndexes.equals(saIndexes)) {
            // We need to update intensional set with standardized apart indices
            IndexExpressionsSet saIndexExpressionsSet = intensionalSet.getIndexExpressions();
            Expression saHead = intensionalSet.getHead();
            Expression saCondition = intensionalSet.getCondition();
            Context intensionalSetContext = context.extendWith(saIndexExpressionsSet);
            for (int i = 0; i < originalIndexes.size(); i++) {
                Expression originalIndex = originalIndexes.get(i);
                Expression saIndex = saIndexes.get(i);
                if (!originalIndex.equals(saIndex)) {
                    saIndexExpressionsSet = saIndexExpressionsSet.replaceSymbol(originalIndex, saIndex, context);
                    saHead = saHead.replaceAllOccurrences(originalIndex, saIndex, intensionalSetContext);
                    saCondition = saCondition.replaceAllOccurrences(originalIndex, saIndex, intensionalSetContext);
                }
            }
            result = (IntensionalSet) IntensionalSet.intensionalSetOfSameKindAs(intensionalSet, saIndexExpressionsSet, saHead, saCondition);
        }
    }
    return result;
}
Also used : Context(com.sri.ai.grinder.api.Context) IntensionalSet(com.sri.ai.expresso.api.IntensionalSet) Expression(com.sri.ai.expresso.api.Expression) ArrayList(java.util.ArrayList) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet)

Aggregations

IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)79 Expression (com.sri.ai.expresso.api.Expression)78 ExtensionalIndexExpressionsSet (com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet)49 IntensionalSet (com.sri.ai.expresso.api.IntensionalSet)24 LambdaExpression (com.sri.ai.expresso.api.LambdaExpression)23 ArrayList (java.util.ArrayList)20 DefaultLambdaExpression (com.sri.ai.expresso.core.DefaultLambdaExpression)17 Type (com.sri.ai.expresso.api.Type)15 QuantifiedExpression (com.sri.ai.expresso.api.QuantifiedExpression)14 Context (com.sri.ai.grinder.api.Context)13 IndexExpressions (com.sri.ai.grinder.library.indexexpression.IndexExpressions)9 Context (com.sri.ai.grinder.sgdpllt.api.Context)9 List (java.util.List)9 FunctionType (com.sri.ai.expresso.type.FunctionType)8 Util.mapIntoArrayList (com.sri.ai.util.Util.mapIntoArrayList)8 Rational (com.sri.ai.util.math.Rational)7 SyntaxTree (com.sri.ai.expresso.api.SyntaxTree)6 DefaultIntensionalMultiSet (com.sri.ai.expresso.core.DefaultIntensionalMultiSet)6 QuantifiedExpressionWithABody (com.sri.ai.expresso.api.QuantifiedExpressionWithABody)5 DefaultCountingFormula (com.sri.ai.expresso.core.DefaultCountingFormula)5