Search in sources :

Example 1 with Wrapper

use of com.sri.ai.util.base.Wrapper 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 Wrapper

use of com.sri.ai.util.base.Wrapper 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)

Aggregations

Expression (com.sri.ai.expresso.api.Expression)2 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 ProceduralAttachments (com.sri.ai.praise.other.integration.proceduralattachment.api.ProceduralAttachments)2 DefaultProceduralAttachments (com.sri.ai.praise.other.integration.proceduralattachment.core.DefaultProceduralAttachments)2 Wrapper (com.sri.ai.util.base.Wrapper)2 Test (org.junit.Test)2