File dicebox.py changed (mode: 100644) (index ecdf0b0..8872faf) |
... |
... |
from . import f_models # for adaptive censoring |
22 |
22 |
from scipy import stats # for adaptive censoring |
from scipy import stats # for adaptive censoring |
23 |
23 |
from scipy import optimize # for BlackSimpleX |
from scipy import optimize # for BlackSimpleX |
24 |
24 |
|
|
25 |
|
import inspect # for ._log() function |
|
26 |
25 |
|
|
27 |
26 |
from .samplebox import SampleBox # for candidates packing |
from .samplebox import SampleBox # for candidates packing |
28 |
27 |
from . import simplex as sx |
from . import simplex as sx |
|
... |
... |
class GuessBox: |
101 |
100 |
else: |
else: |
102 |
101 |
print('GuessBox is in air mode') |
print('GuessBox is in air mode') |
103 |
102 |
|
|
104 |
|
|
|
|
103 |
|
#оӵ дӥсё |
105 |
104 |
class DiceBox: |
class DiceBox: |
106 |
105 |
""" |
""" |
107 |
106 |
DiceBox pěčlivě ukladá věškerá data, |
DiceBox pěčlivě ukladá věškerá data, |
|
... |
... |
class DiceBox: |
111 |
110 |
""" |
""" |
112 |
111 |
def __init__(bx, sample_box): |
def __init__(bx, sample_box): |
113 |
112 |
bx.sample_box = sample_box |
bx.sample_box = sample_box |
114 |
|
# not really needed |
|
115 |
|
bx.f = sample_box.sampled_plan() |
|
116 |
|
# sample density (just helpful thing) |
|
117 |
|
bx.h = bx.f |
|
118 |
|
# don't ask me |
|
119 |
|
bx.candidates = sample_box.sampled_plan() |
|
|
113 |
|
|
|
114 |
|
# user candidates |
|
115 |
|
#ё шоб было |
|
116 |
|
bx.candidates = CandyBox(bx.f_model()) |
120 |
117 |
|
|
121 |
118 |
# nové uložiště odhadů zadám explicitně, aby se pak |
# nové uložiště odhadů zadám explicitně, aby se pak |
122 |
119 |
# odhady v stm kodu přířazovaly správné krabičce |
# odhady v stm kodu přířazovaly správné krabičce |
|
... |
... |
class DiceBox: |
143 |
140 |
""" |
""" |
144 |
141 |
# I do not see nothing illegal here |
# I do not see nothing illegal here |
145 |
142 |
# LHS_like_correction do right conversion |
# LHS_like_correction do right conversion |
146 |
|
return bx.LHS_like_correction(bx.h(1)) |
|
|
143 |
|
return bx.LHS_like_correction(bx.f_model(1)) |
147 |
144 |
|
|
148 |
145 |
def __getitem__(bx, slice): |
def __getitem__(bx, slice): |
149 |
146 |
# stačí vratit sample_box |
# stačí vratit sample_box |
|
... |
... |
class DiceBox: |
162 |
159 |
# По всем вопросам обращайтесь |
# По всем вопросам обращайтесь |
163 |
160 |
# на нашу горячую линию |
# на нашу горячую линию |
164 |
161 |
else: |
else: |
165 |
|
return getattr(bx.sample_box, attr) |
|
|
162 |
|
return getattr(dx.sample_box, attr) |
166 |
163 |
|
|
167 |
164 |
# just plot, green points, red points... |
# just plot, green points, red points... |
168 |
165 |
plot2D = plot.plot2D |
plot2D = plot.plot2D |
|
... |
... |
class DiceBox: |
172 |
169 |
|
|
173 |
170 |
# přidávání vzorků musí bejt explicitní! |
# přidávání vzorků musí bejt explicitní! |
174 |
171 |
def add_sample(bx, input_sample): |
def add_sample(bx, input_sample): |
175 |
|
bx._log("we have got new data:", str(input_sample)) |
|
|
172 |
|
bx._logger(msg="we have got new data:", data=input_sample) |
176 |
173 |
bx.sample_box.add_sample(input_sample) |
bx.sample_box.add_sample(input_sample) |
177 |
174 |
# tohle musí převest rozdělení vstupního vzorku na vlastní rozdělení skříňky |
# tohle musí převest rozdělení vstupního vzorku na vlastní rozdělení skříňky |
178 |
175 |
inner_sample = bx.sample_box.new_sample(input_sample) |
inner_sample = bx.sample_box.new_sample(input_sample) |
|
... |
... |
class DiceBox: |
183 |
180 |
bx._LHS_increment(input_sample) |
bx._LHS_increment(input_sample) |
184 |
181 |
|
|
185 |
182 |
def regen(bx): |
def regen(bx): |
186 |
|
# шайтан регенираци лэзьиз |
|
|
183 |
|
#ё шайтан регенираци лэзьиз |
187 |
184 |
bx._logger(msg='regeneration started') |
bx._logger(msg='regeneration started') |
|
185 |
|
bx._LHS_regen() |
|
186 |
|
|
|
187 |
|
def _LHS_regen(bx): |
188 |
188 |
# pro LHS_like_correction |
# pro LHS_like_correction |
189 |
189 |
bx.sorted_plan_U = [i for i in range(bx.nvar)] # just create list |
bx.sorted_plan_U = [i for i in range(bx.nvar)] # just create list |
190 |
190 |
for i in range(bx.nvar): |
for i in range(bx.nvar): |
|
... |
... |
class DiceBox: |
198 |
198 |
# what is input? |
# what is input? |
199 |
199 |
# as we need transformation anyway, |
# as we need transformation anyway, |
200 |
200 |
# I'll ask for conversion to f sample |
# I'll ask for conversion to f sample |
201 |
|
# Здесь вижу железную конвертацию до f-ка, |
|
202 |
|
# которая пройдёт по R координатам |
|
203 |
|
# Kruci drát, tady by se nemohlo nic posrat |
|
204 |
|
to_sample_node = bx.f.new_sample(input_sample) |
|
|
201 |
|
#ё Здесь вижу железную конвертацию до f-ка, |
|
202 |
|
#ё которая пройдёт по R координатам |
|
203 |
|
#č Kruci drát, tady by se nemohlo nic posrat |
|
204 |
|
to_sample_node = bx.f_model.new_sample(input_sample) |
205 |
205 |
|
|
206 |
206 |
LHS_node = np.empty(bx.nvar, dtype=float) |
LHS_node = np.empty(bx.nvar, dtype=float) |
207 |
207 |
for i in range(bx.nvar): |
for i in range(bx.nvar): |
|
... |
... |
class DiceBox: |
214 |
214 |
# vzdy |
# vzdy |
215 |
215 |
LHS_node[i] = (bx.sorted_plan_U[i][plan_index] + bx.sorted_plan_U[i][plan_index - 1]) / 2 |
LHS_node[i] = (bx.sorted_plan_U[i][plan_index] + bx.sorted_plan_U[i][plan_index - 1]) / 2 |
216 |
216 |
|
|
217 |
|
return bx.f.new_sample(LHS_node, 'U') |
|
|
217 |
|
return bx.f_model.new_sample(LHS_node, 'U') |
218 |
218 |
|
|
219 |
219 |
|
|
220 |
220 |
def _LHS_increment(bx, input_sample): |
def _LHS_increment(bx, input_sample): |
|
... |
... |
class DiceBox: |
224 |
224 |
bx.sorted_plan_U[i] = np.insert(bx.sorted_plan_U[i], plan_index, input_sample.U[j,i]) |
bx.sorted_plan_U[i] = np.insert(bx.sorted_plan_U[i], plan_index, input_sample.U[j,i]) |
225 |
225 |
|
|
226 |
226 |
|
|
227 |
|
def _log(bx, *msg, indent=0): |
|
228 |
|
print(bx.__class__.__name__ + ":", *msg) |
|
229 |
|
|
|
230 |
|
def _logi(bx, *msg, indent=1): |
|
231 |
|
print("\t"*indent, inspect.currentframe().f_back.f_code.co_name + ":", *msg) |
|
232 |
|
|
|
233 |
|
|
|
234 |
227 |
# The DiceBox Observer |
# The DiceBox Observer |
235 |
228 |
def _logger(self, *args, msg="", indent=0, **kwargs): |
def _logger(self, *args, msg="", indent=0, **kwargs): |
236 |
229 |
print(self.__class__.__name__ + ":" + msg, *args, kwargs) |
print(self.__class__.__name__ + ":" + msg, *args, kwargs) |
|
... |
... |
class DiceBox: |
247 |
240 |
|
|
248 |
241 |
|
|
249 |
242 |
|
|
250 |
|
class Censoring(DiceBox): |
|
251 |
|
def __init__(bx, sample_object, tri_space='Rn'): |
|
252 |
|
bx.tri_space = tri_space |
|
253 |
|
super().__init__(sample_object) |
|
254 |
|
|
|
255 |
|
def __repr__(bx): |
|
256 |
|
return "%s(%s, %s)"%(bx.__class__.__name__, repr(bx.sample_box), repr(bx.tri_space)) |
|
257 |
|
|
|
258 |
|
def __str__(bx): |
|
259 |
|
return str(bx.__class__.__name__) |
|
260 |
|
|
|
261 |
|
def increment(bx, input_sample): |
|
262 |
|
super().increment(input_sample) |
|
263 |
|
|
|
264 |
|
if "tri" in dir(bx): |
|
265 |
|
# tri - Deloneho triangulace |
|
266 |
|
# sample je jíž převeden na f (v .add_sample()), takže je to bezpěčný |
|
267 |
|
bx.tri.add_points(getattr(input_sample, bx.tri_space)) |
|
268 |
|
# print('increment se podaril') |
|
269 |
|
if len(bx.tri.coplanar): # pokud triangulace není v pořadku |
|
270 |
|
#print('triangulace v pořádku není') |
|
271 |
|
bx._log('triangulation is coplanar') |
|
272 |
|
else: |
|
273 |
|
bx._log('Triangulace (zatím?) neexistuje') |
|
274 |
|
bx.regen() |
|
275 |
|
|
|
276 |
|
|
|
277 |
|
def regen(bx): |
|
278 |
|
super().regen() |
|
279 |
|
#č chcu zachytit spadnuti QHull na začatku, kdy ještě není dostatek teček. |
|
280 |
|
#č Jinak je třeba nechat QHull spadnout |
|
281 |
|
if bx.nsim > 2*bx.nvar + 3: |
|
282 |
|
# tady je to OK |
|
283 |
|
bx.tri = spatial.Delaunay(getattr(bx.sampled_plan, bx.tri_space), incremental=True) |
|
284 |
|
if len(bx.tri.coplanar): |
|
285 |
|
#print('triangulace v pořádku není') |
|
286 |
|
bx._logger(msg='triangulation is coplanar') |
|
287 |
|
else: |
|
288 |
|
#print('triangulace je v pořádku') |
|
289 |
|
bx._logger(msg='triangulation is OK') |
|
290 |
|
|
|
291 |
|
else: # lze přípustit chybu triangulace |
|
292 |
|
try: |
|
293 |
|
bx.tri = spatial.Delaunay(getattr(bx.sampled_plan, bx.tri_space), incremental=True) |
|
294 |
|
if len(bx.tri.coplanar): |
|
295 |
|
#print('triangulace v pořádku není') |
|
296 |
|
bx._logger(msg='triangulation is coplanar') |
|
297 |
|
else: |
|
298 |
|
#print('triangulace je v pořádku') |
|
299 |
|
bx._logger(msg='triangulation is OK') |
|
300 |
|
except: |
|
301 |
|
bx._logger(msg='triangulation failed') |
|
302 |
|
|
|
303 |
|
|
|
304 |
|
|
|
305 |
|
# tato metoda je vlastně pro MinEnergyCensoredSampling |
|
306 |
|
# ale zde se taky může hodit |
|
307 |
|
def get_events(bx, simplices=None): |
|
308 |
|
""" |
|
309 |
|
Metoda musí simplexům přiřazovat jev |
|
310 |
|
0=success, 1=failure, 2=mix |
|
311 |
|
""" |
|
312 |
|
if simplices is None: |
|
313 |
|
simplices = bx.tri.simplices |
|
314 |
|
|
|
315 |
|
in_failure = np.isin(simplices, bx.failure_points) |
|
316 |
|
has_failure = in_failure.any(axis=1) |
|
317 |
|
all_failure = in_failure.all(axis=1) |
|
318 |
|
return np.int8(np.where(has_failure, np.where(all_failure, 1, 2), 0)) |
|
319 |
|
|
|
320 |
|
|
|
321 |
|
|
|
322 |
|
def __call__(bx): |
|
323 |
|
# je treba si uvedomit ze pravdepodobnost chytnuti muze byt min jak pf. V soucasne realizaci |
|
324 |
|
try: |
|
325 |
|
# očekávám, že projde kandidaty |
|
326 |
|
# vodkaď jsou - netuším |
|
327 |
|
to_sample, rate = bx.filter() |
|
328 |
|
# když nepovedlo s kandidaty, zkusíme sami nagenerovat |
|
329 |
|
while len(to_sample) == 0: # nekonečno |
|
330 |
|
# tak, děcka, server má spoustu času a nikam nespejchá |
|
331 |
|
# ale pokud tohle sežere věškerou paměť (ne když, ale kdy) |
|
332 |
|
# dostaneš co proto! |
|
333 |
|
# vomezíme pole na 10e6 (desitky mega teda vodhadově) |
|
334 |
|
# by bylo možně použit chytrejší vzoreček s logaritmem |
|
335 |
|
# ale snad tohle postačí |
|
336 |
|
to_sample, rate = bx.filter(bx.h(int(0.9999*rate + 100))) |
|
337 |
|
return bx.LHS_like_correction(to_sample) |
|
338 |
|
|
|
339 |
|
# chcu zachytit spadnuti QHull na začatku, kdy ještě není |
|
340 |
|
# dostatek teček. Je-li bx.tri fakticky existuje, tj. |
|
341 |
|
# triangulace jíž existovala - je třeba nechat QHull spadnout |
|
342 |
|
except AttributeError: |
|
343 |
|
# kdyby neco - berem kramle |
|
344 |
|
print("Triangulation doesn't exist. Censoring failed") |
|
345 |
|
return bx.LHS_like_correction(bx.h(1)) # it should be OK |
|
346 |
|
|
|
347 |
|
def filter(bx, candidates=[]): |
|
348 |
|
""" |
|
349 |
|
supports both sample and sample.R input |
|
350 |
|
|
|
351 |
|
logika metody, nebo, přesněji, implementaci |
|
352 |
|
je taková, že bule-li někdo-něco mimo doménu, |
|
353 |
|
tak funkce je vrátí a zbytek už neřeší |
|
354 |
|
""" |
|
355 |
|
# co to bylo na vstupu? |
|
356 |
|
# když nebyl žádný, |
|
357 |
|
# projdeme vlastními kandidaty |
|
358 |
|
if len(candidates)==0: |
|
359 |
|
candidates = bx.candidates |
|
360 |
|
|
|
361 |
|
|
|
362 |
|
# tady byl problém. Funkce byla původně navržena tak, |
|
363 |
|
# aby ji nezajimalo co je na vstupu |
|
364 |
|
# to ale nefunguje |
|
365 |
|
# další funkce jako výstup očekavají něco s validním R-kem |
|
366 |
|
candidates_to_sample_node = getattr(bx.f.new_sample(candidates), bx.tri_space) |
|
367 |
|
|
|
368 |
|
|
|
369 |
|
found_simplices = bx.tri.find_simplex(candidates_to_sample_node) |
|
370 |
|
# ouside of domain - it's easy |
|
371 |
|
outside = candidates[found_simplices < 0] |
|
372 |
|
if len(outside) > 0: |
|
373 |
|
# my hodnotili svých kandidatov? |
|
374 |
|
if bx.candidates == candidates: |
|
375 |
|
bx.candidates = outside |
|
376 |
|
return outside, len(candidates)/len(outside) |
|
377 |
|
|
|
378 |
|
# tady já chcu vrátit první vhodný vzorek a tím končít |
|
379 |
|
for candidate_id in range(len(candidates)): |
|
380 |
|
# simplex_id >= 0: # inside of domain |
|
381 |
|
simplex_id = found_simplices[candidate_id] |
|
382 |
|
simplex = bx.tri.simplices[simplex_id] |
|
383 |
|
|
|
384 |
|
# fp like a failure points. Number of failure points |
|
385 |
|
fp = len(np.setdiff1d(simplex, bx.failure_points)) |
|
386 |
|
|
|
387 |
|
# pokud je simplex není jednobarevny.. |
|
388 |
|
if (fp != 0) and (fp != bx.nvar+1): |
|
389 |
|
# my hodnotili svých kandidatov? |
|
390 |
|
if bx.candidates == candidates: |
|
391 |
|
bx.candidates = candidates[candidate_id:] |
|
392 |
|
return candidates[candidate_id], candidate_id |
|
393 |
|
|
|
394 |
|
# nepovedlo. nic |
|
395 |
|
# mě nenapadá žádný lepší způsob vrátit prázdnou matici |
|
396 |
|
return candidates[0:0], len(candidates) |
|
397 |
|
|
|
398 |
|
|
|
399 |
|
|
|
400 |
|
|
|
401 |
|
class AdaptiveCensoring(Censoring): |
|
402 |
|
def __init__(bx, sample_object, tri_space='Rn', pf_lim=(1,0)): |
|
403 |
|
bx._logger(msg="instance creating") |
|
404 |
|
bx.sball = sball.Sball(sample_object.nvar) |
|
405 |
|
bx.pf_lim = pf_lim |
|
406 |
|
|
|
407 |
|
bx.base_r = bx.sball.get_r(0.5) |
|
408 |
|
|
|
409 |
|
# pro jistotu pridame |
|
410 |
|
bx.simplex_index = {'failure':[], 'success':[], 'mix':[]} |
|
411 |
|
|
|
412 |
|
|
|
413 |
|
# overall estimations |
|
414 |
|
#bx.oiss = IS_stat.ISSI(['failure', 'success', 'out', 'mix']) |
|
415 |
|
# -1 = 'out', 0=success, 1=failure, 2=mix |
|
416 |
|
bx.oiss = IS_stat.ISSI([-1,0,1,2]) |
|
417 |
|
# current estimations |
|
418 |
|
bx.ciss = IS_stat.ISSI([-1,0,1,2]) |
|
419 |
|
|
|
420 |
|
super().__init__(sample_object, tri_space) |
|
421 |
|
|
|
422 |
|
|
|
423 |
|
def __repr__(bx): |
|
424 |
|
return "%s(%s, %s, %s)"%(bx.__class__.__name__, repr(bx.sample_box), repr(bx.tri_space), repr(bx.pf_lim)) |
|
425 |
|
|
|
426 |
|
def __str__(bx): |
|
427 |
|
return str(bx.__class__.__name__) |
|
428 |
|
|
|
429 |
|
def regen(bx): |
|
430 |
|
bx._logi(inspect.currentframe().f_back.f_code.co_name, "launched regeneration") |
|
431 |
|
|
|
432 |
|
super().regen() |
|
433 |
|
if bx.pf_lim[1] == 0: |
|
434 |
|
bx.drop_r = float("inf") |
|
435 |
|
else: |
|
436 |
|
bx.drop_r = bx.sball.get_r(bx.pf_lim[1]) |
|
437 |
|
|
|
438 |
|
# dropneme (pro jistotu) odhady |
|
439 |
|
bx.oiss = bx.ciss |
|
440 |
|
# -1 = 'out', 0=success, 1=failure, 2=mix |
|
441 |
|
bx.ciss = IS_stat.ISSI([-1,0,1,2]) |
|
442 |
|
|
|
443 |
|
# drop indexes |
|
444 |
|
bx.simplex_index = {'failure':[], 'success':[], 'mix':[]} |
|
445 |
|
|
|
446 |
|
def increment(bx, input_sample): |
|
447 |
|
super().increment(input_sample) |
|
448 |
|
|
|
449 |
|
# drop indexes |
|
450 |
|
bx.simplex_index = {'failure':[], 'success':[], 'mix':[]} |
|
451 |
|
|
|
452 |
|
# current estimations |
|
453 |
|
try: # to čo já vidím v kódu - ISSI slovníky se pokažde generujóu znovu, |
|
454 |
|
# není nutně je explicitně kopirovať |
|
455 |
|
bx.guessbox.guess('TRI_overall_estimations', bx.nsim-1, bx.oiss.estimations) |
|
456 |
|
bx.guessbox.guess('TRI_current_estimations', bx.nsim-1, bx.ciss.estimations) |
|
457 |
|
except AttributeError: |
|
458 |
|
bx.guessbox.guess('TRI_upper_pf', bx.nsim-1, 1) |
|
459 |
|
|
|
460 |
|
# a znovu začneme počítat |
|
461 |
|
# -1 = 'out', 0=success, 1=failure, 2=mix |
|
462 |
|
bx.ciss = IS_stat.ISSI([-1,0,1,2]) |
|
463 |
|
|
|
464 |
|
|
|
465 |
|
def __call__(bx): |
|
466 |
|
bx._logger(msg="we were asked for an recommendation") |
|
467 |
|
# je treba si uvedomit ze pravdepodobnost chytnuti muze byt min jak pf. V soucasne realizaci |
|
468 |
|
try: |
|
469 |
|
# očekávám, že projde kandidaty |
|
470 |
|
# odkaď jsou - netuším |
|
471 |
|
to_sample, rate, simplex_id = bx.filter() |
|
472 |
|
# když nepovedlo s kandidaty, zkusíme sami nagenerovat |
|
473 |
|
while len(to_sample) == 0: # nekonečno |
|
474 |
|
# pokusme se nastavit rate tak, abychom získali právě jedneho kandidata |
|
475 |
|
try: # try uvnitř traja |
|
476 |
|
p_rate = bx.oiss.estimations[-1] + bx.oiss.estimations[2] |
|
477 |
|
except: |
|
478 |
|
p_rate = bx.pf_lim[0] - bx.pf_lim[1] |
|
479 |
|
if p_rate < 1e-5: |
|
480 |
|
rate = 100000 |
|
481 |
|
else: |
|
482 |
|
rate = int(1/p_rate) + 1 |
|
483 |
|
to_sample, rate, simplex_id = bx.filter(bx.get_candidates(rate)) |
|
484 |
|
choose = bx.LHS_like_correction(to_sample) |
|
485 |
|
bx._log("finally we choose", str(choose), "of", simplex_id, "simplex") |
|
486 |
|
return choose |
|
487 |
|
|
|
488 |
|
# chcu zachytit spadnuti QHull na začatku, kdy ještě není |
|
489 |
|
# dostatek teček. Je-li bx.tri fakticky existuje, tj. |
|
490 |
|
# triangulace jíž existovala - je třeba nechat QHull spadnout |
|
491 |
|
except AttributeError: |
|
492 |
|
choose = bx.LHS_like_correction(bx.get_candidates(1)) |
|
493 |
|
if bx.nsim < bx.nvar + 1: # je to legální |
|
494 |
|
bx._log("we have no enough points to build triangulation, so", str(choose), "is our recommendation") |
|
495 |
|
return choose |
|
496 |
|
|
|
497 |
|
elif bx.nsim < 2*bx.nvar + 3: # to je ještě budiž |
|
498 |
|
bx._log("we have troubles with triangulation, so we offer random sample for now:", str(choose)) |
|
499 |
|
return choose |
|
500 |
|
else: # no to teda ne! |
|
501 |
|
raise ValueError("AdaptiveCensoring: s tou triangulací je fakt něco není v pořadku") |
|
502 |
|
|
|
503 |
|
|
|
504 |
|
def filter(bx, candidates=[]): |
|
505 |
|
""" |
|
506 |
|
za pvré, jako vstup očekávám kandidaty od .get_candidates() funkce, |
|
507 |
|
zabalené do полукустарного sample_boxu s zadaným .implicit_multiplicator |
|
508 |
|
(je to drobnost pro přesnějši zpracování sad IS IS_statem). |
|
509 |
|
|
|
510 |
|
Metoda musí souřádnicím přiřazovat jev |
|
511 |
|
"success", "failure", "mix", "outside" |
|
512 |
|
|
|
513 |
|
TATO metoda jakmile narazí na "mix" nebo "outside" |
|
514 |
|
ukladá zjištěné informace do ISSI a nalezeného kandidata vrací |
|
515 |
|
""" |
|
516 |
|
# co to bylo na vstupu? |
|
517 |
|
# když nebyl žádný, |
|
518 |
|
# projdeme vlastními kandidaty |
|
519 |
|
if len(candidates)==0: |
|
520 |
|
candidates = bx.candidates |
|
521 |
|
|
|
522 |
|
bx._logi("kandidaty:", candidates) |
|
523 |
|
|
|
524 |
|
# je třeba lokálně zachovat implicit_multiplicator |
|
525 |
|
# jinak se ztrací při slajsingu |
|
526 |
|
# nechceš přepsat SampleBox, Alexi? |
|
527 |
|
try: |
|
528 |
|
implicit_multiplicator = candidates.implicit_multiplicator |
|
529 |
|
except AttributeError: # kandidaty můžou bejt odkudkoliv |
|
530 |
|
# s nekonečným rozptylem nebudou mít váhu "absenční" jevy |
|
531 |
|
# moc to odhadům nepomůže, protože je-li kandidaty |
|
532 |
|
# nemajú .implicit_multiplicator |
|
533 |
|
# asi nebudou mať ani váhy IS v .g_values |
|
534 |
|
implicit_multiplicator = float("inf") |
|
535 |
|
bx._logi("Dobrý den, kandidaty nemajú .implicit_multiplicator")#. S pozdravem, AdaptiveCensoring") |
|
536 |
|
|
|
537 |
|
# tady byl problém. Funkce byla původně navržena tak, |
|
538 |
|
# aby ji nezajimalo co je na vstupu |
|
539 |
|
# to ale nefunguje |
|
540 |
|
# další funkce jako výstup očekavají něco s validním R-kem |
|
541 |
|
# no tj. já zde provádím posouzení transformací z R-ka vstupních souřadnic |
|
542 |
|
candidates_to_sample_node = getattr(bx.f.new_sample(candidates), bx.tri_space) |
|
543 |
|
|
|
544 |
|
|
|
545 |
|
current_simplices = bx.tri.find_simplex(candidates_to_sample_node) |
|
546 |
|
|
|
547 |
|
|
|
548 |
|
# tak bacha |
|
549 |
|
# budeme přepísovat jevy in-place |
|
550 |
|
found_simplices = np.ma.array(current_simplices.copy()) #.copy() |
|
551 |
|
# nemaskované - obsahuji číslo simplexu |
|
552 |
|
# maskované - číslo jevu |
|
553 |
|
# -1 = 'out', 0=success, 1=failure, 2=mix |
|
554 |
|
# tj. procházíme simplexy z náhodné sady vzorků, |
|
555 |
|
# nahrazujeme čislo simplexu odpovidajicím mu jevem |
|
556 |
|
# a skryváme ho |
|
557 |
|
# pote ty "skryté", "projduté" vzorky využiváme k žískání odhadů |
|
558 |
|
|
|
559 |
|
while len(current_simplices):# > 0: |
|
560 |
|
bx._logi("current simplices", current_simplices) |
|
561 |
|
# berem hned prvního kandidata |
|
562 |
|
# a posuzujeme co je zač |
|
563 |
|
simplex_id = current_simplices[0] |
|
564 |
|
mask = found_simplices==simplex_id |
|
565 |
|
|
|
566 |
|
if simplex_id < 0: # -1 means ouside |
|
567 |
|
# berem kramle |
|
568 |
|
break |
|
569 |
|
elif simplex_id in bx.simplex_index['success']: |
|
570 |
|
found_simplices[mask] = 0 |
|
571 |
|
elif simplex_id in bx.simplex_index['failure']: |
|
572 |
|
found_simplices[mask] = 1 |
|
573 |
|
elif simplex_id in bx.simplex_index['mix']: |
|
574 |
|
found_simplices[mask] = 2 |
|
575 |
|
# kramle |
|
576 |
|
break |
|
577 |
|
else: # no index information |
|
578 |
|
# tady já chcu vrátit první vhodný vzorek a tím končít |
|
579 |
|
# simplex_id >= 0: # inside of domain |
|
580 |
|
# asi tady získavam množinu s čísly vrcholů |
|
581 |
|
# kteří zakladají simplex |
|
582 |
|
simplex = bx.tri.simplices[simplex_id] |
|
583 |
|
|
|
584 |
|
# for debug |
|
585 |
|
bx._logi("провал индексу", simplex_id, indent=2) |
|
586 |
|
|
|
587 |
|
# fp like a failure points. Number of failure points |
|
588 |
|
# setdiff "Return the unique values in ar1 that are not in ar2." |
|
589 |
|
fp = len(np.setdiff1d(simplex, bx.failure_points)) |
|
590 |
|
|
|
591 |
|
if fp == bx.nvar+1: # |
|
592 |
|
bx.simplex_index['success'].append(simplex_id) |
|
593 |
|
found_simplices[mask] = 0 |
|
594 |
|
elif fp == 0: |
|
595 |
|
bx.simplex_index['failure'].append(simplex_id) |
|
596 |
|
found_simplices[mask] = 1 |
|
597 |
|
#print("failure simplex", simplex_id) |
|
598 |
|
else: |
|
599 |
|
bx.simplex_index['mix'].append(simplex_id) |
|
600 |
|
found_simplices[mask] = 2 |
|
601 |
|
bx._logi("mixed simplex", simplex_id) |
|
602 |
|
# bacha! kramle |
|
603 |
|
break |
|
604 |
|
|
|
605 |
|
# pridame do seznamu známého |
|
606 |
|
found_simplices[mask] = np.ma.masked |
|
607 |
|
# eště raz |
|
608 |
|
cmask = current_simplices==simplex_id |
|
609 |
|
# vyfiltrujeme |
|
610 |
|
current_simplices = current_simplices[~cmask] |
|
611 |
|
|
|
612 |
243 |
|
|
613 |
244 |
|
|
614 |
|
# zde je třeba перехватить ситуацию, куке одӥг но кандидат ӧвӧл |
|
615 |
|
# нужно ли? |
|
616 |
|
# if len(current_simplices) == 0: |
|
617 |
|
# # nepovedlo. nic |
|
618 |
|
# bx.candidates = candidates[0:0] |
|
619 |
|
# # mě nenapadá žádný lepší způsob vrátit prázdnou matici |
|
620 |
|
# return candidates[0:0], len(candidates), -2 # simple_id |
|
621 |
|
|
|
622 |
|
# nemaskované, včetně současného kandidata (nevím proč) - ke kandidatům |
|
623 |
|
# землю - крестьянам, фабрики - рабочим |
|
624 |
|
# předpokladam, že kandidaty jsou se všim všudy |
|
625 |
|
# s vahami (.g_value) a se svým .implicit_multiplicator'em |
|
626 |
|
## zde True hodnoty roušky - to co jíž bylo skryto |
|
627 |
|
bx.candidates = candidates[~np.ma.getmaskarray(found_simplices)][1:] # toho prvního prečo nechcem |
|
628 |
|
try: # na zacatku je tam prazdný f_model, kterému atribut pripsat nemůžeme |
|
629 |
|
bx.candidates.implicit_multiplicator = implicit_multiplicator |
|
630 |
|
except: |
|
631 |
|
pass |
|
632 |
|
|
|
633 |
|
# vrátíme kandidaty, všechny-ne všechny? |
|
634 |
|
# малы одӥг гинэ? уг тодӥськы чик... |
|
635 |
|
selected_candidate = candidates[~np.ma.getmaskarray(found_simplices)][:1] # chcem toho prvního |
|
636 |
|
|
|
637 |
|
|
|
638 |
|
# odešleme ISSI |
|
639 |
|
try: |
|
640 |
|
# pridame do seznamu známého |
|
641 |
|
# rouška musí zůstat z cyklu |
|
642 |
|
# proboha, Alexi, co to je za roušku, co se tu děje? |
|
643 |
|
# jakmile v tom hlavním cyklu nalezli jsme mix nebo outside |
|
644 |
|
# my hned z cyklu vylezli a je neskryli - abychom je vzali jako kandidaty |
|
645 |
|
# teď je však skryváme s tou "rouškou", co musela být před opuštěním cyklu nastavena |
|
646 |
|
# tak ISSI bude mít možnost odhadovat i pravděpodobnosti mix a outside |
|
647 |
|
found_simplices[mask] = np.ma.masked |
|
648 |
|
# zde získáme True hodnoty roušek |
|
649 |
|
# ukazatel |
|
650 |
|
imask = found_simplices.mask |
|
651 |
|
found_simplices.mask = ~imask # invertujem, dotkne to i samotnou imask |
|
652 |
|
events = found_simplices.compressed() |
|
653 |
|
print(candidates) |
|
654 |
|
print(candidates.g_values) |
|
655 |
|
print("imask", imask) |
|
656 |
|
print(candidates.g_values[~imask]) |
|
657 |
|
print(events) |
|
658 |
|
bx.oiss.add_IS_serie(candidates.g_values[~imask], events, implicit_multiplicator) |
|
659 |
|
print("global estimation", bx.oiss.estimations) |
|
660 |
|
bx.ciss.add_IS_serie(candidates.g_values[~imask], events, implicit_multiplicator) |
|
661 |
|
print("current estimation", bx.ciss.estimations) |
|
662 |
|
except AttributeError: |
|
663 |
|
bx._logi("Это вы мне прислали неваженых кандидатов?") |
|
664 |
|
except UnboundLocalError: # čo tu chybu způsobuje? |
|
665 |
|
# asi nebyly žádné kandidaty (třeba hned na začátku) |
|
666 |
|
assert len(candidates)==0 and len(bx.candidates)==0, "AdaptiveCensoring: Что за бурда с этими кандидатама?" |
|
667 |
|
return candidates, 0, -2 |
|
668 |
|
|
|
669 |
|
|
|
670 |
|
# rate = kolík bylo - kolik zůstalo |
|
671 |
|
return selected_candidate, len(candidates) - len(bx.candidates), simplex_id |
|
672 |
|
|
|
673 |
|
|
|
674 |
|
|
|
675 |
|
|
|
676 |
|
def get_candidates(bx, Nsim=int(1e4)): |
|
677 |
|
# -1 = 'out', 0=success, 1=failure, 2=mix |
|
678 |
|
|
|
679 |
|
# не мудрствуя лукаво |
|
680 |
|
user_pf = np.mean(bx.pf_lim) |
|
681 |
|
try: |
|
682 |
|
low_pf = bx.oiss.estimations[1] # failure |
|
683 |
|
upper_pf = 1 - bx.ciss.estimations[0] # sucess |
|
684 |
|
self_pf = (low_pf + upper_pf)/2 |
|
685 |
|
except AttributeError: |
|
686 |
|
self_pf = 0.5 |
|
687 |
|
|
|
688 |
|
# bereme *mean* od svého a uživatelského odhadu |
|
689 |
|
# minimum nejede |
|
690 |
|
sampling_r, __ = bx.sball.get_r_iteration(np.mean((self_pf, user_pf))) |
|
691 |
|
# asi tam bylo sampling_r/bx.base_r, že? |
|
692 |
|
# u stats.norm zadáváme směrodatnou odchylku, je to asi správné |
|
693 |
|
h = f_models.UnCorD([stats.norm(0, sampling_r/bx.base_r) for i in range(bx.nvar)]) |
|
694 |
|
|
|
695 |
|
# for IS_stats |
|
696 |
|
svar = (sampling_r/bx.base_r)**2 # svar like sampling_variance |
|
697 |
|
# něco takovýho bych nahrubo placnul |
|
698 |
|
implicit_multiplicator = svar**bx.nvar * np.exp(bx.nvar/svar - bx.nvar) |
|
699 |
245 |
|
|
700 |
|
# |
|
701 |
|
# jdeme na to, koťě! |
|
702 |
|
# |
|
703 |
|
|
|
704 |
|
# zgenerujeme vzorky |
|
705 |
|
# nic zajimavýho |
|
706 |
|
h = h(Nsim) |
|
707 |
|
# dropneme priliš vzdálené kandidaty |
|
708 |
|
distance_from_zero = np.sum(h.R**2, axis=1) |
|
709 |
|
mask = distance_from_zero < bx.drop_r |
|
710 |
|
|
|
711 |
|
# a teď bacha! |
|
712 |
|
# tady musíme provést jeden trik |
|
713 |
|
to_sample = bx.f.new_sample(h.R[mask], 'G') # R-ko smerdžíme ako G-čko |
|
714 |
|
w = to_sample.pdf_G / h.pdf_R # snad je to správně |
|
715 |
|
# zabalme do boxu |
|
716 |
|
candidates = SampleBox(to_sample, w, 'BlackBox internal samples and weights') |
|
717 |
|
candidates.implicit_multiplicator = implicit_multiplicator |
|
718 |
|
# vahy máme, zbytek už nejsou naši starosti |
|
719 |
|
return candidates |
|
720 |
|
|
|
721 |
|
|
|
722 |
|
|
|
723 |
|
|
|
724 |
|
|
|
725 |
246 |
|
|
726 |
247 |
|
|
727 |
|
class MinEnergyCensoredSampling(Censoring): |
|
728 |
|
# už mě to dědění nebaví |
|
729 |
|
# без поллитры не разберёшься, что этот слоёный пирог делает |
|
|
248 |
|
class MinEnergyCensoredSampling(DiceBox): |
|
249 |
|
#č už mě to dědění nebaví |
|
250 |
|
#ё без поллитры не разберёшься, что этот слоёный пирог делает |
730 |
251 |
def __init__(bx, sample_object, tri_space='Rn', tree_space=None,\ |
def __init__(bx, sample_object, tri_space='Rn', tree_space=None,\ |
731 |
252 |
sampling_space=None, kechato_space='U', potencial='psee',\ |
sampling_space=None, kechato_space='U', potencial='psee',\ |
732 |
253 |
p_norm=2, budget=1000, simplex_budget=100, LHS_correction=False): |
p_norm=2, budget=1000, simplex_budget=100, LHS_correction=False): |
733 |
254 |
|
|
734 |
255 |
|
|
735 |
|
|
|
|
256 |
|
bx.tri_space = tri_space |
736 |
257 |
if tree_space is None: |
if tree_space is None: |
737 |
258 |
bx.tree_space = tri_space |
bx.tree_space = tri_space |
738 |
259 |
else: |
else: |
|
... |
... |
class MinEnergyCensoredSampling(Censoring): |
744 |
265 |
bx.sampling_space = sampling_space |
bx.sampling_space = sampling_space |
745 |
266 |
|
|
746 |
267 |
|
|
747 |
|
|
|
748 |
268 |
bx.kechato_space = kechato_space |
bx.kechato_space = kechato_space |
749 |
|
# pro simplexy. Chcu ukladat jejich míry |
|
750 |
|
# viz. .regen() |
|
751 |
|
#bx.siss = IS_stat.ISSI() |
|
752 |
269 |
bx.budget = budget |
bx.budget = budget |
753 |
270 |
bx.simplex_budget = simplex_budget |
bx.simplex_budget = simplex_budget |
754 |
271 |
bx.p_norm = p_norm |
bx.p_norm = p_norm |
|
... |
... |
class MinEnergyCensoredSampling(Censoring): |
762 |
279 |
# krám, přece třidíme odpad! |
# krám, přece třidíme odpad! |
763 |
280 |
bx.former_candidates = [] |
bx.former_candidates = [] |
764 |
281 |
bx.unjudged_candidates = [] |
bx.unjudged_candidates = [] |
765 |
|
# user .candidates should be initializated in the base class |
|
766 |
|
# but we need to set additional attributes to them |
|
767 |
|
super().__init__(sample_object, tri_space) |
|
768 |
|
bx.candidates = CandyBox(bx.f()) |
|
769 |
282 |
|
|
|
283 |
|
super().__init__(sample_object) |
|
284 |
|
|
|
285 |
|
|
|
286 |
|
def init_parameters(bx): |
|
287 |
|
""" |
|
288 |
|
Returns dictionary of parameters the DiceBox was initialized with |
|
289 |
|
""" |
|
290 |
|
return {'sample_object':bx.sample_box, \ |
|
291 |
|
'tri_space':bx.tri_space, 'tree_space':bx.tree_space,\ |
|
292 |
|
'sampling_space':bx.sampling_space, 'kechato_space':bx.kechato_space,\ |
|
293 |
|
'potencial':bx.potencial, 'p_norm':bx.p_norm, 'budget':bx.budget,\ |
|
294 |
|
'simplex_budget':bx.simplex_budget, 'LHS_correction':bx.LHS_correction} |
|
295 |
|
|
|
296 |
|
|
|
297 |
|
def __repr__(bx): |
|
298 |
|
return "%s(**%s)"%(bx.__class__.__name__, repr(bx.init_parameters())) |
|
299 |
|
|
|
300 |
|
def __str__(bx): |
|
301 |
|
return "%s(%s)"%(bx.__class__.__name__, str(bx.init_parameters())) |
|
302 |
|
|
|
303 |
|
|
|
304 |
|
def get_events(bx, simplices=None): |
|
305 |
|
""" |
|
306 |
|
Metoda musí simplexům přiřazovat jev |
|
307 |
|
0=success, 1=failure, 2=mix |
|
308 |
|
""" |
|
309 |
|
if simplices is None: |
|
310 |
|
simplices = bx.tri.simplices |
|
311 |
|
|
|
312 |
|
in_failure = np.isin(simplices, bx.failure_points) |
|
313 |
|
has_failure = in_failure.any(axis=1) |
|
314 |
|
all_failure = in_failure.all(axis=1) |
|
315 |
|
return np.int8(np.where(has_failure, np.where(all_failure, 1, 2), 0)) |
770 |
316 |
|
|
771 |
317 |
|
|
772 |
318 |
|
|
|
... |
... |
class MinEnergyCensoredSampling(Censoring): |
775 |
321 |
regen() recreates data structures of the box. |
regen() recreates data structures of the box. |
776 |
322 |
It shouldn't be called without reason, changed distribution, settings or so. |
It shouldn't be called without reason, changed distribution, settings or so. |
777 |
323 |
""" |
""" |
778 |
|
# super TRYES to create .tri triangulation |
|
779 |
|
super().regen() |
|
|
324 |
|
|
|
325 |
|
#ё шайтан регенираци лэзьиз |
|
326 |
|
bx._logger(msg='regeneration started') |
|
327 |
|
bx._LHS_regen() |
780 |
328 |
|
|
781 |
329 |
# kind of interface to CandidatesWidget |
# kind of interface to CandidatesWidget |
782 |
330 |
bx.candidates_index = dict() |
bx.candidates_index = dict() |
783 |
331 |
|
|
784 |
|
|
|
785 |
332 |
if bx.nsim > 0: |
if bx.nsim > 0: |
786 |
333 |
# needed for potencial calculation |
# needed for potencial calculation |
787 |
334 |
sampled_plan_tree = getattr(bx.sample_box, bx.tree_space) |
sampled_plan_tree = getattr(bx.sample_box, bx.tree_space) |
|
... |
... |
class MinEnergyCensoredSampling(Censoring): |
792 |
339 |
|
|
793 |
340 |
|
|
794 |
341 |
|
|
795 |
|
def _regen_outside(bx): |
|
796 |
|
# function reserved for childs |
|
797 |
|
pass |
|
798 |
|
|
|
|
342 |
|
def _regen_outside(bx): |
|
343 |
|
if bx.nsim >= bx.nvar + 1: |
|
344 |
|
try: |
|
345 |
|
bx.shull = sx.Shull(bx.f_model, bx.budget, bx.tri_space, bx.sampling_space, powerset_correction=False) |
|
346 |
|
bx.siss = bx.shull.oiss |
|
347 |
|
bx.estimate_outside() |
|
348 |
|
#č a máme hotovo |
|
349 |
|
return # Ok, go away |
|
350 |
|
except BaseException as e: |
|
351 |
|
msg = "error of creating ConvexHull " |
|
352 |
|
error_msg = bx.__class__.__name__ + ": " + msg + repr(e) |
|
353 |
|
bx._logger(msg=error_msg) |
|
354 |
|
|
|
355 |
|
# nothing happened? We are still here? |
|
356 |
|
# we need to generate at least something |
|
357 |
|
if bx.nsim > 0: #č požaduji, aby nějaké těčíčky byly vždy pritomné |
799 |
358 |
|
|
|
359 |
|
#č současný CandyBox odmítne vytvořit neprazdný object bez atributů |
|
360 |
|
candidates = CandyBox(bx.f_model(bx.budget), event_id=np.full(bx.budget, -1, dtype=np.int8)) |
|
361 |
|
bx.assess_candidates(candidates) |
|
362 |
|
# uložíme |
|
363 |
|
bx.candidates_index[-1] = candidates |
|
364 |
|
|
800 |
365 |
|
|
801 |
366 |
def _regen_inside(bx): |
def _regen_inside(bx): |
|
367 |
|
#č .tri nemusí vůbec existovat |
802 |
368 |
if "tri" in dir(bx): |
if "tri" in dir(bx): |
803 |
369 |
#č tri - Deloneho triangulace |
#č tri - Deloneho triangulace |
804 |
|
bx.sball = sball.Sball(bx.nvar) |
|
805 |
|
bx.base_r = bx.sball.get_r(0.5) |
|
806 |
|
|
|
807 |
|
#č odhady |
|
808 |
|
bx.siss = IS_stat.ISSI() |
|
809 |
|
|
|
|
370 |
|
if len(bx.tri.coplanar): |
|
371 |
|
#print('triangulace v pořádku není') |
|
372 |
|
bx._logger(msg='triangulation is coplanar') |
|
373 |
|
else: |
|
374 |
|
#print('triangulace je v pořádku') |
|
375 |
|
bx._logger(msg='triangulation is OK') |
|
376 |
|
|
810 |
377 |
bx.simplex_events = bx.get_events() |
bx.simplex_events = bx.get_events() |
811 |
|
bx.estimate_outside() |
|
812 |
378 |
for simplex_id in range(bx.tri.nsimplex): |
for simplex_id in range(bx.tri.nsimplex): |
813 |
379 |
# zde jen počítame |
# zde jen počítame |
814 |
|
bx.estimate_simplex(simplex_id) |
|
|
380 |
|
bx.estimate_simplex(simplex_id) |
|
381 |
|
def _regen_inside(bx): |
|
382 |
|
# create .tri triangulation |
|
383 |
|
#č chcu zachytit spadnuti QHull na začatku, kdy ještě není dostatek teček. |
|
384 |
|
#č Jinak je třeba nechat QHull spadnout |
|
385 |
|
if bx.nsim > 2*bx.nvar + 3: |
|
386 |
|
bx.tri = spatial.Delaunay(getattr(bx.sampled_plan, bx.tri_space), incremental=True) |
|
387 |
|
|
|
388 |
|
|
|
389 |
|
else: # lze přípustit chybu triangulace |
|
390 |
|
try: |
|
391 |
|
bx.tri = spatial.Delaunay(getattr(bx.sampled_plan, bx.tri_space), incremental=True) |
|
392 |
|
except: |
|
393 |
|
bx._logger(msg='triangulation failed') |
815 |
394 |
|
|
816 |
|
elif bx.nsim > 0: # požaduji, aby nějaké těčíčky byly vždy pritomné |
|
817 |
|
#č IS nesmí modifikovat f-ko, |
|
818 |
|
#č a vidím, že tam je to v pořádku |
|
819 |
|
candidates = IS_stat.IS(bx.f_model, bx.h, space_from_h='R', space_to_f=bx.sampling_space, Nsim=bx.budget) |
|
820 |
|
bx.assess_candidates(candidates) |
|
821 |
|
# uložíme |
|
822 |
|
bx.candidates_index[-1] = candidates |
|
823 |
395 |
|
|
824 |
396 |
|
|
825 |
397 |
|
|
|
... |
... |
class MinEnergyCensoredSampling(Censoring): |
848 |
420 |
|
|
849 |
421 |
|
|
850 |
422 |
def increment(bx, input_sample): |
def increment(bx, input_sample): |
851 |
|
# ну нахрен это ваше наследование-расследование |
|
|
423 |
|
#ё ну нахрен это ваше наследование-расследование |
852 |
424 |
|
|
853 |
|
# nechť bude, asi nikomu nevadí |
|
|
425 |
|
#č nechť bude, asi nikomu nevadí |
854 |
426 |
bx._LHS_increment(input_sample) |
bx._LHS_increment(input_sample) |
855 |
427 |
|
|
856 |
428 |
#č strom posuneme sem |
#č strom posuneme sem |
|
... |
... |
class MinEnergyCensoredSampling(Censoring): |
879 |
451 |
""" |
""" |
880 |
452 |
Triangulace zajistěně existuje |
Triangulace zajistěně existuje |
881 |
453 |
""" |
""" |
882 |
|
# чыры-пыры |
|
883 |
|
# invalidujeme staré vzorky |
|
884 |
|
if -2 in bx.candidates_index: |
|
885 |
|
bx.former_candidates.append(bx.candidates_index.pop(-2)) |
|
886 |
|
|
|
887 |
|
|
|
|
454 |
|
|
888 |
455 |
former_simplices = bx.tri.simplices |
former_simplices = bx.tri.simplices |
889 |
456 |
mixed = bx.is_mixed() |
mixed = bx.is_mixed() |
890 |
457 |
|
|
|
... |
... |
class MinEnergyCensoredSampling(Censoring): |
897 |
464 |
bx._logger(msg='triangulation has coplanar points') |
bx._logger(msg='triangulation has coplanar points') |
898 |
465 |
|
|
899 |
466 |
|
|
|
467 |
|
|
|
468 |
|
|
900 |
469 |
# zkontrolujeme co se změnilo |
# zkontrolujeme co se změnilo |
901 |
470 |
# předpokladám, že se počet simplexů přidaním bodů nezměnší |
# předpokladám, že se počet simplexů přidaním bodů nezměnší |
902 |
471 |
equal_mask = former_simplices == bx.tri.simplices[:len(former_simplices)] |
equal_mask = former_simplices == bx.tri.simplices[:len(former_simplices)] |
|
... |
... |
class MinEnergyCensoredSampling(Censoring): |
906 |
475 |
for simplex_id in changed_simplices_ids: |
for simplex_id in changed_simplices_ids: |
907 |
476 |
bx.siss.delete_event_data(simplex_id) |
bx.siss.delete_event_data(simplex_id) |
908 |
477 |
|
|
909 |
|
# popajem pouze mixy, ty musel jsem spočítat před aktualizací |
|
|
478 |
|
#č popajem pouze mixy, ty musel jsem spočítat před aktualizací |
910 |
479 |
for simplex_id in changed_simplices_ids[mixed[changed_simplices_ids]]: |
for simplex_id in changed_simplices_ids[mixed[changed_simplices_ids]]: |
911 |
|
bx.former_candidates.append(bx.candidates_index.pop(simplex_id)) |
|
|
480 |
|
bx.candidates_index.pop(simplex_id) |
912 |
481 |
|
|
913 |
482 |
|
|
914 |
483 |
# pokud není splněná podmínka, |
# pokud není splněná podmínka, |
|
... |
... |
class MinEnergyCensoredSampling(Censoring): |
931 |
500 |
bx.estimate_simplex(simplex_id) |
bx.estimate_simplex(simplex_id) |
932 |
501 |
|
|
933 |
502 |
|
|
|
503 |
|
|
934 |
504 |
def _handle_changed_outside(bx, input_sample): |
def _handle_changed_outside(bx, input_sample): |
|
505 |
|
""" |
|
506 |
|
ConvexHull by měl existovat |
|
507 |
|
""" |
|
508 |
|
# sample je jíž převeden na f (v .add_sample()), takže je to bezpěčný |
|
509 |
|
bx.convex_hull.add_points(getattr(input_sample, bx.tri_space)) |
|
510 |
|
|
935 |
511 |
""" |
""" |
936 |
512 |
Triangulace zajistěně existuje |
Triangulace zajistěně existuje |
937 |
513 |
""" |
""" |
|
... |
... |
class MinEnergyCensoredSampling(Censoring): |
956 |
532 |
|
|
957 |
533 |
|
|
958 |
534 |
def _handle_candidates(bx): |
def _handle_candidates(bx): |
959 |
|
# pokud -2 jíž existuje, tak pro dnešek stačí |
|
960 |
|
if (-2 not in bx.candidates_index) and (len(bx.former_candidates) > 0): |
|
961 |
|
# nikdo nám neslibil, že u starých kandidatu |
|
962 |
|
# třeba se nebude zvýšovat potanciál |
|
963 |
|
# (je to prostě, opravdu ríct, jednodušší) |
|
964 |
|
|
|
965 |
|
# prohrabeme odpad |
|
966 |
|
candidates = bx.former_candidates.pop() |
|
967 |
|
for i in range(len(bx.former_candidates)): |
|
968 |
|
candidates.add_sample(bx.former_candidates.pop()) |
|
969 |
|
for i in range(len(bx.unjudged_candidates)): |
|
970 |
|
candidates.add_sample(bx.unjudged_candidates.pop()) |
|
971 |
|
|
|
972 |
|
# hodil by se ještě nám? |
|
973 |
|
bx.judge_candidates(candidates) |
|
974 |
|
# profiltrujeme |
|
975 |
|
# -1 = 'out', 0=success, 1=failure, 2=mix |
|
976 |
|
candidates = candidates[candidates.event != 0] |
|
977 |
|
candidates = candidates[candidates.event != 1] |
|
978 |
|
# uvalíme pokutu |
|
979 |
|
bx.assess_candidates(candidates) |
|
980 |
|
#č někoho z tyto hromady dostaneme |
|
981 |
|
for __ in range((bx.nvar+1)*2): |
|
982 |
|
if len(candidates) > 0: |
|
983 |
|
mc_id = collections.Counter(candidates.simplex).most_common()[0][0] |
|
984 |
|
#č uložíme |
|
985 |
|
bx.candidates_index[mc_id].add_sample(candidates[candidates.simplex == mc_id]) |
|
986 |
|
#č a teď bacha - ty co jsem uložil do simplexu nechcu v -2 videt |
|
987 |
|
candidates = candidates[candidates.simplex != mc_id] |
|
988 |
|
else: |
|
989 |
|
break |
|
990 |
|
if len(candidates) > 0: |
|
991 |
|
#č -2 je určen pro zbytky, кылем-мылем |
|
992 |
|
bx.candidates_index[-2] = candidates |
|
993 |
|
|
|
994 |
|
|
|
995 |
|
def is_mixed(bx, simplices=None): |
|
996 |
|
""" |
|
997 |
|
Metoda musí simplexům přiřazovat jev |
|
998 |
|
0=success, 1=failure, 2=mix |
|
999 |
|
""" |
|
1000 |
|
if simplices is None: |
|
1001 |
|
simplices = bx.tri.simplices |
|
1002 |
|
|
|
1003 |
|
in_failure = np.isin(simplices, bx.failure_points) |
|
1004 |
|
has_failure = in_failure.any(axis=1) |
|
1005 |
|
all_failure = in_failure.all(axis=1) |
|
1006 |
|
return np.logical_xor(has_failure, all_failure) |
|
|
535 |
|
#č prohrabeme odpad |
|
536 |
|
bx._judge_unjudged(bx.unjudged_candidates) |
|
537 |
|
bx._judge_unjudged(bx.former_candidates) |
|
538 |
|
|
|
539 |
|
|
|
540 |
|
def is_mixed(bx, simplices=None): |
|
541 |
|
""" |
|
542 |
|
Metoda musí simplexům přiřazovat jev |
|
543 |
|
0=success, 1=failure, 2=mix |
|
544 |
|
""" |
|
545 |
|
if simplices is None: |
|
546 |
|
simplices = bx.tri.simplices |
|
547 |
|
|
|
548 |
|
in_failure = np.isin(simplices, bx.failure_points) |
|
549 |
|
has_failure = in_failure.any(axis=1) |
|
550 |
|
all_failure = in_failure.all(axis=1) |
|
551 |
|
return np.logical_xor(has_failure, all_failure) |
1007 |
552 |
|
|
1008 |
553 |
|
|
1009 |
554 |
def __call__(bx): |
def __call__(bx): |
|
... |
... |
class MinEnergyCensoredSampling(Censoring): |
1045 |
590 |
bx.assess_candidates(bx.candidates) |
bx.assess_candidates(bx.candidates) |
1046 |
591 |
bids = getattr(bx.candidates, bx.potencial) |
bids = getattr(bx.candidates, bx.potencial) |
1047 |
592 |
# -1 = 'out', 0=success, 1=failure, 2=mix |
# -1 = 'out', 0=success, 1=failure, 2=mix |
1048 |
|
bids *= (bx.candidates.event == -1) + (bx.candidates.event == 2) |
|
|
593 |
|
bids *= (bx.candidates.event_id == -1) + (bx.candidates.event_id == 2) |
1049 |
594 |
bid = max(bids) |
bid = max(bids) |
1050 |
595 |
# side effect |
# side effect |
1051 |
596 |
if bid > highest_bid: |
if bid > highest_bid: |
|
... |
... |
class MinEnergyCensoredSampling(Censoring): |
1063 |
608 |
|
|
1064 |
609 |
|
|
1065 |
610 |
def estimate_outside(bx): |
def estimate_outside(bx): |
1066 |
|
# předpokládám, že triangulece jíž existuje |
|
1067 |
|
|
|
|
611 |
|
# předpokládám, že convex_hull jíž existuje |
|
612 |
|
|
1068 |
613 |
# -1 = 'out', 0=success, 1=failure, 2=mix |
# -1 = 'out', 0=success, 1=failure, 2=mix |
1069 |
614 |
#current outside probability estimation |
#current outside probability estimation |
1070 |
615 |
try: |
try: |
|
... |
... |
class MinEnergyCensoredSampling(Censoring): |
1076 |
621 |
if p_out == 0: |
if p_out == 0: |
1077 |
622 |
p_out = 1/(bx.nsim + 1) |
p_out = 1/(bx.nsim + 1) |
1078 |
623 |
bx._logger(msg="suppose p_out=1/(bx.nsim + 1)="+str(p_out)) |
bx._logger(msg="suppose p_out=1/(bx.nsim + 1)="+str(p_out)) |
1079 |
|
|
|
|
624 |
|
|
|
625 |
|
# |
|
626 |
|
# get candidates! |
|
627 |
|
# |
1080 |
628 |
if p_out > 0.5: |
if p_out > 0.5: |
1081 |
629 |
# zužovat nechceme |
# zužovat nechceme |
1082 |
630 |
# trapit sa generacema taky ne |
# trapit sa generacema taky ne |
1083 |
631 |
candidates = IS_stat.IS(bx.f, bx.h, space_from_h='R', space_to_f=bx.sampling_space, Nsim=bx.budget) |
candidates = IS_stat.IS(bx.f, bx.h, space_from_h='R', space_to_f=bx.sampling_space, Nsim=bx.budget) |
1084 |
632 |
|
|
1085 |
633 |
# nevím co tam bylo za h-ko, ale nechť IM zůstane 1 |
# nevím co tam bylo za h-ko, ale nechť IM zůstane 1 |
1086 |
|
implicit_multiplicator = 1 |
|
|
634 |
|
#implicit_multiplicator = 1 |
1087 |
635 |
|
|
1088 |
|
else: # tak deme... trapit sa generacema |
|
|
636 |
|
else: # tak jdeme... trapit sa generacema |
1089 |
637 |
|
|
1090 |
638 |
sampling_r, __ = bx.sball.get_r_iteration(p_out) |
sampling_r, __ = bx.sball.get_r_iteration(p_out) |
1091 |
639 |
# asi tam bylo sampling_r/bx.base_r, že? |
# asi tam bylo sampling_r/bx.base_r, že? |
1092 |
640 |
# u stats.norm zadáváme směrodatnou odchylku, je to asi správné |
# u stats.norm zadáváme směrodatnou odchylku, je to asi správné |
1093 |
641 |
h = f_models.UnCorD([stats.norm(0, sampling_r/bx.base_r) for i in range(bx.nvar)]) |
h = f_models.UnCorD([stats.norm(0, sampling_r/bx.base_r) for i in range(bx.nvar)]) |
1094 |
|
|
|
1095 |
|
# for IS_stats |
|
1096 |
|
#svar = (sampling_r/bx.base_r)**2 # svar like sampling_variance |
|
1097 |
|
# něco takovýho bych nahrubo placnul |
|
1098 |
|
#implicit_multiplicator = svar**bx.nvar * np.exp(bx.nvar/svar - bx.nvar) |
|
1099 |
|
implicit_multiplicator = np.inf |
|
1100 |
642 |
candidates = IS_stat.IS(bx.f, h, space_from_h='R', space_to_f=bx.sampling_space, Nsim=bx.budget) |
candidates = IS_stat.IS(bx.f, h, space_from_h='R', space_to_f=bx.sampling_space, Nsim=bx.budget) |
1101 |
643 |
|
|
1102 |
|
|
|
1103 |
|
bx.judge_candidates(candidates) |
|
1104 |
|
# odhady zlobily, zkusím jako multiplikator posílat nekonečno |
|
1105 |
|
bx.siss.add_IS_serie(candidates.w, candidates.simplex, implicit_multiplicator=implicit_multiplicator) |
|
|
644 |
|
bx.is_outside(candidates) |
|
645 |
|
candidates = candidates[candidates.is_outside] |
1106 |
646 |
|
|
1107 |
|
# odebereme kus práce u selectu |
|
1108 |
|
# -1 = 'out', 0=success, 1=failure, 2=mix |
|
1109 |
|
candidates = candidates[candidates.event != 0] |
|
1110 |
|
candidates = candidates[candidates.event != 1] |
|
1111 |
|
# uvalíme pokutu |
|
|
647 |
|
|
|
648 |
|
#č řeším problém, že při načítaní ze souboru |
|
649 |
|
#č DiceBox nemá adekvatní odhad Brno-venkova |
|
650 |
|
while len(candidates) < 2: |
|
651 |
|
p_out = p_out/2 |
|
652 |
|
bx._logger(msg="suppose p_out="+str(p_out)) |
|
653 |
|
|
|
654 |
|
sampling_r, __ = bx.sball.get_r_iteration(p_out) |
|
655 |
|
# asi tam bylo sampling_r/bx.base_r, že? |
|
656 |
|
# u stats.norm zadáváme směrodatnou odchylku, je to asi správné |
|
657 |
|
h = f_models.UnCorD([stats.norm(0, sampling_r/bx.base_r) for i in range(bx.nvar)]) |
|
658 |
|
candidates = IS_stat.IS(bx.f, h, space_from_h='R', space_to_f=bx.sampling_space, Nsim=bx.budget) |
|
659 |
|
|
|
660 |
|
bx.is_outside(candidates) |
|
661 |
|
candidates = candidates[candidates.is_outside] |
|
662 |
|
|
|
663 |
|
|
|
664 |
|
|
|
665 |
|
#č necháme ISSI trapit sa pravděpodobnostma |
|
666 |
|
bx.siss.add_single_event_data(candidates.w, event=-1, nis=bx.budget) |
|
667 |
|
|
|
668 |
|
candidates.simplex = candidates.event = np.full(len(candidates), -1, dtype=np.int8) |
|
669 |
|
|
|
670 |
|
|
|
671 |
|
#č vyhodnotíme |
1112 |
672 |
bx.assess_candidates(candidates) |
bx.assess_candidates(candidates) |
1113 |
|
# uložíme |
|
1114 |
|
bx.candidates_index[-1] = candidates[candidates.event == -1] |
|
1115 |
|
if len(candidates[candidates.event == 2]) > 0: |
|
1116 |
|
# -2 je určen pro zbytky, кылем-мылем |
|
1117 |
|
bx.candidates_index[-2] = candidates[candidates.event == 2] |
|
|
673 |
|
# vzorky je třeba přidát ke kandidatům |
|
674 |
|
# jako, nic nepokazí, ale čo tam připadně bylo - přepíše |
|
675 |
|
bx.candidates_index[-1] = candidates |
|
676 |
|
|
|
677 |
|
|
|
678 |
|
|
|
679 |
|
|
|
680 |
|
def is_outside(bx, candidates): |
|
681 |
|
|
|
682 |
|
x = getattr(candidates, bx.tri_space) |
|
683 |
|
|
|
684 |
|
#E [normal, offset] forming the hyperplane equation of the facet (see Qhull documentation for more) |
|
685 |
|
A = bx.convex_hull.equations[:,:-1] |
|
686 |
|
b = bx.convex_hull.equations[:,-1] |
|
687 |
|
|
|
688 |
|
# N=nsim |
|
689 |
|
NxN = A @ x.T + np.atleast_2d(b).T |
|
690 |
|
mask = np.any(NxN > 0, axis=0) |
|
691 |
|
candidates.is_outside = mask |
1118 |
692 |
|
|
1119 |
693 |
|
|
1120 |
694 |
|
|
|
... |
... |
class MinEnergyCensoredSampling(Censoring): |
1222 |
796 |
candidates.ksee = ksee |
candidates.ksee = ksee |
1223 |
797 |
|
|
1224 |
798 |
|
|
1225 |
|
|
|
|
799 |
|
def _judge_unjudged(bx, list): |
|
800 |
|
for i in range(len(list)): |
|
801 |
|
candidates = list.pop() |
|
802 |
|
bx.is_outside(candidates) |
|
803 |
|
|
|
804 |
|
mask = candidates.is_outside |
|
805 |
|
if np.any(mask): #č pokud aspoň jeden bydlí mimo Brno |
|
806 |
|
candidates = candidates[mask] |
|
807 |
|
candidates.simplex = candidates.event = np.full(len(candidates), -1, dtype=np.int8) |
|
808 |
|
#č vyhodnotíme |
|
809 |
|
bx.assess_candidates(candidates) |
|
810 |
|
#č vzorky je třeba přidát ke kandidatům |
|
811 |
|
# jako, nic nepokazí, ale čo tam připadně bylo - přepíše |
|
812 |
|
bx.candidates_index[-1].add_sample(candidates) |
1226 |
813 |
|
|
1227 |
814 |
|
|
1228 |
815 |
# ještě by asi hodily funkce pro pridaní uživatelských kandidatů |
# ještě by asi hodily funkce pro pridaní uživatelských kandidatů |
|
... |
... |
class MinEnergyCensoredSampling(Censoring): |
1249 |
836 |
|
|
1250 |
837 |
|
|
1251 |
838 |
|
|
1252 |
|
class OptimizedCensoredSampling(MinEnergyCensoredSampling): |
|
1253 |
|
|
|
1254 |
|
|
|
1255 |
|
def _regen_outside(bx): |
|
1256 |
|
if bx.nsim >= bx.nvar + 1: |
|
1257 |
|
try: |
|
1258 |
|
bx.shull = sx.Shull(bx.f_model, bx.budget, bx.tri_space, bx.sampling_space, powerset_correction=False) |
|
1259 |
|
bx.siss = bx.shull.oiss |
|
1260 |
|
bx.estimate_outside() |
|
1261 |
|
#č a máme hotovo |
|
1262 |
|
return # Ok, go away |
|
1263 |
|
except BaseException as e: |
|
1264 |
|
msg = "error of creating ConvexHull " |
|
1265 |
|
error_msg = bx.__class__.__name__ + ": " + msg + repr(e) |
|
1266 |
|
bx._logger(msg=error_msg) |
|
1267 |
|
|
|
1268 |
|
# nothing happened? We are still here? |
|
1269 |
|
# we need to generate at least something |
|
1270 |
|
if bx.nsim > 0: #č požaduji, aby nějaké těčíčky byly vždy pritomné |
|
1271 |
|
|
|
1272 |
|
#č IS nesmí modifikovat f-ko, |
|
1273 |
|
#č a vidím, že tam je to v pořádku |
|
1274 |
|
candidates = IS_stat.IS(bx.f_model, bx.h, space_from_h='R', space_to_f=bx.sampling_space, Nsim=bx.budget) |
|
1275 |
|
|
|
1276 |
|
bx.assess_candidates(candidates) |
|
1277 |
|
# uložíme |
|
1278 |
|
bx.candidates_index[-1] = candidates |
|
1279 |
|
|
|
1280 |
|
|
|
1281 |
|
|
|
1282 |
|
def _regen_inside(bx): |
|
1283 |
|
#č .tri nemusí vůbec existovat |
|
1284 |
|
if "tri" in dir(bx): |
|
1285 |
|
#č tri - Deloneho triangulace |
|
1286 |
|
bx.simplex_events = bx.get_events() |
|
1287 |
|
for simplex_id in range(bx.tri.nsimplex): |
|
1288 |
|
# zde jen počítame |
|
1289 |
|
bx.estimate_simplex(simplex_id) |
|
1290 |
|
|
|
1291 |
|
|
|
1292 |
|
|
|
1293 |
|
|
|
1294 |
|
|
|
1295 |
|
def _handle_changed_triangulation(bx, input_sample): |
|
1296 |
|
""" |
|
1297 |
|
Triangulace zajistěně existuje |
|
1298 |
|
""" |
|
1299 |
|
|
|
1300 |
|
former_simplices = bx.tri.simplices |
|
1301 |
|
mixed = bx.is_mixed() |
|
1302 |
|
|
|
1303 |
|
# sample je jíž převeden na f (v .add_sample()), takže je to bezpěčný |
|
1304 |
|
bx.tri.add_points(getattr(input_sample, bx.tri_space)) |
|
1305 |
|
bx.simplex_events = bx.get_events() |
|
1306 |
|
# print('increment se podaril') |
|
1307 |
|
if len(bx.tri.coplanar): # pokud triangulace není v pořadku |
|
1308 |
|
#print('triangulace v pořádku není') |
|
1309 |
|
bx._logger(msg='triangulation has coplanar points') |
|
1310 |
|
|
|
1311 |
|
|
|
1312 |
|
|
|
1313 |
|
|
|
1314 |
|
# zkontrolujeme co se změnilo |
|
1315 |
|
# předpokladám, že se počet simplexů přidaním bodů nezměnší |
|
1316 |
|
equal_mask = former_simplices == bx.tri.simplices[:len(former_simplices)] |
|
1317 |
|
changed_simplices_ids = np.argwhere(~equal_mask.all(axis=1)).flatten() |
|
1318 |
|
|
|
1319 |
|
# invalidirujeme jejich odhady |
|
1320 |
|
for simplex_id in changed_simplices_ids: |
|
1321 |
|
bx.siss.delete_event_data(simplex_id) |
|
1322 |
|
|
|
1323 |
|
#č popajem pouze mixy, ty musel jsem spočítat před aktualizací |
|
1324 |
|
for simplex_id in changed_simplices_ids[mixed[changed_simplices_ids]]: |
|
1325 |
|
bx.candidates_index.pop(simplex_id) |
|
1326 |
|
|
|
1327 |
|
|
|
1328 |
|
# pokud není splněná podmínka, |
|
1329 |
|
# tak nemáme jistotu, že se potenciály nezměni |
|
1330 |
|
# ani u kandidatů, které se nacházejí v pojíštěných státem buňkách |
|
1331 |
|
if (bx.tree_space != bx.tri_space) or (bx.p_norm != 2): |
|
1332 |
|
for candidates in bx.candidates_index.values(): |
|
1333 |
|
bx.assess_candidates(candidates) |
|
1334 |
|
|
|
1335 |
|
# změněné simplexy přepočítáme |
|
1336 |
|
for simplex_id in changed_simplices_ids: |
|
1337 |
|
bx.estimate_simplex(simplex_id) |
|
1338 |
|
|
|
1339 |
|
|
|
1340 |
|
|
|
1341 |
|
# teď nové simplexy |
|
1342 |
|
# simplexy свежего разлива |
|
1343 |
|
for simplex_id in range(len(former_simplices), bx.tri.nsimplex): |
|
1344 |
|
# zde jen počítame |
|
1345 |
|
bx.estimate_simplex(simplex_id) |
|
1346 |
|
|
|
1347 |
|
|
|
1348 |
|
|
|
1349 |
|
def _handle_changed_outside(bx, input_sample): |
|
1350 |
|
""" |
|
1351 |
|
ConvexHull by měl existovat |
|
1352 |
|
""" |
|
1353 |
|
# sample je jíž převeden na f (v .add_sample()), takže je to bezpěčný |
|
1354 |
|
bx.convex_hull.add_points(getattr(input_sample, bx.tri_space)) |
|
1355 |
|
super()._handle_changed_outside(input_sample) |
|
1356 |
|
|
|
1357 |
|
|
|
1358 |
|
def _handle_candidates(bx): |
|
1359 |
|
#č prohrabeme odpad |
|
1360 |
|
bx._judge_unjudged(bx.unjudged_candidates) |
|
1361 |
|
bx._judge_unjudged(bx.former_candidates) |
|
1362 |
|
|
|
1363 |
|
|
|
1364 |
|
|
|
1365 |
|
|
|
1366 |
|
def _judge_unjudged(bx, list): |
|
1367 |
|
for i in range(len(list)): |
|
1368 |
|
candidates = list.pop() |
|
1369 |
|
bx.is_outside(candidates) |
|
1370 |
|
|
|
1371 |
|
mask = candidates.is_outside |
|
1372 |
|
if np.any(mask): #č pokud aspoň jeden bydlí mimo Brno |
|
1373 |
|
candidates = candidates[mask] |
|
1374 |
|
candidates.simplex = candidates.event = np.full(len(candidates), -1, dtype=np.int8) |
|
1375 |
|
#č vyhodnotíme |
|
1376 |
|
bx.assess_candidates(candidates) |
|
1377 |
|
#č vzorky je třeba přidát ke kandidatům |
|
1378 |
|
# jako, nic nepokazí, ale čo tam připadně bylo - přepíše |
|
1379 |
|
bx.candidates_index[-1].add_sample(candidates) |
|
1380 |
|
|
|
1381 |
|
|
|
1382 |
|
|
|
1383 |
|
def estimate_outside(bx): |
|
1384 |
|
# předpokládám, že convex_hull jíž existuje |
|
1385 |
|
|
|
1386 |
|
# -1 = 'out', 0=success, 1=failure, 2=mix |
|
1387 |
|
#current outside probability estimation |
|
1388 |
|
try: |
|
1389 |
|
p_out = bx.siss.estimations[-1] |
|
1390 |
|
except: |
|
1391 |
|
p_out = 1/(bx.nsim + 1) |
|
1392 |
|
bx._logger(msg="suppose p_out=1/(bx.nsim + 1)="+str(p_out)) |
|
1393 |
|
|
|
1394 |
|
if p_out == 0: |
|
1395 |
|
p_out = 1/(bx.nsim + 1) |
|
1396 |
|
bx._logger(msg="suppose p_out=1/(bx.nsim + 1)="+str(p_out)) |
|
1397 |
|
|
|
1398 |
|
# |
|
1399 |
|
# get candidates! |
|
1400 |
|
# |
|
1401 |
|
if p_out > 0.5: |
|
1402 |
|
# zužovat nechceme |
|
1403 |
|
# trapit sa generacema taky ne |
|
1404 |
|
candidates = IS_stat.IS(bx.f, bx.h, space_from_h='R', space_to_f=bx.sampling_space, Nsim=bx.budget) |
|
1405 |
|
|
|
1406 |
|
# nevím co tam bylo za h-ko, ale nechť IM zůstane 1 |
|
1407 |
|
#implicit_multiplicator = 1 |
|
1408 |
|
|
|
1409 |
|
else: # tak jdeme... trapit sa generacema |
|
1410 |
|
|
|
1411 |
|
sampling_r, __ = bx.sball.get_r_iteration(p_out) |
|
1412 |
|
# asi tam bylo sampling_r/bx.base_r, že? |
|
1413 |
|
# u stats.norm zadáváme směrodatnou odchylku, je to asi správné |
|
1414 |
|
h = f_models.UnCorD([stats.norm(0, sampling_r/bx.base_r) for i in range(bx.nvar)]) |
|
1415 |
|
candidates = IS_stat.IS(bx.f, h, space_from_h='R', space_to_f=bx.sampling_space, Nsim=bx.budget) |
|
1416 |
|
|
|
1417 |
|
bx.is_outside(candidates) |
|
1418 |
|
candidates = candidates[candidates.is_outside] |
|
1419 |
|
|
|
1420 |
|
|
|
1421 |
|
#č řeším problém, že při načítaní ze souboru |
|
1422 |
|
#č DiceBox nemá adekvatní odhad Brno-venkova |
|
1423 |
|
while len(candidates) < 2: |
|
1424 |
|
p_out = p_out/2 |
|
1425 |
|
bx._logger(msg="suppose p_out="+str(p_out)) |
|
1426 |
|
|
|
1427 |
|
sampling_r, __ = bx.sball.get_r_iteration(p_out) |
|
1428 |
|
# asi tam bylo sampling_r/bx.base_r, že? |
|
1429 |
|
# u stats.norm zadáváme směrodatnou odchylku, je to asi správné |
|
1430 |
|
h = f_models.UnCorD([stats.norm(0, sampling_r/bx.base_r) for i in range(bx.nvar)]) |
|
1431 |
|
candidates = IS_stat.IS(bx.f, h, space_from_h='R', space_to_f=bx.sampling_space, Nsim=bx.budget) |
|
1432 |
|
|
|
1433 |
|
bx.is_outside(candidates) |
|
1434 |
|
candidates = candidates[candidates.is_outside] |
|
1435 |
|
|
|
1436 |
|
|
|
1437 |
|
|
|
1438 |
|
#č necháme ISSI trapit sa pravděpodobnostma |
|
1439 |
|
bx.siss.add_single_event_data(candidates.w, event=-1, nis=bx.budget) |
|
1440 |
|
|
|
1441 |
|
candidates.simplex = candidates.event = np.full(len(candidates), -1, dtype=np.int8) |
|
1442 |
|
|
|
1443 |
|
|
|
1444 |
|
#č vyhodnotíme |
|
1445 |
|
bx.assess_candidates(candidates) |
|
1446 |
|
# vzorky je třeba přidát ke kandidatům |
|
1447 |
|
# jako, nic nepokazí, ale čo tam připadně bylo - přepíše |
|
1448 |
|
bx.candidates_index[-1] = candidates |
|
1449 |
|
|
|
1450 |
|
|
|
1451 |
|
|
|
1452 |
|
|
|
1453 |
|
def is_outside(bx, candidates): |
|
1454 |
|
|
|
1455 |
|
x = getattr(candidates, bx.tri_space) |
|
1456 |
|
|
|
1457 |
|
#E [normal, offset] forming the hyperplane equation of the facet (see Qhull documentation for more) |
|
1458 |
|
A = bx.convex_hull.equations[:,:-1] |
|
1459 |
|
b = bx.convex_hull.equations[:,-1] |
|
1460 |
|
|
|
1461 |
|
# N=nsim |
|
1462 |
|
NxN = A @ x.T + np.atleast_2d(b).T |
|
1463 |
|
mask = np.any(NxN > 0, axis=0) |
|
1464 |
|
candidates.is_outside = mask |
|
1465 |
|
|
|
1466 |
|
|
|
1467 |
839 |
|
|
1468 |
840 |
class FastSimpleX(OptimizedCensoredSampling): |
class FastSimpleX(OptimizedCensoredSampling): |
1469 |
841 |
|
|
|
... |
... |
class FastSimpleX(OptimizedCensoredSampling): |
1674 |
1046 |
#č vyhodnotíme je |
#č vyhodnotíme je |
1675 |
1047 |
bx.assess_candidates(candidates) |
bx.assess_candidates(candidates) |
1676 |
1048 |
|
|
1677 |
|
#č chce se event přejmenovat na event_id |
|
1678 |
|
candidates.event = np.full(len(candidates), event_id, dtype=np.int8) |
|
|
1049 |
|
candidates.event_id = np.full(len(candidates), event_id, dtype=np.int8) |
1679 |
1050 |
|
|
1680 |
1051 |
#č vzorky je třeba přidát ke kandidatům |
#č vzorky je třeba přidát ke kandidatům |
1681 |
1052 |
#č jako, nic nepokazí, ale čo tam připadně bylo - přepíše |
#č jako, nic nepokazí, ale čo tam připadně bylo - přepíše |