Search in sources :

Example 21 with ExtensionalIndexExpressionsSet

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

the class FactorComponent method calculate.

public Expression calculate() {
    Theory theory = this.model.theory;
    Context context = this.model.context;
    Expression childrenMessage = parse("{ 1 }");
    for (VariableComponent children : this.children) {
        childrenMessage = apply(TIMES, childrenMessage, children.calculate());
    }
    childrenMessage = apply(TIMES, childrenMessage, this.phi);
    for (Expression cutset : this.cutsetInsideSubModel) {
        // String str = "sum({{ (on " + cutset + " in Boolean ) " + childrenMessage + " }})";
        // childrenMessage = parse(str);
        // childrenMessage = theory.evaluate(childrenMessage, context);
        Expression valuesTakenByVariableToSum = this.model.getValues(cutset);
        IndexExpressionsSet indices = new ExtensionalIndexExpressionsSet(apply(IN, cutset, valuesTakenByVariableToSum));
        Expression intensionalMultiSet = IntensionalSet.makeMultiSet(indices, childrenMessage, parse("true"));
        Expression summation = apply(SUM, intensionalMultiSet);
        childrenMessage = summation;
    // String str = "sum({{ (on " + variableToSum + " in " + this.model.getValues(variableToSum) +" ) " + childrenMessage + " }})";
    // childrenMessage = parse(str);
    }
    Set<Expression> toSum = model.getNeighbors(phi);
    for (Expression e : this.parent) {
        toSum.remove(e);
    }
    toSum.removeAll(this.cutsetOutsideSubModel);
    toSum.removeAll(this.cutsetInsideSubModel);
    for (Expression variableToSum : toSum) {
        Expression expressionToSum = theory.evaluate(childrenMessage, context);
        Expression valuesTakenByVariableToSum = this.model.getValues(variableToSum);
        IndexExpressionsSet indices = new ExtensionalIndexExpressionsSet(apply(IN, variableToSum, valuesTakenByVariableToSum));
        Expression intensionalMultiSet = IntensionalSet.makeMultiSet(indices, expressionToSum, parse("true"));
        Expression summation = apply(SUM, intensionalMultiSet);
        // String str = "sum({{ (on " + variableToSum + " in " + this.model.getValues(variableToSum) +" ) " + childrenMessage + " }})";
        // childrenMessage = parse(str);
        childrenMessage = summation;
    }
    return theory.evaluate(childrenMessage, context);
}
Also used : Context(com.sri.ai.grinder.api.Context) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) Theory(com.sri.ai.grinder.api.Theory) Expression(com.sri.ai.expresso.api.Expression) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet)

Example 22 with ExtensionalIndexExpressionsSet

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

the class VariableComponent method calculate.

public Expression calculate() {
    Theory theory = this.model.theory;
    Context context = this.model.context;
    Expression childrenMessage = parse("1");
    for (FactorComponent children : this.children) {
        childrenMessage = apply(TIMES, childrenMessage, children.calculate());
        childrenMessage = theory.evaluate(childrenMessage, context);
    }
    for (Expression cutsetVariable : this.cutsetInsideSubModel) {
        // childrenMessage = theory.evaluate(childrenMessage, context);
        // String str = "sum({{ (on " + cutsetVariable + " in " + this.model.getValues(cutsetVariable) +" ) " + childrenMessage + " }})";
        // childrenMessage = parse(str);
        Expression valuesTakenByVariableToSum = this.model.getValues(cutsetVariable);
        IndexExpressionsSet indices = new ExtensionalIndexExpressionsSet(apply(IN, cutsetVariable, valuesTakenByVariableToSum));
        Expression intensionalMultiSet = IntensionalSet.makeMultiSet(indices, childrenMessage, parse("true"));
        Expression summation = apply(SUM, intensionalMultiSet);
        childrenMessage = summation;
    }
    return theory.evaluate(childrenMessage, context);
}
Also used : Context(com.sri.ai.grinder.api.Context) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) Theory(com.sri.ai.grinder.api.Theory) Expression(com.sri.ai.expresso.api.Expression) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet)

Example 23 with ExtensionalIndexExpressionsSet

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

the class Context method extendWith.

