1
|
|
|
"""! |
2
|
|
|
|
3
|
|
|
@brief CCORE Wrapper for ant colony based algorithm for travelling salesman problem. |
4
|
|
|
|
5
|
|
|
@authors Andrei Novikov, Alexey Kukushkin ([email protected]) |
6
|
|
|
@date 2014-2017 |
7
|
|
|
@copyright GNU Public License |
8
|
|
|
|
9
|
|
|
@cond GNU_PUBLIC_LICENSE |
10
|
|
|
PyClustering is free software: you can redistribute it and/or modify |
11
|
|
|
it under the terms of the GNU General Public License as published by |
12
|
|
|
the Free Software Foundation, either version 3 of the License, or |
13
|
|
|
(at your option) any later version. |
14
|
|
|
|
15
|
|
|
PyClustering is distributed in the hope that it will be useful, |
16
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
17
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18
|
|
|
GNU General Public License for more details. |
19
|
|
|
|
20
|
|
|
You should have received a copy of the GNU General Public License |
21
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. |
22
|
|
|
@endcond |
23
|
|
|
|
24
|
|
|
""" |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
from pyclustering.core.wrapper import *; |
|
|
|
|
28
|
|
|
|
29
|
|
|
import types; |
|
|
|
|
30
|
|
|
from pyclustering.core.pyclustering_package import pyclustering_package,\ |
31
|
|
|
package_builder, package_extractor |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
CITIES_DISTANCE_SET_BY_MATRIX = True |
35
|
|
|
CITIES_DISTANCE_SET_BY_LIST_OF_COORDINATES = False |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
class c_antcolony_tsp_parameters(Structure): |
39
|
|
|
""" |
40
|
|
|
double q; |
41
|
|
|
double ro; |
42
|
|
|
double alpha; |
43
|
|
|
double beta; |
44
|
|
|
double gamma; |
45
|
|
|
double initial_pheramone; |
46
|
|
|
unsigned int iterations; |
47
|
|
|
unsigned int ants_per_iteration; |
48
|
|
|
|
49
|
|
|
""" |
50
|
|
|
_fields_ = [("q" , c_double), |
51
|
|
|
("ro" , c_double), |
52
|
|
|
("alpha" , c_double), |
53
|
|
|
("beta" , c_double), |
54
|
|
|
("gamma" , c_double), |
55
|
|
|
("qinit_pheramone" , c_double), |
56
|
|
|
("iterations" , c_uint), |
57
|
|
|
("ants_per_iteration" , c_uint) ]; |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
class c_antcolony_tsp_objects(Structure): |
61
|
|
|
""" |
62
|
|
|
unsigned int size; |
63
|
|
|
unsigned int dimension; |
64
|
|
|
double *data; |
65
|
|
|
|
66
|
|
|
""" |
67
|
|
|
_fields_ = [("size" , c_uint), |
68
|
|
|
("dimension" , c_uint), |
69
|
|
|
("data" , POINTER(c_double)) ]; |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
class c_antcolony_tsp_matrix(Structure): |
73
|
|
|
""" |
74
|
|
|
unsigned int size; |
75
|
|
|
double **data; |
76
|
|
|
|
77
|
|
|
""" |
78
|
|
|
_fields_ = [("size" , c_uint), |
79
|
|
|
("data" , POINTER(POINTER(c_double))) ]; |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
class c_antcolony_tsp_result(Structure): |
83
|
|
|
""" |
84
|
|
|
unsigned int size; |
85
|
|
|
double path_length; |
86
|
|
|
unsigned int *cities_num; |
87
|
|
|
|
88
|
|
|
""" |
89
|
|
|
_fields_ = [("size" , c_uint), |
90
|
|
|
("path_length" , c_double), |
91
|
|
|
("object_sequence" , POINTER(c_uint)) ]; |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
|
95
|
|
|
def get_algo_params(params): |
96
|
|
|
algorithm_params = c_antcolony_tsp_parameters(); |
97
|
|
|
algorithm_params.q = c_double(params.q); |
98
|
|
|
algorithm_params.ro = c_double(params.ro); |
99
|
|
|
algorithm_params.alpha = c_double(params.alpha); |
100
|
|
|
algorithm_params.beta = c_double(params.beta); |
101
|
|
|
algorithm_params.gamma = c_double(params.gamma); |
102
|
|
|
algorithm_params.qinit_pheramone = c_double(params.qinit_pheramone); |
103
|
|
|
algorithm_params.iterations = c_uint(params.iterations); |
104
|
|
|
algorithm_params.ants_per_iteration = c_uint(params.ants_per_iteration); |
105
|
|
|
|
106
|
|
|
algorithm_params = pointer(algorithm_params); |
107
|
|
|
|
108
|
|
|
return algorithm_params |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
def antcolony_tsp_prepare_matrix(matrix): |
112
|
|
|
dist_matrix = c_antcolony_tsp_matrix() |
113
|
|
|
|
114
|
|
|
dist_matrix.size = len(matrix) |
115
|
|
|
|
116
|
|
|
p_dist = (POINTER(c_double) * dist_matrix.size)(); |
117
|
|
|
|
118
|
|
|
for i in range(dist_matrix.size): |
119
|
|
|
tmp_p_dist = (c_double * dist_matrix.size)(); |
120
|
|
|
for j in range(dist_matrix.size): |
121
|
|
|
tmp_p_dist[j] = matrix[i][j]; |
122
|
|
|
|
123
|
|
|
p_dist[i] = cast(tmp_p_dist, POINTER(c_double)); |
124
|
|
|
|
125
|
|
|
dist_matrix.data = cast(p_dist, POINTER(POINTER(c_double))); |
126
|
|
|
dist_matrix = pointer(dist_matrix) |
127
|
|
|
|
128
|
|
|
return dist_matrix |
129
|
|
|
|
130
|
|
|
|
131
|
|
|
def antcolony_tsp_prepare_cities_list(cities): |
132
|
|
|
dimension = len(cities[0]); |
133
|
|
|
|
134
|
|
|
cities_coord = c_antcolony_tsp_objects(); |
135
|
|
|
cities_coord.size = c_uint(len(cities) * dimension); |
136
|
|
|
cities_coord.dimension = c_uint(dimension); |
137
|
|
|
|
138
|
|
|
cities_coord.data = (c_double * cities_coord.size)(); |
139
|
|
|
|
140
|
|
|
for i in range(0, cities_coord.size): |
141
|
|
|
cities_coord.data[i] = cities[i // dimension][i % dimension]; |
142
|
|
|
|
143
|
|
|
cities_coord = pointer(cities_coord); |
144
|
|
|
|
145
|
|
|
return cities_coord |
146
|
|
|
|
147
|
|
|
|
148
|
|
|
def antcolony_tsp_process(cities, params, citiesDistRepresent = CITIES_DISTANCE_SET_BY_LIST_OF_COORDINATES): |
149
|
|
|
algorithm_params = get_algo_params(params) |
150
|
|
|
|
151
|
|
|
ccore = load_core(); |
152
|
|
|
|
153
|
|
|
if citiesDistRepresent == CITIES_DISTANCE_SET_BY_MATRIX: |
154
|
|
|
cities_coord = package_builder(cities, c_double).create(); # antcolony_tsp_prepare_matrix(cities); |
155
|
|
|
ccore.antcolony_tsp_process_by_matrix.restype = POINTER(pyclustering_package); |
156
|
|
|
result_package = ccore.antcolony_tsp_process_by_matrix(cities_coord, algorithm_params); |
157
|
|
|
else: |
158
|
|
|
cities_coord = package_builder(cities, c_double).create(); # antcolony_tsp_prepare_cities_list(cities); |
159
|
|
|
ccore.antcolony_tsp_process.restype = POINTER(pyclustering_package); |
160
|
|
|
result_package = ccore.antcolony_tsp_process(cities_coord, algorithm_params); |
161
|
|
|
|
162
|
|
|
result = package_extractor(result_package).extract(); |
163
|
|
|
ccore.free_pyclustering_package(result_package); |
164
|
|
|
|
165
|
|
|
return result; |
166
|
|
|
|
167
|
|
|
|