Search in sources :

Example 1 with ProceduralAttachments

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

the class ProceduralAttachmentFactorTest method testReals.

@Test
public void testReals() {
    ExpressoConfiguration.setDisplayNumericsExactlyForSymbols(false);
    ExpressoConfiguration.setDisplayNumericsMostDecimalPlacesInApproximateRepresentationOfNumericalSymbols(3);
    Theory theory;
    Context context;
    Procedure<Double> procedure1;
    Procedure<Double> procedure2;
    Procedure<Double> procedure3;
    Procedure<Double> procedure4;
    Procedure<Double> procedure5;
    Wrapper<Boolean> ran1 = new Wrapper<Boolean>(false);
    Wrapper<Boolean> ran2 = new Wrapper<Boolean>(false);
    Wrapper<Boolean> ran3 = new Wrapper<Boolean>(false);
    Wrapper<Boolean> ran4 = new Wrapper<Boolean>(false);
    Wrapper<Boolean> ran5 = new Wrapper<Boolean>(false);
    ExpressionFactorNetwork network;
    Factor queryResult;
    theory = new CommonTheory();
    context = new TrueContext(theory);
    context = context.extendWithSymbolsAndTypes(parse("V1"), parse("Boolean"), parse("V2"), parse("Boolean"), parse("V3"), parse("Boolean"), parse("V4"), parse("Boolean"), parse("V5"), parse("Boolean"), parse("O1"), parse("Real"), parse("O2"), parse("Real"), parse("O3"), parse("Real"), parse("O4"), parse("Real"), parse("O5"), parse("Real"));
    procedure1 = p -> {
        println("Proceeding with procedure 1!");
        ran1.value = true;
        return 1.0;
    };
    procedure2 = p -> {
        println("Proceeding with procedure 2!");
        ran2.value = true;
        return 2.0;
    };
    procedure3 = p -> {
        println("Proceeding with procedure 3!");
        ran3.value = true;
        // this is going to make the condition fail!
        return 30.0;
    };
    procedure4 = p -> {
        println("Proceeding with procedure 4!");
        ran4.value = true;
        return 4.0;
    };
    procedure5 = p -> {
        println("Proceeding with procedure 5!");
        ran5.value = true;
        return 5.0;
    };
    ProceduralAttachments proceduralAttachments = new DefaultProceduralAttachments(map("O1", procedure1, "O2", procedure2, "O3", procedure3, "O4", procedure4, "O5", procedure5));
    List<Expression> factorExpressions = list(parse("if V1 and O1 > 0.9 and O1 < 1.1 and V2 then 0.8 else 0.2"), parse("if V2 and O2 > 1.9 and O2 < 2.1 and V3 then 0.8 else 0.2"), parse("if V3 and O3 > 2.9 and O3 < 3.1 and V4 then 0.8 else 0.2"), parse("if V4 and O4 > 3.9 and O4 < 4.1 and V5 then 0.8 else 0.2"), parse("if V5 and O5 > 4.9 and O5 < 5.1 then 0.8 else 0.2"));
    network = expressionFactorNetwork(factorExpressions, proceduralAttachments, context);
    ExactBP algorithm = new ExactBP(new DefaultExpressionVariable(parse("V1")), network);
    queryResult = algorithm.apply();
    println("Result computed for query V1.");
    println("Result: " + queryResult);
    assertTrue(ran1.value);
    assertTrue(ran2.value);
    assertTrue(ran3.value);
    assertFalse(ran4.value);
    assertFalse(ran5.value);
    ran1.value = false;
    ran2.value = false;
    ran3.value = false;
    ran4.value = false;
    ran5.value = false;
// TODO: why is the test below failing?
// // We need to create the procedural attachments and network again because procedures are run only once and then just use cached values.
// proceduralAttachments =
// new DefaultProceduralAttachments(
// map(
// "O1", procedure1,
// "O2", procedure2,
// "O3", procedure3,
// "O4", procedure4,
// "O5", procedure5
// ));
// network = expressionFactorNetwork(factorExpressions, proceduralAttachments, context);
// 
// algorithm = new ExactBP(new DefaultExpressionVariable(parse("O1")), network);
// queryResult = algorithm.apply();
// println("Result computed for query O1.");
// println("Result: " + queryResult);
// 
// assertTrue(ran1.value);
// assertTrue(ran2.value);
// assertTrue(ran3.value);
// assertFalse(ran4.value);
// assertFalse(ran5.value);
// assertEquals(queryResult.toString(), "if O1 > 0.99 then if O1 < 1.01 then 0.006 else 0 else 0");
}
Also used : TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) Wrapper(com.sri.ai.util.base.Wrapper) ExpressionFactorNetwork(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.ExpressionFactorNetwork) DefaultExpressionVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionVariable) CommonTheory(com.sri.ai.grinder.application.CommonTheory) Theory(com.sri.ai.grinder.api.Theory) TrueContext(com.sri.ai.grinder.core.TrueContext) CommonTheory(com.sri.ai.grinder.application.CommonTheory) ExactBP(com.sri.ai.praise.core.inference.byinputrepresentation.interfacebased.core.exactbp.fulltime.core.ExactBP) Expression(com.sri.ai.expresso.api.Expression) Factor(com.sri.ai.praise.core.representation.interfacebased.factor.api.Factor) 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 ProceduralAttachments

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

