Search in sources :

Example 26 with TrueContext

use of com.sri.ai.grinder.sgdpllt.core.TrueContext in project aic-praise by aic-sri-international.

the class UAIUtil method convertGenericTableToInstance.

public static Expression convertGenericTableToInstance(FunctionTable functionTable, Expression genericFunctionTableExpr, List<Integer> instanceVarIdxs) {
    Expression result = genericFunctionTableExpr;
    Context context = new TrueContext();
    for (int i = 0; i < functionTable.numberVariables(); i++) {
        // Replace the generic variable name with the correct instance name
        result = SyntacticSubstitute.replace(result, Expressions.makeSymbol(genericVariableName(i)), Expressions.makeSymbol(instanceVariableName(instanceVarIdxs.get(i))), context);
        int varCardinality = functionTable.cardinality(i);
        for (int c = 0; c < varCardinality; c++) {
            // Replace the generic constants with constants for the variable index (if they differ)
            Expression genericConstant = Expressions.makeSymbol(genericConstantValueForVariable(c, i, varCardinality));
            Expression instanceConstant = Expressions.makeSymbol(instanceConstantValueForVariable(c, instanceVarIdxs.get(i), varCardinality));
            if (!genericConstant.equals(instanceConstant)) {
                result = SyntacticSubstitute.replace(result, genericConstant, instanceConstant, context);
            }
        }
    }
    return result;
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext) Expression(com.sri.ai.expresso.api.Expression) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext)

Example 27 with TrueContext

use of com.sri.ai.grinder.sgdpllt.core.TrueContext in project aic-expresso by aic-sri-international.

the class RecursiveTest method runTest.

private void runTest(Rewriter rewriter, Expression initial, Expression expected, Map<Expression, Expression> symbolsAndTypes) {
    CompoundTheory theory = new CompoundTheory(new PropositionalTheory(), new DifferenceArithmeticTheory(false, true));
    Context context = new TrueContext(theory);
    context = context.registerAdditionalSymbolsAndTypes(symbolsAndTypes);
    Rewriter recursive = new Recursive(rewriter);
    Expression solution = recursive.apply(initial, context);
    System.out.println("Solution: " + solution);
    assertEquals(expected, solution);
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext) Expression(com.sri.ai.expresso.api.Expression) DifferenceArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory) Rewriter(com.sri.ai.grinder.sgdpllt.rewriter.api.Rewriter) PropositionalTheory(com.sri.ai.grinder.sgdpllt.theory.propositional.PropositionalTheory) CompoundTheory(com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory) Recursive(com.sri.ai.grinder.sgdpllt.rewriter.core.Recursive) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext)

Example 28 with TrueContext

use of com.sri.ai.grinder.sgdpllt.core.TrueContext in project aic-expresso by aic-sri-international.

the class SetDNFRewriterTest method setUp.

@Before
public void setUp() {
    context = new TrueContext(new CompoundTheory(new DifferenceArithmeticTheory(false, false), new TupleTheory()));
    IntegerInterval intType = new IntegerInterval(1, 10);
    context = (Context) GrinderUtil.extendRegistryWith(map("M", intType.toString(), "N", intType.toString()), Arrays.asList(intType), context);
    rewriter = new SetDNFRewriter();
}
Also used : DifferenceArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory) IntegerInterval(com.sri.ai.expresso.type.IntegerInterval) CompoundTheory(com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext) TupleTheory(com.sri.ai.grinder.sgdpllt.theory.tuple.TupleTheory) SetDNFRewriter(com.sri.ai.grinder.sgdpllt.library.set.invsupport.SetDNFRewriter) Before(org.junit.Before)

Example 29 with TrueContext

use of com.sri.ai.grinder.sgdpllt.core.TrueContext in project aic-expresso by aic-sri-international.

the class SetExpressionIsEqualToEmptySetTest method setUp.

@Before
public void setUp() {
    context = new TrueContext(new CompoundTheory(new DifferenceArithmeticTheory(false, false), new TupleTheory()));
    IntegerInterval intType = new IntegerInterval(1, 10);
    context = (Context) GrinderUtil.extendRegistryWith(map("M", intType.toString(), "N", intType.toString(), "X'", intType.toString(), "X''", intType.toString(), "Y", intType.toString()), Arrays.asList(intType), context);
    rewriter = new SetExpressionIsEqualToEmptySet();
}
Also used : SetExpressionIsEqualToEmptySet(com.sri.ai.grinder.sgdpllt.library.set.invsupport.SetExpressionIsEqualToEmptySet) DifferenceArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory) IntegerInterval(com.sri.ai.expresso.type.IntegerInterval) CompoundTheory(com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext) TupleTheory(com.sri.ai.grinder.sgdpllt.theory.tuple.TupleTheory) Before(org.junit.Before)

Example 30 with TrueContext

