Search in sources :

Example 6 with ExpressionBasedModel

use of com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel in project aic-praise by aic-sri-international.

the class ParameterEstimationForHOGModel method optimizeWhenModelIsHOGModel.

/**
 * Main method to optimize the parameters of the model given the queries and evidences when the model is HOGModel based.
 */
public HashMap<Expression, Double> optimizeWhenModelIsHOGModel(HOGModel hogmModel, GoalType goalType, double[] startPoint) {
    ExpressionBasedModel expressionBasedModel = UsefulOperationsParameterEstimation.parseHOGModelToExpressionBasedModel(hogmModel);
    ParameterEstimationForExpressionBasedModel parameterEstimationForExpressionBasedModel = new ParameterEstimationForExpressionBasedModel(expressionBasedModel, pairsQueryEvidence);
    System.out.println("expression based model after parsing : " + expressionBasedModel);
    HashMap<Expression, Double> result = parameterEstimationForExpressionBasedModel.optimize(startPoint);
    return result;
}
Also used : ExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel) Expression(com.sri.ai.expresso.api.Expression)

Example 7 with ExpressionBasedModel

use of com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel in project aic-praise by aic-sri-international.

the class ExpressionBasedModelExamples method buildModel5.

public static ExpressionBasedModel buildModel5() {
    String modelString = "random earthquake: Boolean;\n" + "random burglary: Boolean;\n" + "random alarm: Boolean;\n" + "constant Alpha: Real;\n" + "constant Beta: Real;\n" + "earthquake 0.01;\n" + "burglary 0.1;\n" + "if earthquake\n" + "then if burglary\n" + "then alarm Alpha\n" + "else alarm Beta\n" + "else if burglary\n" + "then alarm 0.9\n" + "else alarm 0.01;\n";
    List<HOGMProblemError> modelErrors = new ArrayList<>();
    HOGModel hogmModel = parseModelStringToHOGMModel(modelString, modelErrors);
    ExpressionBasedModel expressionBasedModel = parseHOGModelToExpressionBasedModel(hogmModel);
    return expressionBasedModel;
}
Also used : HOGMProblemError(com.sri.ai.praise.core.inference.byinputrepresentation.classbased.hogm.parsing.HOGMProblemError) HOGModel(com.sri.ai.praise.core.representation.classbased.hogm.HOGModel) DefaultExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.expressionbased.core.DefaultExpressionBasedModel) ExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel) UsefulOperationsParameterEstimation.parseHOGModelToExpressionBasedModel(com.sri.ai.praise.learning.symbolicparameterestimation.util.UsefulOperationsParameterEstimation.parseHOGModelToExpressionBasedModel) ArrayList(java.util.ArrayList)

Example 8 with ExpressionBasedModel

use of com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel in project aic-praise by aic-sri-international.

the class ExpressionBasedModelExamples method buildModel1.

public static ExpressionBasedModel buildModel1() {
    // The definitions of types
    Map<String, String> mapFromCategoricalTypeNameToSizeString = map();
    // The definitions of variables
    Map<String, String> mapFromRandomVariableNameToTypeName = map("earthquake", "Boolean", "burglary", "Boolean", "alarm", "Boolean");
    // The definitions of non-uniquely named constants
    Map<String, String> mapFromNonUniquelyNamedConstantNameToTypeName = map("Alpha", "Real", "Beta", "Real");
    // The definitions of non-uniquely named constants
    Map<String, String> mapFromUniquelyNamedConstantNameToTypeName = map();
    // a variant of the earthquake/burglary model in which some burglars are more active than others.
    boolean isBayesianNetwork = true;
    List<Expression> factors = getMultiplicands(parse("" + "(if earthquake then Alpha else 1-Alpha) * " + "(if burglary then Beta else 1-Beta) * " + "(if burglary or earthquake " + "then if alarm then 0.9 else 0.1 " + "else if alarm then 0.05 else 0.95) " + ""));
    ExpressionBasedModel expressionBasedModel = new DefaultExpressionBasedModel(factors, mapFromRandomVariableNameToTypeName, mapFromNonUniquelyNamedConstantNameToTypeName, mapFromUniquelyNamedConstantNameToTypeName, mapFromCategoricalTypeNameToSizeString, list(), isBayesianNetwork);
    return expressionBasedModel;
}
Also used : DefaultExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.expressionbased.core.DefaultExpressionBasedModel) Expression(com.sri.ai.expresso.api.Expression) DefaultExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.expressionbased.core.DefaultExpressionBasedModel) ExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel) UsefulOperationsParameterEstimation.parseHOGModelToExpressionBasedModel(com.sri.ai.praise.learning.symbolicparameterestimation.util.UsefulOperationsParameterEstimation.parseHOGModelToExpressionBasedModel)

Example 9 with ExpressionBasedModel

use of com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel in project aic-praise by aic-sri-international.

the class ExpressionBasedModelExamples method buildModel3.

