Search in sources :

Example 1 with Variable

use of com.sri.ai.praise.core.representation.interfacebased.factor.api.Variable in project aic-praise by aic-sri-international.

the class ProceduralAttachmentExpressionFactor method computeInnerExpression.

@Override
protected Expression computeInnerExpression() {
    Variable variable = proceduralAttachment.getVariable();
    Object value = proceduralAttachment.getProcedureValue();
    Symbol valueExpression = DefaultSymbol.createSymbol(value);
    Type type = getVariableType(variable, getContext());
    Expression condition;
    if (value instanceof Double) {
        assertVariableIsOfRealType(variable, type, getContext());
        condition = makeIntervalFactorCondition(valueExpression);
    } else if (value instanceof Integer) {
        assertVariableIsOfIntegerType(variable, type, getContext());
        condition = makeEquality(valueExpression);
    } else if (value instanceof Boolean) {
        assertVariableIsOfBooleanType(variable, type, getContext());
        condition = makeBooleanCondition(valueExpression);
    } else {
        throw new Error("Procedural attachments must provide only Double or Integer objects, but procedure for " + proceduralAttachment.getVariable() + " provided " + proceduralAttachment.getProcedureValue() + " of type " + proceduralAttachment.getProcedureValue().getClass());
    }
    Expression result = IfThenElse.make(condition, Expressions.ONE, Expressions.ZERO);
    return result;
}
Also used : Type(com.sri.ai.expresso.api.Type) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) IntegerExpressoType(com.sri.ai.expresso.type.IntegerExpressoType) Variable(com.sri.ai.praise.core.representation.interfacebased.factor.api.Variable) ExpressionVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionVariable) Expression(com.sri.ai.expresso.api.Expression) Symbol(com.sri.ai.expresso.api.Symbol) DefaultSymbol(com.sri.ai.expresso.core.DefaultSymbol)

Example 2 with Variable

use of com.sri.ai.praise.core.representation.interfacebased.factor.api.Variable in project aic-praise by aic-sri-international.

the class Polytopes method makeProjectedPolytopeOfIntensionalConvexHull.

private static Polytope makeProjectedPolytopeOfIntensionalConvexHull(List<? extends Variable> variablesToBeSummedOut, List<Polytope> dependentOfVariablesToBeSummedOut) {
    List<Variable> simplexVariables = collectSimplexVariables(dependentOfVariablesToBeSummedOut);
    List<Variable> indicesFromConvexHulls = collectIndicesFromIntensionalPolytopes(dependentOfVariablesToBeSummedOut);
    List<Factor> factors = collectFactorsFromIntensionalPolytopes(dependentOfVariablesToBeSummedOut);
    Factor productOfFactors = Factor.multiply(factors);
    List<? extends Variable> variablesToBeSummedOutOnceSimplexesAreDealtWith = subtract(variablesToBeSummedOut, simplexVariables);
    Factor projectedFactor = productOfFactors.sumOut(variablesToBeSummedOutOnceSimplexesAreDealtWith);
    List<Variable> finalIndices = union(indicesFromConvexHulls, simplexVariables);
    Polytope projectedPolytope = new IntensionalConvexHullOfFactors(finalIndices, projectedFactor);
    return projectedPolytope;
}
Also used : AtomicPolytope(com.sri.ai.praise.core.representation.interfacebased.polytope.api.AtomicPolytope) Polytope(com.sri.ai.praise.core.representation.interfacebased.polytope.api.Polytope) Variable(com.sri.ai.praise.core.representation.interfacebased.factor.api.Variable) TableVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable) TableBoxVariable(com.sri.ai.praise.core.representation.interfacebased.polytope.core.byexpressiveness.box.TableBoxVariable) Factor(com.sri.ai.praise.core.representation.interfacebased.factor.api.Factor) TableFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor)

Example 3 with Variable

use of com.sri.ai.praise.core.representation.interfacebased.factor.api.Variable in project aic-praise by aic-sri-international.

