EqualToMaxOfIntArray.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. import java.util.ArrayList;
  19. import java.util.Iterator;
  20. import java.util.List;

  21. import org.csp.constraint.model.UnsizedConstraint;
  22. import org.csp.constraint.model.Variable;

  23. /**
  24.  * Constraint specifying that one specific integer variable is the maximum of
  25.  * the integer variables of a given array.
  26.  */
  27. public class EqualToMaxOfIntArray extends UnsizedConstraint<IntValue> {

  28.     /** Array of variables. */
  29.     private List<IntVar> array_;

  30.     /** Maximum. */
  31.     private IntVar max_;

  32.     /**
  33.      * Constructor.
  34.      * @param max
  35.      *            Maximum of the array
  36.      * @param array
  37.      *            Array of variables
  38.      */
  39.     public EqualToMaxOfIntArray(final IntVar max, final List<IntVar> array) {
  40.         super(max.getName() + "=MAX" + array,
  41.                 new ArrayList<Variable<IntValue>>());
  42.         // Add the values of the array and the maximum to the array of variables
  43.         for (IntVar variable : array) {
  44.             addVariable(variable);
  45.         }
  46.         addVariable(max);
  47.         // Initialize attributes
  48.         max_ = max;
  49.         array_ = array;
  50.     }

  51.     /**
  52.      * {@inheritDoc}
  53.      */
  54.     @Override
  55.     public void propagate() {

  56.         // Modification indicator
  57.         boolean minModif = false;
  58.         boolean maxModif = false;

  59.         // New min and max for each variable (initialized with the absolute
  60.         // minimum value since the max function is applied on it)
  61.         IntValue newMin = IntValue.MIN_INT_VALUE;
  62.         IntValue newMax = IntValue.MIN_INT_VALUE;
  63.         // Variables iterator
  64.         Iterator<IntVar> var = array_.iterator();

  65.         // Computes the minimum and maximum values of all the values of the
  66.         // right hand
  67.         while (var.hasNext()) {
  68.             final IntVar variable = var.next();
  69.             if (!variable.isDomainEmpty()) {
  70.                 // New minimum
  71.                 if (newMin.compareTo(variable.getMinValue()) < 0) {
  72.                     newMin = variable.getMinValue();
  73.                 }
  74.                 // New maximum
  75.                 if (newMax.compareTo(variable.getMaxValue()) < 0) {
  76.                     newMax = variable.getMaxValue();
  77.                 }
  78.             }
  79.         }

  80.         // Reduce the maximum variable
  81.         minModif = max_.reduceWithMinValue(newMin);
  82.         maxModif = max_.reduceWithMaxValue(newMax);
  83.         if (minModif || maxModif) {
  84.             addRecentChangedVariable(max_);
  85.         }

  86.         // Reduce the array of variables
  87.         var = array_.iterator();
  88.         while (var.hasNext()) {
  89.             // Propagate on each variable
  90.             final IntVar variable = var.next();
  91.             if (!max_.isDomainEmpty()) {
  92.                 // Reduce the domain
  93.                 if (variable.reduceWithMaxValue(max_.getMaxValue())) {
  94.                     addRecentChangedVariable(variable);
  95.                 }
  96.             }
  97.         }
  98.     }

  99.     /**
  100.      * {@inheritDoc}
  101.      */
  102.     @Override
  103.     public String toString() {

  104.         // String builder
  105.         final StringBuilder builder = new StringBuilder();

  106.         // Display the constraint
  107.         builder.append(max_.getName());
  108.         builder.append("=MAX(");
  109.         final Iterator<IntVar> vars = array_.iterator();
  110.         while (vars.hasNext()) {
  111.             // Display every variable
  112.             builder.append(vars.next().getName());
  113.             if (vars.hasNext()) {
  114.                 // Next variable
  115.                 builder.append(", ");
  116.             } else {
  117.                 // Last variable
  118.                 builder.append(")");
  119.             }
  120.         }

  121.         return builder.toString();
  122.     }

  123. }