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