Search in sources :

Example 51 with ExtensionalIndexExpressionsSet

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

the class Expressions method makeDefaultUniversallyQuantifiedFormulaFromLabelAndSubTrees.

private static Expression makeDefaultUniversallyQuantifiedFormulaFromLabelAndSubTrees(Object label, Object[] subTreeObjects) {
    ArrayList<Expression> subTreeExpressions = Util.mapIntoArrayList(subTreeObjects, Expressions::makeFromObject);
    Expression indexExpressionsKleeneList = subTreeExpressions.get(0);
    IndexExpressionsSet indexExpressions = new ExtensionalIndexExpressionsSet(ensureListFromKleeneList(indexExpressionsKleeneList));
    Expression body = subTreeExpressions.get(1);
    Expression result = new DefaultUniversallyQuantifiedFormula(indexExpressions, body);
    return result;
}
Also used : ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) DefaultUniversallyQuantifiedFormula(com.sri.ai.expresso.core.DefaultUniversallyQuantifiedFormula) DefaultLambdaExpression(com.sri.ai.expresso.core.DefaultLambdaExpression) Expression(com.sri.ai.expresso.api.Expression) LambdaExpression(com.sri.ai.expresso.api.LambdaExpression) IndexExpressions(com.sri.ai.grinder.library.indexexpression.IndexExpressions) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet)

Example 52 with ExtensionalIndexExpressionsSet

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

the class Registry method extendWithSymbolsAndTypes.

/**
 * Extends with pairs of symbols and their respective types represented as strings.
 * @param symbolsAndTypes
 * @return
 */
default Registry extendWithSymbolsAndTypes(Expression... symbolsAndTypes) {
    Util.myAssert(symbolsAndTypes.length % 2 == 0, () -> "Need to extend registry with a sequence of symbols and their types");
    List<Expression> indexExpressions = getIndexExpressionsFromSymbolsAndTypes(symbolsAndTypes);
    Registry result = extendWith(new ExtensionalIndexExpressionsSet(indexExpressions));
    return result;
}
Also used : ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) Expression(com.sri.ai.expresso.api.Expression) GrinderUtil.getIndexExpressionsForIndicesInListAndTypesInRegistry(com.sri.ai.grinder.helper.GrinderUtil.getIndexExpressionsForIndicesInListAndTypesInRegistry)

Example 53 with ExtensionalIndexExpressionsSet

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

Example 54 with ExtensionalIndexExpressionsSet

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

the class GrinderUtil method extendRegistryWith.

/**
 * @param mapFromSymbolNameToTypeName
 * @param types
 * @param registry
 * @return
 */
public static Registry extendRegistryWith(Map<String, String> mapFromSymbolNameToTypeName, Collection<? extends Type> types, Registry registry) {
    List<Expression> symbolDeclarations = new ArrayList<>();
    for (Map.Entry<String, String> variableNameAndTypeName : mapFromSymbolNameToTypeName.entrySet()) {
        String symbolName = variableNameAndTypeName.getKey();
        String typeName = variableNameAndTypeName.getValue();
        symbolDeclarations.add(parse(symbolName + " in " + typeName));
    }
    Registry result = registry.extendWith(new ExtensionalIndexExpressionsSet(symbolDeclarations));
    registry = result;
    for (Type type : types) {
        registry = registry.makeNewContextWithAddedType(type);
        // System.out.println("Cardinality of type being computed: " + type);
        Expression cardinality = type.cardinality();
        registry = registry.putGlobalObject(parse("|" + type.getName() + "|"), cardinality);
    }
    return registry;
}
Also used : ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) IntegerExpressoType(com.sri.ai.expresso.type.IntegerExpressoType) TupleType(com.sri.ai.expresso.type.TupleType) Type(com.sri.ai.expresso.api.Type) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) FunctionType(com.sri.ai.expresso.type.FunctionType) Expression(com.sri.ai.expresso.api.Expression) LambdaExpression(com.sri.ai.expresso.api.LambdaExpression) Util.mapIntoArrayList(com.sri.ai.util.Util.mapIntoArrayList) ArrayList(java.util.ArrayList) Registry(com.sri.ai.grinder.api.Registry) Map(java.util.Map)

Example 55 with ExtensionalIndexExpressionsSet

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

the class GrinderUtil method getIndexExpressionsForIndicesInListAndTypesInRegistry.

/**
 * Returns a list of index expressions corresponding to the given indices and their types per the registry, if any.
 */
public static ExtensionalIndexExpressionsSet getIndexExpressionsForIndicesInListAndTypesInRegistry(Collection<? extends Expression> indices, Registry registry) {
    List<Expression> indexExpressions = new LinkedList<Expression>();
    for (Expression index : indices) {
        Expression type = registry.getTypeExpressionOfRegisteredSymbol(index);
        Expression indexExpression = IndexExpressions.makeIndexExpression(index, type);
        indexExpressions.add(indexExpression);
    }
    return new ExtensionalIndexExpressionsSet(indexExpressions);
}
Also used : ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) Expression(com.sri.ai.expresso.api.Expression) LambdaExpression(com.sri.ai.expresso.api.LambdaExpression) LinkedList(java.util.LinkedList)

Aggregations

Expression (com.sri.ai.expresso.api.Expression)56 ExtensionalIndexExpressionsSet (com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet)56 IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)24 ArrayList (java.util.ArrayList)14 LambdaExpression (com.sri.ai.expresso.api.LambdaExpression)12 IntensionalSet (com.sri.ai.expresso.api.IntensionalSet)10 DefaultLambdaExpression (com.sri.ai.expresso.core.DefaultLambdaExpression)10 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 Pair (com.sri.ai.util.base.Pair)4 Collection (java.util.Collection)4 Map (java.util.Map)4 Set (java.util.Set)4 Predicate (com.google.common.base.Predicate)3 Symbol (com.sri.ai.expresso.api.Symbol)3