use of com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel in project aic-praise by aic-sri-international.
the class AbstractHOGMv1_to_Target_Translator method translate.
// END-Translator
//
@Override
protected void translate(String inputIdentifier, Reader[] inputModelReaders, PrintWriter[] translatedOutputs) throws Exception {
//
// 1. Get the HOGM FactorNetwork Definition and Parse It
String hogmv1Model = Util.readAll(inputModelReaders[0]);
HOGMParserWrapper parser = new HOGMParserWrapper();
HOGModel parsedModel = parser.parseModel(hogmv1Model);
ExpressionBasedModel factorsAndTypes = new HOGMExpressionBasedModel(parsedModel);
// Each additional input is treated as an evidence expression
List<Expression> evidence = new ArrayList<>();
if (inputModelReaders.length > 1) {
for (int i = 1; i < inputModelReaders.length; i++) {
evidence.add(Expressions.parse(Util.readAll(inputModelReaders[i])));
}
}
translate(inputIdentifier, factorsAndTypes, evidence, translatedOutputs);
}
use of com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel in project aic-praise by aic-sri-international.
the class Evaluation method getDomainSizes.
private String getDomainSizes(String model) {
ExpressionBasedModel factorsAndTypes = new HOGMExpressionBasedModel(model);
Collection<Type> types = factorsAndTypes.getAdditionalTypes();
List<Integer> domainSizes = mapIntoList(types, t -> t.cardinality().intValueExact());
String result = Util.join(domainSizes);
return result;
}
use of com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel 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.api.ExpressionBasedModel in project aic-praise by aic-sri-international.
the class PRAiSEController method importUAIModel.
private void importUAIModel(ActionEvent ae) {
File uaiModelFile = uaiFileChooser.showOpenDialog(mainStage);
if (uaiModelFile != null) {
File uaiEvidenceFile = new File(uaiModelFile.getParent(), uaiModelFile.getName() + ".evid");
StringWriter hogmWriter = new StringWriter();
try (BufferedReader uaiModelReader = new BufferedReader(new FileReader(uaiModelFile));
BufferedReader uaiEvidenceReader = new BufferedReader(new FileReader(uaiEvidenceFile));
PrintWriter hogmPrintWriter = new PrintWriter(hogmWriter)) {
UAI_to_HOGMv1_Using_Equalities_Translator translator = new UAI_to_HOGMv1_Using_Equalities_Translator();
translator.translate(uaiModelFile.getName(), new Reader[] { uaiModelReader, uaiEvidenceReader }, new PrintWriter[] { hogmPrintWriter }, new TranslatorOptions());
String hogmModel = hogmWriter.toString();
// For convenience, pull out all possible queries
HOGMParserWrapper parser = new HOGMParserWrapper();
HOGModel parsedModel = parser.parseModel(hogmModel);
ExpressionBasedModel factorsAndTypes = new HOGMExpressionBasedModel(parsedModel);
List<String> queries = new ArrayList<>(factorsAndTypes.getMapFromRandomVariableNameToTypeName().keySet());
newModel(hogmModel, queries);
} catch (Throwable th) {
FXUtil.exception(th);
}
}
}
use of com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel in project aic-praise by aic-sri-international.
the class ExpressionBayesianModel method learnModelParametersFromCompleteDataAndConvertToAnExpressionBasedModel.
/**
* Learn the parameters of the model and convert the learned ExpressionBayesianModel into an ExpressionBasedModel, ready for inferences.
*
* @param dataset
* @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 final learned and already converted model
*/
public ExpressionBasedModel learnModelParametersFromCompleteDataAndConvertToAnExpressionBasedModel(Dataset dataset, Map<String, String> mapFromCategoricalTypeNameToSizeString, Collection<Type> additionalTypes) {
ExpressionBayesianModel learnedModel = (ExpressionBayesianModel) this.learnModelParametersFromCompleteData(dataset);
ExpressionBasedModel convertedModel = learnedModel.convertToAnExpressionBasedModelAfterLearning(mapFromCategoricalTypeNameToSizeString, additionalTypes);
return convertedModel;
}
Aggregations