IntVar.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 org.csp.constraint.model.Domain;
  19. import org.csp.constraint.model.IntervalDomain;
  20. import org.csp.constraint.model.Variable;

  21. /**
  22.  * Represents an integer variable of a constraint solving problem.
  23.  */
  24. public class IntVar extends Variable<IntValue> {

  25.     /**
  26.      * Constructor.
  27.      * @param minimum
  28.      *            Minimum possible integer value for the variable
  29.      * @param maximum
  30.      *            Maximum possible integer value for the variable
  31.      * @param name
  32.      *            Name of the variable
  33.      */
  34.     public IntVar(final int minimum, final int maximum, final String name) {
  35.         super(name);

  36.         // Domain
  37.         final IntValue min = new IntValue(minimum);
  38.         final IntValue max = new IntValue(maximum);
  39.         final Domain<IntValue> domain = new IntervalDomain<IntValue>(min, max);
  40.         addDomain(domain);

  41.         // Flags corresponding to the domain
  42.         addDomainChange(false);
  43.         addSelectionFlag(true);
  44.     }

  45.     /**
  46.      * Constructor.
  47.      * @param name
  48.      *            Name of the variable
  49.      */
  50.     public IntVar(final String name) {
  51.         super(name);

  52.         // Domain
  53.         final IntValue min = IntValue.MIN_INT_VALUE;
  54.         final IntValue max = IntValue.MAX_INT_VALUE;
  55.         final Domain<IntValue> domain = new IntervalDomain<IntValue>(min, max);
  56.         addDomain(domain);

  57.         // Flags corresponding to the domain
  58.         addDomainChange(false);
  59.         addSelectionFlag(true);
  60.     }

  61.     /**
  62.      * Get the integer interval domain.
  63.      * @return Domain for integer values
  64.      */
  65.     private IntervalDomain<IntValue> getIntDomain() {
  66.         return (IntervalDomain<IntValue>) getDomain();
  67.     }

  68.     /**
  69.      * Get the maximum value.
  70.      * @return Maximum value of the integer variable
  71.      */
  72.     public IntValue getMaxValue() {
  73.         return getIntDomain().getMaxValue();
  74.     }

  75.     /**
  76.      * Get the minimum value.
  77.      * @return Minimum value of the integer variable
  78.      */
  79.     public IntValue getMinValue() {
  80.         return getIntDomain().getMinValue();
  81.     }

  82.     /**
  83.      * Reduce the domain with a new maximum value.
  84.      * @param newMax
  85.      *            New maximum value
  86.      * @return "true" if the domain has been reduced
  87.      */
  88.     public boolean reduceWithMaxValue(final IntValue newMax) {
  89.         return setDomainChanged(getIntDomain().reduceWithMaxValue(newMax));
  90.     }

  91.     /**
  92.      * Reduce the domain with a new minimum value.
  93.      * @param newMin
  94.      *            New minimum value
  95.      * @return "true" if the domain has been reduced
  96.      */
  97.     public boolean reduceWithMinValue(final IntValue newMin) {
  98.         return setDomainChanged(getIntDomain().reduceWithMinValue(newMin));
  99.     }

  100. }