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;
}
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;
}
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;
}
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;
}
Aggregations