Search in sources :

Example 26 with Context

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

the class DefaultIntensionalBound method boundProduct.

public static DefaultIntensionalBound boundProduct(Theory theory, Context context, Bound... listOfBounds) {
    if (listOfBounds.length == 0) {
        DefaultIntensionalBound result = new DefaultIntensionalBound();
        return result;
    }
    Set<Expression> alreadyDefined = Util.set();
    alreadyDefined.addAll(context.getSymbols());
    Predicate<Expression> isAlreadyDefined = e -> alreadyDefined.contains(e);
    ArrayList<Expression> productIndexExpressionList = new ArrayList<>();
    Object[] productHeadArray = new Expression[listOfBounds.length];
    Object[] productConditionArray = new Expression[listOfBounds.length];
    int k = 0;
    for (Bound bound : Arrays.asList(listOfBounds)) {
        if (!bound.isIntensionalBound()) {
            return null;
        }
        DefaultIntensionalBound intensionalBound = (DefaultIntensionalBound) bound;
        ExtensionalIndexExpressionsSet indexExpressions = (ExtensionalIndexExpressionsSet) intensionalBound.getIndexExpressions();
        Expression Head = intensionalBound.getHead();
        Expression condition = intensionalBound.getCondition();
        ArrayList<Expression> newIndexExpressionsList = new ArrayList<>(indexExpressions.getList());
        for (int i = 0; i != newIndexExpressionsList.size(); i++) {
            Expression indexExpression = newIndexExpressionsList.get(i);
            Symbol index = (Symbol) indexExpression.get(0);
            Expression type = indexExpression.get(1);
            PairOf<Expression> newIndexAndNewExpressionInScope = Expressions.standardizeApart(index, isAlreadyDefined, Head);
            Expression newIndex = newIndexAndNewExpressionInScope.first;
            Head = newIndexAndNewExpressionInScope.second;
            // type should not contain the index
            Expression newIndexExpression = apply(IN, newIndex, type);
            context = context.extendWithSymbolsAndTypes(newIndex, type);
            newIndexExpressionsList.set(i, newIndexExpression);
            alreadyDefined.add(newIndex);
            for (int j = i + 1; j != newIndexExpressionsList.size(); j++) {
                Expression anotherIndexExpression = newIndexExpressionsList.get(j);
                Expression anotherIndex = anotherIndexExpression.get(0);
                Expression anotherType = anotherIndexExpression.get(1);
                Expression newAnotherType = anotherType.replaceSymbol(index, newIndex, context);
                // anotherIndex is a symbols and does not contain index
                Expression newAnotherIndexExpression = apply(IN, anotherIndex, newAnotherType);
                newIndexExpressionsList.set(j, newAnotherIndexExpression);
            }
        }
        productIndexExpressionList.addAll(newIndexExpressionsList);
        productHeadArray[k] = Head;
        productConditionArray[k] = condition;
        k++;
    }
    Expression productCondition = apply(AND, productConditionArray);
    productCondition = theory.evaluate(productCondition, context);
    Expression productHead = apply(TIMES, productHeadArray);
    productHead = theory.evaluate(productHead, context);
    DefaultIntensionalBound result = new DefaultIntensionalBound(productIndexExpressionList, productHead, productCondition);
    return result;
}
Also used : Arrays(java.util.Arrays) Model(com.sri.ai.grinder.anytime.Model) Expressions(com.sri.ai.expresso.helper.Expressions) PairOf(com.sri.ai.util.base.PairOf) SUM(com.sri.ai.grinder.library.FunctorConstants.SUM) Expression(com.sri.ai.expresso.api.Expression) ArrayList(java.util.ArrayList) EQUAL(com.sri.ai.grinder.library.FunctorConstants.EQUAL) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) Symbol(com.sri.ai.expresso.api.Symbol) Expressions.apply(com.sri.ai.expresso.helper.Expressions.apply) IN(com.sri.ai.grinder.library.FunctorConstants.IN) IndexExpressions(com.sri.ai.grinder.library.indexexpression.IndexExpressions) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet) AND(com.sri.ai.grinder.library.FunctorConstants.AND) Context(com.sri.ai.grinder.api.Context) DefaultExtensionalUniSet(com.sri.ai.expresso.core.DefaultExtensionalUniSet) Set(java.util.Set) IntensionalSet(com.sri.ai.expresso.api.IntensionalSet) IF_THEN_ELSE(com.sri.ai.grinder.library.FunctorConstants.IF_THEN_ELSE) List(java.util.List) Theory(com.sri.ai.grinder.api.Theory) Expressions.makeSymbol(com.sri.ai.expresso.helper.Expressions.makeSymbol) Predicate(com.google.common.base.Predicate) GrinderUtil.getIndexExpressionsOfFreeVariablesIn(com.sri.ai.grinder.helper.GrinderUtil.getIndexExpressionsOfFreeVariablesIn) Util(com.sri.ai.util.Util) TIMES(com.sri.ai.grinder.library.FunctorConstants.TIMES) Symbol(com.sri.ai.expresso.api.Symbol) Expressions.makeSymbol(com.sri.ai.expresso.helper.Expressions.makeSymbol) ArrayList(java.util.ArrayList) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) Expression(com.sri.ai.expresso.api.Expression)

