Search in sources :

Example 1 with HOGMExpressionBasedModel

use of com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMExpressionBasedModel in project aic-praise by aic-sri-international.

the class AbstractHOGMv1_to_Target_Translator method translate.

// END-Translator
// 
@Override
protected void translate(String inputIdentifier, Reader[] inputModelReaders, PrintWriter[] translatedOutputs) throws Exception {
    // 
    // 1. Get the HOGM FactorNetwork Definition and Parse It
    String hogmv1Model = Util.readAll(inputModelReaders[0]);
    HOGMParserWrapper parser = new HOGMParserWrapper();
    HOGModel parsedModel = parser.parseModel(hogmv1Model);
    ExpressionBasedModel factorsAndTypes = new HOGMExpressionBasedModel(parsedModel);
    // Each additional input is treated as an evidence expression
    List<Expression> evidence = new ArrayList<>();
    if (inputModelReaders.length > 1) {
        for (int i = 1; i < inputModelReaders.length; i++) {
            evidence.add(Expressions.parse(Util.readAll(inputModelReaders[i])));
        }
    }
    translate(inputIdentifier, factorsAndTypes, evidence, translatedOutputs);
}
Also used : HOGModel(com.sri.ai.praise.core.representation.classbased.hogm.HOGModel) HOGMExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMExpressionBasedModel) ExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel) Expression(com.sri.ai.expresso.api.Expression) ArrayList(java.util.ArrayList) HOGMExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMExpressionBasedModel) HOGMParserWrapper(com.sri.ai.praise.core.representation.classbased.hogm.parsing.HOGMParserWrapper)

Example 2 with HOGMExpressionBasedModel

use of com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMExpressionBasedModel in project aic-praise by aic-sri-international.

the class ExpressionBasedProblemTest method runTest.

private void runTest(String modelString, String queryString, String expectedFactorExpressionsIncludingQueryDefinitionIfAnyString, String expectedQuerySymbolString, boolean expectedQueryIsCompound, String expectedSymbolsAndTypesString) {
    HOGMExpressionBasedModel model = new HOGMExpressionBasedModel(modelString);
    Expression queryExpression = parse(queryString);
    ExpressionBasedProblem problem = new DefaultExpressionBasedProblem(queryExpression, model);
    String actualFactorExpressionsIncludingQueryDefinitionIfAnyString = problem.getFactorExpressionsIncludingQueryDefinitionIfAny().toString();
    println(expectedFactorExpressionsIncludingQueryDefinitionIfAnyString);
    println(actualFactorExpressionsIncludingQueryDefinitionIfAnyString);
    assertEquals(expectedFactorExpressionsIncludingQueryDefinitionIfAnyString, actualFactorExpressionsIncludingQueryDefinitionIfAnyString);
    String actualQuerySymbol = problem.getQuerySymbol().toString();
    println("expected querySymbol: " + expectedQuerySymbolString);
    println("actual   querySymbol: " + actualQuerySymbol);
    assertEquals(expectedQuerySymbolString, actualQuerySymbol);
    boolean actualQueryIsCompound = problem.getQueryIsCompound();
    println("expected queryIsCompound: " + expectedQueryIsCompound);
    println("actual   queryIsCompound: " + actualQueryIsCompound);
    assertEquals(expectedQueryIsCompound, actualQueryIsCompound);
    String actualSymbolsAndTypesString = problem.getContext().getSymbolsAndTypes().toString();
    println("expected context string: " + expectedSymbolsAndTypesString);
    println("actual   context string: " + actualSymbolsAndTypesString);
    assertEquals(expectedSymbolsAndTypesString, actualSymbolsAndTypesString);
}
Also used : Expression(com.sri.ai.expresso.api.Expression) DefaultExpressionBasedProblem(com.sri.ai.praise.core.representation.classbased.expressionbased.core.DefaultExpressionBasedProblem) ExpressionBasedProblem(com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedProblem) HOGMExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMExpressionBasedModel) DefaultExpressionBasedProblem(com.sri.ai.praise.core.representation.classbased.expressionbased.core.DefaultExpressionBasedProblem)

Example 3 with HOGMExpressionBasedModel

use of com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMExpressionBasedModel in project aic-praise by aic-sri-international.

the class ExpressionBasedSolverTest 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) {
    HOGMExpressionBasedModel model = new HOGMExpressionBasedModel(factors, mapFromRandomVariableNameToTypeName, mapFromNonUniquelyNamedConstantNameToTypeName, mapFromUniquelyNamedConstantNameToTypeName, mapFromCategoricalTypeNameToSizeString, additionalTypes, isBayesianNetwork);
    model = model.getConditionedModel(evidence);
    runTest(queryExpression, model, expected, useFactorization);
}
Also used : HOGMExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMExpressionBasedModel)

Example 4 with HOGMExpressionBasedModel

use of com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMExpressionBasedModel in project aic-praise by aic-sri-international.