the class Polytopes method mergeIntensionalConvexHulls.

private static IntensionalConvexHullOfFactors mergeIntensionalConvexHulls(List<? extends AtomicPolytope> convexHulls) {
    List<Variable> indicesFromConvexHulls = collectIndicesFromIntensionalPolytopes(convexHulls);
    Factor productOfFactors = makeProductOfFactors(convexHulls);
    IntensionalConvexHullOfFactors result = new IntensionalConvexHullOfFactors(indicesFromConvexHulls, productOfFactors);
    return result;
}
Also used : Variable(com.sri.ai.praise.core.representation.interfacebased.factor.api.Variable) TableVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable) TableBoxVariable(com.sri.ai.praise.core.representation.interfacebased.polytope.core.byexpressiveness.box.TableBoxVariable) Factor(com.sri.ai.praise.core.representation.interfacebased.factor.api.Factor) TableFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor)

Example 4 with Variable

use of com.sri.ai.praise.core.representation.interfacebased.factor.api.Variable in project aic-praise by aic-sri-international.

the class AnytimeExactBPTest method solveWithAnytimeExactBP.

private Expression solveWithAnytimeExactBP(Expression query, ExpressionFactorNetwork factorNetwork, Context context) {
    Expression result;
    println("\nSolving with Anytime\n");
    long initialTime = System.currentTimeMillis();
    ExactBPNode<Variable, Factor> exactBP = new ExpressionExactBP(query, factorNetwork);
    AnytimeExactBP<Variable, Factor> anytimeExactBP = new AnytimeExactBP<>(exactBP);
    Approximation<Factor> current = null;
    while (anytimeExactBP.hasNext()) {
        current = anytimeExactBP.next();
        println("Current bound on " + exactBP.getMessageVariable() + ": " + current);
    }
    long finalTime = System.currentTimeMillis();
    if (current == null) {
        throw new Error("Anytime BP should have at least one approximation, but had none.");
    } else {
        ExpressionFactor resultFactor = (ExpressionFactor) ((IntensionalConvexHullOfFactors) current).getFactor();
        result = PRAiSEUtil.normalize(query, resultFactor, context);
        println("P(" + exactBP.getMessageVariable() + "): " + result);
        println("Time: " + (finalTime - initialTime) + " ms.");
    }
    return result;
}
Also used : ExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor) Variable(com.sri.ai.praise.core.representation.interfacebased.factor.api.Variable) AnytimeExactBP(com.sri.ai.praise.core.inference.byinputrepresentation.interfacebased.core.exactbp.anytime.rodrigo.AnytimeExactBP) Expression(com.sri.ai.expresso.api.Expression) ExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor) Factor(com.sri.ai.praise.core.representation.interfacebased.factor.api.Factor) ExpressionExactBP(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.ExpressionExactBP)

Example 5 with Variable

use of com.sri.ai.praise.core.representation.interfacebased.factor.api.Variable in project aic-praise by aic-sri-international.

the class AnytimeExactBPTest3 method solveGabrielWithBoxing.

