EmptySpaceSearch 0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: EmptySpaceSearch
|
|
3
|
+
Version: 0.1
|
|
4
|
+
Summary: Empty Space Search
|
|
5
|
+
Home-page: https://github.com/mariolpantunes/ess
|
|
6
|
+
Author: Mário Antunes
|
|
7
|
+
Author-email: mario.antunes@ua.pt
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.8
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Requires-Dist: numpy>=2.1.0
|
|
15
|
+
Requires-Dist: numba>=0.61.0
|
|
16
|
+
Requires-Dist: hnswlib>=0.8.0
|
|
17
|
+
Dynamic: license-file
|
|
18
|
+
|
|
19
|
+
# Empty Space Search (ESS)
|
|
20
|
+
MIT License
|
|
21
|
+
|
|
22
|
+
Copyright (c) 2025 Mário Antunes
|
|
23
|
+
|
|
24
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
25
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
26
|
+
in the Software without restriction, including without limitation the rights
|
|
27
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
28
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
29
|
+
furnished to do so, subject to the following conditions:
|
|
30
|
+
|
|
31
|
+
The above copyright notice and this permission notice shall be included in all
|
|
32
|
+
copies or substantial portions of the Software.
|
|
33
|
+
|
|
34
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
35
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
36
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
37
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
38
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
39
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
40
|
+
SOFTWARE.
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
emptyspacesearch-0.1.dist-info/licenses/LICENSE,sha256=MPNexf4bysfCPQ-bGY55IGzFD3c7yCnlRfma82vzpdA,1071
|
|
2
|
+
ess/__init__.py,sha256=yp56uah5EdIM2dWpI5OCb07z7Lm-rtouRknnC571roU,21
|
|
3
|
+
ess/ess.py,sha256=lsVX8h5q7xwotqGmqp7ceidyOMu1papdX_dPrdEuTXU,7411
|
|
4
|
+
emptyspacesearch-0.1.dist-info/METADATA,sha256=6DdMNRpW6PpL-kvUcEwaY07n6qIhTouEhCs4Vvbcrgo,1631
|
|
5
|
+
emptyspacesearch-0.1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
6
|
+
emptyspacesearch-0.1.dist-info/top_level.txt,sha256=0JxTCgMKPLKtp14wb1-RKisQPQWX7i96innZNvHBr-s,4
|
|
7
|
+
emptyspacesearch-0.1.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Mário Antunes
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
ess
|
ess/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import src.ess as ess
|
ess/ess.py
ADDED
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import numba
|
|
2
|
+
import hnswlib
|
|
3
|
+
import logging
|
|
4
|
+
import numpy as np
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
logging.basicConfig(level=logging.INFO, format='%(message)s')
|
|
8
|
+
logger = logging.getLogger(__name__)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@numba.jit(nopython=True, parallel=True, fastmath=True)
|
|
12
|
+
def clip(a, vmin, vmax):
|
|
13
|
+
return np.maximum(np.minimum(a, vmax), vmin)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@numba.jit(nopython=True, parallel=True, fastmath=True)
|
|
17
|
+
def mean(a):
|
|
18
|
+
return np.divide(np.sum(a), a.shape[0])
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _scale(arr, min_val=None, max_val=None):
|
|
22
|
+
if min_val is None:
|
|
23
|
+
min_val = min(arr)
|
|
24
|
+
|
|
25
|
+
if max_val is None:
|
|
26
|
+
max_val = max(arr)
|
|
27
|
+
|
|
28
|
+
scl_arr = (arr - min_val) / (max_val - min_val)
|
|
29
|
+
return scl_arr, min_val, max_val
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _inv_scale(scl_arr, min_val, max_val):
|
|
33
|
+
return scl_arr*(max_val - min_val) + min_val
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@numba.jit(nopython=True, parallel=True, fastmath=True)
|
|
37
|
+
def _force(sigma, d):
|
|
38
|
+
"""
|
|
39
|
+
Optimized Force function.
|
|
40
|
+
"""
|
|
41
|
+
#ratio = sigma / d # Reuse this computation
|
|
42
|
+
#np.clip(ratio, a_min=None, a_max=3.1622, out=ratio) # Avoids overflow
|
|
43
|
+
#ratio = clip(sigma / d, 0, 3.1622)
|
|
44
|
+
ratio = np.minimum(sigma/d, 3.1622)
|
|
45
|
+
#attrac = ratio ** 6
|
|
46
|
+
#np.clip(attrac, a_min=None, a_max=1000, out=attrac) # Avoids overflow
|
|
47
|
+
#attrac = clip(attrac, 0, 1000)
|
|
48
|
+
attrac = np.minimum(ratio ** 6, 1000)
|
|
49
|
+
|
|
50
|
+
return np.abs(6 * (2 * attrac ** 2 - attrac) / d)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _elastic(es, neighbors, neighbors_dist):
|
|
54
|
+
"""
|
|
55
|
+
Optimized Elastic force with vectorization.
|
|
56
|
+
"""
|
|
57
|
+
sigma = mean(neighbors_dist) / 5.0
|
|
58
|
+
neighbors_dist = np.maximum(neighbors_dist, 0.001) # Avoids distances < 0.001
|
|
59
|
+
|
|
60
|
+
# Vectorized force computation
|
|
61
|
+
forces = _force(sigma, neighbors_dist)
|
|
62
|
+
|
|
63
|
+
# Vectorized displacement computation
|
|
64
|
+
vecs = (es - neighbors) / neighbors_dist[:, np.newaxis]
|
|
65
|
+
|
|
66
|
+
# Compute the directional force
|
|
67
|
+
#TODO: check this one
|
|
68
|
+
direc = np.sum(vecs * forces[:, np.newaxis], axis=0)
|
|
69
|
+
|
|
70
|
+
return direc
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def _empty_center(coor, data, neigh, movestep, iternum:int=100, bounds=np.array([[-1, 1]])):
|
|
74
|
+
"""
|
|
75
|
+
Empty center search process.
|
|
76
|
+
"""
|
|
77
|
+
|
|
78
|
+
for i in range(iternum):
|
|
79
|
+
adjs_, distances_ = neigh.knn_query(coor, k=data.shape[1]+1)
|
|
80
|
+
|
|
81
|
+
logger.debug(f'Empty Centers {adjs_} {distances_}')
|
|
82
|
+
|
|
83
|
+
direc = _elastic(coor, data[adjs_[0]], distances_[0])
|
|
84
|
+
mag = np.linalg.norm(direc)
|
|
85
|
+
if mag < 1e-7:
|
|
86
|
+
break
|
|
87
|
+
direc /= mag
|
|
88
|
+
coor += direc * movestep
|
|
89
|
+
|
|
90
|
+
# TODO (4): should the bounds be fixed to [0, 1]?
|
|
91
|
+
# may help code here
|
|
92
|
+
if (coor < bounds[:, 0]).any() or (coor > bounds[:, 1]).any():
|
|
93
|
+
np.clip(coor, bounds[:, 0], bounds[:, 1], out=coor)
|
|
94
|
+
break
|
|
95
|
+
|
|
96
|
+
return coor
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def _esa_01(samples, bounds, n:int=None, seed:int=None):
|
|
100
|
+
'''
|
|
101
|
+
apply esa in the experiment
|
|
102
|
+
'''
|
|
103
|
+
min_val = bounds[:,0]
|
|
104
|
+
max_val = bounds[:,1]
|
|
105
|
+
samples, _, _ = _scale(samples, min_val, max_val)
|
|
106
|
+
|
|
107
|
+
neigh = hnswlib.Index(space='l2', dim=samples.shape[1])
|
|
108
|
+
if seed is not None:
|
|
109
|
+
neigh.init_index(max_elements=len(samples)+n, ef_construction = 200, M=48,
|
|
110
|
+
random_seed = seed)
|
|
111
|
+
else:
|
|
112
|
+
neigh.init_index(max_elements=len(samples)+n, ef_construction = 200, M=48)
|
|
113
|
+
neigh.add_items(samples)
|
|
114
|
+
|
|
115
|
+
#TODO (2): improve by adding one point at a time (avoiding clustering points together)
|
|
116
|
+
coors = np.random.uniform(0, 1, (n, samples.shape[1]))
|
|
117
|
+
logger.debug(f'Coors({n}, {samples.shape[1]})\n{coors}')
|
|
118
|
+
es_params = []
|
|
119
|
+
logger.debug(f'Samples\n{samples}')
|
|
120
|
+
es_params = [_empty_center(coor.reshape(1, -1), samples, neigh,
|
|
121
|
+
movestep=0.01, iternum=100, bounds=np.array([[0, 1]]))[0] for coor in coors]
|
|
122
|
+
logger.debug(f'Params({len(es_params)})\n{es_params}')
|
|
123
|
+
#rv = np.array(es_params)[:n]
|
|
124
|
+
rv = np.array(es_params)
|
|
125
|
+
rv = _inv_scale(rv, min_val=min_val, max_val=max_val)
|
|
126
|
+
|
|
127
|
+
logger.debug(f'RV({rv.shape})\n{rv}')
|
|
128
|
+
|
|
129
|
+
return rv
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def _esa_02(samples, bounds, n:int=None, seed:int=None):
|
|
133
|
+
'''
|
|
134
|
+
apply esa in the experiment
|
|
135
|
+
'''
|
|
136
|
+
min_val = bounds[:,0]
|
|
137
|
+
max_val = bounds[:,1]
|
|
138
|
+
samples, _, _ = _scale(samples, min_val, max_val)
|
|
139
|
+
|
|
140
|
+
neigh = hnswlib.Index(space='l2', dim=samples.shape[1])
|
|
141
|
+
if seed is not None:
|
|
142
|
+
neigh.init_index(max_elements=len(samples)+n, ef_construction = 200, M=48,
|
|
143
|
+
random_seed = seed)
|
|
144
|
+
else:
|
|
145
|
+
neigh.init_index(max_elements=len(samples)+n, ef_construction = 200, M=48)
|
|
146
|
+
neigh.add_items(samples)
|
|
147
|
+
|
|
148
|
+
#TODO (2): improve by adding one point at a time (avoiding clustering points together)
|
|
149
|
+
coors = np.random.uniform(0, 1, (n, samples.shape[1]))
|
|
150
|
+
logger.debug(f'Coors({n}, {samples.shape[1]})\n{coors}')
|
|
151
|
+
es_params = []
|
|
152
|
+
logger.debug(f'Samples\n{samples}')
|
|
153
|
+
for c in coors:
|
|
154
|
+
es_param = _empty_center(c.reshape(1, -1), samples, neigh,
|
|
155
|
+
movestep=0.01, iternum=100, bounds=np.array([[0, 1]]))
|
|
156
|
+
es_params.append(es_param[0])
|
|
157
|
+
samples = np.concatenate((samples, es_param), axis=0)
|
|
158
|
+
#samples = np.append(samples, es_param)
|
|
159
|
+
logger.debug(f'Samples\n{samples}')
|
|
160
|
+
neigh.add_items(es_param)
|
|
161
|
+
#es_params = [_empty_center(coor.reshape(1, -1), samples, neigh,
|
|
162
|
+
#movestep=0.01, iternum=100, bounds=np.array([[0, 1]]))[0] for coor in coors]
|
|
163
|
+
logger.debug(f'Params({len(es_params)})\n{es_params}')
|
|
164
|
+
#rv = np.array(es_params)[:n]
|
|
165
|
+
rv = np.array(es_params)
|
|
166
|
+
rv = _inv_scale(rv, min_val=min_val, max_val=max_val)
|
|
167
|
+
|
|
168
|
+
logger.debug(f'RV({rv.shape})\n{rv}')
|
|
169
|
+
|
|
170
|
+
return rv
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
def esa(samples, bounds, n:int=None, seed:int=None):
|
|
174
|
+
'''
|
|
175
|
+
apply esa in the experiment
|
|
176
|
+
'''
|
|
177
|
+
min_val = bounds[:,0]
|
|
178
|
+
max_val = bounds[:,1]
|
|
179
|
+
samples, _, _ = _scale(samples, min_val, max_val)
|
|
180
|
+
samples = samples.astype(np.float32)
|
|
181
|
+
|
|
182
|
+
neigh = hnswlib.Index(space='l2', dim=samples.shape[1])
|
|
183
|
+
if seed is not None:
|
|
184
|
+
neigh.init_index(max_elements=len(samples)+n, ef_construction = 200, M=48,
|
|
185
|
+
random_seed = seed)
|
|
186
|
+
else:
|
|
187
|
+
neigh.init_index(max_elements=len(samples)+n, ef_construction = 200, M=48)
|
|
188
|
+
|
|
189
|
+
#TODO: apply seed number here
|
|
190
|
+
coors = np.random.uniform(0, 1, (n, samples.shape[1])).astype(np.float32)
|
|
191
|
+
# increase the sample pool and keep original size as idx
|
|
192
|
+
idx = len(samples)
|
|
193
|
+
samples = np.concatenate((samples, coors), axis=0)
|
|
194
|
+
neigh.add_items(samples)
|
|
195
|
+
|
|
196
|
+
iternum = 100
|
|
197
|
+
movestep=0.01
|
|
198
|
+
|
|
199
|
+
for _ in range(iternum):
|
|
200
|
+
for i in range(idx, len(samples)):
|
|
201
|
+
p = samples[i]
|
|
202
|
+
|
|
203
|
+
adjs_, distances_ = neigh.knn_query(p, k=samples.shape[1]+2)
|
|
204
|
+
direc = _elastic(p, samples[adjs_[0, 1:]], distances_[0, 1:])
|
|
205
|
+
p += (direc/np.linalg.norm(direc)) * movestep
|
|
206
|
+
|
|
207
|
+
samples[i] = p
|
|
208
|
+
|
|
209
|
+
samples = clip(samples, 0, 1)
|
|
210
|
+
neigh = hnswlib.Index(space='l2', dim=samples.shape[1])
|
|
211
|
+
if seed is not None:
|
|
212
|
+
neigh.init_index(max_elements=len(samples)+n, ef_construction = 200, M=48,
|
|
213
|
+
random_seed = seed)
|
|
214
|
+
else:
|
|
215
|
+
neigh.init_index(max_elements=len(samples)+n, ef_construction = 200, M=48)
|
|
216
|
+
neigh.add_items(samples)
|
|
217
|
+
|
|
218
|
+
rv = samples[idx:]
|
|
219
|
+
rv = _inv_scale(rv, min_val=min_val, max_val=max_val)
|
|
220
|
+
|
|
221
|
+
return rv
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
def ess(samples, bounds, n:int=None, seed:int=None):
|
|
225
|
+
if type(samples) is not np.ndarray:
|
|
226
|
+
samples = np.array(samples).astype(np.float32)
|
|
227
|
+
rv = esa(samples=samples, bounds=bounds, n=n, seed=seed)
|
|
228
|
+
return np.concatenate((samples, rv), axis=0)
|