Class NearestCityHeuristic

java.lang.Object
org.cicirello.search.problems.tsp.NearestCityHeuristic
All Implemented Interfaces:
Splittable<ConstructiveHeuristic<Permutation>>, ConstructiveHeuristic<Permutation>

public final class NearestCityHeuristic extends Object implements ConstructiveHeuristic<Permutation>
This class implements a nearest city constructive heuristic for the TSP for use by stochastic sampling algorithms. The nearest city heuristic prefers cities that are closest to the city most recently added to the tour. Since the stochastic sampling algorithms of the library require higher heuristic values to imply preferred choice, this heuristic is implemented as: h(j) == 1.0 / (1.0 + distance(i, j)), where h(j) is the heuristic value for city j, and i is the most recently added city. If no cities have been added yet, the heuristic simply returns 1.
  • Constructor Details

    • NearestCityHeuristic

      public NearestCityHeuristic(BaseTSP problem)
      Constructs a nearest city heuristic for an instance of the TSP.
      Parameters:
      problem - The TSP instance to solve.
  • Method Details

    • h

      public double h(Partial<Permutation> p, int element, IncrementalEvaluation<Permutation> incEval)
      Description copied from interface: ConstructiveHeuristic
      Heuristically evaluates the possible addition of an element to the end of a Partial. Higher evaluations imply that the element is a better choice for the next element to add. For example, if you evaluate two elements, x and y, with h, and h returns a higher value for y than for x, then this means that y is believed to be the better choice according to the heuristic. Implementations of this interface must ensure that h always returns a positive result. This is because stochastic sampling algorithms such as HBSS and VBSS assume that the constructive heuristic returns only positive values.
      Specified by:
      h in interface ConstructiveHeuristic<Permutation>
      Parameters:
      p - The current state of the Partial
      element - The element under consideration for adding to the Partial
      incEval - An IncrementalEvaluation of p. This method assumes that incEval is of the same runtime type as the object returned by ConstructiveHeuristic.createIncrementalEvaluation().
      Returns:
      The heuristic evaluation of the hypothetical addition of element to the end of p. The higher the evaluation, the more important the heuristic believes that element should be added next. The intention is to compare the value returned with the heuristic evaluations of other elements. Individual results in isolation are not necessarily meaningful.
    • getProblem

      public final Problem<Permutation> getProblem()
      Description copied from interface: ConstructiveHeuristic
      Gets a reference to the instance of the optimization problem that is the subject of this heuristic.
      Specified by:
      getProblem in interface ConstructiveHeuristic<Permutation>
      Returns:
      the instance of the optimization problem that is the subject of this heuristic.
    • createPartial

      public final Partial<Permutation> createPartial(int n)
      Description copied from interface: ConstructiveHeuristic
      Creates an empty Partial solution, which will be incrementally transformed into a complete solution of a specified length.
      Specified by:
      createPartial in interface ConstructiveHeuristic<Permutation>
      Parameters:
      n - the desired length of the final complete solution.
      Returns:
      an empty Partial solution
    • completeLength

      public final int completeLength()
      Description copied from interface: ConstructiveHeuristic
      Gets the required length of complete solutions to the problem instance for which this constructive heuristic is configured.
      Specified by:
      completeLength in interface ConstructiveHeuristic<Permutation>
      Returns:
      length of solutions to the problem instance for which this heuristic is configured