use of com.sri.ai.grinder.sgdpllt.api.MultiIndexQuantifierEliminator in project aic-expresso by aic-sri-international.
the class BruteForceFunctionTheory method getSingleVariableConstraintQuantifierEliminatorStepSolver.
@Override
public ExpressionLiteralSplitterStepSolver getSingleVariableConstraintQuantifierEliminatorStepSolver(AssociativeCommutativeGroup group, SingleVariableConstraint constraint, Expression body, Context context) {
Expression variable = constraint.getVariable();
Expression type = GrinderUtil.getTypeExpression(variable, context);
Expression indexExpression = IndexExpressions.makeIndexExpression(variable, type);
ExtensionalIndexExpressionsSet indexExpressionsSet = new ExtensionalIndexExpressionsSet(indexExpression);
MultiIndexQuantifierEliminator quantifierEliminator = new BruteForceMultiIndexQuantifierEliminator(context.getTheory().getTopRewriter());
Expression solution = quantifierEliminator.solve(group, indexExpressionsSet, constraint, body, context);
return new ConstantExpressionStepSolver(solution);
}
use of com.sri.ai.grinder.sgdpllt.api.MultiIndexQuantifierEliminator in project aic-expresso by aic-sri-international.
the class Compilation method compile.
/**
* Compiles an expression to a normalized (decision-tree-like) expression.
* @param inputExpression
* @param mapFromVariableNameToTypeName
* @param mapFromCategoricalTypeNameToSizeString
* @param additionalTypes
* @param solverListener if not null, invoked on solver used for compilation, before and after compilation starts; returned solver on 'before' invocation is used (it may be the same one used as argument, of course).
* @return
*/
public static Expression compile(Expression inputExpression, Theory theory, Map<String, String> mapFromVariableNameToTypeName, Map<String, String> mapFromUniquelyNamedConstantToTypeName, Map<String, String> mapFromCategoricalTypeNameToSizeString, Collection<Type> additionalTypes, Function<MultiIndexQuantifierEliminator, MultiIndexQuantifierEliminator> solverListener) {
// the group actually does not matter, because we are not going to have any indices.
AssociativeCommutativeGroup group = new Max();
// The solver for the parameters above.
MultiIndexQuantifierEliminator solver = new SGDPLLT();
if (solverListener != null) {
solver = solverListener.apply(solver);
}
// We use the Prolog convention of small-letter initials for constants, but we need an exception for the random variables.
Predicate<Expression> isPrologConstant = new PrologConstantPredicate();
Predicate<Expression> isUniquelyNamedConstantPredicate = e -> isPrologConstant.apply(e) && !mapFromVariableNameToTypeName.containsKey(e);
Map<String, String> mapFromSymbolNameToTypeName = new LinkedHashMap<>(mapFromVariableNameToTypeName);
mapFromSymbolNameToTypeName.putAll(mapFromUniquelyNamedConstantToTypeName);
// Solve the problem.
// no indices; we want to keep all variables
List<Expression> indices = Util.list();
Expression result = solver.solve(group, inputExpression, indices, mapFromSymbolNameToTypeName, mapFromCategoricalTypeNameToSizeString, additionalTypes, isUniquelyNamedConstantPredicate, theory);
if (solverListener != null) {
solverListener.apply(null);
}
return result;
}
use of com.sri.ai.grinder.sgdpllt.api.MultiIndexQuantifierEliminator in project aic-praise by aic-sri-international.
the class TranslationOfTableToInequalities method constructGenericTableExpressionUsingInequalities.
/**
* Returns an {@link Expression} equivalent to a given {@link FunctionTable} but in the form of a decision tree
* (so hopefully more compact) using inequalities.
* @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 constructGenericTableExpressionUsingInequalities(FunctionTable functionTable, Function<MultiIndexQuantifierEliminator, MultiIndexQuantifierEliminator> solverListener) {
// the strategy in this method is the following:
// we collect all the contiguous indices sub-sets of the function table sharing their function value.
// They are kept in a map from each value to a list of indices sub-sets with that value.
//
// Then, we sort these groups of indices sub-sets by the sum of their sizes (number of entries), from smallest to largest.
// This will help us later to create an expression that tests for the largest groups first.
//
// Finally, we create an if-then-else expression, starting from the leaf (least common value).
// For each group of indices sub-sets with the same value, we obtain an inequalities expression describing
// the conditions for a variable assignment to be in that indices sub-set of the function table.
// Each portion generates a conjunction, and the group of portions generates a disjunction.
//
// The resulting if-then-else expression is linearly organized (only else clauses have nested if-then-else expressions).
// A more balanced (and thus efficient) representation is obtained by compiling it using SGDPLL(T).
Map<Double, List<FunctionTableIndicesSubSet>> functionValuesAndCorrespondingIndicesSubSet = map();
Double currentSubSetFunctionValueIfAny = null;
List<Integer> firstIndicesOfCurrentSubSetIfAny = null;
List<Integer> previousIndices = null;
List<Integer> indices = null;
CartesianProductEnumeration<Integer> cartesianProduct = new CartesianProductEnumeration<>(UAIUtil.cardinalityValues(functionTable));
while (cartesianProduct.hasMoreElements()) {
previousIndices = indices;
indices = new ArrayList<>(cartesianProduct.nextElement());
Double functionValue = Math.round(functionTable.entryFor(indices) * 100) / 100.0;
boolean hitNewFunctionValue = currentSubSetFunctionValueIfAny == null || !functionValue.equals(currentSubSetFunctionValueIfAny);
if (hitNewFunctionValue) {
storeIndicesSubSetOnAllVariables(functionTable, firstIndicesOfCurrentSubSetIfAny, previousIndices, currentSubSetFunctionValueIfAny, functionValuesAndCorrespondingIndicesSubSet);
// get information for next indices sub-set
currentSubSetFunctionValueIfAny = functionValue;
firstIndicesOfCurrentSubSetIfAny = indices;
}
}
previousIndices = indices;
storeIndicesSubSetOnAllVariables(functionTable, firstIndicesOfCurrentSubSetIfAny, previousIndices, currentSubSetFunctionValueIfAny, functionValuesAndCorrespondingIndicesSubSet);
// we sort (by using TreeMap) lists of indices sub-set with the same function value from those with smaller to greater sizes,
// and form the final expression backwards, thus prioritizing larger sub-sets
// whose conditions will be more often satisfied and leading to greater simplifications during inference.
List<Pair<BigInteger, List<FunctionTableIndicesSubSet>>> listOfPairsOfSizeAndListsOfIndicesSubSetsWithSameFunctionValue = new ArrayList<>(functionValuesAndCorrespondingIndicesSubSet.size());
for (Map.Entry<Double, List<FunctionTableIndicesSubSet>> functionValueAndIndicesSubSet : functionValuesAndCorrespondingIndicesSubSet.entrySet()) {
List<FunctionTableIndicesSubSet> indicesSubSetsWithSameFunctionValue = functionValueAndIndicesSubSet.getValue();
BigInteger sumOfSizes = BigInteger.ZERO;
for (FunctionTableIndicesSubSet indicesSubSet : indicesSubSetsWithSameFunctionValue) {
sumOfSizes = sumOfSizes.add(indicesSubSet.size());
}
listOfPairsOfSizeAndListsOfIndicesSubSetsWithSameFunctionValue.add(Pair.make(sumOfSizes, indicesSubSetsWithSameFunctionValue));
}
Collections.sort(listOfPairsOfSizeAndListsOfIndicesSubSetsWithSameFunctionValue, (Comparator<? super Pair<BigInteger, List<FunctionTableIndicesSubSet>>>) (p1, p2) -> p1.first.compareTo(p2.first));
List<List<FunctionTableIndicesSubSet>> listsOfIndicesSubSetsWithSameFunctionValue = mapIntoList(listOfPairsOfSizeAndListsOfIndicesSubSetsWithSameFunctionValue, p -> p.second);
Iterator<List<FunctionTableIndicesSubSet>> listsOfIndicesSubSetsWithSameFunctionValueIterator = listsOfIndicesSubSetsWithSameFunctionValue.iterator();
List<FunctionTableIndicesSubSet> firstListOfIndicesSubSets = listsOfIndicesSubSetsWithSameFunctionValueIterator.next();
Double valueOfFirstListOfIndicesSubSets = getFirstOrNull(firstListOfIndicesSubSets).getFunctionValue();
Expression currentExpression = makeSymbol(valueOfFirstListOfIndicesSubSets);
while (listsOfIndicesSubSetsWithSameFunctionValueIterator.hasNext()) {
List<FunctionTableIndicesSubSet> indicesSubSetsWithSameFunctionValue = listsOfIndicesSubSetsWithSameFunctionValueIterator.next();
Expression functionValueOfIndicesSubSetsWithSameFunctionValue = makeSymbol(getFirstOrNull(indicesSubSetsWithSameFunctionValue).getFunctionValue());
Expression conditionForThisFunctionValue = Or.make(mapIntoList(indicesSubSetsWithSameFunctionValue, TranslationOfTableToInequalities::getInequalitiesExpressionForFunctionTableIndicesSubSet));
currentExpression = IfThenElse.make(conditionForThisFunctionValue, functionValueOfIndicesSubSetsWithSameFunctionValue, currentExpression);
}
return currentExpression;
}
use of com.sri.ai.grinder.sgdpllt.api.MultiIndexQuantifierEliminator 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<MultiIndexQuantifierEliminator, MultiIndexQuantifierEliminator> 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.sgdpllt.api.Theory theory = new EqualityTheory(true, true);
Expression result = Compilation.compile(inputExpression, theory, mapFromVariableNameToTypeName, mapFromUniquelyNamedConstantToTypeName, mapFromCategoricalTypeNameToSizeString, list(), solverListener);
return result;
}
Aggregations