Search in sources :

Example 26 with ExtensionalIndexExpressionsSet

use of com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet in project aic-expresso by aic-sri-international.

the class DefaultIntensionalBound method simplex.

public static DefaultIntensionalBound simplex(List<Expression> variables, Theory theory, Context context) {
    if (variables.size() == 0) {
        return new DefaultIntensionalBound();
    }
    Expression one = makeSymbol("1");
    Expression zero = makeSymbol("0");
    List<Expression> indexExpressionsList = new ArrayList<>(variables.size());
    // ExtensionalIndexExpressionsSet indexExpressions = GrinderUtil.makeIndexExpressionsForIndicesInListAndTypesInRegistry(Variables, context);
    Expression head = one;
    for (Expression var : variables) {
        Expression index = Expressions.primedUntilUnique(var, var, context);
        Expression type = context.getTypeExpressionOfRegisteredSymbol(var);
        Expression indexExpression = IndexExpressions.makeIndexExpression(index, type);
        indexExpressionsList.add(indexExpression);
        head = apply(IF_THEN_ELSE, apply(EQUAL, var, index), head, zero);
    }
    Expression noCondition = makeSymbol(true);
    ExtensionalIndexExpressionsSet indexExpressions = new ExtensionalIndexExpressionsSet(indexExpressionsList);
    DefaultIntensionalBound simplex = new DefaultIntensionalBound(indexExpressions, head, noCondition);
    return simplex;
}
Also used : ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) Expression(com.sri.ai.expresso.api.Expression) ArrayList(java.util.ArrayList)

Example 27 with ExtensionalIndexExpressionsSet

use of com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet in project aic-expresso by aic-sri-international.

the class DefaultIntensionalBound method summingPhiTimesBound.

private DefaultIntensionalBound summingPhiTimesBound(Expression variablesToBeSummedOut, Expression phi, Bound bound, Context context, Theory theory) {
    if (!bound.isIntensionalBound()) {
        return null;
    }
    DefaultIntensionalBound intensionalBound = (DefaultIntensionalBound) bound;
    ExtensionalIndexExpressionsSet indexExpressions = (ExtensionalIndexExpressionsSet) intensionalBound.getIndexExpressions();
    for (Expression indexExpression : indexExpressions.getList()) {
        Expression index = indexExpression.get(0);
        Expression type = indexExpression.get(1);
        context = context.extendWithSymbolsAndTypes(index, type);
    }
    Expression x = makeSymbol("l");
    Expression f = apply(TIMES, x, phi);
    DefaultIntensionalBound fOfBound = applyFunctionToBound(f, x, bound, theory, context);
    DefaultIntensionalBound result = summingBound(variablesToBeSummedOut, fOfBound, context, theory);
    return result;
}
Also used : ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) Expression(com.sri.ai.expresso.api.Expression)

Example 28 with ExtensionalIndexExpressionsSet

use of com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet in project aic-expresso by aic-sri-international.

the class DefaultIntensionalBound method normalize.

