Search in sources :

Example 6 with DefaultExistentiallyQuantifiedFormula

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

the class DefaultExtensionalBound 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, Bound 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));
    // (there exists) ci in Real
    ArrayList<Expression> listOfCiInReal = new ArrayList<>(listOfB.size());
    for (i = 0; i < n; i++) {
        listOfCiInReal.add(apply(IN, c[i], "Real"));
        context = context.extendWithSymbolsAndTypes(c[i], parse("Real"));
    }
    IndexExpressionsSet thereExistsCiInReal = new ExtensionalIndexExpressionsSet(listOfCiInReal);
    // (for all) variables in their domains
    IndexExpressionsSet forAllVariablesEvaluations = getIndexExpressionsOfFreeVariablesIn(bound, context);
    Expression body = apply(AND, allcibetwen0And1, sumOverCiEqualsOne, convexSum);
    Expression isExtreme = new DefaultExistentiallyQuantifiedFormula(thereExistsCiInReal, body);
    isExtreme = new DefaultUniversallyQuantifiedFormula(forAllVariablesEvaluations, isExtreme);
    // println(isExtreme);
    Expression result = theory.evaluate(isExtreme, context);
    return !result.booleanValue();
}
Also used : ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) DefaultUniversallyQuantifiedFormula(com.sri.ai.expresso.core.DefaultUniversallyQuantifiedFormula) 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)

Aggregations

Expression (com.sri.ai.expresso.api.Expression)6 DefaultExistentiallyQuantifiedFormula (com.sri.ai.expresso.core.DefaultExistentiallyQuantifiedFormula)6 IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)3 ExtensionalIndexExpressionsSet (com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet)3 Util.mapIntoArrayList (com.sri.ai.util.Util.mapIntoArrayList)2 ArrayList (java.util.ArrayList)2 LambdaExpression (com.sri.ai.expresso.api.LambdaExpression)1 Type (com.sri.ai.expresso.api.Type)1 DefaultIntensionalMultiSet (com.sri.ai.expresso.core.DefaultIntensionalMultiSet)1 DefaultLambdaExpression (com.sri.ai.expresso.core.DefaultLambdaExpression)1 DefaultUniversallyQuantifiedFormula (com.sri.ai.expresso.core.DefaultUniversallyQuantifiedFormula)1 Context (com.sri.ai.grinder.api.Context)1 Theory (com.sri.ai.grinder.api.Theory)1 CommonTheory (com.sri.ai.grinder.application.CommonTheory)1 TrueContext (com.sri.ai.grinder.core.TrueContext)1 IndexExpressions (com.sri.ai.grinder.library.indexexpression.IndexExpressions)1 ExpressionVariable (com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionVariable)1 DefaultExpressionVariable (com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionVariable)1