Search in sources :

Example 1 with Procedure

use of com.sri.ai.praise.other.integration.proceduralattachment.api.Procedure 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 2 with Procedure

use of com.sri.ai.praise.other.integration.proceduralattachment.api.Procedure 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)

Example 3 with Procedure

use of com.sri.ai.praise.other.integration.proceduralattachment.api.Procedure in project aic-praise by aic-sri-international.

the class ExpressionFactorNetwork method makeProceduralAttachmentExpressionFactor.

private static ProceduralAttachmentExpressionFactor makeProceduralAttachmentExpressionFactor(Entry<String, Procedure> variableNameAndProcedure, Context context) {
    DefaultExpressionVariable variable = makeVariable(variableNameAndProcedure);
    Procedure procedure = variableNameAndProcedure.getValue();
    ProceduralAttachmentExpressionFactor result = new ProceduralAttachmentExpressionFactor(variable, procedure, context);
    return result;
}
Also used : ProceduralAttachmentExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.proceduralattachment.ProceduralAttachmentExpressionFactor) Procedure(com.sri.ai.praise.other.integration.proceduralattachment.api.Procedure)

Aggregations

Procedure (com.sri.ai.praise.other.integration.proceduralattachment.api.Procedure)3 Expression (com.sri.ai.expresso.api.Expression)2 HOGMMultiQueryProblemSolver (com.sri.ai.praise.core.inference.byinputrepresentation.classbased.hogm.solver.HOGMMultiQueryProblemSolver)2 HOGMProblemResult (com.sri.ai.praise.core.inference.byinputrepresentation.classbased.hogm.solver.HOGMProblemResult)2 ProceduralAttachments (com.sri.ai.praise.other.integration.proceduralattachment.api.ProceduralAttachments)2 DefaultProceduralAttachments (com.sri.ai.praise.other.integration.proceduralattachment.core.DefaultProceduralAttachments)2 ProceduralAttachmentExpressionFactor (com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.proceduralattachment.ProceduralAttachmentExpressionFactor)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Test (org.junit.Test)1