GreaterEqual.java

  1. /* Copyright 2015 Laurent COCAULT
  2.  * Licensed to Laurent COCAULT under one or more contributor license agreements.
  3.  * See the NOTICE file distributed with this work for additional information
  4.  * regarding copyright ownership. Laurent COCAULT licenses this file to You
  5.  * under the Apache License, Version 2.0 (the "License"); you may not use this
  6.  * file except in compliance with the License.  You may obtain a copy of the
  7.  * License at
  8.  *
  9.  *   http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.csp.constraint.model.integer;

  18. /**
  19.  * Constraint specifying that one specific integer variable is greater than or
  20.  * equal to another variable augmented by a constant delta.
  21.  */
  22. public class GreaterEqual extends BinaryIntConstraint {

  23.     /**
  24.      * Constructor of the constraint "LEFT >= RIGHT + DELTA".
  25.      * @param left
  26.      *            Left part of the comparison
  27.      * @param right
  28.      *            Right part of the comparison
  29.      * @param delta
  30.      *            Delta added to the right part
  31.      */
  32.     public GreaterEqual(final IntVar left, final IntVar right,
  33.             final IntValue delta) {
  34.         super(left.getName() + ">=" + right.getName() + "+" + delta, left,
  35.                 right, delta);
  36.     }

  37.     /**
  38.      * {@inheritDoc}
  39.      */
  40.     @Override
  41.     public void propagate() {

  42.         // Propagate on the left part
  43.         final IntValue newMin = IntValue.sum(getRight().getMinValue(),
  44.                 getDelta());
  45.         final boolean leftChanged = getLeft().reduceWithMinValue(newMin);

  46.         // Propagate on the right part
  47.         final IntValue newMax = IntValue.diff(getLeft().getMaxValue(),
  48.                 getDelta());
  49.         final boolean rightChanged = getRight().reduceWithMaxValue(newMax);

  50.         // Update the list of changed variables
  51.         if (leftChanged) {
  52.             addRecentChangedVariable(getLeft());
  53.         }
  54.         if (rightChanged) {
  55.             addRecentChangedVariable(getRight());
  56.         }
  57.     }

  58.     /**
  59.      * Get the left argument of the constraint.
  60.      * @return First argument
  61.      */
  62.     protected IntVar getLeft() {
  63.         return (IntVar) getFirstVariable();
  64.     }

  65.     /**
  66.      * Get the right argument of the constraint.
  67.      * @return Second argument
  68.      */
  69.     protected IntVar getRight() {
  70.         return (IntVar) getSecondVariable();
  71.     }

  72. }