Search in sources :

Example 41 with ExtensionalIndexExpressionsSet

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

the class Sets method expandApplicationOfAssociativeCommutativeFunctionToIntensionalSetWithMultipleIndexExpressionsFrom.

private static Expression expandApplicationOfAssociativeCommutativeFunctionToIntensionalSetWithMultipleIndexExpressionsFrom(int i, Expression functor, IntensionalSet intensionalSet, ExtensionalIndexExpressionsSet indexExpressions) {
    Expression result;
    int numberOfIndexExpressions = indexExpressions.getList().size();
    if (i == numberOfIndexExpressions - 1) {
        Expression iThIndexExpression = indexExpressions.getList().get(i);
        ExtensionalIndexExpressionsSet indexExpressionsSetWithIthIndexExpressionOnly = new ExtensionalIndexExpressionsSet(iThIndexExpression);
        result = apply(functor, intensionalSet.setIndexExpressions(indexExpressionsSetWithIthIndexExpressionOnly));
    } else {
        Expression iThIndexExpression = indexExpressions.getList().get(i);
        ExtensionalIndexExpressionsSet indexExpressionsSetWithIthIndexExpressionOnly = new ExtensionalIndexExpressionsSet(iThIndexExpression);
        Expression innerHead = expandApplicationOfAssociativeCommutativeFunctionToIntensionalSetWithMultipleIndexExpressionsFrom(i + 1, functor, intensionalSet, indexExpressions);
        IntensionalSet innerSet = intensionalSet.setHeadAndCondition(innerHead, TRUE);
        innerSet = innerSet.setIndexExpressions(indexExpressionsSetWithIthIndexExpressionOnly);
        result = apply(functor, innerSet);
    }
    return result;
}
Also used : ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) IntensionalSet(com.sri.ai.expresso.api.IntensionalSet) Expression(com.sri.ai.expresso.api.Expression)

Example 42 with ExtensionalIndexExpressionsSet

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

the class Sets method expandApplicationOfAssociativeCommutativeFunction.

/**
	 * Transforms an application of a associative commutative function to an intensional set with one or more indices
	 * to an equivalent expression in which intensional sets have a single index each.
	 * An example is <code>sum( {{ (on X in 1..10, Y in 1..10) 3 | X != Y }} )</code>
	 * being transformed to <code>sum( {{ (on X in 1..10) sum({{ (on Y in 1..10) 3 | X != Y }}) | true }} )</code>
	 * @param functionApplicationOnIntensionalSet
	 * @return
	 */
public static Expression expandApplicationOfAssociativeCommutativeFunction(Expression functionApplicationOnIntensionalSet) {
    IntensionalSet intensionalSet = (IntensionalSet) functionApplicationOnIntensionalSet.get(0);
    ExtensionalIndexExpressionsSet indexExpressions = (ExtensionalIndexExpressionsSet) intensionalSet.getIndexExpressions();
    myAssert(() -> indexExpressions.getList().size() != 0, () -> "There must be at least one index expression");
    Expression result;
    if (indexExpressions.getList().size() == 1) {
        result = functionApplicationOnIntensionalSet;
    } else {
        Expression functor = functionApplicationOnIntensionalSet.getFunctor();
        result = expandApplicationOfAssociativeCommutativeFunctionToIntensionalSetWithMultipleIndexExpressionsFrom(0, functor, intensionalSet, indexExpressions);
    }
    return result;
}
Also used : ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) IntensionalSet(com.sri.ai.expresso.api.IntensionalSet) Expression(com.sri.ai.expresso.api.Expression)

Example 43 with ExtensionalIndexExpressionsSet

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

the class MultiIndexQuantifierEliminator method solve.

