Search in sources :

Example 1 with DefaultExistentiallyQuantifiedFormula

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

the class Expressions method makeDefaultExistentiallyQuantifiedFormulaFromLabelAndSubTrees.

private static Expression makeDefaultExistentiallyQuantifiedFormulaFromLabelAndSubTrees(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 DefaultExistentiallyQuantifiedFormula(indexExpressions, body);
    return result;
}
Also used : ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) DefaultLambdaExpression(com.sri.ai.expresso.core.DefaultLambdaExpression) Expression(com.sri.ai.expresso.api.Expression) LambdaExpression(com.sri.ai.expresso.api.LambdaExpression) DefaultExistentiallyQuantifiedFormula(com.sri.ai.expresso.core.DefaultExistentiallyQuantifiedFormula) IndexExpressions(com.sri.ai.grinder.library.indexexpression.IndexExpressions) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet)

Example 2 with DefaultExistentiallyQuantifiedFormula

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

the class Disjunction method makeQuantifiedExpression.

@Override
public Expression makeQuantifiedExpression(List<Expression> indexExpressions, Expression body) {
    Expression current = body;
    ListIterator<Expression> iterator = indexExpressions.listIterator(indexExpressions.size());
    while (iterator.hasPrevious()) {
        Expression indexExpression = iterator.previous();
        current = new DefaultExistentiallyQuantifiedFormula(indexExpression, current);
    }
    return current;
}
Also used : Expression(com.sri.ai.expresso.api.Expression) DefaultExistentiallyQuantifiedFormula(com.sri.ai.expresso.core.DefaultExistentiallyQuantifiedFormula)

Example 3 with DefaultExistentiallyQuantifiedFormula

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

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

the class UsefulExpressionOperations method main.