Example 27 with Context

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

the class IntensionalSetConditionSimplifier method simplify.

public static Expression simplify(Expression expression, Context context) {
    Expression result = expression;
    if (Sets.isIntensionalSet(expression)) {
        IntensionalSet intensionalSet = (IntensionalSet) expression;
        Expression condition = intensionalSet.getCondition();
        if (!(condition.equals(Expressions.TRUE) || condition.equals(Expressions.FALSE))) {
            IndexExpressionsSet indexExpressionsSet = intensionalSet.getIndexExpressions();
            Context extendedContext = context.extendWith(indexExpressionsSet);
            Expression evaluatedCondition = context.getTheory().evaluate(condition, extendedContext);
            if (!evaluatedCondition.equals(condition)) {
                result = IntensionalSet.make(Sets.isIntensionalMultiSet(expression) ? IntensionalSet.MULTI_SET_LABEL : IntensionalSet.UNI_SET_LABEL, indexExpressionsSet, intensionalSet.getHead(), evaluatedCondition);
            }
        }
    }
    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) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet)

Example 28 with Context

use of com.sri.ai.grinder.api.Context 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 29 with Context

use of com.sri.ai.grinder.api.Context 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)

Example 30 with Context

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

the class InversionSimplifier method applyInversion.

private static Expression applyInversion(Expression summationIndexedByFunction, Expression summationIndexFunctionName, FunctionType summationIndexFunctionType, List<Expression> originalQuantifierOrder, List<Expression> inversionQuantifierOrder, Context context) {
    Expression result;
    int indexOfSummationIndexedByFunction = inversionQuantifierOrder.indexOf(summationIndexedByFunction);
    // NOTE: only products will bubble up before the summation.
    List<Expression> productsBefore = inversionQuantifierOrder.subList(0, indexOfSummationIndexedByFunction);
    Expression lastQuantifierHead = getHead(originalQuantifierOrder.get(originalQuantifierOrder.size() - 1));
    Context innerContext = context;
    for (Expression quantifier : originalQuantifierOrder) {
        innerContext = innerContext.extendWith(getIndexExpressions(quantifier));
    }
    Context lastQuantifierHeadContext = innerContext;
    // TODO - remove temporary hack which collapses the function's argument domains
    // due to all the domain arguments being treated as constants. Instead should be using
    // set expression types on singletons.
    // Determine which domain arguments from the summation function can be collapsed
    List<Expression> productsBeforeIndices = new ArrayList<>();
    for (Expression productBefore : productsBefore) {
        productsBeforeIndices.add(getIndexAndType(productBefore).first);
    }
    Set<Integer> domainArgsToRemove = new HashSet<>();
    Set<Integer> domainArgsHaveVar = new HashSet<>();
    new SubExpressionsDepthFirstIterator(lastQuantifierHead).forEachRemaining(e -> {
        if (e.hasFunctor(summationIndexFunctionName)) {
            for (int i = 0; i < e.numberOfArguments(); i++) {
                if (lastQuantifierHeadContext.getTheory().isVariable(e.get(0), lastQuantifierHeadContext)) {
                    domainArgsHaveVar.add(i);
                }
                if (productsBeforeIndices.contains(e.get(i)) || Util.thereExists(new SubExpressionsDepthFirstIterator(e.get(i)), es -> productsBeforeIndices.contains(es))) {
                    domainArgsToRemove.add(i);
                }
            }
        }
    });
    // Remove arg positions that were not populated by a variable.
    for (int i = 0; i < summationIndexFunctionType.getArity(); i++) {
        if (!domainArgsHaveVar.contains(i)) {
            domainArgsToRemove.add(i);
        }
    }
    List<Expression> argTypes = new ArrayList<>();
    for (int i = 0; i < summationIndexFunctionType.getArity(); i++) {
        if (!domainArgsToRemove.contains(i)) {
            argTypes.add(parse(summationIndexFunctionType.getArgumentTypes().get(i).getName()));
        }
    }
    Expression codomainType = parse(summationIndexFunctionType.getCodomain().getName());
    Expression summationIndexReducedType;
    if (argTypes.size() == 0) {
        summationIndexReducedType = codomainType;
    } else {
        summationIndexReducedType = FunctionType.make(codomainType, argTypes);
    }
    Expression summationIndex = IndexExpressions.makeIndexExpression(summationIndexFunctionName, summationIndexReducedType);
    Expression phi = lastQuantifierHead.replaceAllOccurrences(e -> {
        Expression r = e;
        if (e.hasFunctor(summationIndexFunctionName)) {
            List<Expression> argsToKeep = new ArrayList<>();
            for (int i = 0; i < e.numberOfArguments(); i++) {
                if (!domainArgsToRemove.contains(i)) {
                    argsToKeep.add(e.get(i));
                }
            }
            if (argsToKeep.size() > 0) {
                r = Expressions.apply(summationIndexFunctionName, argsToKeep);
            } else {
                r = summationIndexFunctionName;
            }
        }
        return r;
    }, lastQuantifierHeadContext);
    Expression summationHead = phi;
    for (int i = indexOfSummationIndexedByFunction + 1; i < inversionQuantifierOrder.size(); i++) {
        Expression quantifier = inversionQuantifierOrder.get(i);
        Expression quantifierIntensionalSet = IntensionalSet.intensionalMultiSet(getIndexExpressions(quantifier), summationHead, getCondition(quantifier));
        summationHead = Expressions.apply(quantifier.getFunctor(), quantifierIntensionalSet);
    }
    Expression innerSummation = IntensionalSet.intensionalMultiSet(new ExtensionalIndexExpressionsSet(summationIndex), summationHead, getCondition(summationIndexedByFunction));
    result = Expressions.apply(FunctorConstants.SUM, innerSummation);
    for (int i = productsBefore.size() - 1; i >= 0; i--) {
        Expression product = productsBefore.get(i);
        product = IntensionalSet.intensionalMultiSet(getIndexExpressions(product), result, getCondition(product));
        result = Expressions.apply(FunctorConstants.PRODUCT, product);
    }
    return result;
}
Also used : Context(com.sri.ai.grinder.api.Context) SubExpressionsDepthFirstIterator(com.sri.ai.expresso.helper.SubExpressionsDepthFirstIterator) Expressions(com.sri.ai.expresso.helper.Expressions) Expression(com.sri.ai.expresso.api.Expression) Sets(com.sri.ai.grinder.library.set.Sets) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) GrinderUtil(com.sri.ai.grinder.helper.GrinderUtil) And(com.sri.ai.grinder.library.boole.And) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) Equality(com.sri.ai.grinder.library.Equality) IndexExpressions(com.sri.ai.grinder.library.indexexpression.IndexExpressions) Expressions.parse(com.sri.ai.expresso.helper.Expressions.parse) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet) Context(com.sri.ai.grinder.api.Context) Pair(com.sri.ai.util.base.Pair) Implication(com.sri.ai.grinder.library.boole.Implication) Type(com.sri.ai.expresso.api.Type) Disequality(com.sri.ai.grinder.library.Disequality) Set(java.util.Set) IntensionalSet(com.sri.ai.expresso.api.IntensionalSet) FunctionType(com.sri.ai.expresso.type.FunctionType) List(java.util.List) ForAll(com.sri.ai.grinder.library.boole.ForAll) Simplifier(com.sri.ai.grinder.rewriter.api.Simplifier) Util(com.sri.ai.util.Util) FunctorConstants(com.sri.ai.grinder.library.FunctorConstants) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) Expression(com.sri.ai.expresso.api.Expression) ArrayList(java.util.ArrayList) SubExpressionsDepthFirstIterator(com.sri.ai.expresso.helper.SubExpressionsDepthFirstIterator) HashSet(java.util.HashSet)

