Class TSPProblem

java.lang.Object
org.ddolib.examples.nolayer.tsp.TSPProblem
All Implemented Interfaces:
Problem<TSPState>

public class TSPProblem extends Object implements Problem<TSPState>
Traveling Salesman Problem (TSP), using the no-layer modeling API.

Models the classic TSP: starting from and returning to city 0, visit every other city exactly once while minimizing the total travelled distance. States are represented by TSPState (current city and set of cities still to visit).

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    final double[][]
    Distance (cost) matrix between cities, indexed by origin and destination city.
    final int
    Number of cities.
  • Constructor Summary

    Constructors
    Constructor
    Description
    TSPProblem(double[][] distanceMatrix)
    Creates a new unnamed TSP instance from a distance matrix.
    Reads a TSP instance from an XML file.
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns the domain of possible labels/actions from a given state.
    double
    evaluate(List<Integer> solution)
    Given a solution (a sequence of applied labels), returns its value and checks if the solution respects the problem's constraints.
    Returns the initial state of the problem.
    double
    Returns the initial objective value associated with the initial state.
    boolean
    Checks whether the given state is a target (sink) state.
    singleton(int singletonValue)
    Creates a BitSet containing only the given value.
     
    transition(TSPState state, int label)
    Applies a label/action to a state, computing the next state according to the problem's transition function.
    double
    transitionCost(TSPState state, int label)
    Computes the change in objective value resulting from applying a label to a given state.

    Methods inherited from class java.lang.Object

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

    Methods inherited from interface org.ddolib.nolayer.modeling.Problem

    hasNonNegativeCosts, optimalValue
  • Field Details

    • distanceMatrix

      public final double[][] distanceMatrix
      Distance (cost) matrix between cities, indexed by origin and destination city.
    • n

      public final int n
      Number of cities.
  • Constructor Details

    • TSPProblem

      public TSPProblem(double[][] distanceMatrix)
      Creates a new unnamed TSP instance from a distance matrix.
      Parameters:
      distanceMatrix - distance (cost) matrix between cities, indexed by origin and destination city
    • TSPProblem

      public TSPProblem(String fname) throws IOException
      Reads a TSP instance from an XML file.
      Parameters:
      fname - path to the instance file
      Throws:
      IOException - if the instance file cannot be read or parsed
  • Method Details

    • initialState

      public TSPState initialState()
      Description copied from interface: Problem
      Returns the initial state of the problem.
      Specified by:
      initialState in interface Problem<TSPState>
      Returns:
      the state representing the starting point of the optimization
    • initialValue

      public double initialValue()
      Description copied from interface: Problem
      Returns the initial objective value associated with the initial state.
      Specified by:
      initialValue in interface Problem<TSPState>
      Returns:
      the starting value of the objective function
    • isTarget

      public boolean isTarget(TSPState state)
      Description copied from interface: Problem
      Checks whether the given state is a target (sink) state.
      Specified by:
      isTarget in interface Problem<TSPState>
      Parameters:
      state - the current state
      Returns:
      true if the state is a target state, false otherwise
    • domain

      public Iterator<Integer> domain(TSPState state)
      Description copied from interface: Problem
      Returns the domain of possible labels/actions from a given state.
      Specified by:
      domain in interface Problem<TSPState>
      Parameters:
      state - the current state
      Returns:
      an iterator over all feasible labels for the given state
    • transition

      public TSPState transition(TSPState state, int label)
      Description copied from interface: Problem
      Applies a label/action to a state, computing the next state according to the problem's transition function.
      Specified by:
      transition in interface Problem<TSPState>
      Parameters:
      state - the state from which the transition originates
      label - the label to apply
      Returns:
      the resulting state after applying the label
    • transitionCost

      public double transitionCost(TSPState state, int label)
      Description copied from interface: Problem
      Computes the change in objective value resulting from applying a label to a given state.
      Specified by:
      transitionCost in interface Problem<TSPState>
      Parameters:
      state - the state from which the transition originates
      label - the label to apply
      Returns:
      the incremental objective cost/value associated with this label
    • evaluate

      public double evaluate(List<Integer> solution) throws InvalidSolutionException
      Description copied from interface: Problem
      Given a solution (a sequence of applied labels), returns its value and checks if the solution respects the problem's constraints.
      Specified by:
      evaluate in interface Problem<TSPState>
      Parameters:
      solution - a solution of the problem (sequence of labels)
      Returns:
      the value of the input solution
      Throws:
      InvalidSolutionException - if the solution does not respect problem's constraints
    • singleton

      public BitSet singleton(int singletonValue)
      Creates a BitSet containing only the given value.
      Parameters:
      singletonValue - the value to set in the returned bit set
      Returns:
      a bit set with only singletonValue set
    • toString

      public String toString()
      Overrides:
      toString in class Object