use of com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable in project aic-praise by aic-sri-international.
the class TableFactorTest method testf2f1SumOutV1V2.
@Test
public void testf2f1SumOutV1V2() {
println();
println("SUM OUT V1 and V2 from F2*F1");
println("------------------");
ArrayList<TableVariable> variablesToSumOut = Util.arrayList(V1, V2);
TableFactor f2f1SumOutV1V2 = (TableFactor) (f2.multiply(f1)).sumOut(variablesToSumOut);
f2f1SumOutV1V2.setName("f2f1SumOutV1V2");
println(f2f1SumOutV1V2);
assertEquals("f2f1SumOutV1V2[{V4:card=2}, {V3:card=4}]: [126.0, 126.0, 126.0, 126.0, 132.0, 132.0, 132.0, 132.0]", f2f1SumOutV1V2.toString());
println();
}
use of com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable in project aic-praise by aic-sri-international.
the class FromTableToExpressionFactorConverter method ifThenElseTreeExpressionFromCurrentPositionOf.
private Expression ifThenElseTreeExpressionFromCurrentPositionOf(CartesianProductIterator<Integer> assignmentsIterator, TableFactor tableFactor, int variableIndex) {
myAssert(assignmentsIterator.hasNext(), () -> "ifThenElseExpressionFromCurrentPositionOf: requires assignmentsIterator to be non-empty");
ArrayList<TableVariable> variables = tableFactor.getVariables();
int varCardinality = variables.get(variableIndex).getCardinality();
int nextVariableIndex = variableIndex + 1;
ArrayList<Expression> subBranchExpressions = new ArrayList<>(varCardinality);
if (variableIndex == variables.size() - 1) {
for (int i = 0; i < varCardinality; ++i) {
ArrayList<? extends Integer> assignment = assignmentsIterator.next();
Double potentialForAssignment = tableFactor.getEntryFor(assignment);
Expression potentialExpression = createSymbol(potentialForAssignment);
subBranchExpressions.add(potentialExpression);
}
} else {
for (int i = 0; i < varCardinality; ++i) {
Expression branchExpression = ifThenElseTreeExpressionFromCurrentPositionOf(assignmentsIterator, tableFactor, nextVariableIndex);
subBranchExpressions.add(branchExpression);
}
}
int firstAssignmentValue = 0;
TableVariable variable = variables.get(variableIndex);
Expression result = combineVariableSubBranchExpressionsIntoIfElseIFElse(variable, firstAssignmentValue, subBranchExpressions);
return result;
}
use of com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable in project aic-praise by aic-sri-international.
the class FromTableToExpressionFactorConverter method makeExpressionEquivalentToTableFactor.
private Expression makeExpressionEquivalentToTableFactor(TableFactor tableFactor, boolean convertAsTreeBasedExpression) {
List<Integer> cardinalities = mapIntoArrayList(tableFactor.getVariables(), TableVariable::getCardinality);
CartesianProductIterator<Integer> assignmentsIterator = makeAssignmentsIterator(cardinalities);
Expression expression;
if (convertAsTreeBasedExpression) {
int startingVariableIndex = 0;
expression = ifThenElseTreeExpressionFromCurrentPositionOf(assignmentsIterator, tableFactor, startingVariableIndex);
} else {
expression = ifThenElseLinearTableExpressionFromCurrentPositionOf(assignmentsIterator, tableFactor);
}
return expression;
}
use of com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable in project aic-praise by aic-sri-international.
the class FromTableToExpressionFactorConverter method makeComparisonToAssignedValue.
private Expression makeComparisonToAssignedValue(Integer variableIndex, List<TableVariable> tableVariables, ArrayList<? extends Integer> assignment) {
Integer assignedValue = assignment.get(variableIndex);
Symbol assignedValueExpression = createSymbol(assignedValue);
TableVariable tableVariable = tableVariables.get(variableIndex);
Symbol variableExpression = makeVariableExpression(tableVariable);
Expression result = Equality.make(variableExpression, assignedValueExpression);
return result;
}
use of com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable in project aic-praise by aic-sri-international.
the class TestCases method treeWithRandomEntries.
public static List<TableFactor> treeWithRandomEntries(int depth, int childrenPerNode, int cardinality, Function<Random, Double> randomGen) {
Random r = new Random();
Function<ArrayList<TableVariable>, ArrayList<Double>> entryGen = (l) -> Util.fill(pow(cardinality, l.size()), () -> randomGen.apply(r));
List<TableFactor> result = tree(depth, childrenPerNode, entryGen, cardinality);
return result;
}
Aggregations