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);
}
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);
}
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;
}
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;
}
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;
}
Aggregations