the class ProceduralAttachmentFactorTest method testBooleans.

@Test
public void testBooleans() {
    ExpressoConfiguration.setDisplayNumericsExactlyForSymbols(false);
    ExpressoConfiguration.setDisplayNumericsMostDecimalPlacesInApproximateRepresentationOfNumericalSymbols(3);
    Theory theory;
    Context context;
    Procedure<Boolean> procedure1;
    Procedure<Boolean> procedure2;
    Procedure<Boolean> procedure3;
    Procedure<Boolean> procedure4;
    Procedure<Boolean> procedure5;
    Wrapper<Boolean> ran1 = new Wrapper<Boolean>(false);
    Wrapper<Boolean> ran2 = new Wrapper<Boolean>(false);
    Wrapper<Boolean> ran3 = new Wrapper<Boolean>(false);
    Wrapper<Boolean> ran4 = new Wrapper<Boolean>(false);
    Wrapper<Boolean> ran5 = new Wrapper<Boolean>(false);
    ExpressionFactorNetwork network;
    Factor queryResult;
    theory = new CommonTheory();
    context = new TrueContext(theory);
    context = context.extendWithSymbolsAndTypes(parse("V1"), parse("Boolean"), parse("V2"), parse("Boolean"), parse("V3"), parse("Boolean"), parse("V4"), parse("Boolean"), parse("V5"), parse("Boolean"), parse("O1"), parse("Boolean"), parse("O2"), parse("Boolean"), parse("O3"), parse("Boolean"), parse("O4"), parse("Boolean"), parse("O5"), parse("Boolean"));
    procedure1 = p -> {
        println("Proceeding with procedure 1!");
        ran1.value = true;
        return true;
    };
    procedure2 = p -> {
        println("Proceeding with procedure 2!");
        ran2.value = true;
        return true;
    };
    procedure3 = p -> {
        println("Proceeding with procedure 3!");
        ran3.value = true;
        // THIS ONE RETURNS FALSE! This will short-circuit and procedures 4 and 5 will not be run
        return false;
    };
    procedure4 = p -> {
        println("Proceeding with procedure 4!");
        ran4.value = true;
        return true;
    };
    procedure5 = p -> {
        println("Proceeding with procedure 5!");
        ran5.value = true;
        return true;
    };
    ProceduralAttachments proceduralAttachments = new DefaultProceduralAttachments(map("O1", procedure1, "O2", procedure2, "O3", procedure3, "O4", procedure4, "O5", procedure5));
    List<Expression> factorExpressions = list(parse("if V1 and O1 and V2 then 0.8 else 0.2"), parse("if V2 and O2 and V3 then 0.8 else 0.2"), parse("if V3 and O3 and V4 then 0.8 else 0.2"), parse("if V4 and O4 and V5 then 0.8 else 0.2"), parse("if V5 and O5 then 0.8 else 0.2"));
    network = expressionFactorNetwork(factorExpressions, proceduralAttachments, context);
    ExactBP algorithm = new ExactBP(new DefaultExpressionVariable(parse("V1")), network);
    queryResult = algorithm.apply();
    println("Result computed for query V1.");
    println("Result: " + queryResult);
    assertTrue(ran1.value);
    assertTrue(ran2.value);
    assertTrue(ran3.value);
    assertFalse(ran4.value);
    assertFalse(ran5.value);
}
Also used : TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) Wrapper(com.sri.ai.util.base.Wrapper) ExpressionFactorNetwork(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.ExpressionFactorNetwork) DefaultExpressionVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionVariable) CommonTheory(com.sri.ai.grinder.application.CommonTheory) Theory(com.sri.ai.grinder.api.Theory) TrueContext(com.sri.ai.grinder.core.TrueContext) CommonTheory(com.sri.ai.grinder.application.CommonTheory) ExactBP(com.sri.ai.praise.core.inference.byinputrepresentation.interfacebased.core.exactbp.fulltime.core.ExactBP) Expression(com.sri.ai.expresso.api.Expression) Factor(com.sri.ai.praise.core.representation.interfacebased.factor.api.Factor) 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 3 with ProceduralAttachments

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

