Search in sources :

Example 1 with CommonTheory

use of com.sri.ai.grinder.application.CommonTheory 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 CommonTheory

use of com.sri.ai.grinder.application.CommonTheory 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 CommonTheory

use of com.sri.ai.grinder.application.CommonTheory in project aic-praise by aic-sri-international.

the class AnytimeExactBPTest method runRodrigos.

private void runRodrigos(String[] variableAndTypes, String factorNetworkString, String queryVariableString, Expression expected) {
    Context context = new TrueContext(new CommonTheory()).extendWithSymbolsAndTypes(variableAndTypes);
    ExpressionFactorNetwork factorNetwork = expressionFactorNetwork(factorNetworkString, context);
    Expression query = Expressions.parse(queryVariableString);
    printProblem(factorNetworkString, queryVariableString, expected);
    println("Solving P(" + queryVariableString + ") given " + factorNetworkString);
    runRodrigos(factorNetwork, query, expected);
}
Also used : TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) CommonTheory(com.sri.ai.grinder.application.CommonTheory) ExpressionFactorNetwork(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.ExpressionFactorNetwork) Expression(com.sri.ai.expresso.api.Expression) TrueContext(com.sri.ai.grinder.core.TrueContext)

Example 4 with CommonTheory

use of com.sri.ai.grinder.application.CommonTheory in project aic-praise by aic-sri-international.

the class ExpressionFactorTest method testSumOut.

@Test
public void testSumOut() {
    Context context = new TrueContext(new CommonTheory()).extendWithSymbolsAndTypes("U", "Boolean", "V", "Boolean");
    ExpressionVariable u = new DefaultExpressionVariable(parse("U"));
    List<ExpressionVariable> variablesToSumOut = new ArrayList<>();
    variablesToSumOut.add(u);
    ExpressionFactor factorUV = new DefaultExpressionFactor(parse("if U and V then 2 else 3"), context);
    Factor summedOutFactor = factorUV.sumOut(variablesToSumOut);
    assertEquals("if V then 5 else 6", summedOutFactor.toString());
}
Also used : TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) CommonTheory(com.sri.ai.grinder.application.CommonTheory) ExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor) DefaultExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionFactor) DefaultExpressionVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionVariable) DefaultExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionFactor) ConstantFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.ConstantFactor) ExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor) Factor(com.sri.ai.praise.core.representation.interfacebased.factor.api.Factor) DefaultExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionFactor) ArrayList(java.util.ArrayList) DefaultExpressionVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionVariable) ExpressionVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionVariable) TrueContext(com.sri.ai.grinder.core.TrueContext) Test(org.junit.Test)

Example 5 with CommonTheory

use of com.sri.ai.grinder.application.CommonTheory in project aic-praise by aic-sri-international.

the class ExpressionFactorTest method testAdd2.

@Test
public void testAdd2() {
    Theory theory = new CommonTheory();
    Context context = new TrueContext(theory);
    Expression a = DefaultSymbol.createSymbol("a");
    ExpressionFactor aFactor = new DefaultExpressionFactor(a, context);
    Factor result = (ExpressionFactor) aFactor.add(IDENTITY_FACTOR);
    System.out.println(result.toString());
    assertEquals("1 + a", result.toString());
}
Also used : CommonTheory(com.sri.ai.grinder.application.CommonTheory) TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) ExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor) DefaultExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionFactor) DefaultExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionFactor) PropositionalTheory(com.sri.ai.grinder.theory.propositional.PropositionalTheory) EqualityTheory(com.sri.ai.grinder.theory.equality.EqualityTheory) CommonTheory(com.sri.ai.grinder.application.CommonTheory) LinearRealArithmeticTheory(com.sri.ai.grinder.theory.linearrealarithmetic.LinearRealArithmeticTheory) Theory(com.sri.ai.grinder.api.Theory) DifferenceArithmeticTheory(com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory) CompoundTheory(com.sri.ai.grinder.theory.compound.CompoundTheory) TupleTheory(com.sri.ai.grinder.theory.tuple.TupleTheory) Expression(com.sri.ai.expresso.api.Expression) ConstantFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.ConstantFactor) ExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor) Factor(com.sri.ai.praise.core.representation.interfacebased.factor.api.Factor) DefaultExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionFactor) TrueContext(com.sri.ai.grinder.core.TrueContext) Test(org.junit.Test)

Aggregations

Context (com.sri.ai.grinder.api.Context)58 CommonTheory (com.sri.ai.grinder.application.CommonTheory)58 TrueContext (com.sri.ai.grinder.core.TrueContext)58 Expression (com.sri.ai.expresso.api.Expression)55 Test (org.junit.Test)47 Theory (com.sri.ai.grinder.api.Theory)36 Factor (com.sri.ai.praise.core.representation.interfacebased.factor.api.Factor)13 ExpressionFactor (com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor)13 DefaultExpressionFactor (com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionFactor)12 ConstantFactor (com.sri.ai.praise.core.representation.interfacebased.factor.core.ConstantFactor)11 CompoundTheory (com.sri.ai.grinder.theory.compound.CompoundTheory)9 DifferenceArithmeticTheory (com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory)9 EqualityTheory (com.sri.ai.grinder.theory.equality.EqualityTheory)9 LinearRealArithmeticTheory (com.sri.ai.grinder.theory.linearrealarithmetic.LinearRealArithmeticTheory)9 PropositionalTheory (com.sri.ai.grinder.theory.propositional.PropositionalTheory)9 TupleTheory (com.sri.ai.grinder.theory.tuple.TupleTheory)9 DefaultExpressionVariable (com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionVariable)7 ExpressionVariable (com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionVariable)5 ExpressionFactorNetwork (com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.ExpressionFactorNetwork)4 ArrayList (java.util.ArrayList)3