public static AEBPTestingDataFrame solveGabrielWithBoxing(Variable query, EditableFactorNetwork factorNetwork, long maximunTimeInSeconds, String PGMName) {
    println("\nSolving with Gabriel's Anytime - boxing\n");
    Predicate<Polytope> criteria = p -> ((IntensionalConvexHullOfFactors) p).getIndices().size() > 10;
    AEBP aebp = new AEBP(factorNetwork, query, criteria, true);
    /*while(aebp.hasNext()) {
			Polytope p = aebp.next();
			println(p);
		}*/
    return AEBPSolver.solve(aebp, query, maximunTimeInSeconds, 0, PGMName, "Boxing");
}
Also used : Polytope(com.sri.ai.praise.core.representation.interfacebased.polytope.api.Polytope) ExactBP(com.sri.ai.praise.core.inference.byinputrepresentation.interfacebased.core.exactbp.fulltime.core.ExactBP) Util.list(com.sri.ai.util.Util.list) TableFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor) AEBPRPlotting(com.sri.ai.util.rplot.AEBPRPlotting) Factor(com.sri.ai.praise.core.representation.interfacebased.factor.api.Factor) Polytope(com.sri.ai.praise.core.representation.interfacebased.polytope.api.Polytope) File(java.io.File) AnytimeExactBP(com.sri.ai.praise.core.inference.byinputrepresentation.interfacebased.core.exactbp.anytime.rodrigo.AnytimeExactBP) Variable(com.sri.ai.praise.core.representation.interfacebased.factor.api.Variable) TableVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable) ArrayList(java.util.ArrayList) TestCases(com.sri.ai.praise.core.inference.byinputrepresentation.interfacebased.core.exactbp.anytime.gabriel.TestCases) List(java.util.List) TableFactorNetwork(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactorNetwork) ExactBPNode(com.sri.ai.praise.core.inference.byinputrepresentation.interfacebased.core.exactbp.fulltime.api.ExactBPNode) Predicate(com.google.common.base.Predicate) AEBP(com.sri.ai.praise.core.inference.byinputrepresentation.interfacebased.core.exactbp.anytime.gabriel.AEBP) EditableFactorNetwork(com.sri.ai.praise.core.representation.interfacebased.factor.api.EditableFactorNetwork) AEBPTestingDataFrame(com.sri.ai.util.rplot.dataframe.AEBPTestingDataFrame) IntensionalConvexHullOfFactors(com.sri.ai.praise.core.representation.interfacebased.polytope.core.byexpressiveness.convexhull.IntensionalConvexHullOfFactors) Util.println(com.sri.ai.util.Util.println) LinkedHashSet(java.util.LinkedHashSet) AssignmentsIterator(com.sri.ai.grinder.helper.AssignmentsIterator) IntensionalConvexHullOfFactors(com.sri.ai.praise.core.representation.interfacebased.polytope.core.byexpressiveness.convexhull.IntensionalConvexHullOfFactors) AEBP(com.sri.ai.praise.core.inference.byinputrepresentation.interfacebased.core.exactbp.anytime.gabriel.AEBP)

Aggregations

Variable (com.sri.ai.praise.core.representation.interfacebased.factor.api.Variable)23 Factor (com.sri.ai.praise.core.representation.interfacebased.factor.api.Factor)11 ArrayList (java.util.ArrayList)10 TableFactor (com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor)9 TableVariable (com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable)9 ExpressionFactor (com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor)4 AtomicPolytope (com.sri.ai.praise.core.representation.interfacebased.polytope.api.AtomicPolytope)4 Polytope (com.sri.ai.praise.core.representation.interfacebased.polytope.api.Polytope)4 Util.mapIntoArrayList (com.sri.ai.util.Util.mapIntoArrayList)4 List (java.util.List)4 Expression (com.sri.ai.expresso.api.Expression)3 TableFactorNetwork (com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactorNetwork)3 TableBoxVariable (com.sri.ai.praise.core.representation.interfacebased.polytope.core.byexpressiveness.box.TableBoxVariable)3 IntensionalConvexHullOfFactors (com.sri.ai.praise.core.representation.interfacebased.polytope.core.byexpressiveness.convexhull.IntensionalConvexHullOfFactors)3 Predicate (com.google.common.base.Predicate)2 AnytimeExactBP (com.sri.ai.praise.core.inference.byinputrepresentation.interfacebased.core.exactbp.anytime.rodrigo.AnytimeExactBP)2 ExactBP (com.sri.ai.praise.core.inference.byinputrepresentation.interfacebased.core.exactbp.fulltime.core.ExactBP)2 ExpressionVariable (com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionVariable)2 ExpressionExactBP (com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.ExpressionExactBP)2 RandomTableFactorMaker.makeRandomTableFactor (com.sri.ai.praise.core.representation.interfacebased.factor.core.table.helper.RandomTableFactorMaker.makeRandomTableFactor)2