Search in sources :

Example 1 with SelectExpressionsSatisfyingComparisonStepSolver

use of com.sri.ai.grinder.sgdpllt.helper.SelectExpressionsSatisfyingComparisonStepSolver in project aic-expresso by aic-sri-international.

the class AbstractSingleVariableDifferenceArithmeticConstraintFeasibilityRegionStepSolver method getSolutionStepAfterBoundsAreCheckedForFeasibility.

@Override
protected Step getSolutionStepAfterBoundsAreCheckedForFeasibility(Expression maximumLowerBound, Expression minimumUpperBound, AbstractSingleVariableNumericConstraintFeasibilityRegionStepSolver sequelBaseAsNumericStepSolver, Context context) {
    AbstractSingleVariableDifferenceArithmeticConstraintFeasibilityRegionStepSolver sequelBase = (AbstractSingleVariableDifferenceArithmeticConstraintFeasibilityRegionStepSolver) sequelBaseAsNumericStepSolver;
    StepSolver<List<Expression>> disequalsGreaterThanMaximumLowerBoundStepSolver;
    if (initialDisequalsGreaterThanMaximumLowerBoundStepSolver == null) {
        disequalsGreaterThanMaximumLowerBoundStepSolver = new SelectExpressionsSatisfyingComparisonStepSolver(getDisequals(), GREATER_THAN, // relies on this class's enforcing of all lower bounds being strict
        maximumLowerBound);
    } else {
        disequalsGreaterThanMaximumLowerBoundStepSolver = initialDisequalsGreaterThanMaximumLowerBoundStepSolver;
    }
    StepSolver.Step<List<Expression>> disequalsGreaterThanGreatestStrictLowerBoundStep = disequalsGreaterThanMaximumLowerBoundStepSolver.step(context);
    if (disequalsGreaterThanGreatestStrictLowerBoundStep.itDepends()) {
        AbstractSingleVariableDifferenceArithmeticConstraintFeasibilityRegionStepSolver ifTrue = makeSequelStepSolver(sequelBase);
        ifTrue.initialDisequalsGreaterThanMaximumLowerBoundStepSolver = disequalsGreaterThanGreatestStrictLowerBoundStep.getStepSolverForWhenSplitterIsTrue();
        AbstractSingleVariableDifferenceArithmeticConstraintFeasibilityRegionStepSolver ifFalse = makeSequelStepSolver(sequelBase);
        ifFalse.initialDisequalsGreaterThanMaximumLowerBoundStepSolver = disequalsGreaterThanGreatestStrictLowerBoundStep.getStepSolverForWhenSplitterIsFalse();
        ItDependsOn result = new ItDependsOn(disequalsGreaterThanGreatestStrictLowerBoundStep.getSplitter(), disequalsGreaterThanGreatestStrictLowerBoundStep.getContextSplittingWhenSplitterIsLiteral(), ifTrue, ifFalse);
        return result;
    }
    List<Expression> disequalsGreaterThanGreatestStrictLowerBound = disequalsGreaterThanGreatestStrictLowerBoundStep.getValue();
    sequelBase.initialDisequalsGreaterThanMaximumLowerBoundStepSolver = new ConstantStepSolver<List<Expression>>(disequalsGreaterThanGreatestStrictLowerBound);
    StepSolver<List<Expression>> disequalsWithinBoundsStepSolver;
    if (initialDisequalsWithinBoundsStepSolver == null) {
        disequalsWithinBoundsStepSolver = new SelectExpressionsSatisfyingComparisonStepSolver(disequalsGreaterThanGreatestStrictLowerBound, LESS_THAN_OR_EQUAL_TO, // relies on this class's enforcing of all upper bounds being non-strict
        minimumUpperBound);
    } else {
        disequalsWithinBoundsStepSolver = initialDisequalsWithinBoundsStepSolver;
    }
    StepSolver.Step<List<Expression>> disequalsWithinBoundsStep = disequalsWithinBoundsStepSolver.step(context);
    if (disequalsWithinBoundsStep.itDepends()) {
        AbstractSingleVariableDifferenceArithmeticConstraintFeasibilityRegionStepSolver ifTrue = makeSequelStepSolver(sequelBase);
        ifTrue.initialDisequalsWithinBoundsStepSolver = disequalsWithinBoundsStep.getStepSolverForWhenSplitterIsTrue();
        AbstractSingleVariableDifferenceArithmeticConstraintFeasibilityRegionStepSolver ifFalse = makeSequelStepSolver(sequelBase);
        ifFalse.initialDisequalsWithinBoundsStepSolver = disequalsWithinBoundsStep.getStepSolverForWhenSplitterIsFalse();
        ItDependsOn result = new ItDependsOn(disequalsWithinBoundsStep.getSplitter(), disequalsWithinBoundsStep.getContextSplittingWhenSplitterIsLiteral(), ifTrue, ifFalse);
        return result;
    }
    ArrayList<Expression> disequalsWithinBounds = new ArrayList<>(disequalsWithinBoundsStep.getValue());
    sequelBase.initialDisequalsWithinBoundsStepSolver = new ConstantStepSolver<List<Expression>>(disequalsWithinBounds);
    Expression boundsDifference = applyAndSimplify(MINUS, arrayList(minimumUpperBound, maximumLowerBound), context);
    // the goal of the upcoming 'if' is to define the values for these two next declared variables:
    boolean weKnowThatNumberOfDistinctDisequalsExceedsNumberOfValuesWithinBounds;
    // if true, number of distinct disequals exceeds number of values within bounds;
    // if false, that may be true or false, we don't know.
    DistinctExpressionsStepSolver distinctExpressionsStepSolver;
    if (isNumber(boundsDifference)) {
        ExpressionLiteralSplitterStepSolver numberOfDistinctDisequalsIsLessThanBoundsDifferenceStepSolver;
        if (initialNumberOfDistinctDisequalsIsLessThanBoundsDifferenceStepSolver == null) {
            numberOfDistinctDisequalsIsLessThanBoundsDifferenceStepSolver = new NumberOfDistinctExpressionsIsLessThanStepSolver(boundsDifference.intValue(), disequalsWithinBounds);
        } else {
            numberOfDistinctDisequalsIsLessThanBoundsDifferenceStepSolver = initialNumberOfDistinctDisequalsIsLessThanBoundsDifferenceStepSolver;
        }
        ExpressionLiteralSplitterStepSolver.Step numberOfDistinctDisequalsIsLessThanBoundsDifferenceStep = numberOfDistinctDisequalsIsLessThanBoundsDifferenceStepSolver.step(context);
        if (numberOfDistinctDisequalsIsLessThanBoundsDifferenceStep.itDepends()) {
            AbstractSingleVariableDifferenceArithmeticConstraintFeasibilityRegionStepSolver ifTrue = makeSequelStepSolver(sequelBase);
            ifTrue.initialNumberOfDistinctDisequalsIsLessThanBoundsDifferenceStepSolver = numberOfDistinctDisequalsIsLessThanBoundsDifferenceStep.getStepSolverForWhenSplitterIsTrue();
            AbstractSingleVariableDifferenceArithmeticConstraintFeasibilityRegionStepSolver ifFalse = makeSequelStepSolver(sequelBase);
            ifFalse.initialNumberOfDistinctDisequalsIsLessThanBoundsDifferenceStepSolver = numberOfDistinctDisequalsIsLessThanBoundsDifferenceStep.getStepSolverForWhenSplitterIsFalse();
            ItDependsOn result = new ItDependsOn(numberOfDistinctDisequalsIsLessThanBoundsDifferenceStep.getSplitterLiteral(), numberOfDistinctDisequalsIsLessThanBoundsDifferenceStep.getContextSplittingWhenSplitterIsLiteral(), ifTrue, ifFalse);
            return result;
        }
        Expression numberOfDistinctDisequalsIsLessThanBoundsDifference = numberOfDistinctDisequalsIsLessThanBoundsDifferenceStep.getValue();
        sequelBase.initialNumberOfDistinctDisequalsIsLessThanBoundsDifferenceStepSolver = new ConstantExpressionStepSolver(numberOfDistinctDisequalsIsLessThanBoundsDifference);
        weKnowThatNumberOfDistinctDisequalsExceedsNumberOfValuesWithinBounds = numberOfDistinctDisequalsIsLessThanBoundsDifference.equals(FALSE);
        if (initialDistinctDisequalsStepSolver == null) {
            // if initialDistinctDisequalsStepSolver has not been set yet, it is because the predecessor of this step solver did not get to the point of using distinctExpressionsStepSolver; this means numberOfDistinctDisequalsIsLessThanBoundsDifferenceStepSolver is not a ConstantExpressionStepSolver (if it were, then the predecessor would have proceeded to use distinctExpressionsStepSolver), so it must be a NumberOfDistinctExpressionsIsLessThanStepSolver.
            distinctExpressionsStepSolver = ((NumberOfDistinctExpressionsIsLessThanStepSolver) numberOfDistinctDisequalsIsLessThanBoundsDifferenceStepSolver).getDistinctExpressionsStepSolver();
        } else {
            distinctExpressionsStepSolver = initialDistinctDisequalsStepSolver;
        }
    } else {
        weKnowThatNumberOfDistinctDisequalsExceedsNumberOfValuesWithinBounds = false;
        if (initialDistinctDisequalsStepSolver == null) {
            distinctExpressionsStepSolver = new DistinctExpressionsStepSolver(disequalsWithinBounds);
        } else {
            distinctExpressionsStepSolver = initialDistinctDisequalsStepSolver;
        }
    }
    Expression solutionExpression;
    if (weKnowThatNumberOfDistinctDisequalsExceedsNumberOfValuesWithinBounds) {
        // there are no available values left
        solutionExpression = getSolutionExpressionGivenContradiction();
    } else if (!getEquals().isEmpty()) {
        // if bound to a value
        solutionExpression = getSolutionExpressionForBoundVariable();
    } else {
        Step distinctDisequalsStep = distinctExpressionsStepSolver.step(context);
        if (distinctDisequalsStep.itDepends()) {
            AbstractSingleVariableDifferenceArithmeticConstraintFeasibilityRegionStepSolver ifTrue = makeSequelStepSolver(sequelBase);
            ifTrue.initialDistinctDisequalsStepSolver = (DistinctExpressionsStepSolver) distinctDisequalsStep.getStepSolverForWhenSplitterIsTrue();
            AbstractSingleVariableDifferenceArithmeticConstraintFeasibilityRegionStepSolver ifFalse = makeSequelStepSolver(sequelBase);
            ifFalse.initialDistinctDisequalsStepSolver = (DistinctExpressionsStepSolver) distinctDisequalsStep.getStepSolverForWhenSplitterIsFalse();
            ItDependsOn result = new ItDependsOn(distinctDisequalsStep.getSplitterLiteral(), distinctDisequalsStep.getContextSplittingWhenSplitterIsLiteral(), ifTrue, ifFalse);
            return result;
        }
        Expression distinctDisequalsExtensionalUniSet = distinctDisequalsStep.getValue();
        solutionExpression = getSolutionExpressionGivenBoundsAndDistinctDisequals(maximumLowerBound, minimumUpperBound, boundsDifference, distinctDisequalsExtensionalUniSet, context);
    }
    return new Solution(solutionExpression);
}
Also used : DistinctExpressionsStepSolver(com.sri.ai.grinder.sgdpllt.theory.equality.DistinctExpressionsStepSolver) ArrayList(java.util.ArrayList) SelectExpressionsSatisfyingComparisonStepSolver(com.sri.ai.grinder.sgdpllt.helper.SelectExpressionsSatisfyingComparisonStepSolver) DistinctExpressionsStepSolver(com.sri.ai.grinder.sgdpllt.theory.equality.DistinctExpressionsStepSolver) AbstractSingleVariableNumericConstraintFeasibilityRegionStepSolver(com.sri.ai.grinder.sgdpllt.theory.numeric.AbstractSingleVariableNumericConstraintFeasibilityRegionStepSolver) ConstantStepSolver(com.sri.ai.grinder.sgdpllt.theory.base.ConstantStepSolver) NumberOfDistinctExpressionsIsLessThanStepSolver(com.sri.ai.grinder.sgdpllt.theory.equality.NumberOfDistinctExpressionsIsLessThanStepSolver) SelectExpressionsSatisfyingComparisonStepSolver(com.sri.ai.grinder.sgdpllt.helper.SelectExpressionsSatisfyingComparisonStepSolver) StepSolver(com.sri.ai.grinder.sgdpllt.api.StepSolver) ConstantExpressionStepSolver(com.sri.ai.grinder.sgdpllt.theory.base.ConstantExpressionStepSolver) ExpressionLiteralSplitterStepSolver(com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver) NumberOfDistinctExpressionsIsLessThanStepSolver(com.sri.ai.grinder.sgdpllt.theory.equality.NumberOfDistinctExpressionsIsLessThanStepSolver) Expression(com.sri.ai.expresso.api.Expression) ExpressionLiteralSplitterStepSolver(com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver) ArrayList(java.util.ArrayList) Util.arrayList(com.sri.ai.util.Util.arrayList) List(java.util.List) ConstantExpressionStepSolver(com.sri.ai.grinder.sgdpllt.theory.base.ConstantExpressionStepSolver)

