net.sf.cpsolver.ifs.algorithms

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

- java.lang.Object
- - net.sf.cpsolver.ifs.algorithms.NeighbourSearch<V,T>

- All Implemented Interfaces:
    NeighbourSelection<V,T>, LazyNeighbour.LazyNeighbourAcceptanceCriterion<V,T>, SolutionListener<V,T>
  Direct Known Subclasses:
    GreatDeluge, HillClimber, SimulatedAnnealing

      public abstract class NeighbourSearch<V extends Variable<V,T>,T extends Value<V,T>>
      extends Object
      implements NeighbourSelection<V,T>, SolutionListener<V,T>, LazyNeighbour.LazyNeighbourAcceptanceCriterion<V,T>

  Base class for the search techniques like hill climber, great deluge, or simulated annealing. It implements the `SolutionListener` and the variable neighbourhood selection.
  Version:
    IFS 1.2 (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/.

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected DecimalFormat` | `iDF2`
    `protected int` | `iIter`
    `protected Logger` | `iLog`
    `protected String` | `iPhase`
    `protected Progress` | `iProgress`
    `protected long` | `iT0`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `NeighbourSearch(DataProperties properties)`

  - ### Method Summary

    All Methods Instance Methods Abstract Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `boolean` | `accept(LazyNeighbour<V,T> neighbour, double value)` Accept lazy neighbour -- calling the acceptance criterion with lazy = true.
    `protected abstract boolean` | `accept(Model<V,T> model, Neighbour<V,T> neighbour, double value, boolean lazy)` Acceptance criterion.
    `protected boolean` | `accept(Solution<V,T> solution, Neighbour<V,T> neighbour)` True if the generated move is to be accepted.
    `protected void` | `activate(Solution<V,T> solution)` Called just before the neighbourhood search is called for the first time.
    `protected void` | `addNeighbourSelection(NeighbourSelection<V,T> ns, double bonus)` Add neighbour selection
    `void` | `bestCleared(Solution<V,T> solution)` Called by the solution when method `Solution.clearBest()` is called.
    `void` | `bestRestored(Solution<V,T> solution)` Called by the solution when method `Solution.restoreBest()` is called.
    `void` | `bestSaved(Solution<V,T> solution)` Called by the solution when method `Solution.saveBest()` is called.
    `protected boolean` | `canContinue(Solution<V,T> solution)` Return false if the search is to be stopped.
    `protected void` | `deactivate(Solution<V,T> solution)` Called when the search cannot continue, just before a null neighbour is returned
    `Neighbour<V,T>` | `generateMove(Solution<V,T> solution)` Generate a random move
    `void` | `getInfo(Solution<V,T> solution, Map<String,String> info)` Called by the solution when it is asked to produce info table, see `Solution.getInfo()`.
    `void` | `getInfo(Solution<V,T> solution, Map<String,String> info, Collection<V> variables)` Called by the solution when it is asked to produce info table, see `Solution.getInfo()`.
    `protected List<? extends NeighbourSelection<V,T>>` | `getNeighbours()` Return list of neighbour selections
    `abstract String` | `getParameterBaseName()` Parameter base name.
    `protected long` | `getTimeMillis()` Running time in milliseconds (since the last call of activate)
    `protected void` | `incIteration(Solution<V,T> solution)` Increment iteration counters etc.
    `void` | `init(Solver<V,T> solver)` Criterion initialization
    `protected void` | `logNeibourStatus()` Log some information about neigbour selections once in a while
    `protected NeighbourSelection<V,T>` | `nextNeighbourSelection()` Generate and return next neighbour selection
    `Neighbour<V,T>` | `selectNeighbour(Solution<V,T> solution)` select a neighbour of a given solution
    `protected void` | `setHCMode(boolean hcMode)` Set HC mode for all the neighbour selections that support the `HillClimberSelection` interface.
    `void` | `solutionUpdated(Solution<V,T> solution)` Called by the solution when it is updated, see `Solution.update(double)`.

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

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

- - ### Field Detail

    - #### iLog

          protected Logger iLog

    - #### iDF2

          protected DecimalFormat iDF2

    - #### iT0

          protected long iT0

    - #### iIter

          protected int iIter

    - #### iPhase

          protected String iPhase

    - #### iProgress

          protected Progress iProgress

  - ### Constructor Detail

    - #### NeighbourSearch

          public NeighbourSearch(DataProperties properties)

  - ### Method Detail

    - #### addNeighbourSelection

          protected void addNeighbourSelection(NeighbourSelection<V,T> ns,
                                               double bonus)

      Add neighbour selection
      Parameters:
        `bonus` - execution bonus (more bonus means more executions of this neighbour selection, see `NeighbourSelector`)

    - #### setHCMode

          protected void setHCMode(boolean hcMode)

      Set HC mode for all the neighbour selections that support the `HillClimberSelection` interface.

    - #### getNeighbours

          protected List<? extends NeighbourSelection<V,T>> getNeighbours()

      Return list of neighbour selections
      Returns:

    - #### 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>>`

    - #### nextNeighbourSelection

          protected NeighbourSelection<V,T> nextNeighbourSelection()

      Generate and return next neighbour selection

    - #### logNeibourStatus

          protected void logNeibourStatus()

      Log some information about neigbour selections once in a while

    - #### generateMove

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

      Generate a random move

    - #### 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

    - #### canContinue

          protected boolean canContinue(Solution<V,T> solution)

      Return false if the search is to be stopped. Null neighbour is returned when this method returns false.

    - #### incIteration

          protected void incIteration(Solution<V,T> solution)

      Increment iteration counters etc.

    - #### getTimeMillis

          protected long getTimeMillis()

      Running time in milliseconds (since the last call of activate)

    - #### accept

          protected boolean accept(Solution<V,T> solution,
                                   Neighbour<V,T> neighbour)

      True if the generated move is to be accepted.

    - #### accept

          public boolean accept(LazyNeighbour<V,T> neighbour,
                                double value)

      Accept lazy neighbour -- calling the acceptance criterion with lazy = true.
      Specified by:
        `accept` in interface `LazyNeighbour.LazyNeighbourAcceptanceCriterion<V extends Variable<V,T>,T extends Value<V,T>>`
      Parameters:
        `neighbour` - neighbour that was assigned
        `value` - change in overall solution value
      Returns:
        true if the neighbour can be accepted (false to undo the assignment)

    - #### accept

          protected abstract boolean accept(Model<V,T> model,
                                            Neighbour<V,T> neighbour,
                                            double value,
                                            boolean lazy)

      Acceptance criterion. If lazy, current assignment already contains the given neighbour.

    - #### activate

          protected void activate(Solution<V,T> solution)

      Called just before the neighbourhood search is called for the first time.

    - #### deactivate

          protected void deactivate(Solution<V,T> solution)

      Called when the search cannot continue, just before a null neighbour is returned

    - #### getParameterBaseName

          public abstract String getParameterBaseName()

      Parameter base name. This can be used to distinguish between parameters of different search algorithms.

    - #### bestSaved

          public void bestSaved(Solution<V,T> solution)

      Description copied from interface: `SolutionListener`
      Called by the solution when method `Solution.saveBest()` is called.
      Specified by:
        `bestSaved` in interface `SolutionListener<V extends Variable<V,T>,T extends Value<V,T>>`
      Parameters:
        `solution` - source solution

    - #### solutionUpdated

          public void solutionUpdated(Solution<V,T> solution)

      Description copied from interface: `SolutionListener`
      Called by the solution when it is updated, see `Solution.update(double)`.
      Specified by:
        `solutionUpdated` in interface `SolutionListener<V extends Variable<V,T>,T extends Value<V,T>>`
      Parameters:
        `solution` - source solution

    - #### getInfo

          public void getInfo(Solution<V,T> solution,
                              Map<String,String> info)

      Description copied from interface: `SolutionListener`
      Called by the solution when it is asked to produce info table, see `Solution.getInfo()`. A listener can also add some its info into this table.
      Specified by:
        `getInfo` in interface `SolutionListener<V extends Variable<V,T>,T extends Value<V,T>>`
      Parameters:
        `solution` - source solution
        `info` - produced info table

    - #### getInfo

          public void getInfo(Solution<V,T> solution,
                              Map<String,String> info,
                              Collection<V> variables)

      Description copied from interface: `SolutionListener`
      Called by the solution when it is asked to produce info table, see `Solution.getInfo()`. A listener can also add some its info into this table.
      Specified by:
        `getInfo` in interface `SolutionListener<V extends Variable<V,T>,T extends Value<V,T>>`
      Parameters:
        `solution` - source solution
        `info` - produced info table
        `variables` - only variables from this set are included

    - #### bestCleared

          public void bestCleared(Solution<V,T> solution)

      Description copied from interface: `SolutionListener`
      Called by the solution when method `Solution.clearBest()` is called.
      Specified by:
        `bestCleared` in interface `SolutionListener<V extends Variable<V,T>,T extends Value<V,T>>`
      Parameters:
        `solution` - source solution

    - #### bestRestored

          public void bestRestored(Solution<V,T> solution)

      Description copied from interface: `SolutionListener`
      Called by the solution when method `Solution.restoreBest()` is called.
      Specified by:
        `bestRestored` in interface `SolutionListener<V extends Variable<V,T>,T extends Value<V,T>>`
      Parameters:
        `solution` - source solution