Search in sources :

Example 1 with ExpressionFactorsAndTypes

use of com.sri.ai.praise.sgsolver.solver.ExpressionFactorsAndTypes 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 Model Definition and Parse It
    String hogmv1Model = Util.readAll(inputModelReaders[0]);
    HOGMParserWrapper parser = new HOGMParserWrapper();
    ParsedHOGModel parsedModel = parser.parseModel(hogmv1Model);
    FactorsAndTypes factorsAndTypes = new ExpressionFactorsAndTypes(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 : 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) Expression(com.sri.ai.expresso.api.Expression) ArrayList(java.util.ArrayList) ExpressionFactorsAndTypes(com.sri.ai.praise.sgsolver.solver.ExpressionFactorsAndTypes) HOGMParserWrapper(com.sri.ai.praise.model.v1.hogm.antlr.HOGMParserWrapper)

Example 2 with ExpressionFactorsAndTypes

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

the class Evaluation method getDomainSizes.

private String getDomainSizes(String model) {
    StringJoiner result = new StringJoiner(",");
    ExpressionFactorsAndTypes factorsAndTypes = new ExpressionFactorsAndTypes(model);
    // TODO - this only works with HOGM models and inequality integer types currently		
    for (com.sri.ai.expresso.api.Type type : factorsAndTypes.getAdditionalTypes()) {
        result.add("" + type.cardinality().intValueExact());
    }
    return result.toString();
}
Also used : ExpressionFactorsAndTypes(com.sri.ai.praise.sgsolver.solver.ExpressionFactorsAndTypes) StringJoiner(java.util.StringJoiner)

Example 3 with ExpressionFactorsAndTypes

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

the class HOGMQueryRunner method query.