default default Expression solve(AssociativeCommutativeGroup group, ExtensionalIndexExpressionsSet indexExpressions, Expression indicesCondition, Expression body, Context context) {
    Triple<Context, ExtensionalIndexExpressionsSet, Expression> extension = context.extendWith(indexExpressions, tuple(indicesCondition, body));
    context = extension.first;
    indexExpressions = extension.second;
    indicesCondition = extension.third.get(0);
    body = extension.third.get(1);
    //		context = context.extendWith(indexExpressions);
    List<Expression> indices = IndexExpressions.getIndices(indexExpressions);
    Expression quantifierFreeExpression = solve(group, indices, indicesCondition, body, context);
    return quantifierFreeExpression;
}
Also used : ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) Expression(com.sri.ai.expresso.api.Expression)

Example 44 with ExtensionalIndexExpressionsSet

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

the class Bounds method isExtremePoint.

/**
	 * Checks if \phi is a convex combination of the elements in bound
	 * @param phi
	 * 			factor
	 * @param bound
	 * @return
	 */
public static boolean isExtremePoint(Expression phi, int indexPhi, Expression bound, Theory theory, Context context) {
    //TODO
    //caro pq recopia a lista toda
    Expression boundWithoutPhi = removeNonDestructively(bound, indexPhi);
    List<Expression> listOfB = getElements(boundWithoutPhi);
    int n = listOfB.size();
    Expression[] c = new Expression[n];
    for (int i = 0; i < n; i++) {
        c[i] = makeSymbol("c" + i);
        context = context.extendWithSymbolsAndTypes("c" + i, "Real");
    }
    // 0<=ci<=1
    ArrayList<Expression> listOfC = new ArrayList<>(listOfB.size());
    for (int i = 0; i < n; i++) {
        Expression cibetwen0And1 = apply(AND, apply(GREATER_THAN_OR_EQUAL_TO, 1, c[i]), apply(GREATER_THAN_OR_EQUAL_TO, c[i], 0));
        listOfC.add(cibetwen0And1);
    }
    Expression allcibetwen0And1 = apply(AND, listOfC);
    //sum over ci =1
    listOfC = new ArrayList<>(Arrays.asList(c));
    Expression sumOverCiEqualsOne = apply(EQUAL, 1, apply(PLUS, listOfC));
    //sum of ci*phi1 = phi
    ArrayList<Expression> prodciphii = new ArrayList<>(listOfB.size());
    int i = 0;
    for (Expression phii : listOfB) {
        prodciphii.add(apply(TIMES, phii, c[i]));
        i++;
    }
    Expression convexSum = apply(EQUAL, phi, apply(PLUS, prodciphii));
    ArrayList<Expression> listOfCiInReal = new ArrayList<>(listOfB.size());
    for (i = 0; i < n; i++) {
        listOfCiInReal.add(apply(IN, c[i], "Real"));
    }
    IndexExpressionsSet thereExistsCiInReal = new ExtensionalIndexExpressionsSet(listOfCiInReal);
    Expression body = apply(AND, allcibetwen0And1, sumOverCiEqualsOne, convexSum);
    Expression isExtreme = new DefaultExistentiallyQuantifiedFormula(thereExistsCiInReal, body);
    if (debug)
        println(isExtreme);
    //Expression result = theory.evaluate(isExtreme, context);
    return true;
}
Also used : ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) Expression(com.sri.ai.expresso.api.Expression) DefaultExistentiallyQuantifiedFormula(com.sri.ai.expresso.core.DefaultExistentiallyQuantifiedFormula) 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 45 with ExtensionalIndexExpressionsSet

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

the class IndexExpressions method makeExtensionalIndexExpressionsSetFromSymbolsAndTypesStrings.

public static ExtensionalIndexExpressionsSet makeExtensionalIndexExpressionsSetFromSymbolsAndTypesStrings(String... symbolsAndTypes) {
    Expression[] symbolsAndTypesExpressions = GrinderUtil.makeListOfSymbolsAndTypesExpressionsFromSymbolsAndTypesStrings(symbolsAndTypes);
    List<Expression> indexExpressions = GrinderUtil.makeIndexExpressionsFromSymbolsAndTypes(symbolsAndTypesExpressions);
    ExtensionalIndexExpressionsSet result = new ExtensionalIndexExpressionsSet(indexExpressions);
    return result;
}
Also used : ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) Expression(com.sri.ai.expresso.api.Expression) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression)

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