Search in sources :

Example 1 with ExpressionBasedModel

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);
}
Also used : HOGModel(com.sri.ai.praise.core.representation.classbased.hogm.HOGModel) HOGMExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMExpressionBasedModel) ExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel) Expression(com.sri.ai.expresso.api.Expression) ArrayList(java.util.ArrayList) HOGMExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMExpressionBasedModel) HOGMParserWrapper(com.sri.ai.praise.core.representation.classbased.hogm.parsing.HOGMParserWrapper)

Example 2 with ExpressionBasedModel

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;
}
Also used : Type(com.sri.ai.expresso.api.Type) HOGMExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMExpressionBasedModel) ExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel) HOGMExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMExpressionBasedModel)

Example 3 with ExpressionBasedModel

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;
}
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 4 with ExpressionBasedModel

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);
        }
    }
}
Also used : HOGMExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMExpressionBasedModel) ExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel) ArrayList(java.util.ArrayList) TranslatorOptions(com.sri.ai.praise.core.representation.translation.ciaranframework.api.TranslatorOptions) HOGMParserWrapper(com.sri.ai.praise.core.representation.classbased.hogm.parsing.HOGMParserWrapper) HOGModel(com.sri.ai.praise.core.representation.classbased.hogm.HOGModel) StringWriter(java.io.StringWriter) BufferedReader(java.io.BufferedReader) HOGMExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMExpressionBasedModel) FileReader(java.io.FileReader) File(java.io.File) PrintWriter(java.io.PrintWriter) UAI_to_HOGMv1_Using_Equalities_Translator(com.sri.ai.praise.core.representation.translation.ciaranframework.core.uai.UAI_to_HOGMv1_Using_Equalities_Translator)

Example 5 with ExpressionBasedModel

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;
}
Also used : DefaultExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.expressionbased.core.DefaultExpressionBasedModel) ExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel)

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