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