Completed
Push — master ( 1a2f6f...14214b )
by Andrei
01:29
created

pyclustering.core.antcolony_tsp_process()   B

Complexity

Conditions 2

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 2
dl 0
loc 34
rs 8.8571
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...
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