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-2016 |
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
|
|
|
from pyclustering.core.wrapper import *; |
|
|
|
|
27
|
|
|
|
28
|
|
|
|
29
|
|
|
class c_antcolony_tsp_parameters(Structure): |
30
|
|
|
""" |
31
|
|
|
double q; |
32
|
|
|
double ro; |
33
|
|
|
double alpha; |
34
|
|
|
double beta; |
35
|
|
|
double gamma; |
36
|
|
|
double initial_pheramone; |
37
|
|
|
unsigned int iterations; |
38
|
|
|
unsigned int ants_per_iteration; |
39
|
|
|
|
40
|
|
|
""" |
41
|
|
|
_fields_ = [("q" , c_double), |
42
|
|
|
("ro" , c_double), |
43
|
|
|
("alpha" , c_double), |
44
|
|
|
("beta" , c_double), |
45
|
|
|
("gamma" , c_double), |
46
|
|
|
("qinit_pheramone" , c_double), |
47
|
|
|
("iterations" , c_uint), |
48
|
|
|
("ants_per_iteration" , c_uint) ]; |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
class c_antcolony_tsp_objects(Structure): |
52
|
|
|
""" |
53
|
|
|
unsigned int size; |
54
|
|
|
unsigned int dimension; |
55
|
|
|
double *data; |
56
|
|
|
|
57
|
|
|
""" |
58
|
|
|
_fields_ = [("size" , c_uint), |
59
|
|
|
("dimension" , c_uint), |
60
|
|
|
("data" , POINTER(c_double)) ]; |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
class c_antcolony_tsp_result(Structure): |
64
|
|
|
""" |
65
|
|
|
unsigned int size; |
66
|
|
|
double path_length; |
67
|
|
|
unsigned int *cities_num; |
68
|
|
|
|
69
|
|
|
""" |
70
|
|
|
_fields_ = [("size" , c_uint), |
71
|
|
|
("path_length" , c_double), |
72
|
|
|
("cities_num" , POINTER(c_uint)) ]; |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
def antcolony_tsp_process(cities, params): |
76
|
|
|
dimension = len(cities[0]); |
77
|
|
|
|
78
|
|
|
cities_coord = c_antcolony_tsp_objects(); |
79
|
|
|
cities_coord.size = c_uint(len(cities) * dimension); |
80
|
|
|
cities_coord.dimension = c_uint(dimension); |
81
|
|
|
|
82
|
|
|
cities_coord.data = (c_double * cities_coord.size)(); |
83
|
|
|
|
84
|
|
|
for i in range(0, cities_coord.size): |
85
|
|
|
cities_coord.data[i] =cities[i // dimension][i % dimension]; |
86
|
|
|
|
87
|
|
|
cities_coord = pointer(cities_coord); |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
algorithm_params = c_antcolony_tsp_parameters(); |
91
|
|
|
algorithm_params.q = c_double(params.q); |
92
|
|
|
algorithm_params.ro = c_double(params.ro); |
93
|
|
|
algorithm_params.alpha = c_double(params.alpha); |
94
|
|
|
algorithm_params.beta = c_double(params.beta); |
95
|
|
|
algorithm_params.gamma = c_double(params.gamma); |
96
|
|
|
algorithm_params.qinit_pheramone = c_double(params.qinit_pheramone); |
97
|
|
|
algorithm_params.iterations = c_uint(params.iterations); |
98
|
|
|
algorithm_params.ants_per_iteration = c_uint(params.ants_per_iteration); |
99
|
|
|
|
100
|
|
|
algorithm_params = pointer(algorithm_params); |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
ccore = cdll.LoadLibrary(PATH_DLL_CCORE_64); |
104
|
|
|
result = ccore.ant_colony_TSP(cities_coord, algorithm_params); |
105
|
|
|
|
106
|
|
|
result = cast(result, POINTER(c_antcolony_tsp_result))[0]; |
107
|
|
|
|
108
|
|
|
return result; |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
# cities = [[0.0, 0.0], [0.0, 1.0], [0.0, 2.0], [1.0, 0.0], [1.0, 1.0], [1.0, 2.0]]; |
112
|
|
|
# |
113
|
|
|
# params = c_antcolony_tsp_parameters(); |
114
|
|
|
# params.q = 1.5; |
115
|
|
|
# params.ro = 0.7; |
116
|
|
|
# params.alpha = 1.0; |
117
|
|
|
# params.beta = 1.0; |
118
|
|
|
# params.gamma = 2.0; |
119
|
|
|
# params.qinitial_pheramone = 0.1; |
120
|
|
|
# params.iterations = 50; |
121
|
|
|
# params.ants_per_iteration = 10; |
122
|
|
|
# |
123
|
|
|
# res = antcolony_tsp_process(cities, params) |
124
|
|
|
# |
125
|
|
|
# print ("Result :") |
126
|
|
|
# print (res.size) |
127
|
|
|
# print (res.path_length) |
128
|
|
|
# for i in range(res.size): |
129
|
|
|
# print (res.cities_num[i]) |
130
|
|
|
|
131
|
|
|
|