use of com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor in project aic-praise by aic-sri-international.
the class ConstantFactor method multiply.
@Override
public Factor multiply(Factor another) {
Factor result = null;
if (another instanceof ConstantFactor) {
ConstantFactor anotherConstant = (ConstantFactor) another;
result = multiplyTwoConstantFactors(anotherConstant);
} else if (another instanceof ExpressionFactor) {
ExpressionFactor anotherExpression = (ExpressionFactor) another;
result = evaluateAsFactor(Times.make(makeSymbol(getConstant()), (Expression) another), anotherExpression.getContext());
} else if (another instanceof TableFactor) {
TableFactor anotherTable = (TableFactor) another;
result = multiplyWithTableFactor(anotherTable);
} else {
throw new Error("Unknown class for another : class was " + another.getClass());
}
return result;
}
use of com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor in project aic-praise by aic-sri-international.
the class ConstantFactor method add.
@Override
public Factor add(Factor another) {
Factor result = null;
if (another instanceof ConstantFactor) {
ConstantFactor anotherConstant = (ConstantFactor) another;
result = addTwoConstantFactors(anotherConstant);
} else if (another instanceof ExpressionFactor) {
ExpressionFactor anotherExpression = (ExpressionFactor) another;
result = evaluateAsFactor(Plus.make(makeSymbol(getConstant()), (Expression) another), anotherExpression.getContext());
} else if (another instanceof TableFactor) {
TableFactor anotherTable = (TableFactor) another;
result = addATableFactor(anotherTable);
} else {
throw new Error("Unknown class for another : class was " + another.getClass());
}
return result;
}
use of com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor 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.core.expression.api.ExpressionFactor in project aic-praise by aic-sri-international.
the class PolytopeTest method testGetEquivalentAtomicPolytope.
@Test
public void testGetEquivalentAtomicPolytope() {
product = Polytope.multiply(list(simplexU));
actual = Polytopes.getEquivalentAtomicPolytopeOn(u, product);
println("Atomic polytope on u equivalent to " + product + ": " + actual);
expected = simplexU;
assertEquals(expected, actual);
product = Polytope.multiply(list(simplexU, convexHullUV));
actual = Polytopes.getEquivalentAtomicPolytopeOn(u, product);
println("Atomic polytope on u equivalent to " + product + ": " + actual);
expected = simplexU;
assertEquals(expected, actual);
product = Polytope.multiply(list(simplexU, convexHullUFreeVBound, convexHullVW));
actual = Polytopes.getEquivalentAtomicPolytopeOn(u, product);
println("Atomic polytope on u equivalent to " + product + ": " + actual);
expected = simplexU;
assertEquals(expected, actual);
product = Polytope.multiply(list(simplexU, convexHullUV));
actual = Polytopes.getEquivalentAtomicPolytopeOn(u, product);
println("Atomic polytope on u equivalent to " + product + ": " + actual);
expected = simplexU;
assertEquals(expected, actual);
product = Polytope.multiply(list(simplexU, convexHullUFreeVBound));
actual = Polytopes.getEquivalentAtomicPolytopeOn(u, product);
println("Atomic polytope on u equivalent to " + product + ": " + actual);
expected = simplexU;
assertEquals(expected, actual);
try {
product = Polytope.multiply(list(simplexU, convexHullUBoundVFree));
actual = Polytopes.getEquivalentAtomicPolytopeOn(u, product);
fail("Should have failed because V is free in polytope but query is U");
} catch (AssertionError e) {
if (!e.getMessage().contains("free variables")) {
fail("Should have complained about free variables");
}
}
product = Polytope.multiply(list(convexHullUFreeVBound, convexHullVW));
actual = Polytopes.getEquivalentAtomicPolytopeOn(u, product);
println("Atomic polytope on u equivalent to " + product + ": " + actual);
ExpressionFactor expectedExpressionFactor = new DefaultExpressionFactor(parse("if U then if V then if W then 8 else 10 else 15 else if V then if W then 12 else 15 else 15"), context);
expected = new IntensionalConvexHullOfFactors(list(v, w), expectedExpressionFactor);
println(expected.toString());
println(actual.toString());
// factor are compared by reference, not value
assertEquals(expected.toString(), actual.toString());
}
use of com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor in project aic-praise by aic-sri-international.
the class AnytimeExactBPTest method runRodrigos.
private void runRodrigos(ExpressionFactorNetwork factorNetwork, Expression query, Expression expected) {
long initialTime = System.currentTimeMillis();
ExpressionFactor resultFactor = solveWithExactBP(query, factorNetwork);
Context context = resultFactor.getContext();
Expression normalizedResult = PRAiSEUtil.normalize(query, resultFactor, context);
long finalTime = System.currentTimeMillis();
println("ExactBP: " + normalizedResult);
println("Time: " + (finalTime - initialTime) + " ms.");
printResults(expected, resultFactor, normalizedResult);
assertEquals(expected, resultFactor);
Expression normalizedAnytimeResult = solveWithAnytimeExactBP(query, factorNetwork, context);
// Expression test = parse("there exists C in Real : ((C*(" + resultFactor.getInnerExpression() + ")) = " + normalizedAnytimeResult + ")");
// println("Solving " + test);
// Expression testResult = context.evaluate(test);
// assertEquals(TRUE, testResult);
Expression test = parse("(" + normalizedResult + ") = (" + normalizedAnytimeResult + ")");
Expression testResult = context.evaluate(test);
assertEquals(TRUE, testResult);
// assertEquals(normalizedResult, normalizedAnytimeResult);
}
Aggregations