Search in sources :

Example 1 with EvaluationExpressionBasedSolver

use of com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.core.byalgorithm.evaluation.EvaluationExpressionBasedSolver in project aic-praise by aic-sri-international.

the class ExpressionBasedSolverTest method testAPI.

@Test
public void testAPI() {
    // IMPORTANT: this test is reproduced in the User Guide as an example,
    // so it should be kept in sync with it.
    String modelString = "" + "random earthquake: Boolean;" + "random burglary: Boolean;" + "random alarm: Boolean;" + "" + "earthquake 0.01;" + "burglary 0.1;" + "" + "if earthquake" + "   then if burglary" + "      then alarm 0.95" + "      else alarm 0.6" + "   else if burglary" + "      then alarm 0.9" + "      else alarm 0.01;" + "     " + "not alarm;" + "";
    Expression evidence = parse("not alarm");
    // can be any boolean expression
    boolean exploitFactorization = true;
    // exploit factorization (that is, employ ExpressionVariable Elimination,
    // as opposed to summing over the entire joint probability distribution).
    DefaultExpressionBasedProblem problem;
    Expression marginal;
    HOGMExpressionBasedModel model = new HOGMExpressionBasedModel(modelString);
    model = model.getConditionedModel(evidence);
    Expression queryExpression;
    ExpressionBasedSolver solver = new EvaluationExpressionBasedSolver(exploitFactorization);
    queryExpression = parse("not earthquake");
    // can be any boolean expression, or any random variable
    problem = new DefaultExpressionBasedProblem(queryExpression, model);
    marginal = solver.solve(problem);
    System.out.println("Marginal is " + marginal);
    queryExpression = parse("earthquake");
    problem = new DefaultExpressionBasedProblem(queryExpression, model);
    marginal = solver.solve(problem);
    System.out.println("Marginal is " + marginal);
}
Also used : ExpressionBasedSolver(com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.api.ExpressionBasedSolver) EvaluationExpressionBasedSolver(com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.core.byalgorithm.evaluation.EvaluationExpressionBasedSolver) ExactBPExpressionBasedSolver(com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.core.byalgorithm.exactbp.ExactBPExpressionBasedSolver) Expression(com.sri.ai.expresso.api.Expression) DefaultExpressionBasedProblem(com.sri.ai.praise.core.representation.classbased.expressionbased.core.DefaultExpressionBasedProblem) HOGMExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMExpressionBasedModel) EvaluationExpressionBasedSolver(com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.core.byalgorithm.evaluation.EvaluationExpressionBasedSolver) Test(org.junit.Test)

Example 2 with EvaluationExpressionBasedSolver

use of com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.core.byalgorithm.evaluation.EvaluationExpressionBasedSolver in project aic-praise by aic-sri-international.

the class ExpressionBasedSolverTest method runTest.

private void runTest(Expression queryExpression, ExpressionBasedModel model, Expression expected, boolean useFactorization) throws AssertionError {
    ExpressionBasedProblem problem = new DefaultExpressionBasedProblem(queryExpression, model);
    ExpressionBasedSolver[] solvers = new ExpressionBasedSolver[] { // new EvaluationExpressionBasedSolver(useFactorization),
    new ExactBPExpressionBasedSolver() };
    for (ExpressionBasedSolver solver : solvers) {
        Expression marginal = solver.solve(problem);
        checkResult(problem, expected, marginal, solver);
    }
}
Also used : ExpressionBasedSolver(com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.api.ExpressionBasedSolver) EvaluationExpressionBasedSolver(com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.core.byalgorithm.evaluation.EvaluationExpressionBasedSolver) ExactBPExpressionBasedSolver(com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.core.byalgorithm.exactbp.ExactBPExpressionBasedSolver) ExactBPExpressionBasedSolver(com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.core.byalgorithm.exactbp.ExactBPExpressionBasedSolver) ExpressionBasedProblem(com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedProblem) DefaultExpressionBasedProblem(com.sri.ai.praise.core.representation.classbased.expressionbased.core.DefaultExpressionBasedProblem) Expression(com.sri.ai.expresso.api.Expression) DefaultExpressionBasedProblem(com.sri.ai.praise.core.representation.classbased.expressionbased.core.DefaultExpressionBasedProblem)

Aggregations

Expression (com.sri.ai.expresso.api.Expression)2 ExpressionBasedSolver (com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.api.ExpressionBasedSolver)2 EvaluationExpressionBasedSolver (com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.core.byalgorithm.evaluation.EvaluationExpressionBasedSolver)2 ExactBPExpressionBasedSolver (com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.core.byalgorithm.exactbp.ExactBPExpressionBasedSolver)2 DefaultExpressionBasedProblem (com.sri.ai.praise.core.representation.classbased.expressionbased.core.DefaultExpressionBasedProblem)2 ExpressionBasedProblem (com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedProblem)1 HOGMExpressionBasedModel (com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMExpressionBasedModel)1 Test (org.junit.Test)1