public static ExpressionBasedModel buildModel3() {
    String modelString = "random earthquake: Boolean;\n" + "random burglary: Boolean;\n" + "random alarm: Boolean;\n" + "constant Alpha: Real;\n" + "constant Beta: Real;\n" + "earthquake Alpha;\n" + "burglary Beta;\n" + "if earthquake\n" + "then if burglary\n" + "then alarm 0.95\n" + "else alarm 0.6\n" + "else if burglary\n" + "then alarm 0.9\n" + "else alarm 0.01;\n";
    List<HOGMProblemError> modelErrors = new ArrayList<>();
    HOGModel hogmModel = parseModelStringToHOGMModel(modelString, modelErrors);
    ExpressionBasedModel expressionBasedModel = parseHOGModelToExpressionBasedModel(hogmModel);
    return expressionBasedModel;
}
Also used : HOGMProblemError(com.sri.ai.praise.core.inference.byinputrepresentation.classbased.hogm.parsing.HOGMProblemError) HOGModel(com.sri.ai.praise.core.representation.classbased.hogm.HOGModel) DefaultExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.expressionbased.core.DefaultExpressionBasedModel) ExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel) UsefulOperationsParameterEstimation.parseHOGModelToExpressionBasedModel(com.sri.ai.praise.learning.symbolicparameterestimation.util.UsefulOperationsParameterEstimation.parseHOGModelToExpressionBasedModel) ArrayList(java.util.ArrayList)

Example 10 with ExpressionBasedModel

use of com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel in project aic-praise by aic-sri-international.

the class ExpressionBasedModelToFeatureBasedModelTranslation method translateExpressionBasedModelToFeatureBasedModel.

/**
 * Build the FeatureBasedModel (not yet optimized) from the ExpressionBasedModel and a list of parameters.
 */
public static FeatureBasedModel translateExpressionBasedModelToFeatureBasedModel(ExpressionBasedModel expressionBasedModel) {
    List<Expression> parameters = UsefulOperationsParameterEstimation.findParameters(expressionBasedModel);
    Map<Expression, Expression> map = new HashMap<Expression, Expression>();
    Context context = expressionBasedModel.getContext();
    for (Expression factor : expressionBasedModel.getFactors()) {
        for (Expression parameter : parameters) {
            IndexExpressionsSet indexExpressionsSet = getIndexExpressionsOfFreeVariablesIn(parameter, context);
            Expression F1 = new DefaultUniversallyQuantifiedFormula(indexExpressionsSet, Equality.make(factor, parameter));
            Expression condition = context.evaluate(F1);
            System.out.println("condition : " + condition);
            if (!condition.getValue().equals(true) && !condition.getValue().equals(false)) {
                map.put(condition, parameter);
            }
        }
    }
    Map<Expression, Expression> sortedMap = map.entrySet().stream().sorted(Entry.comparingByValue()).collect(Collectors.toMap(Entry::getKey, Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
    FeatureBasedModel result = new FeatureBasedModel(sortedMap);
    return result;
}
Also used : Context(com.sri.ai.grinder.api.Context) UsefulOperationsParameterEstimation(com.sri.ai.praise.learning.symbolicparameterestimation.util.UsefulOperationsParameterEstimation) HashMap(java.util.HashMap) Expression(com.sri.ai.expresso.api.Expression) DefaultUniversallyQuantifiedFormula(com.sri.ai.expresso.core.DefaultUniversallyQuantifiedFormula) Collectors(java.util.stream.Collectors) LinkedHashMap(java.util.LinkedHashMap) List(java.util.List) Equality(com.sri.ai.grinder.library.Equality) GrinderUtil.getIndexExpressionsOfFreeVariablesIn(com.sri.ai.grinder.helper.GrinderUtil.getIndexExpressionsOfFreeVariablesIn) Map(java.util.Map) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet) Entry(java.util.Map.Entry) Context(com.sri.ai.grinder.api.Context) ExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel) DefaultUniversallyQuantifiedFormula(com.sri.ai.expresso.core.DefaultUniversallyQuantifiedFormula) Expression(com.sri.ai.expresso.api.Expression) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet)

Aggregations

ExpressionBasedModel (com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel)33 Expression (com.sri.ai.expresso.api.Expression)24 LinkedList (java.util.LinkedList)11 ArrayList (java.util.ArrayList)10 DefaultExpressionBasedModel (com.sri.ai.praise.core.representation.classbased.expressionbased.core.DefaultExpressionBasedModel)8 HOGModel (com.sri.ai.praise.core.representation.classbased.hogm.HOGModel)8 HOGMExpressionBasedModel (com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMExpressionBasedModel)8 Pair (com.sri.ai.util.base.Pair)8 ParameterEstimationForExpressionBasedModel (com.sri.ai.praise.learning.symbolicparameterestimation.ParameterEstimationForExpressionBasedModel)7 UsefulOperationsParameterEstimation.buildOptimizedExpressionBasedModel (com.sri.ai.praise.learning.symbolicparameterestimation.util.UsefulOperationsParameterEstimation.buildOptimizedExpressionBasedModel)7 UsefulOperationsParameterEstimation.parseHOGModelToExpressionBasedModel (com.sri.ai.praise.learning.symbolicparameterestimation.util.UsefulOperationsParameterEstimation.parseHOGModelToExpressionBasedModel)7 HashMap (java.util.HashMap)7 Test (org.junit.Test)6 Context (com.sri.ai.grinder.api.Context)5 HOGMProblemError (com.sri.ai.praise.core.inference.byinputrepresentation.classbased.hogm.parsing.HOGMProblemError)5 Type (com.sri.ai.expresso.api.Type)3 HOGMParserWrapper (com.sri.ai.praise.core.representation.classbased.hogm.parsing.HOGMParserWrapper)3 LinkedHashMap (java.util.LinkedHashMap)3 List (java.util.List)3 ExpressionBasedSolver (com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.api.ExpressionBasedSolver)2