Search in sources :

Example 6 with ExpressionFactorsAndTypes

use of com.sri.ai.praise.sgsolver.solver.ExpressionFactorsAndTypes in project aic-praise by aic-sri-international.

the class HOGModelGrounding method makeInferencer.

/**
	 * Provides an appropriate {@link InferenceForFactorGraphAndEvidence} object.
	 * @param factorsAndTypes
	 * @param newUniqueConstantToTypeMap
	 * @return
	 */
private static InferenceForFactorGraphAndEvidence makeInferencer(FactorsAndTypes factorsAndTypes, Map<String, String> newUniqueConstantToTypeMap) {
    ExpressionFactorsAndTypes groundedFactorsAndTypesInformation = new ExpressionFactorsAndTypes(// factors
    Collections.emptyList(), factorsAndTypes.getMapFromRandomVariableNameToTypeName(), factorsAndTypes.getMapFromNonUniquelyNamedConstantNameToTypeName(), newUniqueConstantToTypeMap, factorsAndTypes.getMapFromCategoricalTypeNameToSizeString(), // additional types
    list());
    InferenceForFactorGraphAndEvidence inferencer = new InferenceForFactorGraphAndEvidence(groundedFactorsAndTypesInformation, false, null, true, null);
    return inferencer;
}
Also used : InferenceForFactorGraphAndEvidence(com.sri.ai.praise.sgsolver.solver.InferenceForFactorGraphAndEvidence) ExpressionFactorsAndTypes(com.sri.ai.praise.sgsolver.solver.ExpressionFactorsAndTypes)

Example 7 with ExpressionFactorsAndTypes

use of com.sri.ai.praise.sgsolver.solver.ExpressionFactorsAndTypes in project aic-praise by aic-sri-international.

the class SGSolverDemoController method importUAIModel.

private void importUAIModel(ActionEvent ae) {
    File uaiModelFile = uaiFileChooser.showOpenDialog(mainStage);
    if (uaiModelFile != null) {
        File uaiEvidenceFile = new File(uaiModelFile.getParent(), uaiModelFile.getName() + ".evid");
        StringWriter hogmWriter = new StringWriter();
        try (BufferedReader uaiModelReader = new BufferedReader(new FileReader(uaiModelFile));
            BufferedReader uaiEvidenceReader = new BufferedReader(new FileReader(uaiEvidenceFile));
            PrintWriter hogmPrintWriter = new PrintWriter(hogmWriter)) {
            UAI_to_HOGMv1_Using_Equalities_Translator translator = new UAI_to_HOGMv1_Using_Equalities_Translator();
            translator.translate(uaiModelFile.getName(), new Reader[] { uaiModelReader, uaiEvidenceReader }, new PrintWriter[] { hogmPrintWriter }, new TranslatorOptions());
            String hogmModel = hogmWriter.toString();
            // For convenience, pull out all possible queries
            HOGMParserWrapper parser = new HOGMParserWrapper();
            ParsedHOGModel parsedModel = parser.parseModel(hogmModel);
            FactorsAndTypes factorsAndTypes = new ExpressionFactorsAndTypes(parsedModel);
            List<String> queries = new ArrayList<>(factorsAndTypes.getMapFromRandomVariableNameToTypeName().keySet());
            newModel(hogmModel, queries);
        } catch (Throwable th) {
            FXUtil.exception(th);
        }
    }
}
Also used : ParsedHOGModel(com.sri.ai.praise.model.v1.hogm.antlr.ParsedHOGModel) FactorsAndTypes(com.sri.ai.praise.sgsolver.solver.FactorsAndTypes) ExpressionFactorsAndTypes(com.sri.ai.praise.sgsolver.solver.ExpressionFactorsAndTypes) ArrayList(java.util.ArrayList) TranslatorOptions(com.sri.ai.praise.lang.translate.TranslatorOptions) ExpressionFactorsAndTypes(com.sri.ai.praise.sgsolver.solver.ExpressionFactorsAndTypes) HOGMParserWrapper(com.sri.ai.praise.model.v1.hogm.antlr.HOGMParserWrapper) StringWriter(java.io.StringWriter) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File) PrintWriter(java.io.PrintWriter) UAI_to_HOGMv1_Using_Equalities_Translator(com.sri.ai.praise.lang.translate.impl.UAI_to_HOGMv1_Using_Equalities_Translator)

Example 8 with ExpressionFactorsAndTypes

