Search in sources :

Example 6 with HOGMProblemResult

use of com.sri.ai.praise.core.inference.byinputrepresentation.classbased.hogm.solver.HOGMProblemResult in project aic-praise by aic-sri-international.

the class HOGMMultiQueryProblemSolverTest method linearRealArithmeticOnPosition.

@Test
public void linearRealArithmeticOnPosition() {
    String model = "random position         : Real; // real, unobserved position of an object\n" + "random observedPosition : Real; // observed, noisy position of the same object\n" + "random event : Boolean;\n" + "random external : 1..5;\n" + "random alpha : Real;\n" + "\n" + "random internal1 : 1..5;\n" + "\n" + "random internal2 : 1..5;\n" + "\n" + "internal1 = internal2 - 2;\n" + "internal2 = external - 1;\n" + "\n" + "// p(position) proportional to inverted parabola around 0 + 10\n" + "if position > -10 and position < 10\n" + "   then -position^2 + 100\n" + "   else 0;\n" + "\n" + "// p(observedPosition | position) proportional to parabola around position + 1\n" + "if observedPosition - position > -1 and observedPosition - position < 1\n" + "   then -(observedPosition - position)^2 + 1\n" + "   else 0;\n" + "\n" + "// observed position is between 4 and 5; note that zero-mass events such as observedPosition = 4 will not work currently\n" + "observedPosition > 4 and observedPosition < 5;\n" + "\n" + "// event of position being between 3 and 6 has probability 1; anything shorter has probability less than 1\n" + "event <=> position > 3 and position < 6;\n" + "";
    String query1 = "event";
    String query2 = "internal1";
    String query3 = "alpha";
    HOGMMultiQueryProblemSolver solver = new HOGMMultiQueryProblemSolver(model, list(query1, query2, query3));
    ProceduralAttachments proceduralAttachments = new DefaultProceduralAttachments(map("external", (Procedure) p -> 5, "alpha", (Procedure) p -> 0.8));
    solver.setProceduralAttachments(proceduralAttachments);
    List<HOGMProblemResult> results = solver.getResults();
    assertEquals(3, results.size());
    HOGMProblemResult result = getFirst(results);
    result.getErrors().stream().forEach(e -> println(e));
    Expression resultValue = result.getResult();
    println(resultValue);
    println("Explanation");
    println(result.getExplanation());
    assertFalse(result.hasErrors());
    assertEquals(parse("if event then 1 else 0"), result.getResult());
    result = results.get(1);
    result.getErrors().stream().forEach(e -> println(e));
    resultValue = result.getResult();
    println(resultValue);
    println("Explanation");
    println(result.getExplanation());
    assertFalse(result.hasErrors());
    assertEquals(parse("if internal1 = 2 then 1 else 0"), result.getResult());
// TODO: BUGGY
// result = results.get(2);
// result.getErrors().stream().forEach(e -> println(e));
// resultValue = result.getResult();
// println(resultValue);
// println("Explanation");
// println(result.getExplanation());
// assertFalse(result.hasErrors());
// assertEquals(parse("if alpha = 0.8 then 1 else 0"), result.getResult());
}
Also used : HOGMMultiQueryProblemSolver(com.sri.ai.praise.core.inference.byinputrepresentation.classbased.hogm.solver.HOGMMultiQueryProblemSolver) HOGMProblemResult(com.sri.ai.praise.core.inference.byinputrepresentation.classbased.hogm.solver.HOGMProblemResult) Expression(com.sri.ai.expresso.api.Expression) Procedure(com.sri.ai.praise.other.integration.proceduralattachment.api.Procedure) ProceduralAttachments(com.sri.ai.praise.other.integration.proceduralattachment.api.ProceduralAttachments) DefaultProceduralAttachments(com.sri.ai.praise.other.integration.proceduralattachment.core.DefaultProceduralAttachments) DefaultProceduralAttachments(com.sri.ai.praise.other.integration.proceduralattachment.core.DefaultProceduralAttachments) Test(org.junit.Test)

