Search in sources :

Example 1 with SingleVariableDifferenceArithmeticConstraint

use of com.sri.ai.grinder.theory.differencearithmetic.SingleVariableDifferenceArithmeticConstraint in project aic-expresso by aic-sri-international.

the class SummationOnDifferenceArithmeticAndPolynomialStepSolverTest method runTest.

private void runTest(Expression variable, String constraintString, Expression body, Expression expected, Context context) {
    Theory theory = context.getTheory();
    SingleVariableDifferenceArithmeticConstraint constraint = new SingleVariableDifferenceArithmeticConstraint(variable, true, theory);
    constraint = (SingleVariableDifferenceArithmeticConstraint) constraint.conjoin(parse(constraintString), context);
    Expression typeExpression = context.getTypeExpressionOfRegisteredSymbol(variable);
    SingleQuantifierEliminationProblem problem = new DefaultSingleQuantifierEliminationProblem(new Sum(), variable, typeExpression, constraint, body);
    ExpressionStepSolver stepSolver = new SummationOnDifferenceArithmeticAndPolynomialStepSolver(problem);
    Expression actual = stepSolver.solve(context);
    expected = simplify(expected, context);
    System.out.println("sum({{ (on " + variable + " in " + GrinderUtil.getTypeExpressionOfExpression(variable, context) + ") " + body + " : " + constraintString + " }} = " + actual + "\n");
    if (!expected.equals(actual)) {
        Expression difference = apply(MINUS, expected, actual);
        Expression differenceResult = simplify(difference, context);
        if (!differenceResult.equals(ZERO)) {
            System.err.println("Expressions are not equal and even difference is not zero");
            System.err.println("Expected: " + expected);
            System.err.println("Actual: " + actual);
            System.err.println("Difference: " + differenceResult);
            fail("Expressions are not equal and even difference is not zero. Expected: " + expected + ", actual: " + actual);
        }
    }
// TODO: correctness test against grounding
}
Also used : SummationOnDifferenceArithmeticAndPolynomialStepSolver(com.sri.ai.grinder.theory.differencearithmetic.SummationOnDifferenceArithmeticAndPolynomialStepSolver) Theory(com.sri.ai.grinder.api.Theory) DifferenceArithmeticTheory(com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory) Expression(com.sri.ai.expresso.api.Expression) ExpressionStepSolver(com.sri.ai.grinder.api.ExpressionStepSolver) DefaultSingleQuantifierEliminationProblem(com.sri.ai.grinder.core.solver.DefaultSingleQuantifierEliminationProblem) SingleQuantifierEliminationProblem(com.sri.ai.grinder.api.SingleQuantifierEliminationProblem) DefaultSingleQuantifierEliminationProblem(com.sri.ai.grinder.core.solver.DefaultSingleQuantifierEliminationProblem) Sum(com.sri.ai.grinder.group.Sum) SingleVariableDifferenceArithmeticConstraint(com.sri.ai.grinder.theory.differencearithmetic.SingleVariableDifferenceArithmeticConstraint)

Example 2 with SingleVariableDifferenceArithmeticConstraint

use of com.sri.ai.grinder.theory.differencearithmetic.SingleVariableDifferenceArithmeticConstraint in project aic-expresso by aic-sri-international.

the class AssignmentsSamplingIterator method getTypeToSampleFrom.

