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