Search in sources :

Example 1 with ExpressionBasedSolver

use of com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.api.ExpressionBasedSolver 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 2 with ExpressionBasedSolver

use of com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.api.ExpressionBasedSolver in project aic-praise by aic-sri-international.

the class RegularSymbolicParameterEstimation method buildGradient.

private Vector<Expression> buildGradient(FeatureBasedModel featureBasedModel) {
    Vector<Expression> gradient = new Vector<Expression>();
    for (Expression condition : featureBasedModel.mapConditionToWeight.keySet()) {
        Expression parameter = featureBasedModel.mapConditionToWeight.get(condition);
        System.out.println("parameter : " + parameter);
        ExpressionBasedSolver solver = new ExactBPExpressionBasedSolver();
        Expression marginal = solver.solve(condition, expressionBasedModel);
        Expression probabilityOfCondition = convertExpression(marginal);
        Expression probabilityOfConditionWithSigmoidTrick = applySigmoidTrick(probabilityOfCondition, expressionBasedModel.getContext());
        System.out.println("probabilityOfConditionWithSigmoidTrick : " + probabilityOfCondition);
        int numberOfOccurences = countOccurencesGivenParameter(parameter);
        int numberOfEvidences = evidences.size();
        Expression secondTerm = apply(DIVISION, makeSymbol(numberOfOccurences), makeSymbol(numberOfEvidences));
        Expression ithTermOfgradient = apply(MINUS, probabilityOfConditionWithSigmoidTrick, secondTerm);
        System.out.println(ithTermOfgradient);
        gradient.add(ithTermOfgradient);
    }
    return gradient;
}
Also used : ExpressionBasedSolver(com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.api.ExpressionBasedSolver) ExactBPExpressionBasedSolver(com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.core.byalgorithm.exactbp.ExactBPExpressionBasedSolver) ExactBPExpressionBasedSolver(com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.core.byalgorithm.exactbp.ExactBPExpressionBasedSolver) Expression(com.sri.ai.expresso.api.Expression) Vector(java.util.Vector)

Example 3 with ExpressionBasedSolver

use of com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.api.ExpressionBasedSolver in project aic-praise by aic-sri-international.

the class ExpressionBasedSolverTest method runTest.

private void runTest(Expression queryExpression, ExpressionBasedModel model, Expression expected, boolean useFactorization) throws AssertionError {
    ExpressionBasedProblem problem = new DefaultExpressionBasedProblem(queryExpression, model);
    ExpressionBasedSolver[] solvers = new ExpressionBasedSolver[] { // new EvaluationExpressionBasedSolver(useFactorization),
    new ExactBPExpressionBasedSolver() };
    for (ExpressionBasedSolver solver : solvers) {
        Expression marginal = solver.solve(problem);
        checkResult(problem, expected, marginal, solver);
    }
}
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) ExactBPExpressionBasedSolver(com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.core.byalgorithm.exactbp.ExactBPExpressionBasedSolver) ExpressionBasedProblem(com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedProblem) DefaultExpressionBasedProblem(com.sri.ai.praise.core.representation.classbased.expressionbased.core.DefaultExpressionBasedProblem) Expression(com.sri.ai.expresso.api.Expression) DefaultExpressionBasedProblem(com.sri.ai.praise.core.representation.classbased.expressionbased.core.DefaultExpressionBasedProblem)

Example 4 with ExpressionBasedSolver

use of com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.api.ExpressionBasedSolver in project aic-praise by aic-sri-international.

the class ParameterEstimationForExpressionBasedModel method getMarginalBeforeTransformation.

/**
 * Return the "raw" marginal, before log and sigmoid transformation.
 */