public static Type getTypeToSampleFrom(Expression variable, Expression condition, Context context) {
    Type result = GrinderUtil.getTypeOfExpression(variable, context);
    if (result instanceof FunctionType) {
        FunctionType functionType = (FunctionType) result;
        result = new LazySampledFunctionType(functionType.getCodomain(), functionType.getArgumentTypes().toArray(new Type[functionType.getArity()]));
    } else {
        if (condition.equals(false)) {
            result = null;
        } else if (condition.equals(true)) {
        // we leave as is.
        } else if (result instanceof RealExpressoType || result instanceof RealInterval) {
            LinearRealArithmeticTheory theory = new LinearRealArithmeticTheory(true, true);
            SingleVariableLinearRealArithmeticConstraint constraint = (SingleVariableLinearRealArithmeticConstraint) theory.makeSingleVariableConstraint(variable, context);
            constraint = (SingleVariableLinearRealArithmeticConstraint) constraint.conjoin(condition, context);
            IntervalWithMeasureEquivalentToSingleVariableLinearRealArithmeticConstraintStepSolver solver = new IntervalWithMeasureEquivalentToSingleVariableLinearRealArithmeticConstraintStepSolver(constraint);
            Expression realInterval = solver.solve(context);
            if (Sets.isEmptySet(realInterval)) {
                // used to indicate an empty set.
                result = null;
            } else if (ExtensionalSets.isExtensionalSet(realInterval) && ExtensionalSets.isSingleton(realInterval)) {
                String singletonValue = realInterval.get(0).toString();
                result = new RealInterval("[" + singletonValue + ";" + singletonValue + "]");
            } else {
                result = new RealInterval(realInterval.toString());
            }
        } else if (result instanceof IntegerExpressoType || result instanceof IntegerInterval) {
            DifferenceArithmeticTheory theory = new DifferenceArithmeticTheory(true, true);
            SingleVariableDifferenceArithmeticConstraint constraint = (SingleVariableDifferenceArithmeticConstraint) theory.makeSingleVariableConstraint(variable, context);
            constraint = (SingleVariableDifferenceArithmeticConstraint) constraint.conjoin(condition, context);
            ValuesOfSingleVariableDifferenceArithmeticConstraintStepSolver solver = new ValuesOfSingleVariableDifferenceArithmeticConstraintStepSolver(constraint);
            // NOTE: the exceptions set returned here is implicit in the condition so no need to use it here.
            RangeAndExceptionsSet rangeAndExceptionsSet = (RangeAndExceptionsSet) solver.solve(context);
            if (rangeAndExceptionsSet.isEmpty()) {
                // used to indicate an empty set.
                result = null;
            } else if (rangeAndExceptionsSet.isSingleton()) {
                result = new IntegerInterval(rangeAndExceptionsSet.getSingleValue().intValueExact(), rangeAndExceptionsSet.getSingleValue().intValueExact());
            } else {
                result = new IntegerInterval(rangeAndExceptionsSet.getStrictLowerBound().intValueExact() + 1, rangeAndExceptionsSet.getNonStrictUpperBound().intValueExact());
            }
        }
    }
    if (result != null && !result.isSampleUniquelyNamedConstantSupported()) {
        throw new IllegalArgumentException("Unable to sample " + variable + " from " + result);
    }
    return result;
}
Also used : RangeAndExceptionsSet(com.sri.ai.grinder.theory.differencearithmetic.RangeAndExceptionsSet) FunctionType(com.sri.ai.expresso.type.FunctionType) IntegerInterval(com.sri.ai.expresso.type.IntegerInterval) DifferenceArithmeticTheory(com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory) LinearRealArithmeticTheory(com.sri.ai.grinder.theory.linearrealarithmetic.LinearRealArithmeticTheory) IntervalWithMeasureEquivalentToSingleVariableLinearRealArithmeticConstraintStepSolver(com.sri.ai.grinder.theory.linearrealarithmetic.IntervalWithMeasureEquivalentToSingleVariableLinearRealArithmeticConstraintStepSolver) RealInterval(com.sri.ai.expresso.type.RealInterval) Type(com.sri.ai.expresso.api.Type) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) IntegerExpressoType(com.sri.ai.expresso.type.IntegerExpressoType) FunctionType(com.sri.ai.expresso.type.FunctionType) ValuesOfSingleVariableDifferenceArithmeticConstraintStepSolver(com.sri.ai.grinder.theory.differencearithmetic.ValuesOfSingleVariableDifferenceArithmeticConstraintStepSolver) Expression(com.sri.ai.expresso.api.Expression) IntegerExpressoType(com.sri.ai.expresso.type.IntegerExpressoType) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) SingleVariableDifferenceArithmeticConstraint(com.sri.ai.grinder.theory.differencearithmetic.SingleVariableDifferenceArithmeticConstraint) SingleVariableLinearRealArithmeticConstraint(com.sri.ai.grinder.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint)

Example 3 with SingleVariableDifferenceArithmeticConstraint

use of com.sri.ai.grinder.theory.differencearithmetic.SingleVariableDifferenceArithmeticConstraint 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;
}
Also used : Assignment(com.sri.ai.grinder.interpreter.Assignment) TupleType(com.sri.ai.expresso.type.TupleType) Type(com.sri.ai.expresso.api.Type) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) IntegerExpressoType(com.sri.ai.expresso.type.IntegerExpressoType) FunctionType(com.sri.ai.expresso.type.FunctionType) Expression(com.sri.ai.expresso.api.Expression) IntegerExpressoType(com.sri.ai.expresso.type.IntegerExpressoType) IntegerInterval(com.sri.ai.expresso.type.IntegerInterval) AssignmentsSamplingIterator(com.sri.ai.grinder.helper.AssignmentsSamplingIterator) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) SingleVariableDifferenceArithmeticConstraint(com.sri.ai.grinder.theory.differencearithmetic.SingleVariableDifferenceArithmeticConstraint) SingleVariableLinearRealArithmeticConstraint(com.sri.ai.grinder.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint) RealInterval(com.sri.ai.expresso.type.RealInterval)

Example 4 with SingleVariableDifferenceArithmeticConstraint

