tsp-solver-nn
Version:
A Typescript implementation of the Nearest Neighbor with 2-opt and 3-opt optimizations to solve the travelling salesman problem (TSP).
50 lines (47 loc) • 1.35 kB
TypeScript
interface TSPSolver {
getPath: (distanceMatrix: number[][], optimizations?: {
twoOpt?: boolean;
threeOpt?: boolean;
}) => {
route: number[];
distance: number;
};
getCycle: (distanceMatrix: number[][], optimizations?: {
twoOpt?: boolean;
threeOpt?: boolean;
}) => {
route: number[];
distance: number;
};
}
declare class TSPSolverNN implements TSPSolver {
#private;
/**
* Validates the distance matrix for TSP.
* Checks for square shape, non-negative values, and no NaNs.
*/
static isValidDistanceMatrix(distanceMatrix: number[][]): boolean;
/**
* Solves the path variant of the Traveling Salesman Problem (TSP),
* using Nearest Neighbour + optional 2-opt / 3-opt refinements.
*/
getPath(distanceMatrix: number[][], optimizations?: {
twoOpt?: boolean;
threeOpt?: boolean;
}): {
route: number[];
distance: number;
};
/**
* Solves the closed cycle variant of the TSP,
* using Nearest Neighbour + optional 2-opt / 3-opt refinements.
*/
getCycle(distanceMatrix: number[][], optimizations?: {
twoOpt?: boolean;
threeOpt?: boolean;
}): {
route: number[];
distance: number;
};
}
export { type TSPSolver, TSPSolverNN };