private DefaultIntensionalBound normalize(Bound bound, Theory theory, Context context) {
    if (!bound.isIntensionalBound()) {
        return null;
    }
    DefaultIntensionalBound intensionalBound = (DefaultIntensionalBound) bound;
    ExtensionalIndexExpressionsSet indexExpressions = (ExtensionalIndexExpressionsSet) intensionalBound.getIndexExpressions();
    Expression Head = intensionalBound.getHead();
    Expression condition = intensionalBound.getCondition();
    Set<Expression> hashSetOfindexVariables = Util.set();
    ExtensionalIndexExpressionsSet freeVariablesOfTheHead = (ExtensionalIndexExpressionsSet) getIndexExpressionsOfFreeVariablesIn(Head, context);
    for (Expression indexExpression : indexExpressions.getList()) {
        Symbol index = (Symbol) indexExpression.get(0);
        Expression type = indexExpression.get(1);
        context = context.extendWithSymbolsAndTypes(index, type);
        hashSetOfindexVariables.add(index);
    }
    ArrayList<Expression> variablesToSumOutList = new ArrayList<>();
    for (Expression indexExpression : freeVariablesOfTheHead.getList()) {
        // Gambiarra!
        Expression index = indexExpression.getFunctorOrSymbol();
        if (!hashSetOfindexVariables.contains(index)) {
            variablesToSumOutList.add(indexExpression);
        }
    }
    ExtensionalIndexExpressionsSet variablesToSumOut = new ExtensionalIndexExpressionsSet(variablesToSumOutList);
    Expression setOfInstantiationsOfTheHead = IntensionalSet.makeMultiSet(variablesToSumOut, // head
    Head, // No Condition
    makeSymbol(true));
    Expression sumOnPhi = apply(SUM, setOfInstantiationsOfTheHead);
    // sumOnPhi = theory.evaluate(sumOnPhi, context);
    Expression normalizedHead = apply("/", Head, sumOnPhi);
    Expression evaluation = theory.evaluate(normalizedHead, context);
    DefaultIntensionalBound normalizedIntensionalSet = new DefaultIntensionalBound(indexExpressions, evaluation, condition);
    return normalizedIntensionalSet;
}
Also used : ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) Expression(com.sri.ai.expresso.api.Expression) Symbol(com.sri.ai.expresso.api.Symbol) Expressions.makeSymbol(com.sri.ai.expresso.helper.Expressions.makeSymbol) ArrayList(java.util.ArrayList)

Example 29 with ExtensionalIndexExpressionsSet

use of com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet 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 30 with ExtensionalIndexExpressionsSet

use of com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet in project aic-expresso by aic-sri-international.

the class DefaultIntensionalBound method summingBound.

private DefaultIntensionalBound summingBound(Expression variablesToBeSummedOut, Bound bound, Context context, Theory theory) {
    if (!bound.isIntensionalBound()) {
        return null;
    }
    DefaultIntensionalBound intensionalBound = (DefaultIntensionalBound) bound;
    ExtensionalIndexExpressionsSet indexExpressions = (ExtensionalIndexExpressionsSet) intensionalBound.getIndexExpressions();
    for (Expression indexExpression : indexExpressions.getList()) {
        Expression index = indexExpression.get(0);
        Expression type = indexExpression.get(1);
        context = context.extendWithSymbolsAndTypes(index, type);
    }
    Expression x = makeSymbol("variableX");
    IndexExpressionsSet indices = getIndexExpressionsOfFreeVariablesIn(variablesToBeSummedOut, context);
    Expression setOfFactorInstantiations = IntensionalSet.makeMultiSet(indices, // head
    x, // No Condition
    makeSymbol(true));
    Expression f = apply(SUM, setOfFactorInstantiations);
    DefaultIntensionalBound result = applyFunctionToBound(f, x, bound, theory, context);
    result = normalize(result, theory, context);
    return result;
}
Also used : ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) Expression(com.sri.ai.expresso.api.Expression) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet)

Aggregations

Expression (com.sri.ai.expresso.api.Expression)53 ExtensionalIndexExpressionsSet (com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet)53 IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)24 ArrayList (java.util.ArrayList)14 LambdaExpression (com.sri.ai.expresso.api.LambdaExpression)11 DefaultLambdaExpression (com.sri.ai.expresso.core.DefaultLambdaExpression)10 IntensionalSet (com.sri.ai.expresso.api.IntensionalSet)9 IndexExpressions (com.sri.ai.grinder.library.indexexpression.IndexExpressions)8 Type (com.sri.ai.expresso.api.Type)7 Context (com.sri.ai.grinder.api.Context)7 Expressions (com.sri.ai.expresso.helper.Expressions)5 Theory (com.sri.ai.grinder.api.Theory)5 Util (com.sri.ai.util.Util)5 QuantifiedExpression (com.sri.ai.expresso.api.QuantifiedExpression)4 Symbol (com.sri.ai.expresso.api.Symbol)4 Pair (com.sri.ai.util.base.Pair)4 Collection (java.util.Collection)4 List (java.util.List)4 Map (java.util.Map)4 Set (java.util.Set)4