Search in sources :

Example 1 with Compilation

use of com.sri.ai.grinder.application.Compilation in project aic-praise by aic-sri-international.

the class UAIUtil method constructGenericTableExpressionUsingEqualities.

/**
 * Returns an {@link Expression} equivalent to a given {@link FunctionTable} but in the form of a decision tree
 * (so hopefully more compact) using equalities.
 * @param functionTable
 * @param solverListener if not null, invoked on solver used for compilation, before and after compilation is performed; returned solver from "before" invocation is used (it may be the same one used as argument, of course).
 * @return
 */
public static Expression constructGenericTableExpressionUsingEqualities(FunctionTable functionTable, Function<MultiQuantifierEliminator, MultiQuantifierEliminator> solverListener) {
    StringBuilder table = new StringBuilder();
    CartesianProductEnumeration<Integer> cartesianProduct = new CartesianProductEnumeration<>(cardinalityValues(functionTable));
    int counter = 0;
    while (cartesianProduct.hasMoreElements()) {
        counter++;
        List<Integer> values = cartesianProduct.nextElement();
        Double entryValue = functionTable.entryFor(values);
        if (counter == cartesianProduct.size().intValue()) {
            // i.e. final value
            table.append(entryValue);
        } else {
            table.append("if ");
            for (int i = 0; i < values.size(); i++) {
                if (i > 0) {
                    table.append(" and ");
                }
                String value = genericConstantValueForVariable(values.get(i), i, functionTable.cardinality(i));
                if (value.equals("true")) {
                    table.append(genericVariableName(i));
                } else if (value.equals("false")) {
                    table.append("not " + genericVariableName(i));
                } else {
                    table.append(genericVariableName(i));
                    table.append(" = ");
                    table.append(value);
                }
            }
            table.append(" then ");
            table.append(entryValue);
            table.append(" else ");
        }
    }
    Expression inputExpression = Expressions.parse(table.toString());
    Function<Integer, Integer> cardinalityOfIthVariable = i -> functionTable.cardinality(i);
    Map<String, String> mapFromCategoricalTypeNameToSizeString = new LinkedHashMap<>();
    Map<String, String> mapFromVariableNameToTypeName = new LinkedHashMap<>();
    Map<String, String> mapFromUniquelyNamedConstantToTypeName = new LinkedHashMap<>();
    for (int i = 0; i < functionTable.numberVariables(); i++) {
        String typeName = genericTypeNameForVariable(i, cardinalityOfIthVariable.apply(i));
        mapFromCategoricalTypeNameToSizeString.put(typeName, "" + cardinalityOfIthVariable.apply(i));
        mapFromVariableNameToTypeName.put(genericVariableName(i), typeName);
        for (int j = 0; j != functionTable.cardinality(i); j++) {
            String jThConstant = genericConstantValueForVariable(j, i, functionTable.cardinality(i));
            mapFromUniquelyNamedConstantToTypeName.put(jThConstant, typeName);
        }
    }
    com.sri.ai.grinder.api.Theory theory = new EqualityTheory(true, true);
    Expression result = Compilation.compile(inputExpression, theory, mapFromVariableNameToTypeName, mapFromUniquelyNamedConstantToTypeName, mapFromCategoricalTypeNameToSizeString, list(), solverListener);
    return result;
}
Also used : EqualityTheory(com.sri.ai.grinder.theory.equality.EqualityTheory) CartesianProductEnumeration(com.sri.ai.util.collect.CartesianProductEnumeration) Expressions(com.sri.ai.expresso.helper.Expressions) Compilation(com.sri.ai.grinder.application.Compilation) Util.list(com.sri.ai.util.Util.list) IOException(java.io.IOException) Expression(com.sri.ai.expresso.api.Expression) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Beta(com.google.common.annotations.Beta) LinkedHashMap(java.util.LinkedHashMap) List(java.util.List) TrueContext(com.sri.ai.grinder.core.TrueContext) Map(java.util.Map) Context(com.sri.ai.grinder.api.Context) SyntacticSubstitute(com.sri.ai.grinder.library.SyntacticSubstitute) BufferedReader(java.io.BufferedReader) FunctionTable(com.sri.ai.praise.core.representation.classbased.table.core.data.FunctionTable) MultiQuantifierEliminator(com.sri.ai.grinder.api.MultiQuantifierEliminator) EqualityTheory(com.sri.ai.grinder.theory.equality.EqualityTheory) CartesianProductEnumeration(com.sri.ai.util.collect.CartesianProductEnumeration) LinkedHashMap(java.util.LinkedHashMap) Expression(com.sri.ai.expresso.api.Expression)

Aggregations

Beta (com.google.common.annotations.Beta)1 Expression (com.sri.ai.expresso.api.Expression)1 Expressions (com.sri.ai.expresso.helper.Expressions)1 Context (com.sri.ai.grinder.api.Context)1 MultiQuantifierEliminator (com.sri.ai.grinder.api.MultiQuantifierEliminator)1 Compilation (com.sri.ai.grinder.application.Compilation)1 TrueContext (com.sri.ai.grinder.core.TrueContext)1 SyntacticSubstitute (com.sri.ai.grinder.library.SyntacticSubstitute)1 EqualityTheory (com.sri.ai.grinder.theory.equality.EqualityTheory)1 FunctionTable (com.sri.ai.praise.core.representation.classbased.table.core.data.FunctionTable)1 Util.list (com.sri.ai.util.Util.list)1 CartesianProductEnumeration (com.sri.ai.util.collect.CartesianProductEnumeration)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Function (java.util.function.Function)1