Class TSPTWProblem

java.lang.Object
org.ddolib.examples.nolayer.tsptw.TSPTWProblem
All Implemented Interfaces:
Problem<TSPTWState>

public class TSPTWProblem extends Object implements Problem<TSPTWState>
Traveling Salesman Problem with Time Windows (TSPTW), using the no-layer modeling API.

Models the TSPTW: starting from and returning to city 0, visit every other city exactly once, arriving within its time window (waiting if arriving early), while minimizing the total elapsed time. States are represented by TSPTWState (current city, current time, and set of cities still to visit).

  • Field Details

    • distance

      public final int[][] distance
      Travel distance (duration) matrix between cities, indexed by origin and destination city.
    • timeWindows

      public final TimeWindow[] timeWindows
      Time window of each city, indexed by city number.
    • nbVars

      public final int nbVars
      Number of cities.
  • Constructor Details

    • TSPTWProblem

      public TSPTWProblem(int[][] distance, TimeWindow[] timeWindows)
      Creates a new TSPTW instance.
      Parameters:
      distance - travel distance (duration) matrix between cities, indexed by origin and destination city
      timeWindows - time window of each city, indexed by city number
  • Method Details

    • fromFile

      public static TSPTWProblem fromFile(String fname) throws IOException
      Reads a TSPTW instance from a file.
      Parameters:
      fname - path to the instance file
      Returns:
      the TSPTW instance described by the file
      Throws:
      IOException - if the instance file cannot be read
    • initialState

      public TSPTWState initialState()
      Description copied from interface: Problem
      Returns the initial state of the problem.
      Specified by:
      initialState in interface Problem<TSPTWState>
      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<TSPTWState>
      Returns:
      the starting value of the objective function
    • isTarget

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

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

      public TSPTWState transition(TSPTWState 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<TSPTWState>
      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(TSPTWState 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<TSPTWState>
      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<TSPTWState>
      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
    • toString

      public String toString()
      Overrides:
      toString in class Object