public List<HOGMQueryResult> query() {
    List<HOGMQueryResult> result = new ArrayList<>();
    Expression queryExpr = null;
    //
    ParsedHOGModel parsedModel = null;
    for (String query : queries) {
        long startQuery = System.currentTimeMillis();
        List<HOGMQueryError> errors = new ArrayList<>();
        try {
            if (model == null || model.trim().equals("")) {
                errors.add(new HOGMQueryError(HOGMQueryError.Context.MODEL, "Model not specified", 0, 0, 0));
            }
            if (query == null || query.trim().equals("")) {
                errors.add(new HOGMQueryError(HOGMQueryError.Context.QUERY, "Query not specified", 0, 0, 0));
            }
            if (errors.size() == 0) {
                HOGMParserWrapper parser = new HOGMParserWrapper();
                if (parsedModel == null) {
                    parsedModel = parser.parseModel(model, new QueryErrorListener(HOGMQueryError.Context.MODEL, errors));
                }
                queryExpr = parser.parseTerm(query, new QueryErrorListener(HOGMQueryError.Context.QUERY, errors));
                if (errors.size() == 0) {
                    FactorsAndTypes factorsAndTypes = new ExpressionFactorsAndTypes(parsedModel);
                    if (!canceled) {
                        inferencer = new InferenceForFactorGraphAndEvidence(factorsAndTypes, false, null, true, getOptionalTheory());
                        startQuery = System.currentTimeMillis();
                        Expression marginal = inferencer.solve(queryExpr);
                        result.add(new HOGMQueryResult(query, queryExpr, parsedModel, marginal, System.currentTimeMillis() - startQuery));
                    }
                }
            }
        } catch (RecognitionException re) {
            errors.add(new HOGMQueryError(HOGMQueryError.Context.MODEL, re.getMessage(), re.getOffendingToken().getLine(), re.getOffendingToken().getStartIndex(), re.getOffendingToken().getStopIndex()));
        } catch (UnableToParseAllTheInputError utpai) {
            errors.add(new HOGMQueryError(utpai));
        } catch (HOGModelException me) {
            me.getErrors().forEach(modelError -> {
                String inStatement = modelError.getInStatementInfo().statement.toString();
                String inSource = modelError.getInStatementInfo().sourceText;
                String inSubStatement = modelError.getMessage();
                String inInfo = "";
                if (inSubStatement.equals("") || inSubStatement.equals(inSource)) {
                    inInfo = " in '" + inStatement + "'";
                } else {
                    inInfo = " ('" + inSubStatement + "') in '" + inStatement + "'";
                }
                if (!inSource.replaceAll(" ", "").replaceAll(";", "").equals(inStatement.replaceAll(" ", ""))) {
                    inInfo = inInfo + " derived from '" + inSource + "'";
                }
                errors.add(new HOGMQueryError(HOGMQueryError.Context.MODEL, modelError.getErrorType().formattedMessage() + inInfo, modelError.getInStatementInfo().line, modelError.getInStatementInfo().startIndex, modelError.getInStatementInfo().endIndex));
            });
        } catch (Throwable t) {
            // Unexpected
            errors.add(new HOGMQueryError(t));
        }
        if (errors.size() > 0) {
            result.add(new HOGMQueryResult(query, queryExpr, parsedModel, errors, System.currentTimeMillis() - startQuery));
        }
    }
    return result;
}
Also used : HOGModelException(com.sri.ai.praise.model.v1.HOGModelException) Expressions(com.sri.ai.expresso.helper.Expressions) Parser(com.sri.ai.expresso.api.Parser) ParsedHOGModel(com.sri.ai.praise.model.v1.hogm.antlr.ParsedHOGModel) Expression(com.sri.ai.expresso.api.Expression) FactorsAndTypes(com.sri.ai.praise.sgsolver.solver.FactorsAndTypes) Context(com.sri.ai.grinder.sgdpllt.api.Context) HOGModelException(com.sri.ai.praise.model.v1.HOGModelException) Theory(com.sri.ai.grinder.sgdpllt.api.Theory) HOGMParserWrapper(com.sri.ai.praise.model.v1.hogm.antlr.HOGMParserWrapper) ArrayList(java.util.ArrayList) Beta(com.google.common.annotations.Beta) GrinderUtil(com.sri.ai.grinder.helper.GrinderUtil) List(java.util.List) ExpressionFactorsAndTypes(com.sri.ai.praise.sgsolver.solver.ExpressionFactorsAndTypes) RecognitionException(org.antlr.v4.runtime.RecognitionException) InferenceForFactorGraphAndEvidence(com.sri.ai.praise.sgsolver.solver.InferenceForFactorGraphAndEvidence) UnableToParseAllTheInputError(com.sri.ai.praise.model.v1.hogm.antlr.UnableToParseAllTheInputError) HOGMSortDeclaration(com.sri.ai.praise.model.v1.HOGMSortDeclaration) Collections(java.util.Collections) 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) ExpressionFactorsAndTypes(com.sri.ai.praise.sgsolver.solver.ExpressionFactorsAndTypes) HOGMParserWrapper(com.sri.ai.praise.model.v1.hogm.antlr.HOGMParserWrapper) InferenceForFactorGraphAndEvidence(com.sri.ai.praise.sgsolver.solver.InferenceForFactorGraphAndEvidence) Expression(com.sri.ai.expresso.api.Expression) UnableToParseAllTheInputError(com.sri.ai.praise.model.v1.hogm.antlr.UnableToParseAllTheInputError) RecognitionException(org.antlr.v4.runtime.RecognitionException)

Example 4 with ExpressionFactorsAndTypes

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

the class HOGModelGroundingTest method test.

/**
	 * @throws AssertionError
	 */