/**
 * Extends context with index expressions, taking into account that new contextual variables may collide with existing ones.
 * In this case, it renames the incoming variables to unique identifiers and replaces them in the types of remaining
 * index expressions. It also renames these indices if they occur in a given expression --
 * this is useful because the client code (invoking this method)
 * often knows that these renamed indices may occur in a known set of expressions that need to be updated accordingly.
 * Returns the new context, the index expressions and expression in scope after the renaming.
 * @param indexExpressions
 * @param expressionInScope
 * @return the new context and the index expressions and expression in scope after the renaming
 */
default Triple<Context, ExtensionalIndexExpressionsSet, Expression> extendWith(ExtensionalIndexExpressionsSet indexExpressions, Expression expressionInScope) {
    Triple<Context, ExtensionalIndexExpressionsSet, Expression> result;
    if (thereExists(getIndices(indexExpressions), index -> this.containsSymbol(index))) {
        // OPTIMIZATION: only kick in this entire procedure when extending with symbol in the context (previous ones could have been dealt with normally).
        // the objects to be returned in the triple:
        Context newContext = this;
        ArrayList<Expression> newIndexExpressionsList = new ArrayList<>(indexExpressions.getList());
        Expression newExpressionInScope = expressionInScope;
        // Collects all existing symbols to be able to create unique symbols
        Set<Expression> alreadyDefined = Util.set();
        alreadyDefined.addAll(this.getSymbols());
        alreadyDefined.addAll(Expressions.freeSymbols(new DefaultTuple(newIndexExpressionsList), this));
        alreadyDefined.addAll(Expressions.freeSymbols(expressionInScope, this));
        Predicate<Expression> isAlreadyDefined = e -> alreadyDefined.contains(e);
        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, newExpressionInScope);
            Expression newIndex = newIndexAndNewExpressionInScope.first;
            newExpressionInScope = newIndexAndNewExpressionInScope.second;
            // type should not contain the index
            Expression newIndexExpression = apply(IN, 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, this);
                // anotherIndex is a symbols and does not contain index
                Expression newAnotherIndexExpression = apply(IN, anotherIndex, newAnotherType);
                newIndexExpressionsList.set(j, newAnotherIndexExpression);
            }
        }
        ExtensionalIndexExpressionsSet newIndexExpressions = new ExtensionalIndexExpressionsSet(newIndexExpressionsList);
        newContext = newContext.extendWith(newIndexExpressions);
        result = triple(newContext, newIndexExpressions, newExpressionInScope);
    } else {
        // no collision; usual extension and the expressions do not change.
        result = triple(extendWith(indexExpressions), indexExpressions, expressionInScope);
    }
    return result;
}
Also used : Type(com.sri.ai.expresso.api.Type) Triple(com.sri.ai.util.base.Triple) Collection(java.util.Collection) Expressions(com.sri.ai.expresso.helper.Expressions) Set(java.util.Set) PairOf(com.sri.ai.util.base.PairOf) Expression(com.sri.ai.expresso.api.Expression) Util.mapIntoList(com.sri.ai.util.Util.mapIntoList) Triple.triple(com.sri.ai.util.base.Triple.triple) DefaultTuple(com.sri.ai.expresso.core.DefaultTuple) ArrayList(java.util.ArrayList) Beta(com.google.common.annotations.Beta) List(java.util.List) 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) Predicate(com.google.common.base.Predicate) IndexExpressions.getIndices(com.sri.ai.grinder.library.indexexpression.IndexExpressions.getIndices) Map(java.util.Map) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet) Util(com.sri.ai.util.Util) Util.thereExists(com.sri.ai.util.Util.thereExists) Symbol(com.sri.ai.expresso.api.Symbol) ArrayList(java.util.ArrayList) DefaultTuple(com.sri.ai.expresso.core.DefaultTuple) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) Expression(com.sri.ai.expresso.api.Expression)

Example 24 with ExtensionalIndexExpressionsSet

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

the class MultiQuantifierEliminator method extendContextAndSolve.

default Expression extendContextAndSolve(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);
    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 25 with ExtensionalIndexExpressionsSet

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

the class ClearExampleEvaluation method main.

