Search in sources :

Example 31 with TrueContext

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

the class InferenceForFactorGraphAndEvidenceTest method runTestWithFactorizationOption.

/**
	 * @param useFactorization
	 * @param queryExpression
	 * @param evidence
	 * @param expected
	 * @param isBayesianNetwork
	 * @param factorGraph
	 * @param mapFromRandomVariableNameToTypeName
	 * @param mapFromNonUniquelyNamedConstantNameToTypeName
	 * @param mapFromUniquelyNamedConstantNameToTypeName
	 * @param mapFromCategoricalTypeNameToSizeString
	 */
private void runTestWithFactorizationOption(boolean useFactorization, Expression queryExpression, Expression evidence, Expression expected, boolean isBayesianNetwork, List<Expression> factors, Map<String, String> mapFromRandomVariableNameToTypeName, Map<String, String> mapFromNonUniquelyNamedConstantNameToTypeName, Map<String, String> mapFromUniquelyNamedConstantNameToTypeName, Map<String, String> mapFromCategoricalTypeNameToSizeString, Collection<Type> additionalTypes) {
    InferenceForFactorGraphAndEvidence inferencer;
    Expression marginal;
    inferencer = new InferenceForFactorGraphAndEvidence(new ExpressionFactorsAndTypes(factors, mapFromRandomVariableNameToTypeName, mapFromNonUniquelyNamedConstantNameToTypeName, mapFromUniquelyNamedConstantNameToTypeName, mapFromCategoricalTypeNameToSizeString, additionalTypes), isBayesianNetwork, evidence, useFactorization, null);
    marginal = inferencer.solve(queryExpression);
    TrueContext context = new TrueContext();
    marginal = Expressions.roundToAGivenPrecision(marginal, 9, context);
    expected = Expressions.roundToAGivenPrecision(expected, 9, context);
    if (expected.equals(marginal)) {
    // Ok!
    } else // check if they are not identical, but equivalent expressions
    if (inferencer.evaluate(apply(MINUS, expected, marginal)).equals(ZERO)) {
    // first attempt was to compare with equality, but this requires a more complete test of equality theory literals to exclude such a complex equality from being considered a literal, which is much more expensive
    // Ok!
    } else {
        throw new AssertionError("expected:<" + expected + "> but was:<" + marginal + ">, which is not even equivalent.");
    }
// Not working yet, need to debug		
//		Expression negationMarginal;
//		negationMarginal = inferencer.solve(Not.make(queryExpression));
//		negationMarginal = Expressions.roundToAGivenPrecision(negationMarginal, 9, context);
//		expected = inferencer.evaluate(parse(negationMarginal + " = 1 - " + marginal));
//		assertEquals(expected, TRUE);
}
Also used : InferenceForFactorGraphAndEvidence(com.sri.ai.praise.sgsolver.solver.InferenceForFactorGraphAndEvidence) Expression(com.sri.ai.expresso.api.Expression) ExpressionFactorsAndTypes(com.sri.ai.praise.sgsolver.solver.ExpressionFactorsAndTypes) 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