Search in sources :

Example 1 with DefaultExpressionBasedModel

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

the class UsefulOperationsParameterEstimation method buildOptimizedExpressionBasedModel.

/**
 * Return an ExpressionBasedModel with the parameters replaced with the
 * optimized values.
 */
public static ExpressionBasedModel buildOptimizedExpressionBasedModel(Map<Expression, Double> optimizedParameters, ExpressionBasedModel model) {
    List<Expression> factors = buildNewFactors(model);
    Map<String, String> mapFromRandomVariableNameToTypeName = buildNewMapFromRandomVariableNameToTypeName(model);
    Map<String, String> mapFromNonUniquelyNamedConstantNameToTypeName = buildNewMapFromNonUniquelyNamedConstantNameToTypeName(model);
    Map<String, String> mapFromUniquelyNamedConstantNameToTypeName = model.getMapFromUniquelyNamedConstantNameToTypeName();
    Map<String, String> mapFromCategoricalTypeNameToSizeString = model.getMapFromCategoricalTypeNameToSizeString();
    Collection<Type> additionalTypes = model.getAdditionalTypes();
    boolean isKnownToBeBayesianNetwork = model.isKnownToBeBayesianNetwork();
    for (Expression parameter : optimizedParameters.keySet()) {
        Double value = optimizedParameters.get(parameter);
        int k = 0;
        for (Expression factor : factors) {
            String factorString = factor.toString();
            if (factorString.contains(parameter.toString())) {
                Double oneMinusValue = 1.0 - value;
                factorString = factorString.replaceAll("\\b1 - " + parameter.toString() + "\\b", oneMinusValue.toString());
                factorString = factorString.replaceAll("\\b" + parameter.toString() + "\\b", value.toString());
                Expression newFactor = parse(factorString);
                factors.set(k, newFactor);
            }
            k++;
        }
        mapFromNonUniquelyNamedConstantNameToTypeName.remove(parameter.toString());
    }
    ExpressionBasedModel result = new DefaultExpressionBasedModel(factors, mapFromRandomVariableNameToTypeName, mapFromNonUniquelyNamedConstantNameToTypeName, mapFromUniquelyNamedConstantNameToTypeName, mapFromCategoricalTypeNameToSizeString, additionalTypes, isKnownToBeBayesianNetwork);
    return result;
}
Also used : Type(com.sri.ai.expresso.api.Type) DefaultExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.expressionbased.core.DefaultExpressionBasedModel) Expression(com.sri.ai.expresso.api.Expression) HOGMExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMExpressionBasedModel) DefaultExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.expressionbased.core.DefaultExpressionBasedModel) ExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel)

Example 2 with DefaultExpressionBasedModel

use of com.sri.ai.praise.core.representation.classbased.expressionbased.core.DefaultExpressionBasedModel 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 3 with DefaultExpressionBasedModel

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

the class ExpressionBasedModelExamples method buildModel4.

public static ExpressionBasedModel buildModel4() {
    Map<String, String> mapFromCategoricalTypeNameToSizeString = map("Folks", "10", "Boolean", "2");
    Map<String, String> mapFromRandomVariableNameToTypeName = map("earthquake", "Boolean", // a multi-value random variable
    "burglar", // a multi-value random variable
    "Folks", "alarm", "Boolean");
    Map<String, String> mapFromNonUniquelyNamedConstantNameToTypeName = map("seismicLocation", "Boolean");
    Map<String, String> mapFromUniquelyNamedConstantNameToTypeName = map("none", "Folks", "tom", "Folks", "Alpha", "Real");
    boolean isBayesianNetwork = true;
    List<Expression> factors = getMultiplicands(parse("" + "(if earthquake then Alpha else 1-Alpha) * " + "(if burglar = none then 0.7 else if burglar = tom then 0.1 else 0.2 / (|Folks| - 2)) * " + // note the division above of the potential by number of remaining values, as the probabilities must sum up to 1
    "(if burglar != none 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 4 with DefaultExpressionBasedModel

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

the class ExpressionBayesianModel method convertToAnExpressionBasedModelAfterLearning.

/**
 * Converting the learned ExpressionBayesianModel into an ExpressionBasedModel, ready for inferences.
 * Some assumptions: there are no constants in the initial expressions of the nodes (would result in errors when learning the parameters)
 *
 * @param mapFromCategoricalTypeNameToSizeString (the user specifies the categorical types used, usually an empty map)
 * @param additionalTypes (the user specifies the additional types used, usually an empty list)
 *
 * @return the equivalent ExpressionBasedModel for inference
 */
public ExpressionBasedModel convertToAnExpressionBasedModelAfterLearning(Map<String, String> mapFromCategoricalTypeNameToSizeString, Collection<Type> additionalTypes) {
    // The nodes of this Bayesian model
    List<? extends Expression> factors = this.getNodes();
    // The definitions of variables
    Map<String, String> mapFromRandomVariableNameToTypeName = map();
    for (ExpressionBayesianNode node : this.getNodes()) {
        ExpressionVariable nodeVariable = node.getChildVariable();
        Context context = node.getContext();
        Type type = context.getTypeOfRegisteredSymbol(nodeVariable);
        mapFromRandomVariableNameToTypeName.put(nodeVariable.toString(), type.toString());
    }
    // The definitions of non-uniquely named constants
    // (we assume that there are no constants in the initial expressions of the nodes - would result in errors when learning)
    Map<String, String> mapFromNonUniquelyNamedConstantNameToTypeName = map();
    // The definitions of uniquely named constants
    // (similar comment as above)
    Map<String, String> mapFromUniquelyNamedConstantNameToTypeName = map();
    // ExpressionBayesianModels are known to be Bayesian models
    boolean isBayesianNetwork = true;
    ExpressionBasedModel convertedModel = new DefaultExpressionBasedModel(factors, mapFromRandomVariableNameToTypeName, mapFromNonUniquelyNamedConstantNameToTypeName, mapFromUniquelyNamedConstantNameToTypeName, mapFromCategoricalTypeNameToSizeString, additionalTypes, isBayesianNetwork);
    return convertedModel;
}
Also used : Context(com.sri.ai.grinder.api.Context) Type(com.sri.ai.expresso.api.Type) DefaultExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.expressionbased.core.DefaultExpressionBasedModel) DefaultExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.expressionbased.core.DefaultExpressionBasedModel) ExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel) ExpressionVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionVariable)

Aggregations

ExpressionBasedModel (com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel)4 DefaultExpressionBasedModel (com.sri.ai.praise.core.representation.classbased.expressionbased.core.DefaultExpressionBasedModel)4 Expression (com.sri.ai.expresso.api.Expression)3 Type (com.sri.ai.expresso.api.Type)2 UsefulOperationsParameterEstimation.parseHOGModelToExpressionBasedModel (com.sri.ai.praise.learning.symbolicparameterestimation.util.UsefulOperationsParameterEstimation.parseHOGModelToExpressionBasedModel)2 Context (com.sri.ai.grinder.api.Context)1 HOGMExpressionBasedModel (com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMExpressionBasedModel)1 ExpressionVariable (com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionVariable)1