use of com.sri.ai.grinder.theory.differencearithmetic.SingleVariableDifferenceArithmeticConstraint 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;
}
Also used : TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) SingleVariableConstraint(com.sri.ai.grinder.api.SingleVariableConstraint) IntegerInterval(com.sri.ai.expresso.type.IntegerInterval) RealInterval(com.sri.ai.expresso.type.RealInterval) Type(com.sri.ai.expresso.api.Type) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) IntegerExpressoType(com.sri.ai.expresso.type.IntegerExpressoType) IntensionalSet(com.sri.ai.expresso.api.IntensionalSet) Expression(com.sri.ai.expresso.api.Expression) SamplingCommonInterpreter(com.sri.ai.grinder.interpreter.SamplingCommonInterpreter) IntegerExpressoType(com.sri.ai.expresso.type.IntegerExpressoType) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet) SingleVariableDifferenceArithmeticConstraint(com.sri.ai.grinder.theory.differencearithmetic.SingleVariableDifferenceArithmeticConstraint) SingleVariableLinearRealArithmeticConstraint(com.sri.ai.grinder.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint)

Example 5 with SingleVariableDifferenceArithmeticConstraint

use of com.sri.ai.grinder.theory.differencearithmetic.SingleVariableDifferenceArithmeticConstraint in project aic-expresso by aic-sri-international.

the class ValuesOfSingleVariableDifferenceArithmeticConstraintStepSolverTest method runTest.

private void runTest(Expression variable, String constraintString, Expression expected, Context context) {
    Constraint constraint = new SingleVariableDifferenceArithmeticConstraint(variable, true, context.getTheory());
    constraint = constraint.conjoin(parse(constraintString), context);
    ExpressionLiteralSplitterStepSolver stepSolver = new ValuesOfSingleVariableDifferenceArithmeticConstraintStepSolver((SingleVariableDifferenceArithmeticConstraint) constraint);
    Expression actual = stepSolver.solve(context);
    System.out.println("Variable " + variable + "\nhas possible values:\n" + actual + "\nsatisfying constraint:\n" + constraintString + "\n");
    assertEquals(expected, actual);
}
Also used : ValuesOfSingleVariableDifferenceArithmeticConstraintStepSolver(com.sri.ai.grinder.theory.differencearithmetic.ValuesOfSingleVariableDifferenceArithmeticConstraintStepSolver) SingleVariableDifferenceArithmeticConstraint(com.sri.ai.grinder.theory.differencearithmetic.SingleVariableDifferenceArithmeticConstraint) Constraint(com.sri.ai.grinder.api.Constraint) Expression(com.sri.ai.expresso.api.Expression) ExpressionLiteralSplitterStepSolver(com.sri.ai.grinder.api.ExpressionLiteralSplitterStepSolver) SingleVariableDifferenceArithmeticConstraint(com.sri.ai.grinder.theory.differencearithmetic.SingleVariableDifferenceArithmeticConstraint)

Aggregations

Expression (com.sri.ai.expresso.api.Expression)5 SingleVariableDifferenceArithmeticConstraint (com.sri.ai.grinder.theory.differencearithmetic.SingleVariableDifferenceArithmeticConstraint)5 Type (com.sri.ai.expresso.api.Type)3 IntegerExpressoType (com.sri.ai.expresso.type.IntegerExpressoType)3 IntegerInterval (com.sri.ai.expresso.type.IntegerInterval)3 RealExpressoType (com.sri.ai.expresso.type.RealExpressoType)3 RealInterval (com.sri.ai.expresso.type.RealInterval)3 SingleVariableLinearRealArithmeticConstraint (com.sri.ai.grinder.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint)3 FunctionType (com.sri.ai.expresso.type.FunctionType)2 DifferenceArithmeticTheory (com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory)2 ValuesOfSingleVariableDifferenceArithmeticConstraintStepSolver (com.sri.ai.grinder.theory.differencearithmetic.ValuesOfSingleVariableDifferenceArithmeticConstraintStepSolver)2 IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)1 IntensionalSet (com.sri.ai.expresso.api.IntensionalSet)1 TupleType (com.sri.ai.expresso.type.TupleType)1 Constraint (com.sri.ai.grinder.api.Constraint)1 Context (com.sri.ai.grinder.api.Context)1 ExpressionLiteralSplitterStepSolver (com.sri.ai.grinder.api.ExpressionLiteralSplitterStepSolver)1 ExpressionStepSolver (com.sri.ai.grinder.api.ExpressionStepSolver)1 SingleQuantifierEliminationProblem (com.sri.ai.grinder.api.SingleQuantifierEliminationProblem)1 SingleVariableConstraint (com.sri.ai.grinder.api.SingleVariableConstraint)1