Completed
Push — master ( 27be37...a60125 )
by Andrei
01:07
created

antcolony_tsp_destroy()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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 *;
0 ignored issues
show
Unused Code introduced by
c_short 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
c_ushort 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
c_void_p 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
wstring_at 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
c_voidp 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
c_long was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
PATH_PYCLUSTERING_CCORE_LIBRARY 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
LittleEndianStructure 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
resize 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
sys 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_uint64 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
memset was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_ssize_t 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
SetPointerType 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
set_errno 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
DllGetClassObject 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
CFUNCTYPE 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
cdll 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
c_int 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
byref 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
pydll 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_size_t 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
HRESULT 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
create_unicode_buffer 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
GetLastError 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
ArgumentError 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
CDLL 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_float 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
c_int32 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
get_errno 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
c_char_p 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
PYFUNCTYPE 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
Array 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
c_uint8 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
windll 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
os 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
addressof 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
BigEndianStructure 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
c_ulong 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
py_object 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
c_wchar was imported with wildcard, but is not used.
Loading history...
28
29
import types;
0 ignored issues
show
Unused Code introduced by
The import types seems to be unused.
Loading history...
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