Search in sources :

Example 1 with ExpressionFactor

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

Example 2 with ExpressionFactor

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

Example 3 with ExpressionFactor

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;
}
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 4 with ExpressionFactor

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());
}
Also used : ExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor) DefaultExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionFactor) DefaultExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionFactor) IntensionalConvexHullOfFactors(com.sri.ai.praise.core.representation.interfacebased.polytope.core.byexpressiveness.convexhull.IntensionalConvexHullOfFactors) Test(org.junit.Test)

Example 5 with ExpressionFactor

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);
}
Also used : TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) ExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor) Expression(com.sri.ai.expresso.api.Expression)

Aggregations

ExpressionFactor (com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor)26 DefaultExpressionFactor (com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionFactor)21 Expression (com.sri.ai.expresso.api.Expression)18 Factor (com.sri.ai.praise.core.representation.interfacebased.factor.api.Factor)18 Context (com.sri.ai.grinder.api.Context)17 TrueContext (com.sri.ai.grinder.core.TrueContext)17 CommonTheory (com.sri.ai.grinder.application.CommonTheory)15 Test (org.junit.Test)12 ConstantFactor (com.sri.ai.praise.core.representation.interfacebased.factor.core.ConstantFactor)11 Theory (com.sri.ai.grinder.api.Theory)9 CompoundTheory (com.sri.ai.grinder.theory.compound.CompoundTheory)9 DifferenceArithmeticTheory (com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory)9 EqualityTheory (com.sri.ai.grinder.theory.equality.EqualityTheory)9 LinearRealArithmeticTheory (com.sri.ai.grinder.theory.linearrealarithmetic.LinearRealArithmeticTheory)9 PropositionalTheory (com.sri.ai.grinder.theory.propositional.PropositionalTheory)9 TupleTheory (com.sri.ai.grinder.theory.tuple.TupleTheory)9 ExpressionVariable (com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionVariable)5 DefaultExpressionVariable (com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionVariable)5 ArrayList (java.util.ArrayList)5 ExpressionFactorNetwork (com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.ExpressionFactorNetwork)4