org.cpsolver.ifs.algorithms.neighbourhoods

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

- java.lang.Object
- - org.cpsolver.ifs.algorithms.neighbourhoods.RandomSwapMove<V,T>

- Type Parameters:
    `V` - Variable
    `T` - Value
  All Implemented Interfaces:
    HillClimberSelection, NeighbourSelection<V,T>
  Direct Known Subclasses:
    RoomSwap, SuggestionMove, TimeSwap

      public class RandomSwapMove<V extends Variable<V,T>,T extends Value<V,T>>
      extends Object
      implements NeighbourSelection<V,T>, HillClimberSelection

  Try to assign a variable with a new value. A variable is selected randomly, a different value is randomly selected for the variable -- the variable is assigned with the new value. If there is a conflict, it tries to resolve these conflicts by assigning conflicting variables to other values as well.
  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

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `class` | `RandomSwapMove.SwapNeighbour`

  - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected boolean` | `iHC`
    `protected int` | `iMaxAttempts`
    `protected int` | `iTimeLimit`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `RandomSwapMove(DataProperties config)`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `init(Solver<V,T> solver)` Criterion initialization
    `protected boolean` | `isTimeLimitReached(long startTime)` Return true if the time limit was reached, number of attempts are limited to 1 in such a case.
    `protected Double` | `resolve(Solution<V,T> solution, double total, long startTime, Map<V,T> assignments, List<T> conflicts, int index)` Try to resolve given conflicts.
    `Neighbour<V,T>` | `selectNeighbour(Solution<V,T> solution)` select a neighbour of a given solution
    `void` | `setHcMode(boolean hcMode)` True if employed by a hill climber, e.g., worsening moves may be skipped.

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

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

- - ### Field Detail

    - #### iMaxAttempts

          protected int iMaxAttempts

    - #### iHC

          protected boolean iHC

    - #### iTimeLimit

          protected int iTimeLimit

  - ### Constructor Detail

    - #### RandomSwapMove

          public RandomSwapMove(DataProperties config)

  - ### Method Detail

    - #### setHcMode

          public void setHcMode(boolean hcMode)

      Description copied from interface: `HillClimberSelection`
      True if employed by a hill climber, e.g., worsening moves may be skipped.
      Specified by:
        `setHcMode` in interface `HillClimberSelection`
      Parameters:
        `hcMode` - true if used by a hill climber (worsening moves are automatically discarded)

    - #### init

          public void init(Solver<V,T> solver)

      Description copied from interface: `NeighbourSelection`
      Criterion initialization
      Specified by:
        `init` in interface `NeighbourSelection<V extends Variable<V,T>,T extends Value<V,T>>`
      Parameters:
        `solver` - current solver

    - #### selectNeighbour

          public Neighbour<V,T> selectNeighbour(Solution<V,T> solution)

      Description copied from interface: `NeighbourSelection`
      select a neighbour of a given solution
      Specified by:
        `selectNeighbour` in interface `NeighbourSelection<V extends Variable<V,T>,T extends Value<V,T>>`
      Parameters:
        `solution` - given solution
      Returns:
        a neighbour assignment

    - #### isTimeLimitReached

          protected boolean isTimeLimitReached(long startTime)

      Return true if the time limit was reached, number of attempts are limited to 1 in such a case.
      Parameters:
        `startTime` - start time
      Returns:
        true if the given time limit was reached

    - #### resolve

          protected Double resolve(Solution<V,T> solution,
                                   double total,
                                   long startTime,
                                   Map<V,T> assignments,
                                   List<T> conflicts,
                                   int index)

      Try to resolve given conflicts. For each conflicting variable it tries to find a value with no conflict that is compatible with some other assignment of the other conflicting variables.
      Parameters:
        `solution` - current solution
        `total` - original value of the current solution
        `startTime` - starting time
        `assignments` - re-assignments to be made
        `conflicts` - list of conflicts to resolve
        `index` - index in the list of conflicts
      Returns:
        value of the modified solution, null if cannot be resolved