use of com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint in project aic-expresso by aic-sri-international.
the class Measure method constructComponentIntensionalSet.
private static Expression constructComponentIntensionalSet(Type indexType, IntensionalSet intensionalSet, Expression additiveIdentityElement, Context intensionalSetContext) {
Expression conditionedBody = IfThenElse.make(intensionalSet.getCondition(), intensionalSet.getHead(), additiveIdentityElement);
Expression componentIndex = Expressions.makeUniqueVariable("C", conditionedBody, intensionalSetContext);
Expression indexExpression = IndexExpressions.makeIndexExpression(componentIndex, Expressions.parse(indexType.getName()));
Expression intensionalCondition = Expressions.TRUE;
// NOTE: handle the REAL cases where an SingleVariableLinearRealArithmeticConstraint is expected.
if (indexType instanceof RealExpressoType || indexType instanceof RealInterval) {
SingleVariableLinearRealArithmeticConstraint svlraConstraint = new SingleVariableLinearRealArithmeticConstraint(componentIndex, true, intensionalSetContext.getTheory());
intensionalCondition = svlraConstraint;
}
Expression result = IntensionalSet.make(Sets.isMultiSet(intensionalSet) ? IntensionalSet.MULTI_SET_LABEL : IntensionalSet.UNI_SET_LABEL, new ExtensionalIndexExpressionsSet(Arrays.asList(indexExpression)), componentIndex, intensionalCondition);
return result;
}
use of com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint in project aic-expresso by aic-sri-international.
the class MeasureTest method measure.
private Rational measure(String testIntensionalSetString) {
Expression testIntensionalSetExpression = parse(testIntensionalSetString);
Expression properlyConditionedIntensionalSetExpression = testIntensionalSetExpression;
if (Sets.isIntensionalSet(testIntensionalSetExpression)) {
IntensionalSet intensionalSet = (IntensionalSet) testIntensionalSetExpression;
List<Expression> indices = IndexExpressions.getIndices(intensionalSet.getIndexExpressions());
if (indices.size() == 1) {
Expression index = indices.get(0);
Context intensionalSetContext = context.extendWith(intensionalSet.getIndexExpressions());
Type type = GrinderUtil.getType(index, intensionalSetContext);
if (type instanceof RealExpressoType || type instanceof RealInterval) {
SingleVariableLinearRealArithmeticConstraint singleVariableConstraint = new SingleVariableLinearRealArithmeticConstraint(index, true, context.getTheory());
singleVariableConstraint = (SingleVariableLinearRealArithmeticConstraint) singleVariableConstraint.conjoin(intensionalSet.getCondition(), intensionalSetContext);
properlyConditionedIntensionalSetExpression = IntensionalSet.make(Sets.isMultiSet(intensionalSet) ? IntensionalSet.MULTI_SET_LABEL : IntensionalSet.UNI_SET_LABEL, intensionalSet.getIndexExpressions(), intensionalSet.getHead(), singleVariableConstraint);
}
}
}
Rational result = Measure.get(properlyConditionedIntensionalSetExpression, context);
return result;
}
use of com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint in project aic-expresso by aic-sri-international.
the class AssignmentsSamplingIteratorTest method newSamplingIterator.
private Iterator<Map<Expression, Expression>> 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.getType(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;
}
Iterator<Map<Expression, Expression>> result = new AssignmentsSamplingIterator(Arrays.asList(index), sampleSize, condition, conditionRewriter, random, context);
return result;
}
use of com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint 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<Constraint, ExpressionStepSolver> stepSolverMaker, Context context) {
System.out.println("Solving " + computedFunction + " for " + variable + " in " + constraintString);
Constraint 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);
}
use of com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint in project aic-expresso by aic-sri-international.
the class SampleCommonInterpreterTest method run.
private Expression run(int sampleSizeN, boolean alwaysSample, String expressionString) {
SampleCommonInterpreter interpreter = new SampleCommonInterpreter(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.getType(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;
}
Aggregations