the class ExpressionBasedSolverTest method testAPI.

@Test
public void testAPI() {
    // IMPORTANT: this test is reproduced in the User Guide as an example,
    // so it should be kept in sync with it.
    String modelString = "" + "random earthquake: Boolean;" + "random burglary: Boolean;" + "random alarm: Boolean;" + "" + "earthquake 0.01;" + "burglary 0.1;" + "" + "if earthquake" + "   then if burglary" + "      then alarm 0.95" + "      else alarm 0.6" + "   else if burglary" + "      then alarm 0.9" + "      else alarm 0.01;" + "     " + "not alarm;" + "";
    Expression evidence = parse("not alarm");
    // can be any boolean expression
    boolean exploitFactorization = true;
    // exploit factorization (that is, employ ExpressionVariable Elimination,
    // as opposed to summing over the entire joint probability distribution).
    DefaultExpressionBasedProblem problem;
    Expression marginal;
    HOGMExpressionBasedModel model = new HOGMExpressionBasedModel(modelString);
    model = model.getConditionedModel(evidence);
    Expression queryExpression;
    ExpressionBasedSolver solver = new EvaluationExpressionBasedSolver(exploitFactorization);
    queryExpression = parse("not earthquake");
    // can be any boolean expression, or any random variable
    problem = new DefaultExpressionBasedProblem(queryExpression, model);
    marginal = solver.solve(problem);
    System.out.println("Marginal is " + marginal);
    queryExpression = parse("earthquake");
    problem = new DefaultExpressionBasedProblem(queryExpression, model);
    marginal = solver.solve(problem);
    System.out.println("Marginal is " + marginal);
}
Also used : ExpressionBasedSolver(com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.api.ExpressionBasedSolver) EvaluationExpressionBasedSolver(com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.core.byalgorithm.evaluation.EvaluationExpressionBasedSolver) ExactBPExpressionBasedSolver(com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.core.byalgorithm.exactbp.ExactBPExpressionBasedSolver) Expression(com.sri.ai.expresso.api.Expression) DefaultExpressionBasedProblem(com.sri.ai.praise.core.representation.classbased.expressionbased.core.DefaultExpressionBasedProblem) HOGMExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMExpressionBasedModel) EvaluationExpressionBasedSolver(com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.core.byalgorithm.evaluation.EvaluationExpressionBasedSolver) Test(org.junit.Test)

Example 5 with HOGMExpressionBasedModel

use of com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMExpressionBasedModel in project aic-praise by aic-sri-international.

the class Evaluation method getDomainSizes.

private String getDomainSizes(String model) {
    ExpressionBasedModel factorsAndTypes = new HOGMExpressionBasedModel(model);
    Collection<Type> types = factorsAndTypes.getAdditionalTypes();
    List<Integer> domainSizes = mapIntoList(types, t -> t.cardinality().intValueExact());
    String result = Util.join(domainSizes);
    return result;
}
Also used : Type(com.sri.ai.expresso.api.Type) HOGMExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMExpressionBasedModel) ExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel) HOGMExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMExpressionBasedModel)

Aggregations

HOGMExpressionBasedModel (com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMExpressionBasedModel)9 ExpressionBasedModel (com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel)5 Expression (com.sri.ai.expresso.api.Expression)4 HOGModel (com.sri.ai.praise.core.representation.classbased.hogm.HOGModel)3 HOGMParserWrapper (com.sri.ai.praise.core.representation.classbased.hogm.parsing.HOGMParserWrapper)3 ArrayList (java.util.ArrayList)3 DefaultExpressionBasedProblem (com.sri.ai.praise.core.representation.classbased.expressionbased.core.DefaultExpressionBasedProblem)2 Test (org.junit.Test)2 Type (com.sri.ai.expresso.api.Type)1 ExpressionBasedSolver (com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.api.ExpressionBasedSolver)1 EvaluationExpressionBasedSolver (com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.core.byalgorithm.evaluation.EvaluationExpressionBasedSolver)1 ExactBPExpressionBasedSolver (com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.core.byalgorithm.exactbp.ExactBPExpressionBasedSolver)1 HOGMModelParsing (com.sri.ai.praise.core.inference.byinputrepresentation.classbased.hogm.parsing.HOGMModelParsing)1 ExpressionBasedProblem (com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedProblem)1 TranslatorOptions (com.sri.ai.praise.core.representation.translation.ciaranframework.api.TranslatorOptions)1 HOGModelGrounding (com.sri.ai.praise.core.representation.translation.ciaranframework.core.uai.HOGModelGrounding)1 UAI_to_HOGMv1_Using_Equalities_Translator (com.sri.ai.praise.core.representation.translation.ciaranframework.core.uai.UAI_to_HOGMv1_Using_Equalities_Translator)1 Pair (com.sri.ai.util.base.Pair)1 Rational (com.sri.ai.util.math.Rational)1 BufferedReader (java.io.BufferedReader)1