org.cpsolver.ifs.model

## Class Variable<V extends Variable<V,T>,T extends Value<V,T>>

- java.lang.Object
- - org.cpsolver.ifs.model.Variable<V,T>

- Type Parameters:
    `V` - Variable
    `T` - Value
  All Implemented Interfaces:
    Comparable<V>
  Direct Known Subclasses:
    Activity, CSPVariable, Exam, Operation, Rectangle, TeachingRequest.Variable, VariableWithContext

      public class Variable<V extends Variable<V,T>,T extends Value<V,T>>
      extends Object
      implements Comparable<V>

  Generic variable.
  Besides a domain of values, a variable also contains information about assigned value, the value assigned in the best ever found solution and also the initial value (minimal perturbations problem). It also knows what constraints are associated with this variable and has a unique id.
  Version:
    IFS 1.3 (Iterative Forward Search)
    Copyright (C) 2006 - 2014 Tomáš Müller
    muller@unitime.org
    http://muller.unitime.org
    This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
    This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
    You should have received a copy of the GNU Lesser General Public License along with this library; if not see http://www.gnu.org/licenses/.
  See Also:
    `Value`, `Model`, `Solver`

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected long` | `iId`
    `protected int` | `iIndex`
    `protected T` | `iValue` Assigned value

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `Variable()` Constructor
    `Variable(T initialValue)` Constructor

  - ### Method Summary

    All Methods Instance Methods Concrete Methods Deprecated Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `addContstraint(Constraint<V,T> constraint)` Adds a constraint.
    `void` | `addVariableListener(VariableListener<T> listener)` Adds variable listener
    `void` | `assign(int iteration, T value)` Deprecated.
    `int` | `compareTo(V variable)`
    `List<Constraint<V,T>>` | `constraints()` Return the list of constraints associated with this variable
    `Map<V,List<Constraint<V,T>>>` | `constraintVariables()` Returns a table of all variables linked with this variable by a constraint.
    `boolean` | `equals(Object o)`
    `T` | `getAssignment()` Deprecated.
    `T` | `getAssignment(Assignment<V,T> assignment)` Returns current assignment
    `Value<V,T>[]` | `getAssignments()` Deprecated.
    `T` | `getBestAssignment()` Returns the value from the best ever found solution.
    `long` | `getBestAssignmentIteration()` Returns the iteration when the best value was assigned
    `String` | `getDescription()` Variable's description -- for printing purposes
    `<X> X` | `getExtra()` Extra information to which can be used by an extension (see `Extension`).
    `long` | `getId()` Unique id
    `int` | `getIndex()` Unique index of a variable that was assigned by `Model.addVariable(Variable)`.
    `T` | `getInitialAssignment()` Returns initial assignment
    `long` | `getLastIteration()` Deprecated.
    `Model<V,T>` | `getModel()` Model, the variable belong to
    `String` | `getName()` Variable's name -- for printing purposes
    `List<VariableListener<T>>` | `getVariableListeners()` Return variable listeners
    `List<Constraint<V,T>>` | `hardConstraints()` Return the list of hard constraints associated with this variable
    `boolean` | `hasAssignment()` Deprecated.
    `boolean` | `hasAssignment(Assignment<V,T> assignment)` Returns true if the variable is assigned
    `int` | `hashCode()`
    `boolean` | `hasInitialAssignment()` Returns true if the variable has an initial assignment
    `boolean` | `hasValues()` Deprecated.
    `void` | `removeContstraint(Constraint<V,T> constraint)` Removes a constraint.
    `void` | `removeInitialValue()` Permanently remove the initial value from the variable's domain -- for testing MPP
    `void` | `removeValue(long iteration, T value)` Permanently remove a value from variable's domain.
    `void` | `removeVariableListener(VariableListener<T> listener)` Removes variable listener
    `void` | `setAssignment(T value)` Deprecated.
    `void` | `setBestAssignment(T value, long iteration)` Sets variable's value of the best ever found solution.
    `<X> void` | `setExtra(X object)` Extra information to which can be used by an extension (see `Extension`).
    `void` | `setIndex(int index)` Unique index of a variable, only to be assigned by `Model.addVariable(Variable)`.
    `void` | `setInitialAssignment(T initialValue)` Sets initial assignment
    `void` | `setLastIteration(long iteration)` Deprecated.
    `void` | `setModel(Model<V,T> model)` Set the model to which the variable belongs to
    `protected void` | `setValues(List<T> values)` Sets the domain
    `List<Constraint<V,T>>` | `softConstraints()` Return the list of soft constraints associated with this variable
    `String` | `toString()`
    `void` | `unassign(int iteration)` Deprecated.
    `List<T>` | `values()` Deprecated.
    `List<T>` | `values(Assignment<V,T> assignment)` Variable's domain
    `void` | `variableAssigned(Assignment<V,T> assignment, long iteration, T value)` A value was assigned to this variable
    `void` | `variableUnassigned(Assignment<V,T> assignment, long iteration, T oldValue)` A value was unassigned from this variable

    - ### Methods inherited from class java.lang.Object

      `clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

