use of com.sri.ai.grinder.interpreter.Assignment 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.grinder.interpreter.Assignment in project aic-expresso by aic-sri-international.
the class AssignmentsSamplingIterator method makeContextExtendedBySampledValue.
private Context makeContextExtendedBySampledValue(Expression sampledValue) {
Assignment sampledAssignment = makeIndexAssignment(sampledValue);
Context contextExtendedWithSampledAssignment = sampledAssignment.extend(context);
return contextExtendedWithSampledAssignment;
}
use of com.sri.ai.grinder.interpreter.Assignment in project aic-expresso by aic-sri-international.
the class AssignmentsSamplingIterator method calculateNext.
@Override
protected Assignment calculateNext() {
if (nothingToSampleFrom) {
return null;
}
Expression sampledValue;
do {
sampledValue = sampleValue();
} while (conditionIsNotSatisfied(sampledValue));
Assignment result = assignment(index, sampledValue);
return result;
}
use of com.sri.ai.grinder.interpreter.Assignment in project aic-expresso by aic-sri-international.
the class AssignmentsSamplingIteratorTest method newSamplingIterator.
private Iterator<Assignment> newSamplingIterator(String indexString, int sampleSize, String conditionString) {
Expression index = parse(indexString);
Expression condition = parse(conditionString);
// Ensure condition of correct type is created
Type indexType = GrinderUtil.getTypeOfExpression(index, context);
if (indexType instanceof RealExpressoType || indexType instanceof RealInterval) {
SingleVariableLinearRealArithmeticConstraint svlraConstraint = new SingleVariableLinearRealArithmeticConstraint(index, true, context.getTheory());
svlraConstraint = (SingleVariableLinearRealArithmeticConstraint) svlraConstraint.conjoin(condition, context);
condition = svlraConstraint;
} else if (indexType instanceof IntegerExpressoType || indexType instanceof IntegerInterval) {
SingleVariableDifferenceArithmeticConstraint svdaConstraint = new SingleVariableDifferenceArithmeticConstraint(index, true, context.getTheory());
svdaConstraint = (SingleVariableDifferenceArithmeticConstraint) svdaConstraint.conjoin(condition, context);
condition = svdaConstraint;
}
AssignmentsSamplingIterator samplingIterator = new AssignmentsSamplingIterator(Arrays.asList(index), condition, conditionRewriter, random, context);
Iterator<Assignment> result = nIterator(sampleSize, samplingIterator);
return result;
}
Aggregations