Completed
Push — master ( 61f52c...bdaec8 )
by
unknown
01:26
created

antcolony_tsp_prepare_matrix()   A

Complexity

Conditions 3

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 3
dl 0
loc 18
rs 9.4285
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 *;
0 ignored issues
show
Unused Code introduced by
c_ssize_t was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_wchar_p was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_bool was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
clustering_result was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
DEFAULT_MODE was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
pythonapi was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
alignment was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
addressof was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
destroy_object was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
kmeans was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
sizeof was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_char_p was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
dynamic_result was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
byref was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_uint64 was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_longdouble was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
Array was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
WINFUNCTYPE was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
windll was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_short was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
kind was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
BigEndianStructure was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_ulong was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
OleDLL was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
string_at was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_longlong was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
HRESULT was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_size_t was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
hierarchical was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_ubyte was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
create_string_buffer was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_int64 was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
ARRAY was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
cluster_representation was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
create_unicode_buffer was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_buffer was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
py_object was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
os was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_byte was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
WinDLL was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_ulonglong was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
pydll was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
set_errno was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
extract_dynamics was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
oledll was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
DllCanUnloadNow was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
rock was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
hsyncnet_analyser_destroy was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
data_representation was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
cure was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
GetLastError was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
core was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
CFUNCTYPE was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_int8 was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_char was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_uint32 was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
wstring_at was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
xmeans was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
set_last_error was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
extract_pyclustering_package was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_void_p was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_int was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
SetPointerType was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_uint16 was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
pyclustering_package was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
WinError was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
ArgumentError was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
get_errno was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
hsyncnet_process was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
FormatError was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_long was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
PyDLL was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
RTLD_LOCAL was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
memmove was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_int32 was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
PYFUNCTYPE was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_ushort was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
get_last_error was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
create_pointer_data was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_wchar was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
resize was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
extract_clusters was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
pyclustering_type_data was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
LibraryLoader was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
hsyncnet_destroy_network was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
Union was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
memset was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_float was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
DllGetClassObject was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_uint8 was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_voidp was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
LittleEndianStructure was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_int16 was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
dbscan was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
RTLD_GLOBAL was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
CDLL was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
hsyncnet_create_network was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
kmedians was imported with wildcard, but is not used.
Loading history...
27
28
import types;
0 ignored issues
show
Unused Code introduced by
The import types seems to be unused.
Loading history...
29
30
31
CITIES_DISTANCE_SET_BY_MATRIX = True
32
CITIES_DISTANCE_SET_BY_LIST_OF_COORDINATES = False
33
34
35
class c_antcolony_tsp_parameters(Structure):
36
    """
37
    double                  q;
38
    double                  ro;
39
    double                  alpha;
40
    double                  beta;
41
    double                  gamma;
42
    double                  initial_pheramone;
43
    unsigned int            iterations;
44
    unsigned int            ants_per_iteration;
45
    
46
    """
47
    _fields_ = [("q"        , c_double),
48
                ("ro"       , c_double),
49
                ("alpha"    , c_double),
50
                ("beta"     , c_double),
51
                ("gamma"    , c_double),
52
                ("qinit_pheramone"          , c_double),
53
                ("iterations"               , c_uint),
54
                ("ants_per_iteration"       , c_uint)    ];
55
56
57
class c_antcolony_tsp_objects(Structure):
58
    """
59
    unsigned int            size;
60
    unsigned int            dimension;
61
    double                  *data;
62
    
63
    """
64
    _fields_ = [("size"        , c_uint),
65
                ("dimension"   , c_uint),
66
                ("data"        , POINTER(c_double)) ];
67
68
69
class c_antcolony_tsp_matrix(Structure):
70
    """
71
    unsigned int            size;
72
    double                  **data;
73
    
74
    """
75
    _fields_ = [("size"        , c_uint),
76
                ("data"        , POINTER(POINTER(c_double))) ];
77
                
78
79
class c_antcolony_tsp_result(Structure):
80
    """
81
    unsigned int            size;
82
    double                  path_length;
83
    unsigned int            *cities_num;
84
    
85
    """
86
    _fields_ = [("size"              , c_uint),
87
                ("path_length"       , c_double),
88
                ("object_sequence"   , POINTER(c_uint)) ];
89
90
91
92
def get_algo_params(params):
93
    
94
    algorithm_params = c_antcolony_tsp_parameters();
95
    algorithm_params.q          = c_double(params.q);
96
    algorithm_params.ro         = c_double(params.ro);
97
    algorithm_params.alpha      = c_double(params.alpha);
98
    algorithm_params.beta       = c_double(params.beta);
99
    algorithm_params.gamma      = c_double(params.gamma);
100
    algorithm_params.qinit_pheramone            = c_double(params.qinit_pheramone);
101
    algorithm_params.iterations                 = c_uint(params.iterations);
102
    algorithm_params.ants_per_iteration         = c_uint(params.ants_per_iteration);
103
    
104
    algorithm_params = pointer(algorithm_params);
105
    
106
    return algorithm_params
107
108
109
def antcolony_tsp_prepare_matrix(matrix):
110
    dist_matrix = c_antcolony_tsp_matrix()
111
    
112
    dist_matrix.size = len(matrix)
113
    
114
    p_dist = (POINTER(c_double) * dist_matrix.size)();
115
     
116
    for i in range(dist_matrix.size):
117
        tmp_p_dist = (c_double * dist_matrix.size)();
118
        for j in range(dist_matrix.size):
119
            tmp_p_dist[j] = matrix[i][j];
120
            
121
        p_dist[i] = cast(tmp_p_dist, POINTER(c_double));
122
    
123
    dist_matrix.data = cast(p_dist, POINTER(POINTER(c_double)));
124
    dist_matrix = pointer(dist_matrix)
125
    
126
    return dist_matrix
127
    
128
    
129
def antcolony_tsp_prepare_cities_list(cities):
130
    dimension = len(cities[0]);
131
    
132
    cities_coord = c_antcolony_tsp_objects();
133
    cities_coord.size = c_uint(len(cities) * dimension);
134
    cities_coord.dimension = c_uint(dimension);
135
    
136
    cities_coord.data = (c_double * cities_coord.size)();
137
    
138
    for i in range(0, cities_coord.size):
139
        cities_coord.data[i] = cities[i // dimension][i % dimension];
140
    
141
    cities_coord = pointer(cities_coord);
142
    
143
    return cities_coord
144
145
146
def antcolony_tsp_process(cities, params, citiesDistRepresent = CITIES_DISTANCE_SET_BY_LIST_OF_COORDINATES):
147
    algorithm_params = get_algo_params(params)
148
    
149
    ccore = cdll.LoadLibrary(PATH_DLL_CCORE_64);
150
     
151
    if citiesDistRepresent == CITIES_DISTANCE_SET_BY_MATRIX:
152
        cities_coord = antcolony_tsp_prepare_matrix(cities)
153
        result_pointer = ccore.ant_colony_tsp_process_by_matrix(cities_coord, algorithm_params);
154
    else:
155
        cities_coord = antcolony_tsp_prepare_cities_list(cities)
156
        result_pointer = ccore.ant_colony_tsp_process(cities_coord, algorithm_params);
157
    
158
    result = cast(result_pointer, POINTER(c_antcolony_tsp_result))[0];
159
    
160
    return (result_pointer, result);
161
162
163
def antcolony_tsp_destroy(tsp_result_pointer):
164
    ccore = cdll.LoadLibrary(PATH_DLL_CCORE_64);
165
    ccore.ant_colony_tsp_destroy(tsp_result_pointer);
166