use of com.sri.ai.grinder.api.SingleVariableConstraint in project aic-expresso by aic-sri-international.
the class SampleCommonInterpreterTest method run.
private Expression run(int sampleSizeN, boolean alwaysSample, String expressionString) {
SamplingCommonInterpreter interpreter = new SamplingCommonInterpreter(sampleSizeN, alwaysSample, random);
Expression expression = parse(expressionString);
if (expression.numberOfArguments() == 1 && Sets.isIntensionalSet(expression.get(0))) {
IntensionalSet intensionalSet = (IntensionalSet) expression.get(0);
IndexExpressionsSet indexExpressions = intensionalSet.getIndexExpressions();
List<Expression> indices = IndexExpressions.getIndices(indexExpressions);
if (indices.size() == 1) {
Expression index = indices.get(0);
Context intensionalSetContext = context.extendWith(indexExpressions);
// Ensure condition of correct type is created
Type indexType = GrinderUtil.getTypeOfExpression(index, intensionalSetContext);
SingleVariableConstraint singleVariableConstraint = null;
if (indexType instanceof RealExpressoType || indexType instanceof RealInterval) {
singleVariableConstraint = new SingleVariableLinearRealArithmeticConstraint(index, true, intensionalSetContext.getTheory());
} else if (indexType instanceof IntegerExpressoType || indexType instanceof IntegerInterval) {
singleVariableConstraint = new SingleVariableDifferenceArithmeticConstraint(index, true, intensionalSetContext.getTheory());
}
if (singleVariableConstraint != null) {
singleVariableConstraint = singleVariableConstraint.conjoin(intensionalSet.getCondition(), intensionalSetContext);
intensionalSet = intensionalSet.setCondition(singleVariableConstraint);
expression = expression.set(0, intensionalSet);
}
}
}
Expression result = interpreter.apply(expression, context);
System.out.println("Evaluation with " + sampleSizeN + " samples of " + expressionString + " = " + result.doubleValue() + " (as rational=" + toString(result) + ")");
return result;
}
use of com.sri.ai.grinder.api.SingleVariableConstraint in project aic-expresso by aic-sri-international.
the class SGDPLLTTester method testModelCountingForSingleVariableConstraints.
/**
* Given a theory and a number <code>n</code> of single-variable constraint tests,
* generates <code>n</code> formulas in the theory
* and see if the model counting solver works (checked by brute force).
* Throws an {@link Error} with the failure description if a test fails.
* @param theoryTestingSupport
* @param numberOfTests
* @param maxNumberOfLiterals
* @param outputCount
*/
public static void testModelCountingForSingleVariableConstraints(boolean testAgainstBruteForce, TheoryTestingSupport theoryTestingSupport, long numberOfTests, int maxNumberOfLiterals, boolean outputCount) {
Context context = theoryTestingSupport.makeContextWithTestingInformation();
Expression variable = parse(theoryTestingSupport.pickTestingVariableAtRandom());
NullaryFunction<Constraint> makeInitialConstraint = () -> theoryTestingSupport.getTheory().makeSingleVariableConstraint(variable, context);
Function<Constraint, Expression> makeRandomLiteral = c -> theoryTestingSupport.makeRandomLiteralOn(((SingleVariableConstraint) c).getVariable().toString(), context);
TestRunner tester = (ls, c, tB, cT, p) -> runModelCountingTestForSingleVariableConstraint(variable, ls, c, tB, cT.getTheory(), p);
runTesterGivenOnSuccessiveConjunctionsOfLiterals("model counting", tester, numberOfTests, maxNumberOfLiterals, testAgainstBruteForce, theoryTestingSupport, makeInitialConstraint, makeRandomLiteral, outputCount, context);
}
use of com.sri.ai.grinder.api.SingleVariableConstraint in project aic-expresso by aic-sri-international.
the class SGDPLLTTester method runGroupProblemSolvingTesterOnEmptyConstraint.
/**
* Same as {@link #runGroupProblemSolvingTesterForSuccessiveConstraints(String, TestRunner, boolean, AssociativeCommutativeGroup, TheoryTestingSupport, long, int, boolean)},
* but running the tester on a single, empty constraint.
* @param problemName
* @param tester
* @param testAgainstBruteForce
* @param group
* @param theoryTestingSupport
* @param numberOfTests
* @param outputCount
* @throws Error
*/
private static void runGroupProblemSolvingTesterOnEmptyConstraint(String problemName, TestRunner tester, boolean testAgainstBruteForce, AssociativeCommutativeGroup group, TheoryTestingSupport theoryTestingSupport, long numberOfTests, boolean outputCount) throws Error {
Context context = theoryTestingSupport.makeContextWithTestingInformation();
SingleVariableConstraint emptyConstraint = theoryTestingSupport.getTheory().makeSingleVariableConstraint(parse(theoryTestingSupport.pickTestingVariableAtRandom()), context);
for (int i = 1; i != numberOfTests + 1; i++) {
tester.runOneTest(list(), emptyConstraint, testAgainstBruteForce, theoryTestingSupport, context);
indicateCompletionOfTest(outputCount, problemName, i, testAgainstBruteForce, theoryTestingSupport);
}
}
use of com.sri.ai.grinder.api.SingleVariableConstraint in project aic-expresso by aic-sri-international.
the class SGDPLLTTester method runGroupProblemSolvingTestForSingleVariableConstraint.
private static void runGroupProblemSolvingTestForSingleVariableConstraint(Constraint constraint, AssociativeCommutativeGroup group, boolean testAgainstBruteForce, TheoryTestingSupport theoryTestingSupport, Collection<Expression> literals, int bodyDepth, Context context) {
SingleVariableConstraint singleVariableConstraint = (SingleVariableConstraint) constraint;
Expression index = singleVariableConstraint.getVariable();
runGroupProblemSolvingTestGivenConstraintOnRandomProblem(list(index), constraint, group, testAgainstBruteForce, theoryTestingSupport, bodyDepth, context);
}
use of com.sri.ai.grinder.api.SingleVariableConstraint in project aic-expresso by aic-sri-international.
the class LinearRealArithmeticTheoryTest method runTest.
/**
* @param variable
* @param constraintString
* @param expected
* @param computedFunction
* @param stepSolverMaker
* @param context
*/
private void runTest(Expression variable, String constraintString, Expression expected, String computedFunction, Function<SingleVariableConstraint, ExpressionStepSolver> stepSolverMaker, Context context) {
System.out.println("Solving " + computedFunction + " for " + variable + " in " + constraintString);
SingleVariableConstraint constraint = new SingleVariableLinearRealArithmeticConstraint(variable, true, context.getTheory());
constraint = constraint.conjoin(parse(constraintString), context);
ExpressionStepSolver stepSolver = stepSolverMaker.apply(constraint);
Expression actual = stepSolver.solve(context);
System.out.println("Variable " + variable + "\nhas " + computedFunction + ":\n" + actual + "\nfor constraint:\n" + constraintString + "\n");
assertEquals(expected, actual);
}
Aggregations