org.cpsolver.ifs.assignment

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

- java.lang.Object
- - org.cpsolver.ifs.assignment.AssignmentAbstract<V,T>

- Type Parameters:
    `V` - Variable
    `T` - Value
  All Implemented Interfaces:
    Assignment<V,T>
  Direct Known Subclasses:
    AssignmentArray, AssignmentMap, DefaultParallelAssignment, DefaultSingleAssignment, EmptyAssignment, OptimisticInheritedAssignment

      public abstract class AssignmentAbstract<V extends Variable<V,T>,T extends Value<V,T>>
      extends Object
      implements Assignment<V,T>

  An abstract implementation of an `Assignment` object. It contains an instance of a given assignment context holder (see `AssignmentContextHolder`) and implements the assignment logic. But the actual process of storing and retrieving values is left on the assignment implementation.
  Version:
    IFS 1.3 (Iterative Forward Search)
    Copyright (C) 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.
  Author:
    Tomáš Müller
  See Also:
    `Assignment`

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected AssignmentContextHolder<V,T>` | `iContexts`
    `protected boolean` | `iHasInitialzedContext`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `AssignmentAbstract(AssignmentContextHolder<V,T> contexts)` Constructor

  - ### Method Summary

    All Methods Instance Methods Abstract Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `T` | `assign(long iteration, T value)` Assign the given value to its variable.
    `protected T` | `assign(long iteration, V variable, T value)` Assigns a variable with the given value.
    `Collection<T>` | `assignedValues()` The list of assigned values in the assignment.
    `<C extends AssignmentContext> void` | `clearContext(AssignmentContextReference<V,T,C> reference)` Clear an assignment context that is associated with the given a reference.
    `protected void` | `ensureInitializedContext(V variable)` Ensure that the model, all criteria, all global constraints and all the related constraints have their assignment contexts initialized.
    `<C extends AssignmentContext> C` | `getAssignmentContext(AssignmentContextReference<V,T,C> reference)` Assignment context for a reference.
    `int` | `getIndex()` Assignment index.
    `T` | `getValue(V variable)` Checks if the variable is `ConstantVariable`, returns `getValueInternal(Variable)` if the variable is not a constant.
    `protected abstract T` | `getValueInternal(V variable)` Returns assignment of a variable, null if not assigned.
    `int` | `nrAssignedVariables()` Number of assigned variables in this assignment.
    `int` | `nrUnassignedVariables(Model<V,T> model)` Number of assigned variables in the assignment.
    `protected abstract void` | `setValueInternal(long iteration, V variable, T value)` Sets an assignment to a variable (unassigns a variable if the given value is null).
    `T` | `unassign(long iteration, V variable)` Unassign the given variable.
    `T` | `unassign(long iteration, V variable, T value)` Unassign the given variable, but only if the current assignment differs from the given value.
    `Collection<V>` | `unassignedVariables(Model<V,T> model)` The list of variables of the model that have no value in this assignment.

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

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

    - ### Methods inherited from interface org.cpsolver.ifs.assignment.Assignment

      `assignedVariables, getIteration`

- - ### Field Detail

    - #### iContexts

          protected AssignmentContextHolder<V extends Variable<V,T>,T extends Value<V,T>> iContexts

    - #### iHasInitialzedContext

          protected boolean iHasInitialzedContext

  - ### Constructor Detail

    - #### AssignmentAbstract

          public AssignmentAbstract(AssignmentContextHolder<V,T> contexts)

      Constructor
      Parameters:
        `contexts` - an instance of the assignment context holder

  - ### Method Detail

    - #### getValue

          public T getValue(V variable)

      Checks if the variable is `ConstantVariable`, returns `getValueInternal(Variable)` if the variable is not a constant.
      Specified by:
        `getValue` in interface `Assignment<V extends Variable<V,T>,T extends Value<V,T>>`
      Parameters:
        `variable` - problem variable
      Returns:
        currently assigned value

    - #### getValueInternal

          protected abstract T getValueInternal(V variable)

      Returns assignment of a variable, null if not assigned. To be implemented.
      Parameters:
        `variable` - a variable in question
      Returns:
        assigned value

    - #### setValueInternal

          protected abstract void setValueInternal(long iteration,
                                                   V variable,
                                                   T value)

      Sets an assignment to a variable (unassigns a variable if the given value is null). To be implemented.
      Parameters:
        `iteration` - current iteration
        `variable` - a variable to be assigned
        `value` - new assignment, null if to be unassigned

    - #### assign

          protected T assign(long iteration,
                             V variable,
                             T value)

      Assigns a variable with the given value. All the appropriate classes are notified about the change. It is using `setValueInternal(long, Variable, Value)` to store the new assignment.
      Parameters:
        `iteration` - current iteration
        `variable` - a variable
        `value` - one of its values, null if the variable is to be unassigned
      Returns:
        previous assignment of the variable

    - #### assign

          public T assign(long iteration,
                          T value)

      Description copied from interface: `Assignment`
      Assign the given value to its variable. Replacement for `Variable.assign(int, Value)`.
      Specified by:
        `assign` in interface `Assignment<V extends Variable<V,T>,T extends Value<V,T>>`
      Parameters:
        `iteration` - current iteration
        `value` - a new value to be assigned to variable `Value.variable()`.
      Returns:
        previous assignment of the variable, null if it was not assigned

    - #### unassign

          public T unassign(long iteration,
                            V variable)

      Description copied from interface: `Assignment`
      Unassign the given variable. Replacement for `Variable.unassign(int)`.
      Specified by:
        `unassign` in interface `Assignment<V extends Variable<V,T>,T extends Value<V,T>>`
      Parameters:
        `iteration` - current iteration
        `variable` - variable to be unassigned
      Returns:
        previous assignment of the variable, null if it was not assigned

    - #### unassign

          public T unassign(long iteration,
                            V variable,
                            T value)

      Description copied from interface: `Assignment`
      Unassign the given variable, but only if the current assignment differs from the given value. Replacement for `Variable.unassign(int)`.
      Specified by:
        `unassign` in interface `Assignment<V extends Variable<V,T>,T extends Value<V,T>>`
      Parameters:
        `iteration` - current iteration
        `variable` - variable to be unassigned
        `value` - target value
      Returns:
        previous assignment of the variable, null if it was not assigned

    - #### nrAssignedVariables

          public int nrAssignedVariables()

      Description copied from interface: `Assignment`
      Number of assigned variables in this assignment. Replacement for `Model.nrAssignedVariables()`.
      Specified by:
        `nrAssignedVariables` in interface `Assignment<V extends Variable<V,T>,T extends Value<V,T>>`
      Returns:
        number of assigned variables in this assignment

    - #### assignedValues

          public Collection<T> assignedValues()

      Description copied from interface: `Assignment`
      The list of assigned values in the assignment. That is a collection of `Assignment.getValue(Variable)` for all assigned variables in this assignment.
      Specified by:
        `assignedValues` in interface `Assignment<V extends Variable<V,T>,T extends Value<V,T>>`
      Returns:
        a collection of assigned values in this assignment

    - #### unassignedVariables

          public Collection<V> unassignedVariables(Model<V,T> model)

      Description copied from interface: `Assignment`
      The list of variables of the model that have no value in this assignment. That is all the variables of the model that `Assignment.getValue(Variable)` is null in this assignment. Replacement for `Model.unassignedVariables()`
      Specified by:
        `unassignedVariables` in interface `Assignment<V extends Variable<V,T>,T extends Value<V,T>>`
      Parameters:
        `model` - existing model (the assignment does not keep track about all existing variables, that is what the `Model.variables()` is for)
      Returns:
        a collection of all not assigned variables in this assignment

    - #### nrUnassignedVariables

          public int nrUnassignedVariables(Model<V,T> model)

      Description copied from interface: `Assignment`
      Number of assigned variables in the assignment. Replacement for `Model.nrUnassignedVariables()`.
      Specified by:
        `nrUnassignedVariables` in interface `Assignment<V extends Variable<V,T>,T extends Value<V,T>>`
      Parameters:
        `model` - existing model (the assignment does not keep track about all existing variables, that is what the `Model.variables()` is for)
      Returns:
        number of not assigned variables in this assignment

    - #### getAssignmentContext

          public <C extends AssignmentContext> C getAssignmentContext(AssignmentContextReference<V,T,C> reference)

      Description copied from interface: `Assignment`
      Assignment context for a reference. This can be used to keep assignment dependent computations (e.g., see `ConstraintWithContext`).
      Specified by:
        `getAssignmentContext` in interface `Assignment<V extends Variable<V,T>,T extends Value<V,T>>`
      Type Parameters:
        `C` - assignment context type
      Parameters:
        `reference` - a reference (which can be stored within the model, e.g., as an instance variable of a constraint)
      Returns:
        an `AssignmentContext`

    - #### clearContext

          public <C extends AssignmentContext> void clearContext(AssignmentContextReference<V,T,C> reference)

      Description copied from interface: `Assignment`
      Clear an assignment context that is associated with the given a reference. If there is any created for the reference.
      Specified by:
        `clearContext` in interface `Assignment<V extends Variable<V,T>,T extends Value<V,T>>`
      Type Parameters:
        `C` - assignment context type
      Parameters:
        `reference` - a reference (which can be stored within the model, e.g., as an instance variable of a constraint)

    - #### getIndex

          public int getIndex()

      Description copied from interface: `Assignment`
      Assignment index. Unique identification of the assignment, starting with zero. This can be used to increase the speed of the search by storing individual values on the variables and an array (indexed by this number).
      Specified by:
        `getIndex` in interface `Assignment<V extends Variable<V,T>,T extends Value<V,T>>`
      Returns:
        assignment index, -1 if not to be used

    - #### ensureInitializedContext

          protected void ensureInitializedContext(V variable)

      Ensure that the model, all criteria, all global constraints and all the related constraints have their assignment contexts initialized.
      Parameters:
        `variable` - a variable to be changed