Search in sources :

Example 26 with 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 buildModel2.

public static ExpressionBasedModel buildModel2() {
    String modelString = "random terrorAttacks : 0..20;\n" + "random newJobs : 0..100000;\n" + "random dow: 11000..18000;\n" + "random economyIsPoor : Boolean;\n" + "random economyIsGreat : Boolean;\n" + "random attackPerception: Boolean;\n" + "random likeIncumbent  : 0..100000000;\n" + "random likeChallenger : 0..100000000;\n" + "constant Alpha: Real;\n" + "economyIsPoor <=> dow < 13000 and newJobs < 30000;\n" + "economyIsGreat <=> dow > 16000 and newJobs > 70000;\n" + "attackPerception <=> terrorAttacks > 4;\n" + "if economyIsGreat\n" + "then if likeIncumbent > 70000000 then Alpha/30000000 else 1-Alpha/(70000000 + 1)\n" + "else if economyIsPoor\n" + "then if likeIncumbent < 40000000 then 0.8/40000000 else 0.2/(60000000 + 1)\n" + "else if attackPerception\n" + "then if likeIncumbent < 60000000 then 0.9/60000000 else 0.1/(40000000 + 1);\n";
    List<HOGMProblemError> modelErrors = new ArrayList<>();
    HOGModel hogmModel = parseModelStringToHOGMModel(modelString, modelErrors);
    ExpressionBasedModel expressionBasedModel = parseHOGModelToExpressionBasedModel(hogmModel);
    return expressionBasedModel;
}
Also used : HOGMProblemError(com.sri.ai.praise.core.inference.byinputrepresentation.classbased.hogm.parsing.HOGMProblemError) HOGModel(com.sri.ai.praise.core.representation.classbased.hogm.HOGModel) 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) ArrayList(java.util.ArrayList)

Example 27 with 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 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 28 with ExpressionBasedModel

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

the class HOGModelGrounding method ground.

public static void ground(ExpressionBasedModel factorsAndTypes, List<Expression> evidence, Listener listener) {
    if (factorsAndTypes.getMapFromNonUniquelyNamedConstantNameToTypeName().size() > 0) {
        throw new IllegalArgumentException("Constants cannot be grounded");
    }
    Map<Expression, Triple<Expression, Integer, List<Expression>>> randomVariableNameToTypeSizeAndUniqueConstants = createRandomVariableNameToTypeSizeAndUniqueConstantsMap(factorsAndTypes);
    Map<Expression, Integer> randomVariableIndexes = new LinkedHashMap<>();
    AtomicInteger atomicVariableIndex = new AtomicInteger(-1);
    listener.numberGroundVariables(randomVariableNameToTypeSizeAndUniqueConstants.size());
    randomVariableNameToTypeSizeAndUniqueConstants.entrySet().forEach(entry -> {
        randomVariableIndexes.put(entry.getKey(), atomicVariableIndex.addAndGet(1));
        listener.groundVariableCardinality(atomicVariableIndex.get(), entry.getValue().second);
    });
    Map<Expression, List<Expression>> typeToValues = createTypeToValuesMap(factorsAndTypes, randomVariableNameToTypeSizeAndUniqueConstants);
    Map<String, String> newUniqueConstantToTypeMap = createGroundedUniqueConstantToTypeMap(typeToValues);
    ExpressionBasedModel groundedExpressionBasedModel = makeGroundedExpressionBasedModel(factorsAndTypes, newUniqueConstantToTypeMap);
    Context context = groundedExpressionBasedModel.getContext();
    listener.numberFactors(factorsAndTypes.getFactors().size());
    int factorIndex = 0;
    for (Expression factor : factorsAndTypes.getFactors()) {
        ArrayList<Expression> randomVariablesInFactor = new ArrayList<>(Expressions.getSubExpressionsSatisfying(factor, randomVariableNameToTypeSizeAndUniqueConstants::containsKey));
        if (randomVariablesInFactor.size() == 0) {
            throw new IllegalArgumentException("ExpressionFactorNode contains no random variables: " + factor);
        }
        int[] participantVariableIndexes = new int[randomVariablesInFactor.size()];
        for (int i = 0; i < randomVariablesInFactor.size(); i++) {
            Expression randomVariable = randomVariablesInFactor.get(i);
            participantVariableIndexes[i] = randomVariableIndexes.get(randomVariable);
        }
        listener.factorParticipants(factorIndex, participantVariableIndexes);
        if (!useContextSensitiveGrounding) {
            fullGrounding(factor, randomVariablesInFactor, listener, randomVariableNameToTypeSizeAndUniqueConstants, typeToValues, context);
        } else {
            contextSensitiveGrounding(factor, randomVariablesInFactor, listener, randomVariableNameToTypeSizeAndUniqueConstants, typeToValues, context);
        }
        factorIndex++;
    }
    // Handle the evidence
    for (Expression evidenceAssignment : evidence) {
        if (Expressions.isFunctionApplicationWithArguments(evidenceAssignment)) {
            // TODO - add support for 'not <variable>' and 'variable = value' and 'value = variable'
            throw new UnsupportedOperationException("Function application of evidence currently not supported: " + evidenceAssignment);
        } else if (Expressions.isSymbol(evidenceAssignment)) {
            int evidenceVariableIndex = randomVariableIndexes.get(evidenceAssignment);
            int evidenceValueIndex = typeToValues.get(randomVariableNameToTypeSizeAndUniqueConstants.get(evidenceAssignment).first).indexOf(Expressions.TRUE);
            listener.evidence(evidenceVariableIndex, evidenceValueIndex);
        }
    }
    listener.groundingComplete();
}
Also used : Context(com.sri.ai.grinder.api.Context) 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) LinkedHashMap(java.util.LinkedHashMap) Triple(com.sri.ai.util.base.Triple) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BigInteger(java.math.BigInteger) Expression(com.sri.ai.expresso.api.Expression) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) List(java.util.List)