use of com.sri.ai.praise.sgsolver.solver.ExpressionFactorsAndTypes in project aic-praise by aic-sri-international.

the class InferenceForFactorGraphAndEvidenceTest method runSimplifyTest.

private void runSimplifyTest() {
    InferenceForFactorGraphAndEvidence inferencer;
    Expression simplification;
    inferencer = new InferenceForFactorGraphAndEvidence(new ExpressionFactorsAndTypes(factors, mapFromRandomVariableNameToTypeName, mapFromNonUniquelyNamedConstantNameToTypeName, mapFromUniquelyNamedConstantNameToTypeName, mapFromCategoricalTypeNameToSizeString, list()), isBayesianNetwork, evidence, true, null);
    simplification = inferencer.simplify(queryExpression);
    assertEquals(expected, simplification);
}
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)

Example 9 with ExpressionFactorsAndTypes

use of com.sri.ai.praise.sgsolver.solver.ExpressionFactorsAndTypes in project aic-praise by aic-sri-international.

the class InferenceForFactorGraphAndEvidenceTest method testBurglary.

@Test
public void testBurglary() {
    // The definitions of types
    mapFromCategoricalTypeNameToSizeString = Util.map("Boolean", "2");
    // The definitions of variables
    mapFromRandomVariableNameToTypeName = Util.map("burglary", "Boolean", "alarm", "Boolean", "call", "Boolean");
    // The definitions of non-uniquely named constants
    mapFromNonUniquelyNamedConstantNameToTypeName = Util.map();
    // The definitions of non-uniquely named constants
    mapFromUniquelyNamedConstantNameToTypeName = Util.map();
    isBayesianNetwork = false;
    factors = Times.getMultiplicands(parse("" + "(if alarm then if call then 0.7 else 0.3 else if call then 0 else 1)*" + "(if burglary then if alarm then 0.9 else 0.1 else if alarm then 0.01 else 0.99)*" + "(if burglary then 0.1 else 0.9)"));
    InferenceForFactorGraphAndEvidence inferencer;
    inferencer = new InferenceForFactorGraphAndEvidence(new ExpressionFactorsAndTypes(factors, mapFromRandomVariableNameToTypeName, mapFromNonUniquelyNamedConstantNameToTypeName, mapFromUniquelyNamedConstantNameToTypeName, mapFromCategoricalTypeNameToSizeString, list()), isBayesianNetwork, evidence, false, null);
    Expression result = inferencer.sum(list(parse("alarm")), Times.make(factors));
    System.out.println(result);
}
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) Test(org.junit.Test)

Example 10 with ExpressionFactorsAndTypes

use of com.sri.ai.praise.sgsolver.solver.ExpressionFactorsAndTypes 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

ExpressionFactorsAndTypes (com.sri.ai.praise.sgsolver.solver.ExpressionFactorsAndTypes)10 Expression (com.sri.ai.expresso.api.Expression)7 InferenceForFactorGraphAndEvidence (com.sri.ai.praise.sgsolver.solver.InferenceForFactorGraphAndEvidence)6 HOGMParserWrapper (com.sri.ai.praise.model.v1.hogm.antlr.HOGMParserWrapper)4 ParsedHOGModel (com.sri.ai.praise.model.v1.hogm.antlr.ParsedHOGModel)4 FactorsAndTypes (com.sri.ai.praise.sgsolver.solver.FactorsAndTypes)4 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)3 StringJoiner (java.util.StringJoiner)2 Beta (com.google.common.annotations.Beta)1 Parser (com.sri.ai.expresso.api.Parser)1 Expressions (com.sri.ai.expresso.helper.Expressions)1 GrinderUtil (com.sri.ai.grinder.helper.GrinderUtil)1 Context (com.sri.ai.grinder.sgdpllt.api.Context)1 Theory (com.sri.ai.grinder.sgdpllt.api.Theory)1 TrueContext (com.sri.ai.grinder.sgdpllt.core.TrueContext)1 HOGModelGrounding (com.sri.ai.praise.lang.grounded.model.HOGModelGrounding)1 TranslatorOptions (com.sri.ai.praise.lang.translate.TranslatorOptions)1 UAI_to_HOGMv1_Using_Equalities_Translator (com.sri.ai.praise.lang.translate.impl.UAI_to_HOGMv1_Using_Equalities_Translator)1 HOGMSortDeclaration (com.sri.ai.praise.model.v1.HOGMSortDeclaration)1