All Implemented Interfaces:
IntegerCostOptimizationProblem<Permutation>, Problem<Permutation>
Enclosing class:
TSP

public static final class TSP.Integer extends TSP implements IntegerCostOptimizationProblem<Permutation>
Cost function for the Traveling Salesperson Problem (TSP), where edge costs are integer valued. This implementation only requires linear memory, but must recompute an edge cost every time it is needed. If your instance of the TSP is small enough to afford quadratic memory, then you may prefer the TSP.IntegerMatrix class which precomputes all edge costs between all pairs of cities.
  • Nested Class Summary

    Nested classes/interfaces inherited from class org.cicirello.search.problems.tsp.TSP

    TSP.Double, TSP.DoubleMatrix, TSP.Integer, TSP.IntegerMatrix
  • Constructor Summary

    Constructors
    Constructor
    Description
    Integer(double[] x, double[] y)
    Constructs a TSP instance with city locations specified by arrays of x and y coordinates.
    Integer(double[] x, double[] y, TSPEdgeDistance distance)
    Constructs a TSP instance with city locations specified by arrays of x and y coordinates.
    Integer(int n, double w)
    Constructs a random TSP instance with cities randomly distributed within a square region.
    Integer(int n, double w, long seed)
    Constructs a random TSP instance with cities randomly distributed within a square region.
    Integer(int n, double w, TSPEdgeDistance distance)
    Constructs a random TSP instance with cities randomly distributed within a square region.
    Integer(int n, double w, TSPEdgeDistance distance, long seed)
    Constructs a random TSP instance with cities randomly distributed within a square region.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    cost(Permutation candidate)
    Computes the cost of a candidate solution to the problem instance.
    int
    A lower bound on the minimum theoretical cost across all possible solutions to the problem instance, where lower cost implies better solution.
    int
    value(Permutation candidate)
    Computes the value of the candidate solution within the usual constraints and interpretation of the problem.

    Methods inherited from class org.cicirello.search.problems.tsp.TSP

    getX, getY, length

    Methods inherited from class java.lang.Object

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

    Methods inherited from interface org.cicirello.search.problems.IntegerCostOptimizationProblem

    costAsDouble, getSolutionCostPair, isMinCost
  • Constructor Details

    • Integer

      public Integer(int n, double w)
      Constructs a random TSP instance with cities randomly distributed within a square region. The edge cost of a pair of cities is the Euclidean distance between them rounded to the nearest integer.
      Parameters:
      n - The number of cities.
      w - The width (and height) of a square region containing the cities.
      Throws:
      IllegalArgumentException - if n < 2.
      IllegalArgumentException - if w ≤ 0.0.
    • Integer

      public Integer(int n, double w, TSPEdgeDistance distance)
      Constructs a random TSP instance with cities randomly distributed within a square region.
      Parameters:
      n - The number of cities.
      w - The width (and height) of a square region containing the cities.
      distance - The distance function to use for the edge costs.
      Throws:
      IllegalArgumentException - if n < 2.
      IllegalArgumentException - if w ≤ 0.0.
    • Integer

      public Integer(int n, double w, long seed)
      Constructs a random TSP instance with cities randomly distributed within a square region. The edge cost of a pair of cities is the Euclidean distance between them rounded to the nearest integer.
      Parameters:
      n - The number of cities.
      w - The width (and height) of a square region containing the cities.
      seed - The seed for the random number generator to enable reproducing the same instance for experiment reproducibility.
      Throws:
      IllegalArgumentException - if n < 2.
      IllegalArgumentException - if w ≤ 0.0.
    • Integer

      public Integer(int n, double w, TSPEdgeDistance distance, long seed)
      Constructs a random TSP instance with cities randomly distributed within a square region.
      Parameters:
      n - The number of cities.
      w - The width (and height) of a square region containing the cities.
      distance - The distance function to use for the edge costs.
      seed - The seed for the random number generator to enable reproducing the same instance for experiment reproducibility.
      Throws:
      IllegalArgumentException - if n < 2.
      IllegalArgumentException - if w ≤ 0.0.
    • Integer

      public Integer(double[] x, double[] y)
      Constructs a TSP instance with city locations specified by arrays of x and y coordinates. The edge cost of a pair of cities is the Euclidean distance between them rounded to the nearest integer.
      Parameters:
      x - Array of x coordinates.
      y - Array of y coordinates.
      Throws:
      IllegalArgumentException - if x.length is not equal to y.length.
      IllegalArgumentException - if the length of the arrays is less than 2.
    • Integer

      public Integer(double[] x, double[] y, TSPEdgeDistance distance)
      Constructs a TSP instance with city locations specified by arrays of x and y coordinates.
      Parameters:
      x - Array of x coordinates.
      y - Array of y coordinates.
      distance - The distance function to use for the edge costs.
      Throws:
      IllegalArgumentException - if x.length is not equal to y.length.
      IllegalArgumentException - if the length of the arrays is less than 2.
  • Method Details

    • cost

      public int cost(Permutation candidate)
      Description copied from interface: IntegerCostOptimizationProblem
      Computes the cost of a candidate solution to the problem instance. The lower the cost, the more optimal the candidate solution.
      Specified by:
      cost in interface IntegerCostOptimizationProblem<Permutation>
      Parameters:
      candidate - The candidate solution to evaluate.
      Returns:
      The cost of the candidate solution. Lower cost means better solution.
    • value

      public int value(Permutation candidate)
      Description copied from interface: IntegerCostOptimizationProblem
      Computes the value of the candidate solution within the usual constraints and interpretation of the problem.
      Specified by:
      value in interface IntegerCostOptimizationProblem<Permutation>
      Parameters:
      candidate - The candidate solution to evaluate.
      Returns:
      The actual optimization value of the candidate solution.
    • minCost

      public int minCost()
      Description copied from interface: IntegerCostOptimizationProblem
      A lower bound on the minimum theoretical cost across all possible solutions to the problem instance, where lower cost implies better solution. The default implementation returns Integer.MIN_VALUE.
      Specified by:
      minCost in interface IntegerCostOptimizationProblem<Permutation>
      Returns:
      A lower bound on the minimum theoretical cost of the problem instance.