use of com.sri.ai.grinder.sgdpllt.core.TrueContext in project aic-praise by aic-sri-international.

the class InferenceForFactorGraphAndEvidence method solve.

/**
	 * Returns the marginal/posterior for the query expression;
	 * if the query expression is not a random variable,
	 * the result is expressed in terms of a symbol 'query'.
	 */
public Expression solve(Expression queryExpression) {
    Expression factorGraphWithEvidence = factorGraph;
    if (evidence != null) {
        // add evidence factor
        factorGraphWithEvidence = Times.make(list(factorGraphWithEvidence, IfThenElse.make(evidence, ONE, ZERO)));
    }
    Expression queryVariable;
    List<Expression> queryVariables;
    List<Expression> indices;
    boolean queryIsCompoundExpression;
    if (allRandomVariables.contains(queryExpression)) {
        queryIsCompoundExpression = false;
        queryVariable = queryExpression;
        queryVariables = list(queryVariable);
        indices = setDifference(allRandomVariables, queryVariables);
    } else {
        queryIsCompoundExpression = true;
        queryVariable = makeSymbol("query");
        queryVariables = list(queryVariable);
        // Add a query variable equivalent to query expression; this introduces no cycles and the model remains a Bayesian network
        factorGraphWithEvidence = Times.make(list(factorGraphWithEvidence, parse("if query <=> " + queryExpression + " then 1 else 0")));
        // 'query' is not in 'allRandomVariables' 
        indices = allRandomVariables;
        // in case it was not there before -- it is ok to leave it there for other queries
        mapFromSymbolNameToTypeName.put("query", "Boolean");
        // in case it was not there before
        mapFromCategoricalTypeNameToSizeString.put("Boolean", "2");
    }
    // Solve the problem.
    Expression unnormalizedMarginal = sum(indices, factorGraphWithEvidence);
    //		System.out.println("Unnormalized marginal: " + unnormalizedMarginal);
    Expression marginal;
    if (evidence == null && isBayesianNetwork) {
        // model was a Bayesian network with no evidence, so marginal is equal to unnormalized marginal.
        marginal = unnormalizedMarginal;
    } else {
        // We now marginalize on all variables. Since unnormalizedMarginal is the marginal on all variables but the query, we simply take that and marginalize on the query alone.
        if (evidenceProbability == null) {
            evidenceProbability = solver.solve(semiRing, unnormalizedMarginal, queryVariables, mapFromSymbolNameToTypeName, mapFromCategoricalTypeNameToSizeString, additionalTypes, isUniquelyNamedConstantPredicate, theory);
        }
        // Bayes theorem: P(Q | E) = P(Q and E)/P(E)
        marginal = Division.make(unnormalizedMarginal, evidenceProbability);
        // now we use the algorithm again for simplifying the above division; this is a lazy way of doing this, as it performs search on the query variable again -- we could instead write an ad hoc function to divide all numerical constants by the normalization constant, but the code would be uglier and the gain very small, since this is a search on a single variable anyway.
        marginal = evaluate(marginal);
    }
    if (queryIsCompoundExpression) {
        // replace the query variable with the query expression
        marginal = marginal.replaceAllOccurrences(queryVariable, queryExpression, new TrueContext());
    }
    return marginal;
}
Also used : Expression(com.sri.ai.expresso.api.Expression) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext)

Aggregations

TrueContext (com.sri.ai.grinder.sgdpllt.core.TrueContext)31 CompoundTheory (com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory)22 DifferenceArithmeticTheory (com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory)22 Expression (com.sri.ai.expresso.api.Expression)18 Context (com.sri.ai.grinder.sgdpllt.api.Context)16 TupleTheory (com.sri.ai.grinder.sgdpllt.theory.tuple.TupleTheory)16 PropositionalTheory (com.sri.ai.grinder.sgdpllt.theory.propositional.PropositionalTheory)12 IntegerInterval (com.sri.ai.expresso.type.IntegerInterval)11 Before (org.junit.Before)11 Test (org.junit.Test)9 EqualityTheory (com.sri.ai.grinder.sgdpllt.theory.equality.EqualityTheory)8 LinearRealArithmeticTheory (com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.LinearRealArithmeticTheory)6 Theory (com.sri.ai.grinder.sgdpllt.api.Theory)5 Rewriter (com.sri.ai.grinder.sgdpllt.rewriter.api.Rewriter)4 Categorical (com.sri.ai.expresso.type.Categorical)3 Type (com.sri.ai.expresso.api.Type)2 Constraint (com.sri.ai.grinder.sgdpllt.api.Constraint)2 AbstractTheoryTestingSupport (com.sri.ai.grinder.sgdpllt.core.constraint.AbstractTheoryTestingSupport)2 CompleteMultiVariableContext (com.sri.ai.grinder.sgdpllt.core.constraint.CompleteMultiVariableContext)2 Beta (com.google.common.annotations.Beta)1