tspgrasp
The operators listed here are intended to be easily available for the user, so they can be imported directy from tspgrap.
Grasp
- class tspgrasp.grasp.Grasp(constructive=None, local_search=None, seed=None)
Greedy Randomized Adaptive Search Procedure for the TSP.
- Parameters:
- constructiveAny, optional
Constructive heuristic. Current options available are CheapestArc, SemiGreedyArc, CheapestInsertion, RandomInsertion, and SemiGreedyInsertion, which should be instantiated beforehand. By default None, which instantiates a CheapestArc operator.
- local_searchAny, optional
Local search heuristic. Current options available are LocalSearch and SimulatedAnnealing, which should be instantiated beforehand. By default None, which uses LocalSearch, a VNS with first improvement
- seedint, optional
Random generator seed (differs behavior from cython to python), by default None
Methods
__call__(D[, max_iter, max_moves, ...])Solves a TSP based on a generic 2-dimensional distance matrix
- __call__(D: ndarray, max_iter: int = 10000, max_moves: int = 100000, time_limit: float = inf, target: float = -inf, verbose: bool = False) Solution
Solves a TSP based on a generic 2-dimensional distance matrix
- Parameters:
- Dnp.ndarray
2-dimensional distance matrix
- max_iterint
Maximum number of complete iterations, by default 10000
- max_movesint
Maximum number of local search moves, by default 100000
- time_limitfloat, optional
Time limit (s) to interrupt the solution, by default float(“inf”)
- targetfloat, optional
Taget value for objective which interrupts optimization process, by default -float(“inf”)
- verbosebool, optional
Either or not to print messages during solution
- Returns:
- Solution
Attributes: - tour : List[int] - cost : float
Constructive Heuristics
- class tspgrasp.environ.CheapestArc(seed: int = None)
Greedy adaptive construction for the TSP inserting the next node at the end of the partial tour. Depot nodes are randomly chosen.
- Parameters:
- seedint, optional
Random generator seed (differs behavior from cython to python), by default None
Methods
__call__(D)Solves a TSP based on a pairwise distance matrix.
- class tspgrasp.environ.SemiGreedyArc(alpha=(0.0, 1.0), seed=None)
Greedy-randomized constructive heuristic for the TSP. It inserts the next node at the end of the partial tour. Depot nodes are randomly chosen.
- Parameters:
- alphatuple, optional
Alpha parameter - randomly generated at each iteration between range or fixed scalar. Use values closer to one for a more greedy approach. By default (0.0, 1.0).
- seedint, optional
Random generator seed (differs behavior from cython to python), by default None
- Attributes:
nodesnodes: object
problemproblem: tspgrasp.cython.problem.Problem
queuequeue: ‘vector[int]’
rngrng: tspgrasp.cython.random.RandomGen
tourtour: tspgrasp.cython.tour.Tour
Methods
__call__(D)Solves a TSP based on a pairwise distance matrix.
do(self, Problem problem)
- class tspgrasp.environ.CheapestInsertion(seed=None)
Greedy adaptive construction for the TSP inserting the next node at the best position between two existing nodes of the partial tour. Depot nodes are randomly chosen.
- Parameters:
- seedint, optional
Random generator seed (differs behavior from cython to python), by default None
- Attributes:
nodesnodes: object
problemproblem: tspgrasp.cython.problem.Problem
queuequeue: ‘vector[int]’
rngrng: tspgrasp.cython.random.RandomGen
tourtour: tspgrasp.cython.tour.Tour
Methods
__call__(D)Solves a TSP based on a pairwise distance matrix.
do(self, Problem problem)
- class tspgrasp.environ.RandomInsertion(seed=None)
Constructive heuristic for the TSP inserting a random next node at the best position between two existing nodes of the partial tour. Depot nodes are randomly chosen.
- Parameters:
- seedint, optional
Random generator seed (differs behavior from cython to python), by default None
- Attributes:
nodesnodes: object
problemproblem: tspgrasp.cython.problem.Problem
queuequeue: ‘vector[int]’
rngrng: tspgrasp.cython.random.RandomGen
tourtour: tspgrasp.cython.tour.Tour
Methods
__call__(D)Solves a TSP based on a pairwise distance matrix.
do(self, Problem problem)
- class tspgrasp.environ.SemiGreedyInsertion(alpha=(0.0, 1.0), seed=None)
Greedy-randomized constructive heuristic for the TSP based on inserting the next node between two existing nodes of the partial tour.
- Parameters:
- alphatuple, optional
Alpha parameter - randomly generated at each iteration between range or fixed scalar. Use values closer to one for a more greedy approach. By default (0.0, 1.0).
- seedint, optional
Random generator seed (differs behavior from cython to python), by default None
- Attributes:
nodesnodes: object
problemproblem: tspgrasp.cython.problem.Problem
queuequeue: ‘vector[int]’
rngrng: tspgrasp.cython.random.RandomGen
tourtour: tspgrasp.cython.tour.Tour
Methods
__call__(D)Solves a TSP based on a pairwise distance matrix.
do(self, Problem problem)
Local Search
- class tspgrasp.environ.LocalSearch(seed: int = None)
Local Search (VNS with first improvement) implementation for TSP.
- Parameters:
- seedint, optional
Random generator seed (differs behavior from cython to python), by default None
Methods
__call__(seq, D[, max_iter])Solve a TSP based on an initial solution and a distance matrix
- __call__(seq: List[int], D: ndarray, max_iter=100000)
Solve a TSP based on an initial solution and a distance matrix
- Parameters:
- seqList[int]
Initial solution
- Dnp.ndarray
2d-distance matrix
- max_iterint, optional
Max number of moves, by default 100000
- Returns:
- Solution
Attributes: - tour : List[int] - cost : float
- class tspgrasp.environ.SimulatedAnnealing(T_start=10.0, T_final=0.001, decay=0.99, seed=None)
Simulated Annealing for the TSP using a VNS.
- Parameters:
- T_startfloat, optional
Initial temperature in each search, by default 10.0
- T_finalfloat, optional
Final temperature (stop searching), by default 0.001
- decayfloat, optional
Decay factor, by default 0.99
- seed_type_, optional
Random generator seed (differs behavior from cython to python), by default None
- Attributes:
TT: ‘double’
T_finalT_final: ‘double’
T_startT_start: ‘double’
decaydecay: ‘double’
max_itermax_iter: ‘int’
n_movesn_moves: ‘int’
tourtour: tspgrasp.cython.tour.Tour
Methods
__call__(seq, D[, max_iter])Solve a TSP based on an initial solution and a distance matrix.
do(self, Tour tour, int max_iter=100000)set_problem(self, Problem problem)