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
|
|
|
import types; |
|
|
|
|
29
|
|
|
|
30
|
|
|
class c_antcolony_tsp_parameters(Structure): |
31
|
|
|
""" |
32
|
|
|
double q; |
33
|
|
|
double ro; |
34
|
|
|
double alpha; |
35
|
|
|
double beta; |
36
|
|
|
double gamma; |
37
|
|
|
double initial_pheramone; |
38
|
|
|
unsigned int iterations; |
39
|
|
|
unsigned int ants_per_iteration; |
40
|
|
|
|
41
|
|
|
""" |
42
|
|
|
_fields_ = [("q" , c_double), |
43
|
|
|
("ro" , c_double), |
44
|
|
|
("alpha" , c_double), |
45
|
|
|
("beta" , c_double), |
46
|
|
|
("gamma" , c_double), |
47
|
|
|
("qinit_pheramone" , c_double), |
48
|
|
|
("iterations" , c_uint), |
49
|
|
|
("ants_per_iteration" , c_uint) ]; |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
class c_antcolony_tsp_objects(Structure): |
53
|
|
|
""" |
54
|
|
|
unsigned int size; |
55
|
|
|
unsigned int dimension; |
56
|
|
|
double *data; |
57
|
|
|
|
58
|
|
|
""" |
59
|
|
|
_fields_ = [("size" , c_uint), |
60
|
|
|
("dimension" , c_uint), |
61
|
|
|
("data" , POINTER(c_double)) ]; |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
class c_antcolony_tsp_result(Structure): |
65
|
|
|
""" |
66
|
|
|
unsigned int size; |
67
|
|
|
double path_length; |
68
|
|
|
unsigned int *cities_num; |
69
|
|
|
|
70
|
|
|
""" |
71
|
|
|
_fields_ = [("size" , c_uint), |
72
|
|
|
("path_length" , c_double), |
73
|
|
|
("object_sequence" , POINTER(c_uint)) ]; |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
def antcolony_tsp_process(cities, params): |
77
|
|
|
dimension = len(cities[0]); |
78
|
|
|
|
79
|
|
|
cities_coord = c_antcolony_tsp_objects(); |
80
|
|
|
cities_coord.size = c_uint(len(cities) * dimension); |
81
|
|
|
cities_coord.dimension = c_uint(dimension); |
82
|
|
|
|
83
|
|
|
cities_coord.data = (c_double * cities_coord.size)(); |
84
|
|
|
|
85
|
|
|
for i in range(0, cities_coord.size): |
86
|
|
|
cities_coord.data[i] = cities[i // dimension][i % dimension]; |
87
|
|
|
|
88
|
|
|
cities_coord = pointer(cities_coord); |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
algorithm_params = c_antcolony_tsp_parameters(); |
92
|
|
|
algorithm_params.q = c_double(params.q); |
93
|
|
|
algorithm_params.ro = c_double(params.ro); |
94
|
|
|
algorithm_params.alpha = c_double(params.alpha); |
95
|
|
|
algorithm_params.beta = c_double(params.beta); |
96
|
|
|
algorithm_params.gamma = c_double(params.gamma); |
97
|
|
|
algorithm_params.qinit_pheramone = c_double(params.qinit_pheramone); |
98
|
|
|
algorithm_params.iterations = c_uint(params.iterations); |
99
|
|
|
algorithm_params.ants_per_iteration = c_uint(params.ants_per_iteration); |
100
|
|
|
|
101
|
|
|
algorithm_params = pointer(algorithm_params); |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
ccore = cdll.LoadLibrary(PATH_DLL_CCORE_64); |
105
|
|
|
result = ccore.ant_colony_tsp_process(cities_coord, algorithm_params); |
106
|
|
|
|
107
|
|
|
result = cast(result, POINTER(c_antcolony_tsp_result))[0]; |
108
|
|
|
|
109
|
|
|
return result; |
110
|
|
|
|
111
|
|
|
|
112
|
|
|
def antcolony_tsp_destroy(tsp_result_pointer): |
113
|
|
|
ccore = cdll.LoadLibrary(PATH_DLL_CCORE_64); |
114
|
|
|
ccore.ant_colony_tsp_destroy(tsp_result_pointer); |
115
|
|
|
|