Search in sources :

Example 1 with ExpressionFactorNetwork

use of com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.ExpressionFactorNetwork 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 ExpressionFactorNetwork

use of com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.ExpressionFactorNetwork 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 ExpressionFactorNetwork

use of com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.ExpressionFactorNetwork 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 ExpressionFactorNetwork

use of com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.ExpressionFactorNetwork in project aic-praise by aic-sri-international.

the class ExpressionBasedProblemToInterfaceBasedProblemConversion method translate.

public static Problem translate(ExpressionBasedProblem expressionBasedProblem) {
    ExpressionVariable queryVariable = new DefaultExpressionVariable(expressionBasedProblem.getQuerySymbol());
    ExpressionFactorNetwork factorNetwork = makeFactorNetwork(expressionBasedProblem);
    Predicate<Expression> isExpressionParameterPredicate = expressionBasedProblem.getIsParameterPredicate();
    Predicate<Variable> isParameterPredicate = makeIsParameterPredicate(isExpressionParameterPredicate);
    Problem problem = new DefaultVariableMarginalQuery(queryVariable, factorNetwork, isParameterPredicate);
    return problem;
}
Also used : 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) ExpressionVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionVariable) Variable(com.sri.ai.praise.core.representation.interfacebased.factor.api.Variable) DefaultExpressionVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionVariable) Expression(com.sri.ai.expresso.api.Expression) DefaultVariableMarginalQuery(com.sri.ai.praise.core.representation.interfacebased.factor.core.DefaultVariableMarginalQuery) 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) ExpressionBasedProblem(com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedProblem) Problem(com.sri.ai.praise.core.representation.interfacebased.factor.api.Problem)

Example 5 with ExpressionFactorNetwork

use of com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.ExpressionFactorNetwork in project aic-praise by aic-sri-international.

the class AnytimeExactBPTest method runGabriels.

private void runGabriels(String[] variableAndTypes, String factorNetworkString, String queryVariableString, Expression expected) {
    Theory theory = new CompoundTheory(new EqualityTheory(false, true), new DifferenceArithmeticTheory(false, false), new LinearRealArithmeticTheory(false, false), new TupleTheory(), new PropositionalTheory());
    Context context = new TrueContext(new CommonTheory()).extendWithSymbolsAndTypes(variableAndTypes);
    ExpressionFactorNetwork factorNetwork = expressionFactorNetwork(factorNetworkString, context);
    Expression query = Expressions.parse(queryVariableString);
    // not sure it will work
    Set<Expression> setOfFactors = new HashSet<>();
    for (IdentityWrapper iw : factorNetwork.getAs()) {
        ExpressionFactor f = (ExpressionFactor) iw.getObject();
        Expression expressionFactor = f;
        boolean successfullyAdded = setOfFactors.add(expressionFactor);
        if (!successfullyAdded) {
            setOfFactors.remove(expressionFactor);
            Expression squareFactor = apply("*", expressionFactor, expressionFactor);
            squareFactor = theory.evaluate(squareFactor, context);
            setOfFactors.add(squareFactor);
        }
    }
    // create model
    Model m = new Model(setOfFactors, theory, context, false, query);
    // do all iterations until the end, storing time
    Iterator<PartitionTree> bfsExpander = new BFS(m);
    IncrementalAnytimeBeliefPropagationWithSeparatorConditioning sbp = new IncrementalAnytimeBeliefPropagationWithSeparatorConditioning(m, bfsExpander);
    long initialTime = System.currentTimeMillis();
    Bound inferenceResult = null;
    println("----------------solving with Gabriels----------------");
    while (bfsExpander.hasNext()) {
        inferenceResult = sbp.expandAndComputeInference();
        // .normalize(theory, context));
        println("Current bound on " + query + ": " + inferenceResult);
    }
    long finalTime = System.currentTimeMillis();
    Expression normalizedResult = inferenceResult.normalize(theory, context);
    normalizedResult = ((IntensionalSet) normalizedResult).getHead();
    Expression normalizedexpected = PRAiSEUtil.normalize(query, expected, context);
    println("Result factor: " + ((IntensionalSet) inferenceResult).getHead());
    println("Normalized   : " + normalizedResult);
    // print the way it is done above
    println("Time: " + (finalTime - initialTime) + " ms.");
    println(normalizedexpected.equals(normalizedResult) ? "Correct!" : "Error!");
    Expression test = parse("(" + normalizedResult + ") = (" + normalizedexpected + ")");
    Expression testResult = context.evaluate(test);
    assertEquals(TRUE, testResult);
}
Also used : PartitionTree(IncrementalAnytimeExactBeliefPropagation.PartitionTree) ExpressionFactorNetwork(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.ExpressionFactorNetwork) EqualityTheory(com.sri.ai.grinder.theory.equality.EqualityTheory) 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) LinearRealArithmeticTheory(com.sri.ai.grinder.theory.linearrealarithmetic.LinearRealArithmeticTheory) Bound(com.sri.ai.grinder.library.bounds.Bound) PropositionalTheory(com.sri.ai.grinder.theory.propositional.PropositionalTheory) CompoundTheory(com.sri.ai.grinder.theory.compound.CompoundTheory) IncrementalAnytimeBeliefPropagationWithSeparatorConditioning(IncrementalAnytimeExactBeliefPropagation.IncrementalAnytimeBeliefPropagationWithSeparatorConditioning) TupleTheory(com.sri.ai.grinder.theory.tuple.TupleTheory) BFS(IncrementalAnytimeExactBeliefPropagation.Model.BFS) ExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor) IntensionalSet(com.sri.ai.expresso.api.IntensionalSet) HashSet(java.util.HashSet) TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) DifferenceArithmeticTheory(com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory) IdentityWrapper(com.sri.ai.util.base.IdentityWrapper) TrueContext(com.sri.ai.grinder.core.TrueContext) CommonTheory(com.sri.ai.grinder.application.CommonTheory) Expression(com.sri.ai.expresso.api.Expression) Model(IncrementalAnytimeExactBeliefPropagation.Model.Model)

Aggregations

ExpressionFactorNetwork (com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.ExpressionFactorNetwork)6 Expression (com.sri.ai.expresso.api.Expression)5 Context (com.sri.ai.grinder.api.Context)4 CommonTheory (com.sri.ai.grinder.application.CommonTheory)4 TrueContext (com.sri.ai.grinder.core.TrueContext)4 Theory (com.sri.ai.grinder.api.Theory)3 DefaultExpressionVariable (com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionVariable)3 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 ExpressionFactor (com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor)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 IncrementalAnytimeBeliefPropagationWithSeparatorConditioning (IncrementalAnytimeExactBeliefPropagation.IncrementalAnytimeBeliefPropagationWithSeparatorConditioning)1 BFS (IncrementalAnytimeExactBeliefPropagation.Model.BFS)1 Model (IncrementalAnytimeExactBeliefPropagation.Model.Model)1 PartitionTree (IncrementalAnytimeExactBeliefPropagation.PartitionTree)1 IntensionalSet (com.sri.ai.expresso.api.IntensionalSet)1 Bound (com.sri.ai.grinder.library.bounds.Bound)1