Example 2 with SelectExpressionsSatisfyingComparisonStepSolver

use of com.sri.ai.grinder.sgdpllt.helper.SelectExpressionsSatisfyingComparisonStepSolver in project aic-expresso by aic-sri-international.

the class SelectExpressionsSatisfyingComparisonStepSolverTest method runTest.

private void runTest(List<String> expressions, Expression bound, Expression expected, Context context) {
    StepSolver<List<Expression>> stepSolver = new SelectExpressionsSatisfyingComparisonStepSolver(mapIntoArrayList(expressions, Expressions::parse), LESS_THAN, bound);
    ExpressionWrapperStepSolver<List<Expression>> wrapInList = new ExpressionWrapperStepSolver<>(stepSolver, selection -> apply("list", selection));
    Expression solution = ContextDependentExpressionProblemSolver.staticSolve(wrapInList, context);
    System.out.println("Elements in " + expressions + " which are less than " + bound + ": " + solution);
    assertEquals(expected, solution);
}
Also used : Expression(com.sri.ai.expresso.api.Expression) SelectExpressionsSatisfyingComparisonStepSolver(com.sri.ai.grinder.sgdpllt.helper.SelectExpressionsSatisfyingComparisonStepSolver) Util.mapIntoArrayList(com.sri.ai.util.Util.mapIntoArrayList) List(java.util.List) ExpressionWrapperStepSolver(com.sri.ai.grinder.sgdpllt.theory.base.ExpressionWrapperStepSolver)

