java.lang.Object
org.optaplanner.core.impl.heuristic.move.AbstractMove<Solution_>
org.optaplanner.core.impl.heuristic.selector.move.generic.list.ListSwapMove<Solution_>
Type Parameters:
Solution_ - the solution type, the class with the PlanningSolution annotation
All Implemented Interfaces:
Move<Solution_>

public class ListSwapMove<Solution_> extends AbstractMove<Solution_>
Swaps two elements of a list variable. Each element is identified by an entity instance and an index in that entity's list variable. The swap move has two sides called left and right. The element at leftIndex in leftEntity's list variable is replaced by the element at rightIndex in rightEntity's list variable and vice versa. Left and right entity can be the same instance.

An undo move is created by flipping the left and right-hand entity and index.

  • Constructor Details

    • ListSwapMove

      public ListSwapMove(ListVariableDescriptor<Solution_> variableDescriptor, Object leftEntity, int leftIndex, Object rightEntity, int rightIndex)
      Create a move that swaps a list variable element at leftEntity.listVariable[leftIndex] with rightEntity.listVariable[rightIndex].

      ListSwapMove anatomy

       
                                       / rightEntity
                      right element \  |   / rightIndex
                                    |  |   |
                     A {Ann[0]} <-> Y {Bob[1]}
                     |  |   |
        left element /  |   \ leftIndex
                        \ leftEntity
       
       

      Example

       
       GIVEN
       Ann.tasks = [A, B, C]
       Bob.tasks = [X, Y]
      
       WHEN
       ListSwapMove: A {Ann[0]} <-> Y {Bob[1]}
      
       THEN
       Ann.tasks = [Y, B, C]
       Bob.tasks = [X, A]
       
       
      Parameters:
      variableDescriptor - descriptor of a list variable, for example Employee.taskList
      leftEntity - together with leftIndex identifies the left element to be moved
      leftIndex - together with leftEntity identifies the left element to be moved
      rightEntity - together with rightIndex identifies the right element to be moved
      rightIndex - together with rightEntity identifies the right element to be moved
  • Method Details

    • getLeftEntity

      public Object getLeftEntity()
    • getLeftIndex

      public int getLeftIndex()
    • getRightEntity

      public Object getRightEntity()
    • getRightIndex

      public int getRightIndex()
    • getLeftValue

      public Object getLeftValue()
    • getRightValue

      public Object getRightValue()
    • isMoveDoable

      public boolean isMoveDoable(ScoreDirector<Solution_> scoreDirector)
      Description copied from interface: Move
      Called before a move is evaluated to decide whether the move can be done and evaluated. A Move is not doable if:
      • Either doing it would change nothing in the PlanningSolution.
      • Either it's simply not possible to do (for example due to built-in hard constraints).

      It is recommended to keep this method implementation simple: do not use it in an attempt to satisfy normal hard and soft constraints.

      Although you could also filter out non-doable moves in for example the MoveSelector or MoveListFactory, this is not needed as the Solver will do it for you.

      Parameters:
      scoreDirector - the ScoreDirector not yet modified by the move.
      Returns:
      true if the move achieves a change in the solution and the move is possible to do on the solution.
    • createUndoMove

      public ListSwapMove<Solution_> createUndoMove(ScoreDirector<Solution_> scoreDirector)
      Description copied from class: AbstractMove
      Called before the move is done, so the move can be evaluated and then be undone without resulting into a permanent change in the solution.
      Specified by:
      createUndoMove in class AbstractMove<Solution_>
      Parameters:
      scoreDirector - the ScoreDirector not yet modified by the move.
      Returns:
      an undoMove which does the exact opposite of this move.
    • doMoveOnGenuineVariables

      protected void doMoveOnGenuineVariables(ScoreDirector<Solution_> scoreDirector)
      Description copied from class: AbstractMove
      Specified by:
      doMoveOnGenuineVariables in class AbstractMove<Solution_>
      Parameters:
      scoreDirector - never null
    • rebase

      public ListSwapMove<Solution_> rebase(ScoreDirector<Solution_> destinationScoreDirector)
      Description copied from interface: Move
      Rebases a move from an origin ScoreDirector to another destination ScoreDirector which is usually on another Thread or JVM. The new move returned by this method translates the entities and problem facts to the destination PlanningSolution of the destination ScoreDirector, That destination PlanningSolution is a deep planning clone (or an even deeper clone) of the origin PlanningSolution that this move has been generated from.

      That new move does the exact same change as this move, resulting in the same PlanningSolution state, presuming that destination PlanningSolution was in the same state as the original PlanningSolution to begin with.

      Generally speaking, an implementation of this method iterates through every entity and fact instance in this move, translates each one to the destination ScoreDirector with ScoreDirector.lookUpWorkingObject(Object) and creates a new move instance of the same move type, using those translated instances.

      The destination PlanningSolution can be in a different state than the original PlanningSolution. So, rebasing can only depend on the identity of planning entities and planning facts, which is usually declared by a PlanningId on those classes. It must not depend on the state of the planning variables. One thread might rebase a move before, amid or after another thread does that same move instance.

      This method is thread-safe.

      Parameters:
      destinationScoreDirector - never null, the ScoreDirector.getWorkingSolution() that the new move should change the planning entity instances of.
      Returns:
      never null, a new move that does the same change as this move on another solution instance
    • getSimpleMoveTypeDescription

      public String getSimpleMoveTypeDescription()
      Description copied from interface: Move
      Describes the move type for statistical purposes. For example "ChangeMove(Process.computer)".

      The format is not formalized. Never parse the String returned by this method.

      Returns:
      never null
    • getPlanningEntities

      public Collection<Object> getPlanningEntities()
      Description copied from interface: Move
      Returns all planning entities that are being changed by this move. Required for AcceptorType.ENTITY_TABU.

      This method is only called after Move.doMove(ScoreDirector) (which might affect the return values).

      Duplicate entries in the returned Collection are best avoided. The returned Collection is recommended to be in a stable order. For example: use List or LinkedHashSet, but not HashSet.

      Returns:
      never null
    • getPlanningValues

      public Collection<Object> getPlanningValues()
      Description copied from interface: Move
      Returns all planning values that entities are being assigned to by this move. Required for AcceptorType.VALUE_TABU.

      This method is only called after Move.doMove(ScoreDirector) (which might affect the return values).

      Duplicate entries in the returned Collection are best avoided. The returned Collection is recommended to be in a stable order. For example: use List or LinkedHashSet, but not HashSet.

      Returns:
      never null
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object