- - ### Field Detail

    - #### iId

          protected long iId

    - #### iValue

          protected T extends Value<V,T> iValue

      Assigned value

    - #### iIndex

          protected int iIndex

  - ### Constructor Detail

    - #### Variable

          public Variable()

      Constructor

    - #### Variable

          public Variable(T initialValue)

      Constructor
      Parameters:
        `initialValue` - initial value (minimal-perturbation problem)

  - ### Method Detail

    - #### getModel

          public Model<V,T> getModel()

      Model, the variable belong to
      Returns:
        problem model

    - #### setModel

          public void setModel(Model<V,T> model)

      Set the model to which the variable belongs to
      Parameters:
        `model` - problem model

    - #### values

          @Deprecated
          public List<T> values()

      Deprecated.
      Variable's domain, use `values(Assignment)` instead.
      Returns:
        all possible values of this variable

    - #### values

          public List<T> values(Assignment<V,T> assignment)

      Variable's domain
      Parameters:
        `assignment` - current assignment (if the domain is dependent on the current assignment)
      Returns:
        all possible values of this variable

    - #### setValues

          protected void setValues(List<T> values)

      Sets the domain
      Parameters:
        `values` - variable's domain to cache

    - #### hasValues

          @Deprecated
          public boolean hasValues()

      Deprecated.
      True, if the variable's domain is not empty
      Returns:
        true if there is at least one value in the domain

    - #### getAssignment

          @Deprecated
          public T getAssignment()

      Deprecated.
      Returns current assignment. Use `Assignment.getValue(Variable)` or `getAssignment(Assignment)` instead.
      Returns:
        currently assigned value

    - #### hasAssignment

          @Deprecated
          public boolean hasAssignment()

      Deprecated.
      Returns true if the variable is assigned. Use `hasAssignment(Assignment)` instead.
      Returns:
        true if currently assigned

    - #### getAssignment

          public T getAssignment(Assignment<V,T> assignment)

      Returns current assignment
      Parameters:
        `assignment` - current assignment
      Returns:
        currently assigned value

    - #### hasAssignment

          public boolean hasAssignment(Assignment<V,T> assignment)

      Returns true if the variable is assigned
      Parameters:
        `assignment` - current assignment
      Returns:
        true if currently assigned

    - #### setAssignment

          @Deprecated
          public void setAssignment(T value)

      Deprecated.
      Sets current assignment. BEWARE: Do not use outside of `DefaultSingleAssignment`.
      Parameters:
        `value` - current assignment

    - #### getAssignments

          @Deprecated
          public Value<V,T>[] getAssignments()

      Deprecated.
      Returns current assignments. BEWARE: Do not use outside of `DefaultParallelAssignment`.
      Returns:
        currently assigned values

    - #### getInitialAssignment

          public T getInitialAssignment()

      Returns initial assignment
      Returns:
        initial assignment (for the minimal perturbation problem)

    - #### setInitialAssignment

          public void setInitialAssignment(T initialValue)

      Sets initial assignment
      Parameters:
        `initialValue` - initial assignment

    - #### hasInitialAssignment

          public boolean hasInitialAssignment()

      Returns true if the variable has an initial assignment
      Returns:
        true if this variable has an initial assignment

    - #### assign

          @Deprecated
          public void assign(int iteration,
                                         T value)

      Deprecated.
      Assign value to this variable. If the variable has already assigned another value, it is unassigned first. Also, all conflicting values are unassigned before the given value is assigned to this variable. Use `Assignment.assign(long, Value)` instead.
      Parameters:
        `iteration` - current iteration
        `value` - the value to be assigned

    - #### unassign

          @Deprecated
          public void unassign(int iteration)

      Deprecated.
      Unassign value from this variable. Use `Assignment.unassign(long, Variable)` instead.
      Parameters:
        `iteration` - current iteration

    - #### getLastIteration

          @Deprecated
          public long getLastIteration()

      Deprecated.
      Returns iteration of the last assignment. Use `Assignment.getIteration(Variable)` instead.
      Returns:
        iteration of the last assignment

    - #### setLastIteration

          @Deprecated
          public void setLastIteration(long iteration)

      Deprecated.
      Sets iteration of the last assignment. BEWARE: Do not use outside of `DefaultSingleAssignment`.
      Parameters:
        `iteration` - current iteration

    - #### variableAssigned

          public void variableAssigned(Assignment<V,T> assignment,
                                       long iteration,
                                       T value)

      A value was assigned to this variable
      Parameters:
        `assignment` - current assignment
        `iteration` - current iteration
        `value` - assigned value

    - #### variableUnassigned

          public void variableUnassigned(Assignment<V,T> assignment,
                                         long iteration,
                                         T oldValue)

      A value was unassigned from this variable
      Parameters:
        `assignment` - current assignment
        `iteration` - current iteration
        `oldValue` - unassigned value

    - #### addContstraint

          public void addContstraint(Constraint<V,T> constraint)

      Adds a constraint. Called automatically when the constraint is added to the model, i.e., `Model.addConstraint(Constraint)` is called.
      Parameters:
        `constraint` - added constraint

    - #### removeContstraint

          public void removeContstraint(Constraint<V,T> constraint)

      Removes a constraint. Called automatically when the constraint is removed from the model, i.e., `Model.removeConstraint(Constraint)` is called.
      Parameters:
        `constraint` - added constraint

    - #### constraints

          public List<Constraint<V,T>> constraints()

      Return the list of constraints associated with this variable
      Returns:
        list of constraints associated with this variable

    - #### hardConstraints

          public List<Constraint<V,T>> hardConstraints()

      Return the list of hard constraints associated with this variable
      Returns:
        list of hard constraints associated with this variable

    - #### softConstraints

          public List<Constraint<V,T>> softConstraints()

      Return the list of soft constraints associated with this variable
      Returns:
        list of soft (not hard) constraints associated with this variable

    - #### toString

          public String toString()

      Overrides:
        `toString` in class `Object`

    - #### getId

          public long getId()

      Unique id
      Returns:
        variable id

    - #### hashCode

          public int hashCode()

      Overrides:
        `hashCode` in class `Object`

    - #### getName

          public String getName()

      Variable's name -- for printing purposes
      Returns:
        variable name

    - #### getDescription

          public String getDescription()

      Variable's description -- for printing purposes
      Returns:
        variable description

    - #### setBestAssignment

          public void setBestAssignment(T value,
                                        long iteration)

      Sets variable's value of the best ever found solution. Called when `Model.saveBest(Assignment)` is called.
      Parameters:
        `value` - a value
        `iteration` - value's assignment iteration

    - #### getBestAssignment

          public T getBestAssignment()

      Returns the value from the best ever found solution.
      Returns:
        best assignment

    - #### getBestAssignmentIteration

          public long getBestAssignmentIteration()

      Returns the iteration when the best value was assigned
      Returns:
        iteration of the best assignment

    - #### compareTo

          public int compareTo(V variable)

      Specified by:
        `compareTo` in interface `Comparable<V extends Variable<V,T>>`

    - #### equals

          public boolean equals(Object o)

      Overrides:
        `equals` in class `Object`

    - #### addVariableListener

          public void addVariableListener(VariableListener<T> listener)

      Adds variable listener
      Parameters:
        `listener` - a variable listener

    - #### removeVariableListener

          public void removeVariableListener(VariableListener<T> listener)

      Removes variable listener
      Parameters:
        `listener` - a variable listener

    - #### getVariableListeners

          public List<VariableListener<T>> getVariableListeners()

      Return variable listeners
      Returns:
        list of variable listeners

    - #### setExtra

          public <X> void setExtra(X object)

      Extra information to which can be used by an extension (see `Extension`).
      Parameters:
        `object` - extra object

    - #### getExtra

          public <X> X getExtra()

      Extra information to which can be used by an extension (see `Extension`).
      Returns:
        extra object

    - #### removeValue

          public void removeValue(long iteration,
                                  T value)

      Permanently remove a value from variable's domain. The variable should not have this value assigned in any existing assignment.
      Parameters:
        `iteration` - current iteration
        `value` - value to be removed from this variable's domain

    - #### constraintVariables

          public Map<V,List<Constraint<V,T>>> constraintVariables()

      Returns a table of all variables linked with this variable by a constraint.
      Returns:
        table (variable, constraint)

    - #### removeInitialValue

          public void removeInitialValue()

      Permanently remove the initial value from the variable's domain -- for testing MPP

    - #### setIndex

          public void setIndex(int index)

      Unique index of a variable, only to be assigned by `Model.addVariable(Variable)`.
      Parameters:
        `index` - an index

    - #### getIndex

          public int getIndex()

      Unique index of a variable that was assigned by `Model.addVariable(Variable)`.
      Returns:
        -1 if not in a model