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;
}
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);
}
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();
}
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();
}
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;
}
Aggregations