|
1
|
|
|
"""!
|
|
2
|
|
|
|
|
3
|
|
|
@brief TSP algorithm: ant colony based
|
|
4
|
|
|
@details Based on article description:
|
|
5
|
|
|
- M.Dorigo, L.M.Gambardella. Ant colonies for the traveling salesman problem. 1996.
|
|
6
|
|
|
- J.Yang, X.Shi, M.Marchese, Y.Liang. An ant colony optimization method for generalized TSP problem. 2008.
|
|
7
|
|
|
|
|
8
|
|
|
@authors Alexey Kukushkin ([email protected])
|
|
9
|
|
|
@date 2014-2016
|
|
10
|
|
|
@copyright GNU Public License
|
|
11
|
|
|
|
|
12
|
|
|
@cond GNU_PUBLIC_LICENSE
|
|
13
|
|
|
PyClustering is free software: you can redistribute it and/or modify
|
|
14
|
|
|
it under the terms of the GNU General Public License as published by
|
|
15
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
16
|
|
|
(at your option) any later version.
|
|
17
|
|
|
|
|
18
|
|
|
PyClustering is distributed in the hope that it will be useful,
|
|
19
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
20
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
21
|
|
|
GNU General Public License for more details.
|
|
22
|
|
|
|
|
23
|
|
|
You should have received a copy of the GNU General Public License
|
|
24
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
25
|
|
|
@endcond
|
|
26
|
|
|
|
|
27
|
|
|
"""
|
|
28
|
|
|
|
|
29
|
|
|
from pyclustering.tsp import tsp_result;
|
|
30
|
|
|
|
|
31
|
|
|
import pyclustering.core.antcolony_tsp_wrapper as wrapper;
|
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
class antcolony_parameters:
|
|
35
|
|
|
"""!
|
|
36
|
|
|
@brief Describes parameters of ant colony based algorithm for TSP problem.
|
|
37
|
|
|
|
|
38
|
|
|
@see antcolony
|
|
39
|
|
|
|
|
40
|
|
|
"""
|
|
41
|
|
|
|
|
42
|
|
|
def __init__(self):
|
|
43
|
|
|
##
|
|
44
|
|
|
self.q = 1.5;
|
|
45
|
|
|
|
|
46
|
|
|
##
|
|
47
|
|
|
self.ro = 0.7;
|
|
48
|
|
|
|
|
49
|
|
|
##
|
|
50
|
|
|
self.alpha = 1.0;
|
|
51
|
|
|
|
|
52
|
|
|
##
|
|
53
|
|
|
self.beta = 1.0;
|
|
54
|
|
|
|
|
55
|
|
|
##
|
|
56
|
|
|
self.gamma = 2.0;
|
|
57
|
|
|
|
|
58
|
|
|
##
|
|
59
|
|
|
self.qinit_pheramone = 0.1;
|
|
60
|
|
|
|
|
61
|
|
|
## Amount of ants that is used on each iteration.
|
|
62
|
|
|
self.ants_per_iteration = 10;
|
|
63
|
|
|
|
|
64
|
|
|
## Amount of iterations that is used for solving TSP.
|
|
65
|
|
|
self.iterations = 50;
|
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
class antcolony:
|
|
69
|
|
|
"""!
|
|
70
|
|
|
@brief Simulates ant colony to solve travelling salesman problem (TSP).
|
|
71
|
|
|
|
|
72
|
|
|
@details Ants of the artificial colony are able to generate successively shorter feasible tours by using information accumulated
|
|
73
|
|
|
in the form of a pheromone trail deposited on the edges of the TSP graph.
|
|
74
|
|
|
|
|
75
|
|
|
@warning Solution is performed only via CCORE library (C/C++ implementation of the library).
|
|
76
|
|
|
|
|
77
|
|
|
Example:
|
|
78
|
|
|
@code
|
|
79
|
|
|
|
|
80
|
|
|
@endcode
|
|
81
|
|
|
|
|
82
|
|
|
"""
|
|
83
|
|
|
|
|
84
|
|
|
def __init__(self, parameters):
|
|
85
|
|
|
"""!
|
|
86
|
|
|
@brief Constructor of ant colony based algorithm for travelling salesman problem.
|
|
87
|
|
|
|
|
88
|
|
|
@param[in] parameters (antcolony_parameters): Parameters of the ant colony algorithm.
|
|
89
|
|
|
|
|
90
|
|
|
"""
|
|
91
|
|
|
|
|
92
|
|
|
self.__parameters = None;
|
|
93
|
|
|
|
|
94
|
|
|
if (parameters is None):
|
|
95
|
|
|
self.__parameters = antcolony_parameters();
|
|
96
|
|
|
else:
|
|
97
|
|
|
self.__parameters = parameters;
|
|
98
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
def process(self, object_locations):
|
|
101
|
|
|
"""!
|
|
102
|
|
|
@brief Perform simulation of ant colony to solve travelling salesman problem.
|
|
103
|
|
|
|
|
104
|
|
|
@param[in] object_locations (list): Coordinates of objects that should be visited.
|
|
105
|
|
|
|
|
106
|
|
|
"""
|
|
107
|
|
|
|
|
108
|
|
|
(result_address, c_pointer_tsp_result) = wrapper.antcolony_tsp_process(object_locations, self.__parameters);
|
|
109
|
|
|
|
|
110
|
|
|
result = tsp_result();
|
|
111
|
|
|
|
|
112
|
|
|
result.shortest_length = c_pointer_tsp_result.path_length;
|
|
113
|
|
|
for i in range(c_pointer_tsp_result.size):
|
|
114
|
|
|
result.object_sequence.append(c_pointer_tsp_result.object_sequence[i]);
|
|
115
|
|
|
|
|
116
|
|
|
wrapper.antcolony_tsp_destroy(result_address);
|
|
117
|
|
|
|
|
118
|
|
|
return result; |