Package org.ddolib.examples.tsp
Class TSPGenerator
java.lang.Object
org.ddolib.examples.tsp.TSPGenerator
Class to generate and handle instances of the Traveling Salesman Problem (TSP).
This class provides functionality to:
- Create TSP instances from a distance matrix (int[][] or double[][]).
- Create Euclidean TSP instances by sampling coordinates within a square.
- Create Euclidean TSP instances from given x/y coordinates.
- Access distances between cities and the number of cities.
- Save a TSP instance in XML format following the XML-TSPLIB standard.
The class maintains an internal distance matrix representing the cost between cities and optionally stores the objective value (best known solution).
Example usage:
// Generate a Euclidean TSP instance with 10 cities
TSPGenerator tsp = new TSPGenerator(10, 42, 100);
double dist = tsp.distance(0, 1);
tsp.saveXml("instance.xml", 1234);
- Author:
- Pierre Schaus
-
Field Summary
FieldsModifier and TypeFieldDescriptiondouble[][]Distance matrix between citiesintNumber of citiesfinal doubleBest known objective value (optional, -1 if unknown) -
Constructor Summary
ConstructorsConstructorDescriptionTSPGenerator(double[][] distanceMatrix) Constructs a TSP instance from a double[][] distance matrix.TSPGenerator(int[][] distanceMatrix) Constructs a TSP instance from an int[][] distance matrix.TSPGenerator(int[] xCoord, int[] yCoord) Constructs a Euclidean TSP instance from given x/y coordinates.TSPGenerator(int n, int seed, int squareLength) Constructs a Euclidean TSP instance by randomly sampling coordinates in a square. -
Method Summary
Modifier and TypeMethodDescriptiondoubledist(double x1, double y1, double x2, double y2) Computes the Euclidean distance between two points.doubledistance(int city1, int city2) Returns the distance between two cities.intnCities()Returns the number of cities in the instance.voidSaves the TSP instance in XML format following XML-TSPLIB standards.
-
Field Details
-
distanceMatrix
public double[][] distanceMatrixDistance matrix between cities -
n
public int nNumber of cities -
objective
public final double objectiveBest known objective value (optional, -1 if unknown)
-
-
Constructor Details
-
TSPGenerator
public TSPGenerator(double[][] distanceMatrix) Constructs a TSP instance from a double[][] distance matrix.- Parameters:
distanceMatrix- the distance matrix
-
TSPGenerator
public TSPGenerator(int[][] distanceMatrix) Constructs a TSP instance from an int[][] distance matrix.- Parameters:
distanceMatrix- the distance matrix
-
TSPGenerator
public TSPGenerator(int n, int seed, int squareLength) Constructs a Euclidean TSP instance by randomly sampling coordinates in a square.- Parameters:
n- number of citiesseed- random seed for reproducibilitysquareLength- length of the square in which coordinates are sampled
-
TSPGenerator
public TSPGenerator(int[] xCoord, int[] yCoord) Constructs a Euclidean TSP instance from given x/y coordinates.- Parameters:
xCoord- array of x-coordinatesyCoord- array of y-coordinates
-
-
Method Details
-
nCities
public int nCities()Returns the number of cities in the instance.- Returns:
- the number of cities
-
distance
public double distance(int city1, int city2) Returns the distance between two cities.- Parameters:
city1- first city indexcity2- second city index- Returns:
- distance between city1 and city2
-
dist
public double dist(double x1, double y1, double x2, double y2) Computes the Euclidean distance between two points.- Parameters:
x1- x-coordinate of first pointy1- y-coordinate of first pointx2- x-coordinate of second pointy2- y-coordinate of second point- Returns:
- Euclidean distance between the points
-
saveXml
Saves the TSP instance in XML format following XML-TSPLIB standards.- Parameters:
path- path to the XML fileobjective- best known solution value
-