Search in sources :

Example 16 with IndexExpressionsSet

use of com.sri.ai.expresso.api.IndexExpressionsSet 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 17 with IndexExpressionsSet

use of com.sri.ai.expresso.api.IndexExpressionsSet 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 18 with IndexExpressionsSet

use of com.sri.ai.expresso.api.IndexExpressionsSet in project aic-expresso by aic-sri-international.

the class VariableComponent method chooseDepthImportantFirst.

public int chooseDepthImportantFirst() {
    Expression min = parse("{ 1 }");
    Integer jToReturn = 0;
    for (int j = 0; j < this.children.size(); j++) {
        if (!this.children.get(j).entirelyDiscover) {
            Set<Expression> toMultiply = this.model.getNeighbors(this.children.get(j).phi);
            toMultiply.removeAll(this.children.get(j).parent);
            DefaultExtensionalUniSet varToMultiply = new DefaultExtensionalUniSet(new ArrayList<>(toMultiply));
            IndexExpressionsSet indices = getIndexExpressionsOfFreeVariablesIn(varToMultiply, this.model.context);
            // TODO Find a way to do : intersection (variablesToBeSummedOut, FreeVariables(phi))
            Expression setOfFactorInstantiations = IntensionalSet.makeMultiSet(indices, // head
            Bounds.normalizeSingleExpression(this.children.get(j).phi, this.model.theory, this.model.context), // No Condition
            makeSymbol(true));
            Expression firstProduct = apply(TIMES, setOfFactorInstantiations);
            Expression evaluation = this.model.theory.evaluate(firstProduct, this.model.context);
            DefaultExtensionalUniSet varToSum = new DefaultExtensionalUniSet(new ArrayList<>(this.children.get(j).parent));
            indices = getIndexExpressionsOfFreeVariablesIn(varToSum, this.model.context);
            setOfFactorInstantiations = IntensionalSet.makeMultiSet(indices, // head
            evaluation, // No Condition
            makeSymbol(true));
            Expression sumOnPhi = apply(SUM, setOfFactorInstantiations);
            evaluation = this.model.theory.evaluate(sumOnPhi, this.model.context);
            if (evaluation.compareTo(min) < 0) {
                min = evaluation;
                jToReturn = j;
            }
        }
    }
    return jToReturn;
}
Also used : Expression(com.sri.ai.expresso.api.Expression) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet) DefaultExtensionalUniSet(com.sri.ai.expresso.core.DefaultExtensionalUniSet)

Example 19 with IndexExpressionsSet

use of com.sri.ai.expresso.api.IndexExpressionsSet in project aic-expresso by aic-sri-international.

the class AbstractIntensionalSet method makeSyntaxTree.

private SyntaxTree makeSyntaxTree() {
    IndexExpressionsSet indexExpressions = getIndexExpressions();
    SyntaxTree parametersSyntaxTree = indexExpressions.getSubSyntaxTree();
    SyntaxTree scopingSyntaxTree = new DefaultCompoundSyntaxTree(IntensionalSet.SCOPED_VARIABLES_LABEL, parametersSyntaxTree);
    SyntaxTree headSyntaxTree = getHead().getSyntaxTree();
    SyntaxTree conditionSyntaxTree = (getCondition() == null || getCondition().equals("true")) ? null : SyntaxTrees.makeCompoundSyntaxTree(IntensionalSet.CONDITION_LABEL, condition.getSyntaxTree());
    cachedSyntaxTree = makeCompoundSyntaxTree(getSyntaxTreeLabel(), scopingSyntaxTree, headSyntaxTree, conditionSyntaxTree);
    return cachedSyntaxTree;
}
Also used : SyntaxTree(com.sri.ai.expresso.api.SyntaxTree) SyntaxTrees.makeCompoundSyntaxTree(com.sri.ai.expresso.helper.SyntaxTrees.makeCompoundSyntaxTree) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet)

Example 20 with IndexExpressionsSet

use of com.sri.ai.expresso.api.IndexExpressionsSet in project aic-expresso by aic-sri-international.

the class AbstractQuantifiedExpressionWithABody method makeSyntaxTree.

private SyntaxTree makeSyntaxTree() {
    IndexExpressionsSet indexExpressions = getIndexExpressions();
    List<Expression> indexExpressionsList = ((ExtensionalIndexExpressionsSet) indexExpressions).getList();
    List<SyntaxTree> indexExpressionsSyntaxTrees = mapIntoArrayList(indexExpressionsList, Expression::getSyntaxTree);
    SyntaxTree parameterList = SyntaxTrees.makeKleeneListIfNeeded(indexExpressionsSyntaxTrees);
    SyntaxTree result = makeCompoundSyntaxTree(getSyntaxTreeLabel(), parameterList, getBody().getSyntaxTree());
    return result;
}
Also used : SyntaxTree(com.sri.ai.expresso.api.SyntaxTree) SyntaxTrees.makeCompoundSyntaxTree(com.sri.ai.expresso.helper.SyntaxTrees.makeCompoundSyntaxTree) Expression(com.sri.ai.expresso.api.Expression) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet)

Aggregations

IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)79 Expression (com.sri.ai.expresso.api.Expression)78 ExtensionalIndexExpressionsSet (com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet)49 IntensionalSet (com.sri.ai.expresso.api.IntensionalSet)24 LambdaExpression (com.sri.ai.expresso.api.LambdaExpression)23 ArrayList (java.util.ArrayList)20 DefaultLambdaExpression (com.sri.ai.expresso.core.DefaultLambdaExpression)17 Type (com.sri.ai.expresso.api.Type)15 QuantifiedExpression (com.sri.ai.expresso.api.QuantifiedExpression)14 Context (com.sri.ai.grinder.api.Context)13 IndexExpressions (com.sri.ai.grinder.library.indexexpression.IndexExpressions)9 Context (com.sri.ai.grinder.sgdpllt.api.Context)9 List (java.util.List)9 FunctionType (com.sri.ai.expresso.type.FunctionType)8 Util.mapIntoArrayList (com.sri.ai.util.Util.mapIntoArrayList)8 Rational (com.sri.ai.util.math.Rational)7 SyntaxTree (com.sri.ai.expresso.api.SyntaxTree)6 DefaultIntensionalMultiSet (com.sri.ai.expresso.core.DefaultIntensionalMultiSet)6 QuantifiedExpressionWithABody (com.sri.ai.expresso.api.QuantifiedExpressionWithABody)5 DefaultCountingFormula (com.sri.ai.expresso.core.DefaultCountingFormula)5