public static void main(String[] args) {
    Theory theory = new CommonTheory();
    Context context = new TrueContext(theory);
    // Only one child and one parent, 2 parameters (Param1 and Param2)
    Expression child = parse("Child");
    Expression parent = parse("Parent");
    Expression param1 = parse("Param1");
    Expression param2 = parse("Param2");
    Expression param3 = parse("Param3");
    context = context.extendWithSymbolsAndTypes("Child", "1..5", "Parent", "1..5", "Param1", "Real", "Param2", "Real", "Param3", "Real", "A", "Boolean");
    // Old way: making parameters become constants
    // Predicate<Expression> isUniquelyNamedConstantPredicate = context.getIsUniquelyNamedConstantPredicate();
    // Predicate<Expression> newIsUniquelyNamedConstantPredicate = s -> s.equals(param1) || s.equals(param2) || s.equals(param3) || isUniquelyNamedConstantPredicate.apply(s);
    // context = context.setIsUniquelyNamedConstantPredicate(newIsUniquelyNamedConstantPredicate);
    println("My context:");
    println(context.getSymbolsAndTypes());
    // Expression E = parse("if Child < 5 then Param1 else Param2");
    Expression E = parse("if Parent != 5 then Param1 else Param2");
    // Expression E = parse("if Parent != 5 then if Child < 5 then Param1 else Param2 else Param3");
    // Expression E = parse("if Parent != 5 then if Child < Parent then Param1 else Param2 else Param3");
    println("\nE = " + E + "\n");
    // Gives you <Child in 1..5>
    childIndexExpressionsSet = getIndexExpressionsForIndicesInListAndTypesInRegistry(list(child), context);
    parametersIndexExpressionsSet = getIndexExpressionsForIndicesInListAndTypesInRegistry(list(param1, param2, param3), context);
    Expression F1 = new DefaultExistentiallyQuantifiedFormula(childIndexExpressionsSet, forAllParametersValues(Equality.make(E, param1)));
    println("F1 = " + F1);
    println(context.evaluate(F1) + "\n");
    // Old way of computing families (taking the parameters as constants):
    // Expression F1 = new DefaultExistentiallyQuantifiedFormula(childIndexExpressionsSet, Equality.make(E, param1));
    // println("F1 = " + F1);
    // println(context.evaluate(F1) + "\n");
    Expression F2 = new DefaultExistentiallyQuantifiedFormula(childIndexExpressionsSet, forAllParametersValues(Equality.make(E, param2)));
    println("F2 = " + F2);
    println(context.evaluate(F2) + "\n");
    Expression F1intersectsF2 = verifyEquivalenceAndGetIntersectionCondition(F1, F2, context);
    println("F1intersectsF2 = " + F1intersectsF2);
    println(context.evaluate(F1intersectsF2));
    // Normalization for Parame1_1
    Expression multiset = new DefaultIntensionalMultiSet(childIndexExpressionsSet, child, forAllParametersValues(Equality.make(E, param1)));
    Expression cardinality = apply(CARDINALITY, multiset);
    println("\nCardinality = " + cardinality);
    Expression cardinalityResult = context.evaluate(cardinality);
    println("N for normalizing Param1_1: " + cardinalityResult);
    // Draft for the Java syntax when shattering the families
    LinkedList<Integer> initialFamilies = Util.list(1, 2, 3, 4, 5);
    List<Integer> finalFamilies = Util.list();
    while (!initialFamilies.isEmpty()) {
        int family1 = initialFamilies.removeFirst();
        for (Iterator<Integer> it = initialFamilies.iterator(); it.hasNext(); ) {
            int family2 = it.next();
            if (family2 == 4) {
                it.remove();
            }
        }
        if (family1 == 3)
            initialFamilies.add(7);
        finalFamilies.add(family1);
    }
    println("\n" + finalFamilies);
    // Testing syntax for context (used for method convertToAnExpressionBasedModelAfterLearning in ExpressionBayesianModel)
    ExpressionVariable expressionVariable = new DefaultExpressionVariable(parse("A"));
    println("\nexpressionVariable.toString() = " + expressionVariable.toString());
    Type type = context.getTypeOfRegisteredSymbol(expressionVariable);
    println("type.toString() = " + type.toString());
    println("child.toStringe() = " + child.toString());
    type = context.getTypeOfRegisteredSymbol(child);
    println("type.toString() = " + type.toString());
    // TODO: Below I let 2 comments about Expresso edge case problems, to be studied and fixed later:
    // 1]
    // Comparison that we would like to be false, Expresso problem with constants - to be seen later (TODO)
    // context.evaluate(Equality.make(parse("Param1"), parse("1-Param1"))); // we would like to have "false" as result here but it gives error, that is why we have to use OneMinusParam1 as other parameter
    // 2]
    // Two Expressions that are equals, equality result should be true (from ExpressionBayesianModelTest, testChildParentModel4), but error with parent been canceled out, problem with Expresso - to be seen later
    // also, TODO: see why it is not simplifying 1/Parent to 1/5 in learnedChild below
    Expression expectedChild = parse("if Parent = 5 then 0.2 else if Child > Parent then (((5 - Parent) + 0) / (5 + 0)) / (5 - Parent) else ((Parent + 0) / (5 + 0)) / Parent");
    Expression learnedChild = parse("if Parent < 5 then if Child > Parent then ((-Parent + 5) / ((-Parent + 5) + Parent)) / (-Parent + 5) else (Parent / ((-Parent + 5) + Parent)) / Parent else 1 / Parent");
    // println(context.evaluate(Equality.make(expectedChild, learnedChild)));
    // Problem when using "for all parameters in parametersValues" instead of handling them as constants - the problem was when adding "and (Parent != 5)" (family.condition) at the end of expression bellow
    Expression expression = parse("(if Parent != 5 then if Child < 5 then Param1 else Param2 else Param3)");
    println("\nexpression = " + expression);
    multiset = new DefaultIntensionalMultiSet(childIndexExpressionsSet, child, forAllParametersValues(Equality.make(expression, param1)));
    cardinality = apply(CARDINALITY, multiset);
    Expression numberOfChildValues = cardinality;
    println("numberOfChildValues = " + numberOfChildValues);
    println(context.evaluate(numberOfChildValues));
}
Also used : TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) DefaultExpressionVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionVariable) CommonTheory(com.sri.ai.grinder.application.CommonTheory) Theory(com.sri.ai.grinder.api.Theory) DefaultExistentiallyQuantifiedFormula(com.sri.ai.expresso.core.DefaultExistentiallyQuantifiedFormula) TrueContext(com.sri.ai.grinder.core.TrueContext) DefaultIntensionalMultiSet(com.sri.ai.expresso.core.DefaultIntensionalMultiSet) CommonTheory(com.sri.ai.grinder.application.CommonTheory) Type(com.sri.ai.expresso.api.Type) Expression(com.sri.ai.expresso.api.Expression) DefaultExpressionVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionVariable) ExpressionVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionVariable)

Example 5 with DefaultExistentiallyQuantifiedFormula

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

the class ExpressionBayesianNode method generateInitialFamilies.

/**
 * Generating one family for each parameter at the beginning, the associated condition being "there exists Child so that this.expression = currentParameter"
 *
 * @return list of the initial families
 */
private LinkedList<Family> generateInitialFamilies() {
    LinkedList<Family> initialFamilies = list();
    for (Expression parameter : parameters) {
        Expression expressionForFamilyCondition = Equality.make(expression, parameter);
        Expression familyCondition = new DefaultExistentiallyQuantifiedFormula(childIndexExpressionsSet, expressionForFamilyCondition);
        familyCondition = context.evaluate(familyCondition);
        Family family = new Family(familyCondition, Util.set(parameter));
        initialFamilies.add(family);
    }
    return initialFamilies;
}
Also used : Expression(com.sri.ai.expresso.api.Expression) DefaultExistentiallyQuantifiedFormula(com.sri.ai.expresso.core.DefaultExistentiallyQuantifiedFormula)

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