Example 29 with ExpressionBasedModel

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

the class HOGModelGrounding method makeGroundedExpressionBasedModel.

private static ExpressionBasedModel makeGroundedExpressionBasedModel(ExpressionBasedModel factorsAndTypes, Map<String, String> newUniqueConstantToTypeMap) {
    boolean isBayesianNetwork = false;
    ExpressionBasedModel groundedFactorsAndTypesInformation = new HOGMExpressionBasedModel(// factors
    Collections.emptyList(), factorsAndTypes.getMapFromRandomVariableNameToTypeName(), factorsAndTypes.getMapFromNonUniquelyNamedConstantNameToTypeName(), newUniqueConstantToTypeMap, factorsAndTypes.getMapFromCategoricalTypeNameToSizeString(), // additional types
    list(), isBayesianNetwork);
    return groundedFactorsAndTypesInformation;
}
Also used : 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 30 with ExpressionBasedModel

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

the class HOGModelGrounding method createRandomVariableNameToTypeSizeAndUniqueConstantsMap.

// 
// PRIVATE
// 
private static Map<Expression, Triple<Expression, Integer, List<Expression>>> createRandomVariableNameToTypeSizeAndUniqueConstantsMap(ExpressionBasedModel factorsAndTypes) {
    Map<Expression, Triple<Expression, Integer, List<Expression>>> result = new LinkedHashMap<>();
    factorsAndTypes.getMapFromRandomVariableNameToTypeName().entrySet().forEach(entry -> {
        Expression randomVariableName = Expressions.parse(entry.getKey());
        Expression type = Expressions.parse(entry.getValue());
        int size = 0;
        List<Expression> uniqueConstants = new ArrayList<>();
        if (Expressions.hasFunctor(type, FunctorConstants.FUNCTION_TYPE)) {
            throw new UnsupportedOperationException("Relational random variables, " + randomVariableName + ", are currently not supported.");
        } else if (Expressions.hasFunctor(type, HOGMSortDeclaration.IN_BUILT_INTEGER.getName()) && type.numberOfArguments() == 2) {
            size = (type.get(1).intValueExact() - type.get(0).intValueExact()) + 1;
        } else if (type.hasFunctor(FunctorConstants.INTEGER_INTERVAL) && type.numberOfArguments() == 2) {
            size = (type.get(1).intValueExact() - type.get(0).intValueExact()) + 1;
        } else {
            String sizeString = factorsAndTypes.getMapFromCategoricalTypeNameToSizeString().get(type.toString());
            if (sizeString == null) {
                throw new IllegalArgumentException("Size of sort " + type + " is unknown");
            }
            size = Integer.parseInt(sizeString);
            factorsAndTypes.getMapFromUniquelyNamedConstantNameToTypeName().entrySet().stream().filter(uniqueConstantAndTypeEntry -> uniqueConstantAndTypeEntry.getValue().equals(entry.getValue())).forEach(uniqueConstantAndTypeEntry -> uniqueConstants.add(Expressions.parse(uniqueConstantAndTypeEntry.getKey())));
        }
        result.put(randomVariableName, new Triple<>(type, size, uniqueConstants));
    });
    return result;
}
Also used : Triple(com.sri.ai.util.base.Triple) IntStream(java.util.stream.IntStream) HOGMExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMExpressionBasedModel) BinaryFunction(com.sri.ai.util.base.BinaryFunction) Triple(com.sri.ai.util.base.Triple) Expressions(com.sri.ai.expresso.helper.Expressions) Rational(com.sri.ai.util.math.Rational) Expression(com.sri.ai.expresso.api.Expression) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) HOGMSortDeclaration(com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMSortDeclaration) Symbol(com.sri.ai.expresso.api.Symbol) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Context(com.sri.ai.grinder.api.Context) BigInteger(java.math.BigInteger) Function(com.google.common.base.Function) TernaryProcedure(com.sri.ai.util.base.TernaryProcedure) MixedRadixNumber(com.sri.ai.util.math.MixedRadixNumber) Util.list(com.sri.ai.util.Util.list) Beta(com.google.common.annotations.Beta) List(java.util.List) Theory(com.sri.ai.grinder.api.Theory) Expressions.makeSymbol(com.sri.ai.expresso.helper.Expressions.makeSymbol) Util.myAssert(com.sri.ai.util.Util.myAssert) Expressions.isNumber(com.sri.ai.expresso.helper.Expressions.isNumber) FunctorConstants(com.sri.ai.grinder.library.FunctorConstants) Collections(java.util.Collections) ExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel) Expression(com.sri.ai.expresso.api.Expression) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap)

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