use of com.sri.ai.expresso.api.QuantifiedExpression in project aic-expresso by aic-sri-international.
the class TupleQuantifierSimplifier method createTuplesOfVarsForTupleTypes.
private static Map<Expression, Expression> createTuplesOfVarsForTupleTypes(QuantifiedExpression quantifiedExpression, List<Map.Entry<Expression, Expression>> indexesOfTupleType) {
Map<Expression, Expression> result = new HashMap<>();
Set<Expression> allSubExpressions = Util.addAllToSet(new SubExpressionsDepthFirstIterator(quantifiedExpression));
for (Map.Entry<Expression, Expression> entry : indexesOfTupleType) {
List<Expression> tupleVars = new ArrayList<>();
for (int i = 1; i <= entry.getValue().numberOfArguments(); i++) {
Expression proposedVar = Expressions.makeSymbol(entry.getKey().toString() + "_" + i);
Expression actualVar = Expressions.primedUntilUnique(proposedVar, expr -> !allSubExpressions.contains(expr));
tupleVars.add(actualVar);
}
result.put(entry.getKey(), Expressions.makeTuple(tupleVars));
}
return result;
}
use of com.sri.ai.expresso.api.QuantifiedExpression in project aic-expresso by aic-sri-international.
the class TupleQuantifierSimplifier method simplify.
public static Expression simplify(Expression expression, Context context) {
Expression result = expression;
if (expression instanceof QuantifiedExpression) {
QuantifiedExpression quantifiedExpression = (QuantifiedExpression) expression;
Map<Expression, Expression> indexToTypeMap = IndexExpressions.getIndexToTypeMapWithDefaultNull(quantifiedExpression);
List<Map.Entry<Expression, Expression>> indexesOfTupleType = indexToTypeMap.entrySet().stream().filter(entry -> entry.getValue() != null && TupleType.isTupleType(entry.getValue())).collect(Collectors.toList());
if (indexesOfTupleType.size() > 0) {
Map<Expression, Expression> indexToTupleOfVars = createTuplesOfVarsForTupleTypes(quantifiedExpression, indexesOfTupleType);
result = rewriteQuantifiedExpression(quantifiedExpression, indexToTypeMap, indexToTupleOfVars, context);
}
}
return result;
}
use of com.sri.ai.expresso.api.QuantifiedExpression in project aic-expresso by aic-sri-international.
the class SGDPLLTTester method runGroupProblemSolvingTestGivenConstraintAndProblem.
/**
* @param problem
* @param indices
* @param constraint
* @param body
* @param testAgainstBruteForce
* @param theoryTestingSupport
* @param context
* @throws Error
*/
public static void runGroupProblemSolvingTestGivenConstraintAndProblem(Expression problem, Collection<Expression> indices, Constraint constraint, Expression body, boolean testAgainstBruteForce, TheoryTestingSupport theoryTestingSupport, Context context) throws Error {
Theory theory = theoryTestingSupport.getTheory();
Collection<Expression> freeVariables = getFreeVariableMinusIndices(indices, constraint, body, context);
String problemDescription = problem.toString();
output(problemDescription);
Simplifier symbolicInterpreter = (e, c) -> theory.evaluate(e, c);
long start = System.currentTimeMillis();
Expression symbolicSolution = symbolicInterpreter.apply(problem, context);
long time = System.currentTimeMillis() - start;
output("Symbolic solution: " + symbolicSolution);
output("Computed in " + time + " ms");
if (Util.thereExists(new SubExpressionsDepthFirstIterator(symbolicSolution), e -> e instanceof QuantifiedExpression || Sets.isIntensionalSet(e))) {
throw new Error("Symbolic solution is not quantifier-free: " + symbolicSolution);
}
if (testAgainstBruteForce) {
BinaryFunction<BruteForceCommonInterpreter, Context, Expression> bruteForceSolutionGivenInterpreterAndContextWithAssignmentToOtherVariables = (i, c) -> i.apply(problem, c);
testSymbolicVsBruteForceComputationForEachAssignment(theory, problemDescription, freeVariables, symbolicSolution, bruteForceSolutionGivenInterpreterAndContextWithAssignmentToOtherVariables, context);
// A more elegant approach would be to create a "for all free variables : symbolic = problem" expression
// and solve it by brute force instead of using testSymbolicVsBruteForceComputation
// which replicates the brute force interpreter to some extent.
// The reason we do not do this is simply due to the fact that the brute force interpreter would return "false"
// in case of failure, without indicating which assignment failed, which is very useful for debugging.
// If interpreters, and in fact the whole framework, provided proofs of its calculations,
// then we could simply use the more elegant approach.
} else {
output("Skipping test againt brute-force.");
}
}
use of com.sri.ai.expresso.api.QuantifiedExpression in project aic-expresso by aic-sri-international.
the class SGDPLLTTester method runModelCountingTestForSingleVariableConstraint.
private static void runModelCountingTestForSingleVariableConstraint(Expression variable, Collection<Expression> literals, Constraint constraint, boolean testAgainstBruteForce, Theory theory, Context context) {
Expression literalsConjunction = And.make(literals);
String problemDescription = "model counting of " + literalsConjunction + " for variable " + variable;
output("Problem: " + problemDescription);
Simplifier symbolicSolver = (e, p) -> computeModelCountBySolver((SingleVariableConstraint) e, p);
SingleVariableConstraint singleVariableConstraint = (SingleVariableConstraint) constraint;
Expression symbolicSolution = symbolicSolver.apply(singleVariableConstraint, context);
if (Util.thereExists(new SubExpressionsDepthFirstIterator(symbolicSolution), e -> e instanceof QuantifiedExpression || Sets.isIntensionalSet(e))) {
throw new Error("Symbolic solution is not quantifier-free: " + symbolicSolution);
}
output("Symbolic result: " + symbolicSolution);
if (testAgainstBruteForce) {
if (singleVariableConstraint.isContradiction()) {
if (!symbolicSolution.equals(ZERO)) {
throw new Error("Constraint is contradiction, but symbolic solver does not produce 0, but instead " + symbolicSolution);
}
} else {
Expression testingVariable = singleVariableConstraint.getVariable();
Set<Expression> allVariables = getVariablesBeingReferenced(singleVariableConstraint, context);
Collection<Expression> otherVariables = removeFromSetNonDestructively(allVariables, v -> v.equals(testingVariable));
BinaryFunction<BruteForceCommonInterpreter, Context, Expression> fromInterpreterAndContextWithAssignmentToOtherVariablesToBruteForceSolution = (interpreter, contextWithAssignmentToOtherVariables) -> bruteForceModelCounterForVariableGivenInterpreterAndAssignmentToOtherVariables(variable, literalsConjunction, interpreter, theory, contextWithAssignmentToOtherVariables);
testSymbolicVsBruteForceComputationForEachAssignment(theory, problemDescription, otherVariables, symbolicSolution, fromInterpreterAndContextWithAssignmentToOtherVariablesToBruteForceSolution, context);
}
} else {
output("Skipping test againt brute-force.");
}
}
use of com.sri.ai.expresso.api.QuantifiedExpression in project aic-expresso by aic-sri-international.
the class SGDPLLTTester method runGroupProblemSolvingTestGivenConstraintAndProblem.
/**
* @param problem
* @param indices
* @param constraint
* @param body
* @param testAgainstBruteForce
* @param theoryTestingSupport
* @param context
* @throws Error
*/
public static void runGroupProblemSolvingTestGivenConstraintAndProblem(Expression problem, Collection<Expression> indices, Constraint constraint, Expression body, boolean testAgainstBruteForce, TheoryTestingSupport theoryTestingSupport, Context context) throws Error {
Theory theory = theoryTestingSupport.getTheory();
Collection<Expression> freeVariables = getFreeVariableMinusIndices(indices, constraint, body, context);
String problemDescription = problem.toString();
output(problemDescription);
Simplifier symbolicInterpreter = (e, c) -> theory.evaluate(e, c);
long start = System.currentTimeMillis();
Expression symbolicSolution = symbolicInterpreter.apply(problem, context);
long time = System.currentTimeMillis() - start;
output("Symbolic solution: " + symbolicSolution);
output("Computed in " + time + " ms");
if (Util.thereExists(new SubExpressionsDepthFirstIterator(symbolicSolution), e -> e instanceof QuantifiedExpression || Sets.isIntensionalSet(e))) {
throw new Error("Symbolic solution is not quantifier-free: " + symbolicSolution);
}
if (testAgainstBruteForce) {
BinaryFunction<BruteForceCommonInterpreter, Context, Expression> bruteForceSolutionGivenInterpreterAndContextWithAssignmentToOtherVariables = (i, c) -> i.apply(problem, c);
testSymbolicVsBruteForceComputationForEachAssignment(theory, problemDescription, freeVariables, symbolicSolution, bruteForceSolutionGivenInterpreterAndContextWithAssignmentToOtherVariables, context);
// A more elegant approach would be to create a "for all free variables : symbolic = problem" expression
// and solve it by brute force instead of using testSymbolicVsBruteForceComputation
// which replicates the brute force interpreter to some extent.
// The reason we do not do this is simply due to the fact that the brute force interpreter would return "false"
// in case of failure, without indicating which assignment failed, which is very useful for debugging.
// If interpreters, and in fact the whole framework, provided proofs of its calculations,
// then we could simply use the more elegant approach.
} else {
output("Skipping test againt brute-force.");
}
}
Aggregations