Example 7 with HOGMProblemResult

use of com.sri.ai.praise.core.inference.byinputrepresentation.classbased.hogm.solver.HOGMProblemResult in project aic-praise by aic-sri-international.

the class HOGMQueryTask method call.

@Override
public HOGMProblemResult call() {
    final AtomicReference<HOGMProblemResult> result = new AtomicReference<>();
    PRAiSEController.computeExpressionWithDesiredPrecision(() -> {
        solver = new HOGMMultiQueryProblemSolver(model, query);
        ProceduralAttachments proceduralAttachments = new DefaultProceduralAttachments(map("ultimateAnswer", (Procedure) p -> 42));
        solver.setProceduralAttachments(proceduralAttachments);
        List<HOGMProblemResult> queryResults = solver.getResults();
        if (queryResults.size() == 1) {
            HOGMProblemResult queryResult = queryResults.get(0);
            if (queryResult.hasErrors()) {
                result.set(queryResult);
            } else {
                Expression answer = solver.simplifyAnswer(queryResult.getResult(), queryResult.getQueryExpression());
                result.set(new HOGMProblemResult(queryResult.getQueryString(), queryResult.getQueryExpression(), queryResult.getParsedModel(), answer, queryResult.getMillisecondsToCompute()));
            }
        }
    });
    return result.get();
}
Also used : HOGMMultiQueryProblemSolver(com.sri.ai.praise.core.inference.byinputrepresentation.classbased.hogm.solver.HOGMMultiQueryProblemSolver) HOGMProblemResult(com.sri.ai.praise.core.inference.byinputrepresentation.classbased.hogm.solver.HOGMProblemResult) Expression(com.sri.ai.expresso.api.Expression) Procedure(com.sri.ai.praise.other.integration.proceduralattachment.api.Procedure) AtomicReference(java.util.concurrent.atomic.AtomicReference) DefaultProceduralAttachments(com.sri.ai.praise.other.integration.proceduralattachment.core.DefaultProceduralAttachments) ProceduralAttachments(com.sri.ai.praise.other.integration.proceduralattachment.api.ProceduralAttachments) DefaultProceduralAttachments(com.sri.ai.praise.other.integration.proceduralattachment.core.DefaultProceduralAttachments)

Aggregations

HOGMMultiQueryProblemSolver (com.sri.ai.praise.core.inference.byinputrepresentation.classbased.hogm.solver.HOGMMultiQueryProblemSolver)7 HOGMProblemResult (com.sri.ai.praise.core.inference.byinputrepresentation.classbased.hogm.solver.HOGMProblemResult)7 Expression (com.sri.ai.expresso.api.Expression)6 Test (org.junit.Test)4 ProceduralAttachments (com.sri.ai.praise.other.integration.proceduralattachment.api.ProceduralAttachments)3 Procedure (com.sri.ai.praise.other.integration.proceduralattachment.api.Procedure)3 DefaultProceduralAttachments (com.sri.ai.praise.other.integration.proceduralattachment.core.DefaultProceduralAttachments)3 ExpressoConfiguration (com.sri.ai.expresso.ExpressoConfiguration)1 Expressions.parse (com.sri.ai.expresso.helper.Expressions.parse)1 Util.getFirst (com.sri.ai.util.Util.getFirst)1 Util.list (com.sri.ai.util.Util.list)1 Util.map (com.sri.ai.util.Util.map)1 Util.println (com.sri.ai.util.Util.println)1 ThreadExplanationLogger.code (com.sri.ai.util.explanation.logging.api.ThreadExplanationLogger.code)1 ThreadExplanationLogger.explanationBlockToFile (com.sri.ai.util.explanation.logging.api.ThreadExplanationLogger.explanationBlockToFile)1 List (java.util.List)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Assert.assertFalse (org.junit.Assert.assertFalse)1