private Expression getMarginalBeforeTransformation(Entry<Pair<Expression, Expression>, Integer> entry) {
    Pair<Expression, Expression> pairEvidenceQuery = entry.getKey();
    ExpressionBasedSolver solver = new ExactBPExpressionBasedSolver();
    Expression query = pairEvidenceQuery.first;
    Expression evidence = pairEvidenceQuery.second;
    ExpressionBasedModel conditionedModel = model.getConditionedModel(evidence);
    Expression marginal = solver.solve(query, conditionedModel);
    return marginal;
}
Also used : ExpressionBasedSolver(com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.api.ExpressionBasedSolver) ExactBPExpressionBasedSolver(com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.core.byalgorithm.exactbp.ExactBPExpressionBasedSolver) ExactBPExpressionBasedSolver(com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.core.byalgorithm.exactbp.ExactBPExpressionBasedSolver) Expression(com.sri.ai.expresso.api.Expression) ExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel)

Example 5 with ExpressionBasedSolver

use of com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.api.ExpressionBasedSolver in project aic-praise by aic-sri-international.

the class GradientLoglikelihoodToOptimize method buildGradient.

/**
 * Build the gradient following the formula described in "Graphical Models in a Nutshell".
 */
private Vector<Expression> buildGradient(Map<Expression, Double> mapParametersToValue) {
    Vector<Expression> gradient = new Vector<Expression>();
    for (Expression condition : featureBasedModel.mapConditionToWeight.keySet()) {
        Expression parameter = featureBasedModel.mapConditionToWeight.get(condition);
        ExpressionBasedSolver solver = new ExactBPExpressionBasedSolver();
        ExpressionBasedModel newExpressionBasedModel = UsefulOperationsParameterEstimation.buildOptimizedExpressionBasedModel(mapParametersToValue, expressionBasedModel);
        Expression marginal = solver.solve(condition, newExpressionBasedModel);
        // System.out.println("marginal : " + marginal);
        Expression probabilityOfConditionWithSigmoidTrick = applySigmoid(marginal, expressionBasedModel.getContext());
        // System.out.println("probabilityOfConditionWithSigmoidTrick : " + probabilityOfConditionWithSigmoidTrick);
        probabilityOfConditionWithSigmoidTrick = convertExpression(probabilityOfConditionWithSigmoidTrick);
        // System.out.println("probabilityOfConditionWithSigmoidTrick after conversion : " + probabilityOfConditionWithSigmoidTrick);
        int numberOfOccurences = countOccurencesGivenParameter(parameter);
        int numberOfEvidences = evidences.size();
        Expression secondTerm = apply(DIVISION, makeSymbol(numberOfOccurences), makeSymbol(numberOfEvidences));
        Expression ithTermOfgradient = apply(MINUS, probabilityOfConditionWithSigmoidTrick, secondTerm);
        gradient.add(ithTermOfgradient);
    }
    return gradient;
}
Also used : ExpressionBasedSolver(com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.api.ExpressionBasedSolver) ExactBPExpressionBasedSolver(com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.core.byalgorithm.exactbp.ExactBPExpressionBasedSolver) ExactBPExpressionBasedSolver(com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.core.byalgorithm.exactbp.ExactBPExpressionBasedSolver) Expression(com.sri.ai.expresso.api.Expression) ExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel) Vector(java.util.Vector)

Aggregations

Expression (com.sri.ai.expresso.api.Expression)5 ExpressionBasedSolver (com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.api.ExpressionBasedSolver)5 ExactBPExpressionBasedSolver (com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.core.byalgorithm.exactbp.ExactBPExpressionBasedSolver)5 EvaluationExpressionBasedSolver (com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.core.byalgorithm.evaluation.EvaluationExpressionBasedSolver)2 ExpressionBasedModel (com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel)2 DefaultExpressionBasedProblem (com.sri.ai.praise.core.representation.classbased.expressionbased.core.DefaultExpressionBasedProblem)2 Vector (java.util.Vector)2 ExpressionBasedProblem (com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedProblem)1 HOGMExpressionBasedModel (com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMExpressionBasedModel)1 Test (org.junit.Test)1