Search in sources :

Example 11 with ExpressionFactor

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

the class ExpressionFactorTest method testMultiply1.

@Test
public void testMultiply1() {
    Theory theory = new CommonTheory();
    Context context = new TrueContext(theory);
    Expression a = DefaultSymbol.createSymbol("a");
    ExpressionFactor aFactor = new DefaultExpressionFactor(a, context);
    Expression b = DefaultSymbol.createSymbol("b");
    ExpressionFactor bFactor = new DefaultExpressionFactor(b, context);
    Factor abFactor = (ExpressionFactor) aFactor.multiply(bFactor);
    assertEquals("a * b", abFactor.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)

Example 12 with ExpressionFactor

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

the class ExpressionFactorTest method testMultiply2.

@Test
public void testMultiply2() {
    Theory theory = new CommonTheory();
    Context context = new TrueContext(theory);
    Expression a = DefaultSymbol.createSymbol("a");
    ExpressionFactor aFactor = new DefaultExpressionFactor(a, context);
    Factor result = (ExpressionFactor) aFactor.multiply(IDENTITY_FACTOR);
    assertEquals(aFactor, result);
}
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)

Example 13 with ExpressionFactor

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

the class ExactBPExpressionBasedSolver method replaceQuerySymbolByQueryExpressionIfNeeded.

protected Expression replaceQuerySymbolByQueryExpressionIfNeeded(ExpressionBasedProblem problem, Expression normalizedMarginal) {
    ExpressionFactor expressionFactor = (ExpressionFactor) normalizedMarginal;
    Expression normalizedMarginalWithQueryExpression = super.replaceQuerySymbolByQueryExpressionIfNeeded(problem, normalizedMarginal);
    ExpressionFactor result = new DefaultExpressionFactor(normalizedMarginalWithQueryExpression, expressionFactor.getContext());
    result.setExplanation(expressionFactor.getExplanation());
    return result;
}
Also used : DefaultExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionFactor) 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) Expression(com.sri.ai.expresso.api.Expression)

Example 14 with ExpressionFactor

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

Example 15 with ExpressionFactor

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

the class ExpressionFactorTest method testInvert.

@Test
public void testInvert() {
    Context context = new TrueContext(new CommonTheory()).extendWithSymbolsAndTypes("U", "Boolean", "V", "Boolean");
    ExpressionFactor factorUV = new DefaultExpressionFactor(parse("if U and V then 2 else 3"), context);
    Factor invertedFactor = factorUV.invert();
    assertEquals("if U then if V then 0.5 else 1/3 else 1/3", invertedFactor.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) 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) TrueContext(com.sri.ai.grinder.core.TrueContext) Test(org.junit.Test)

Aggregations

ExpressionFactor (com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor)26 DefaultExpressionFactor (com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionFactor)21 Expression (com.sri.ai.expresso.api.Expression)18 Factor (com.sri.ai.praise.core.representation.interfacebased.factor.api.Factor)18 Context (com.sri.ai.grinder.api.Context)17 TrueContext (com.sri.ai.grinder.core.TrueContext)17 CommonTheory (com.sri.ai.grinder.application.CommonTheory)15 Test (org.junit.Test)12 ConstantFactor (com.sri.ai.praise.core.representation.interfacebased.factor.core.ConstantFactor)11 Theory (com.sri.ai.grinder.api.Theory)9 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 ExpressionVariable (com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionVariable)5 DefaultExpressionVariable (com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionVariable)5 ArrayList (java.util.ArrayList)5 ExpressionFactorNetwork (com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.ExpressionFactorNetwork)4