@Test
public void test() throws AssertionError {
    long start = System.currentTimeMillis();
    StringJoiner sj = new StringJoiner("\n");
    sj.add("sort People : 10, Putin;");
    sj.add("sort Countries : 10, USA, Russia;");
    sj.add("random country : Countries;");
    sj.add("random president : People;");
    sj.add("random communism : Boolean;");
    sj.add("random democracy : Boolean;");
    sj.add("random votePutin : 1..15;");
    sj.add("if country = Russia then if president = Putin then communism else not communism else if democracy then not communism else communism;");
    sj.add("if country = Russia then if votePutin > 5 then president = Putin else not president = Putin;");
    HOGMParserWrapper parser = new HOGMParserWrapper();
    ParsedHOGModel parsedModel = parser.parseModel(sj.toString());
    FactorsAndTypes factorsAndTypes = new ExpressionFactorsAndTypes(parsedModel);
    List<Expression> evidence = new ArrayList<>();
    evidence.add(Expressions.parse("communism"));
    StringJoiner outputBuffer = new StringJoiner("");
    HOGModelGrounding.ground(factorsAndTypes, evidence, new // NOTE: an example listener that outputs in the UAI format
    HOGModelGrounding.Listener() {

        int numberVariables;

        StringJoiner preamble = new StringJoiner("");

        StringJoiner functionTables = new StringJoiner("");

        List<Pair<Integer, Integer>> evidence = new ArrayList<>();

        @Override
        public void numberGroundVariables(int number) {
            this.numberVariables = number;
            preamble.add("MARKOV\n");
            preamble.add("" + number + "\n");
        }

        @Override
        public void groundVariableCardinality(int variableIndex, int cardinality) {
            preamble.add("" + cardinality);
            if (variableIndex == (numberVariables - 1)) {
                preamble.add("\n");
            } else {
                preamble.add(" ");
            }
        }

        @Override
        public void numberFactors(int number) {
            preamble.add("" + number + "\n");
        }

        @Override
        public void factorParticipants(int factorIndex, int[] variableIndexes) {
            preamble.add("" + variableIndexes.length);
            for (int i = 0; i < variableIndexes.length; i++) {
                preamble.add(" " + variableIndexes[i]);
            }
            preamble.add("\n");
        }

        @Override
        public void factorValue(int numberFactorValues, boolean isFirstValue, boolean isLastValue, Rational value) {
            if (isFirstValue) {
                functionTables.add("\n" + numberFactorValues + "\n");
            } else {
                functionTables.add(" ");
            }
            functionTables.add("" + value.doubleValue());
            if (isLastValue) {
                functionTables.add("\n");
            }
        }

        @Override
        public void evidence(int variableIndex, int valueIndex) {
            evidence.add(new Pair<>(variableIndex, valueIndex));
        }

        @Override
        public void groundingComplete() {
            long end = System.currentTimeMillis() - start;
            outputBuffer.add("--- MODEL ---\n");
            outputBuffer.add(preamble.toString());
            outputBuffer.add(functionTables.toString());
            outputBuffer.add("--- EVIDENCE ---\n");
            outputBuffer.add("" + evidence.size());
            for (Pair<Integer, Integer> evidenceAssignment : evidence) {
                outputBuffer.add(" ");
                outputBuffer.add(evidenceAssignment.first.toString());
                outputBuffer.add(" ");
                outputBuffer.add(evidenceAssignment.second.toString());
            }
            System.out.println(outputBuffer.toString());
            System.out.println("\nTime taken for grounding (not printing): " + end + " ms.");
        }
    });
    String expected = "--- MODEL ---\n" + "MARKOV\n" + "5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5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5\n" + "--- EVIDENCE ---\n" + "1 2 1";
    assertEquals(expected, outputBuffer.toString());
}
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) Rational(com.sri.ai.util.math.Rational) ArrayList(java.util.ArrayList) ExpressionFactorsAndTypes(com.sri.ai.praise.sgsolver.solver.ExpressionFactorsAndTypes) HOGMParserWrapper(com.sri.ai.praise.model.v1.hogm.antlr.HOGMParserWrapper) HOGModelGrounding(com.sri.ai.praise.lang.grounded.model.HOGModelGrounding) Expression(com.sri.ai.expresso.api.Expression) StringJoiner(java.util.StringJoiner) Pair(com.sri.ai.util.base.Pair) Test(org.junit.Test)

Example 5 with ExpressionFactorsAndTypes

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

the class InferenceForFactorGraphAndEvidenceTest 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 isBayesianNetwork = true;
    // is a Bayesian network, that is, factors are normalized
    // and the sum of their product over all assignments to random variables is 1.
    boolean exploitFactorization = true;
    // exploit factorization (that is, employ Variable Elimination,
    // as opposed to summing over the entire joint probability distribution).
    InferenceForFactorGraphAndEvidence inferencer = new InferenceForFactorGraphAndEvidence(new ExpressionFactorsAndTypes(modelString), isBayesianNetwork, evidence, exploitFactorization, null);
    Expression queryExpression;
    Expression marginal;
    queryExpression = parse("not earthquake");
    // can be any boolean expression, or any random variable
    marginal = inferencer.solve(queryExpression);
    System.out.println("Marginal is " + marginal);
    queryExpression = parse("earthquake");
    marginal = inferencer.solve(queryExpression);
    System.out.println("Marginal is " + marginal);
}
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)

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