Class ListChangeMove<Solution_,Entity_,Value_>

java.lang.Object
ai.timefold.solver.core.impl.move.AbstractMove<Solution_>
ai.timefold.solver.core.preview.api.move.builtin.ListChangeMove<Solution_,Entity_,Value_>
Type Parameters:
Solution_ - the solution type, the class with the PlanningSolution annotation
Entity_ - the entity type, the class with the PlanningEntity annotation
Value_ - the variable type, the type of the property with the PlanningVariable annotation
All Implemented Interfaces:
Move<Solution_>

@NullMarked public class ListChangeMove<Solution_,Entity_,Value_> extends AbstractMove<Solution_>
Moves an element of a list variable. The moved element is identified by an entity instance and a position in that entity's list variable. The element is inserted at the given index in the given destination entity's list variable.

An undo move is simply created by flipping the source and destination entity+index.

  • Constructor Details

    • ListChangeMove

      protected ListChangeMove(PlanningListVariableMetaModel<Solution_,Entity_,Value_> variableMetaModel, Entity_ sourceEntity, int sourceIndex, Entity_ destinationEntity, int destinationIndex)
      The move removes a planning value element from sourceEntity.listVariable[sourceIndex] and inserts the planning value at destinationEntity.listVariable[destinationIndex].

      ListChangeMove anatomy

       
                                   / destinationEntity
                                   |   / destinationIndex
                                   |   |
                      A {Ann[0]}->{Bob[2]}
                      |  |   |
       planning value /  |   \ sourceIndex
                         \ sourceEntity
       
       

      Example 1 - source and destination entities are different

       
       GIVEN
       Ann.tasks = [A, B, C]
       Bob.tasks = [X, Y]
      
       WHEN
       ListChangeMove: A {Ann[0]->Bob[2]}
      
       THEN
       Ann.tasks = [B, C]
       Bob.tasks = [X, Y, A]
       
       

      Example 2 - source and destination is the same entity

       
       GIVEN
       Ann.tasks = [A, B, C]
      
       WHEN
       ListChangeMove: A {Ann[0]->Ann[2]}
      
       THEN
       Ann.tasks = [B, C, A]
       
       
      Parameters:
      variableMetaModel - never null
      sourceEntity - planning entity instance from which a planning value will be removed, for example "Ann"
      sourceIndex - index in sourceEntity's list variable from which a planning value will be removed
      destinationEntity - planning entity instance to which a planning value will be moved, for example "Bob"
      destinationIndex - index in destinationEntity's list variable where the moved planning value will be inserted
  • Method Details

    • execute

      public void execute(MutableSolutionView<Solution_> solutionView)
      Description copied from interface: Move
      Runs the move and optionally records the changes done, so that they can be undone later.
      Parameters:
      solutionView - Exposes all possible mutative operations on the variables. Remembers those mutative operations and can replay them in reverse order when the solver needs to undo the move. Do not store this parameter in a field.
    • rebase

      public ListChangeMove<Solution_,Entity_,Value_> rebase(Rebaser rebaser)
      Description copied from interface: Move
      Rebases a move from an origin ScoreDirector to another destination ScoreDirector which is usually on another Thread. It is necessary for multithreaded solving to function.

      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.

      An implementation of this method typically iterates through every entity and fact instance in this move, translates each one to the destination ScoreDirector with Rebaser.rebase(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 problem facts, which are 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.

      The default implementation throws an UnsupportedOperationException, making multithreaded solving impossible unless the move class implements this method.

      Parameters:
      rebaser - Do not store this parameter in a field
      Returns:
      New move that does the same change as this move on another solution instance
    • getPlanningEntities

      public Collection<Entity_> getPlanningEntities()
      Description copied from interface: Move
      Returns all planning entities that this move is changing. Required for entity tabu.

      This method is only called after Move.execute(MutableSolutionView), 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.

      The default implementation throws an UnsupportedOperationException, making tabu search impossible unless the move class implements this method.

      Returns:
      Each entity only once.
    • getPlanningValues

      public Collection<Value_> getPlanningValues()
      Description copied from interface: Move
      Returns all planning values that this move is assigning to entity variables. Required for value tabu.

      This method is only called after Move.execute(MutableSolutionView), 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.

      The default implementation throws an UnsupportedOperationException, making tabu search impossible unless the move class implements this method.

      Returns:
      Each value only once. May contain null.
    • variableMetaModels

      Specified by:
      variableMetaModels in class AbstractMove<Solution_>
    • getSourceEntity

      public Entity_ getSourceEntity()
    • getSourceIndex

      public int getSourceIndex()
    • getDestinationEntity

      public Entity_ getDestinationEntity()
    • getDestinationIndex

      public int getDestinationIndex()
    • equals

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

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

      public String toString()
      Specified by:
      toString in class AbstractMove<Solution_>