the class HOGMMultiQueryProblemSolverTest method linearRealArithmeticSmall.

@Test
public void linearRealArithmeticSmall() {
    String explanationFileName = "explanation.txt";
    explanationBlockToFile(explanationFileName, "Going to solve small linear arithmetic problem", code(() -> {
        String model = "random external : 1..5;\n" + "random internal2 : 1..5;\n" + "\n" + "internal2 = external - 1;\n" + "external = 5;" + "";
        String query2 = "internal2";
        HOGMMultiQueryProblemSolver solver = new HOGMMultiQueryProblemSolver(model, list(query2));
        ProceduralAttachments proceduralAttachments = new DefaultProceduralAttachments();
        solver.setProceduralAttachments(proceduralAttachments);
        List<HOGMProblemResult> results = solver.getResults();
        assertEquals(1, results.size());
        HOGMProblemResult result = results.get(0);
        result.getErrors().stream().forEach(e -> println(e));
        Expression resultValue = result.getResult();
        println(resultValue);
        println("Explanation");
        println(result.getExplanation());
        assertFalse(result.hasErrors());
        assertEquals(parse("if internal2 = 4 then 1 else 0"), result.getResult());
    }));
}
Also used : HOGMMultiQueryProblemSolver(com.sri.ai.praise.core.inference.byinputrepresentation.classbased.hogm.solver.HOGMMultiQueryProblemSolver) Procedure(com.sri.ai.praise.other.integration.proceduralattachment.api.Procedure) ThreadExplanationLogger.explanationBlockToFile(com.sri.ai.util.explanation.logging.api.ThreadExplanationLogger.explanationBlockToFile) Util.list(com.sri.ai.util.Util.list) Test(org.junit.Test) Expression(com.sri.ai.expresso.api.Expression) ProceduralAttachments(com.sri.ai.praise.other.integration.proceduralattachment.api.ProceduralAttachments) Util.map(com.sri.ai.util.Util.map) List(java.util.List) HOGMMultiQueryProblemSolver(com.sri.ai.praise.core.inference.byinputrepresentation.classbased.hogm.solver.HOGMMultiQueryProblemSolver) ExpressoConfiguration(com.sri.ai.expresso.ExpressoConfiguration) DefaultProceduralAttachments(com.sri.ai.praise.other.integration.proceduralattachment.core.DefaultProceduralAttachments) Assert.assertFalse(org.junit.Assert.assertFalse) Expressions.parse(com.sri.ai.expresso.helper.Expressions.parse) ThreadExplanationLogger.code(com.sri.ai.util.explanation.logging.api.ThreadExplanationLogger.code) Util.getFirst(com.sri.ai.util.Util.getFirst) HOGMProblemResult(com.sri.ai.praise.core.inference.byinputrepresentation.classbased.hogm.solver.HOGMProblemResult) Util.println(com.sri.ai.util.Util.println) Assert.assertEquals(org.junit.Assert.assertEquals) HOGMProblemResult(com.sri.ai.praise.core.inference.byinputrepresentation.classbased.hogm.solver.HOGMProblemResult) Expression(com.sri.ai.expresso.api.Expression) List(java.util.List) 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 4 with ProceduralAttachments

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

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

Expression (com.sri.ai.expresso.api.Expression)5 ProceduralAttachments (com.sri.ai.praise.other.integration.proceduralattachment.api.ProceduralAttachments)5 DefaultProceduralAttachments (com.sri.ai.praise.other.integration.proceduralattachment.core.DefaultProceduralAttachments)5 Test (org.junit.Test)4 HOGMMultiQueryProblemSolver (com.sri.ai.praise.core.inference.byinputrepresentation.classbased.hogm.solver.HOGMMultiQueryProblemSolver)3 HOGMProblemResult (com.sri.ai.praise.core.inference.byinputrepresentation.classbased.hogm.solver.HOGMProblemResult)3 Procedure (com.sri.ai.praise.other.integration.proceduralattachment.api.Procedure)3 Context (com.sri.ai.grinder.api.Context)2 Theory (com.sri.ai.grinder.api.Theory)2 CommonTheory (com.sri.ai.grinder.application.CommonTheory)2 TrueContext (com.sri.ai.grinder.core.TrueContext)2 ExactBP (com.sri.ai.praise.core.inference.byinputrepresentation.interfacebased.core.exactbp.fulltime.core.ExactBP)2 Factor (com.sri.ai.praise.core.representation.interfacebased.factor.api.Factor)2 DefaultExpressionVariable (com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionVariable)2 ExpressionFactorNetwork (com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.ExpressionFactorNetwork)2 Wrapper (com.sri.ai.util.base.Wrapper)2 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