public static void main(String[] args) {
    // /// Evaluating expressions
    // The above code shows how to deal with the syntax of expressions.
    // Evaluating expressions requires knowing about the semantics, that is, to what functions each operator corresponds to ("+" to addition, etc).
    // This is provided by a theory, which for now it suffices to know is a collection of methods for evaluating expressions
    // according to an interpretation to some symbols.
    Theory theory = new CompoundTheory(new EqualityTheory(false, true), new DifferenceArithmeticTheory(false, false), new LinearRealArithmeticTheory(false, false), new TupleTheory(), new PropositionalTheory());
    // Because this evaluation is symbolic, evaluated expressions may involve free variables.
    // In this case, the result of the evaluation will be a simplified expression that
    // is equivalent to the original expression for all possible assignments to the free variables.
    // For example, X + 0*Y is evaluate to X because, for any assignment to (X,Y), X + 0*Y = X.
    // true context: all assignments to free variables are of interest
    Context context = new TrueContext(theory);
    // We will later see how we can use contexts that restrict the free variable assignments of interest.
    context = context.makeNewContextWithAddedType(BOOLEAN_TYPE);
    context = context.extendWithSymbolsAndTypes("B", "Integer");
    context = context.extendWithSymbolsAndTypes("J", "Integer");
    // Now that we have a theory and a context, we can evaluate expressions:
    println("1 + 0*X + 1  =  " + theory.evaluate(parse("1 + 1"), context));
    /*evaluate(new String[] {
				"sum({{ (on C in Boolean) (if C then if A then 50 else 50 else if A then 50 else 50) * (if C then if B then 60 else 40 else if B then 40 else 60) }})", "",
		}, theory, context);
		*/
    Expression test = theory.evaluate(parse("sum({{ (on C in Boolean) (if C then if A then 50 else 50 else if A then 50 else 50) * (if C then if B then 60 else 40 else if B then 40 else 60) }})"), context);
    println(test);
    String str = "sum({{ (on I in 1..10) I : I != J }})";
    Expression expr = parse(str);
    Expression test2 = theory.evaluate(expr, context);
    println(test2);
    // Here's how to do it from scratch, but see next the way we typically actually do it.
    Expression p = makeSymbol("P");
    context = context.extendWithSymbolsAndTypes("P", "Integer");
    IndexExpressionsSet indices = new ExtensionalIndexExpressionsSet(apply(IN, p, parse("1..4")));
    println("plop");
    // The "extensional" in ExtensionalIndexExpressionsSet means that the list/set of indices is extensionally defined,
    // even though they will be the indices of an intensionally defined set.
    Expression intensionalUniSet = // IntensionalSet.intensionalUniSet, or simply intensionalUniSet, also works
    IntensionalSet.makeMultiSet(indices, parse("5"), parse("true"));
    // Note that Equality.make(p, "Rodrigo") is the same as apply(FunctorConstants.EQUAL, p, "Rodrigo").
    // We often have 'make' methods for many operators: And.make, Or.make and so on.
    // packages in com.sri.ai.expresso.grinder.sgdpllt.library have many such operator-specific classes.
    println(intensionalUniSet);
    Expression sum = apply(SUM, intensionalUniSet);
    println(sum);
    Expression resultat = theory.evaluate(sum, context);
    println(resultat);
}
Also used : TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) EqualityTheory(com.sri.ai.grinder.theory.equality.EqualityTheory) EqualityTheory(com.sri.ai.grinder.theory.equality.EqualityTheory) PropositionalTheory(com.sri.ai.grinder.theory.propositional.PropositionalTheory) LinearRealArithmeticTheory(com.sri.ai.grinder.theory.linearrealarithmetic.LinearRealArithmeticTheory) Theory(com.sri.ai.grinder.api.Theory) DifferenceArithmeticTheory(com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory) CompoundTheory(com.sri.ai.grinder.theory.compound.CompoundTheory) TupleTheory(com.sri.ai.grinder.theory.tuple.TupleTheory) DifferenceArithmeticTheory(com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory) LinearRealArithmeticTheory(com.sri.ai.grinder.theory.linearrealarithmetic.LinearRealArithmeticTheory) PropositionalTheory(com.sri.ai.grinder.theory.propositional.PropositionalTheory) CompoundTheory(com.sri.ai.grinder.theory.compound.CompoundTheory) TupleTheory(com.sri.ai.grinder.theory.tuple.TupleTheory) TrueContext(com.sri.ai.grinder.core.TrueContext) 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