Search in sources :

Example 21 with TableVariable

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();
}
Also used : TableFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor) TableVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable) Test(org.junit.Test)

Example 22 with TableVariable

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;
}
Also used : Expression(com.sri.ai.expresso.api.Expression) Util.mapIntoArrayList(com.sri.ai.util.Util.mapIntoArrayList) ArrayList(java.util.ArrayList) Util.mapIntegersIntoArrayList(com.sri.ai.util.Util.mapIntegersIntoArrayList) TableVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable)

Example 23 with TableVariable

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;
}
Also used : Expression(com.sri.ai.expresso.api.Expression) TableVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable)

Example 24 with TableVariable

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;
}
Also used : Expression(com.sri.ai.expresso.api.Expression) DefaultSymbol.createSymbol(com.sri.ai.expresso.core.DefaultSymbol.createSymbol) Symbol(com.sri.ai.expresso.api.Symbol) TableVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable)

Example 25 with TableVariable

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;
}
Also used : BiFunction(java.util.function.BiFunction) ExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor) Util.mapIntoArrayList(com.sri.ai.util.Util.mapIntoArrayList) Random(java.util.Random) Expression(com.sri.ai.expresso.api.Expression) Factor(com.sri.ai.praise.core.representation.interfacebased.factor.api.Factor) ArrayList(java.util.ArrayList) EQUAL(com.sri.ai.grinder.library.FunctorConstants.EQUAL) UAIModel(com.sri.ai.praise.core.representation.classbased.table.core.uai.UAIModel) TableFactorNetwork(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactorNetwork) Expressions.apply(com.sri.ai.expresso.helper.Expressions.apply) TrueContext(com.sri.ai.grinder.core.TrueContext) DefaultExpressionVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionVariable) Math.round(java.lang.Math.round) Expressions.parse(com.sri.ai.expresso.helper.Expressions.parse) Math.log(java.lang.Math.log) Context(com.sri.ai.grinder.api.Context) BigInteger(java.math.BigInteger) Double.max(java.lang.Double.max) Util.arrayList(com.sri.ai.util.Util.arrayList) ExpressionVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionVariable) Pair(com.sri.ai.util.base.Pair) Math.exp(java.lang.Math.exp) CommonTheory(com.sri.ai.grinder.application.CommonTheory) Function(com.google.common.base.Function) UAIEvidenceReading(com.sri.ai.praise.core.representation.classbased.table.core.uai.parsing.UAIEvidenceReading) TableFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor) IOException(java.io.IOException) IF_THEN_ELSE(com.sri.ai.grinder.library.FunctorConstants.IF_THEN_ELSE) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) TableVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable) List(java.util.List) DefaultExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionFactor) Expressions.makeSymbol(com.sri.ai.expresso.helper.Expressions.makeSymbol) UAIModelReader(com.sri.ai.praise.core.representation.classbased.table.core.uai.parsing.UAIModelReader) Util(com.sri.ai.util.Util) ExpressionFactorNetwork(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.ExpressionFactorNetwork) Util.println(com.sri.ai.util.Util.println) FileReader(java.io.FileReader) UAIModelToExpressionFactorNetwork(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.UAIModelToExpressionFactorNetwork) TableFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor) Random(java.util.Random) Util.mapIntoArrayList(com.sri.ai.util.Util.mapIntoArrayList) ArrayList(java.util.ArrayList)

Aggregations

TableVariable (com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable)49 TableFactor (com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor)34 ArrayList (java.util.ArrayList)17 Test (org.junit.Test)13 Util.mapIntoArrayList (com.sri.ai.util.Util.mapIntoArrayList)10 Variable (com.sri.ai.praise.core.representation.interfacebased.factor.api.Variable)8 LinkedHashMap (java.util.LinkedHashMap)8 List (java.util.List)8 TableFactor.copyToSubTableFactor (com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor.copyToSubTableFactor)7 TableFactorNetwork (com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactorNetwork)6 Pair (com.sri.ai.util.base.Pair)6 Expression (com.sri.ai.expresso.api.Expression)4 Factor (com.sri.ai.praise.core.representation.interfacebased.factor.api.Factor)3 DefaultDatapoint (com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDatapoint)3 DefaultDataset (com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDataset)3 TableBayesianNode (com.sri.ai.praise.learning.parameterlearning.representation.table.TableBayesianNode)3 Util (com.sri.ai.util.Util)3 Util.arrayList (com.sri.ai.util.Util.arrayList)3 File (java.io.File)3 LinkedList (java.util.LinkedList)3