Aggregations

Context (com.sri.ai.grinder.api.Context)132 Expression (com.sri.ai.expresso.api.Expression)100 Test (org.junit.Test)50 TrueContext (com.sri.ai.grinder.core.TrueContext)40 TheoryTestingSupport (com.sri.ai.grinder.tester.TheoryTestingSupport)36 DifferenceArithmeticTheory (com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory)32 Type (com.sri.ai.expresso.api.Type)31 Theory (com.sri.ai.grinder.api.Theory)24 PropositionalTheory (com.sri.ai.grinder.theory.propositional.PropositionalTheory)23 IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)22 EqualityTheory (com.sri.ai.grinder.theory.equality.EqualityTheory)21 Set (java.util.Set)20 IntensionalSet (com.sri.ai.expresso.api.IntensionalSet)19 CompoundTheory (com.sri.ai.grinder.theory.compound.CompoundTheory)19 CompleteMultiVariableContext (com.sri.ai.grinder.core.constraint.CompleteMultiVariableContext)16 Map (java.util.Map)16 Beta (com.google.common.annotations.Beta)15 Constraint (com.sri.ai.grinder.api.Constraint)15 Expressions.parse (com.sri.ai.expresso.helper.Expressions.parse)14 SingleVariableConstraint (com.sri.ai.grinder.api.SingleVariableConstraint)14