Aggregations

Expression (com.sri.ai.expresso.api.Expression)2 SelectExpressionsSatisfyingComparisonStepSolver (com.sri.ai.grinder.sgdpllt.helper.SelectExpressionsSatisfyingComparisonStepSolver)2 List (java.util.List)2 ExpressionLiteralSplitterStepSolver (com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver)1 StepSolver (com.sri.ai.grinder.sgdpllt.api.StepSolver)1 ConstantExpressionStepSolver (com.sri.ai.grinder.sgdpllt.theory.base.ConstantExpressionStepSolver)1 ConstantStepSolver (com.sri.ai.grinder.sgdpllt.theory.base.ConstantStepSolver)1 ExpressionWrapperStepSolver (com.sri.ai.grinder.sgdpllt.theory.base.ExpressionWrapperStepSolver)1 DistinctExpressionsStepSolver (com.sri.ai.grinder.sgdpllt.theory.equality.DistinctExpressionsStepSolver)1 NumberOfDistinctExpressionsIsLessThanStepSolver (com.sri.ai.grinder.sgdpllt.theory.equality.NumberOfDistinctExpressionsIsLessThanStepSolver)1 AbstractSingleVariableNumericConstraintFeasibilityRegionStepSolver (com.sri.ai.grinder.sgdpllt.theory.numeric.AbstractSingleVariableNumericConstraintFeasibilityRegionStepSolver)1 Util.arrayList (com.sri.ai.util.Util.arrayList)1 Util.mapIntoArrayList (com.sri.ai.util.Util.mapIntoArrayList)1 ArrayList (java.util.ArrayList)1