File closedbox.py copied from file dicebox.py (similarity 51%) (mode: 100644) (index 750fd88..5e8651a) |
... |
... |
teďkom je to spíše jen krabička pro krámy |
13 |
13 |
|
|
14 |
14 |
import numpy as np |
import numpy as np |
15 |
15 |
from scipy import spatial |
from scipy import spatial |
16 |
|
from scipy import stats |
|
17 |
16 |
import pickle |
import pickle |
18 |
17 |
from . import IS_stat |
from . import IS_stat |
19 |
18 |
from .candybox import CandyBox |
from .candybox import CandyBox |
|
... |
... |
from scipy import optimize # for BlackSimpleX |
21 |
20 |
|
|
22 |
21 |
|
|
23 |
22 |
from .samplebox import SampleBox # for candidates packing |
from .samplebox import SampleBox # for candidates packing |
24 |
|
from .ghull import Ghull |
|
25 |
23 |
from . import simplex as sx |
from . import simplex as sx |
26 |
|
from . import convex_hull as khull |
|
27 |
24 |
from . import lukiskon as lk |
from . import lukiskon as lk |
28 |
|
from . import reader # for Goal |
|
|
25 |
|
|
29 |
26 |
from . import estimation as stm # for KechatoLukiskon |
from . import estimation as stm # for KechatoLukiskon |
30 |
27 |
import collections #E for Counter in MinEnergyCensoredSampling |
import collections #E for Counter in MinEnergyCensoredSampling |
31 |
|
|
|
|
28 |
|
|
32 |
29 |
|
|
33 |
30 |
|
|
34 |
31 |
|
|
|
... |
... |
class GuessBox: |
99 |
96 |
else: |
else: |
100 |
97 |
print('GuessBox is in air mode') |
print('GuessBox is in air mode') |
101 |
98 |
|
|
102 |
|
|
|
103 |
|
|
|
104 |
|
#ё нам позарез нужен ещё один, свой собственный словник |
|
105 |
|
#č jako sůl potřebujeme ještě jeden svůj vlastní slovník |
|
106 |
|
class CacheDict(dict): |
|
107 |
|
def __init__(self, potential): |
|
108 |
|
super().__init__() |
|
109 |
|
self.potential = potential |
|
110 |
|
self.cache = dict() |
|
111 |
|
|
|
112 |
|
|
|
113 |
|
def __setitem__(self, key, value): |
|
114 |
|
super().__setitem__(key, value) |
|
115 |
|
|
|
116 |
|
bids = getattr(value, self.potential) |
|
117 |
|
if len(bids): |
|
118 |
|
bid = np.nanmax(bids) |
|
119 |
|
else: |
|
120 |
|
bid = 0 |
|
121 |
|
#print("Cache", key, bid) |
|
122 |
|
self.cache[key] = bid |
|
123 |
|
|
|
124 |
|
def __delitem__(self, key): |
|
125 |
|
super().__delitem__(key) |
|
126 |
|
|
|
127 |
|
#print("throw away", key) |
|
128 |
|
self.cache.__delitem__(key) |
|
129 |
|
|
|
130 |
|
def pop(self, *args): |
|
131 |
|
|
|
132 |
|
#print("throw away", args[0]) |
|
133 |
|
self.cache.pop(*args) |
|
134 |
|
|
|
135 |
|
return super().pop(*args) |
|
136 |
|
|
|
137 |
|
|
|
138 |
|
|
|
139 |
|
|
|
140 |
99 |
#оӵ дӥсё |
#оӵ дӥсё |
141 |
100 |
class DiceBox: |
class DiceBox: |
142 |
|
#č kruci, totálně v tohlenstom ztracím |
|
143 |
101 |
""" |
""" |
144 |
|
DiceBox |
|
145 |
|
Public methods: |
|
146 |
|
__init__(): |
|
147 |
|
regen(): |
|
148 |
|
_LHS_regen() |
|
149 |
|
|
|
150 |
|
add_sample(): |
|
151 |
|
increment(): |
|
152 |
|
_LHS_increment() |
|
153 |
|
|
|
154 |
|
__call__(): |
|
155 |
|
LHS_like_correction() |
|
156 |
|
|
|
157 |
|
Public attributes: |
|
158 |
|
estimations |
|
159 |
|
guessbox |
|
|
102 |
|
DiceBox pěčlivě ukladá věškerá data, |
|
103 |
|
věškeré sady vzorků (well, no yet), průběžné odhady a tak. |
|
104 |
|
Nejsem už jistý, zda DiceBox je šťastný nazev, neboť |
|
105 |
|
teďkom je to spíše jen krabička pro krámy |
160 |
106 |
""" |
""" |
161 |
107 |
def __init__(bx, sample_box): |
def __init__(bx, sample_box): |
162 |
108 |
bx.sample_box = sample_box |
bx.sample_box = sample_box |
|
... |
... |
class DiceBox: |
197 |
143 |
return bx.sample_box[slice] |
return bx.sample_box[slice] |
198 |
144 |
|
|
199 |
145 |
def __getattr__(dx, attr): |
def __getattr__(dx, attr): |
200 |
|
if attr == 'dicebox': |
|
|
146 |
|
if attr in ('dicebox', 'closedbox'): |
201 |
147 |
return dx |
return dx |
202 |
148 |
|
|
203 |
149 |
# branime sa rekurzii |
# branime sa rekurzii |
|
... |
... |
class DiceBox: |
210 |
156 |
# на нашу горячую линию |
# на нашу горячую линию |
211 |
157 |
else: |
else: |
212 |
158 |
return getattr(dx.sample_box, attr) |
return getattr(dx.sample_box, attr) |
213 |
|
|
|
214 |
159 |
|
|
215 |
160 |
# přidávání vzorků musí bejt explicitní! |
# přidávání vzorků musí bejt explicitní! |
216 |
161 |
def add_sample(bx, input_sample): |
def add_sample(bx, input_sample): |
217 |
162 |
bx._logger(msg="we have got new data:", data=input_sample) |
bx._logger(msg="we have got new data:", data=input_sample) |
218 |
163 |
bx.sample_box.add_sample(input_sample) |
bx.sample_box.add_sample(input_sample) |
219 |
|
bx.increment(bx.sample_box[bx._nsim:]) |
|
220 |
|
bx._nsim = bx.nsim |
|
|
164 |
|
# tohle musí převest rozdělení vstupního vzorku na vlastní rozdělení skříňky |
|
165 |
|
inner_sample = bx.sample_box.new_sample(input_sample) |
|
166 |
|
bx.increment(inner_sample) |
|
167 |
|
|
221 |
168 |
|
|
222 |
169 |
def increment(bx, input_sample): |
def increment(bx, input_sample): |
223 |
170 |
bx._LHS_increment(input_sample) |
bx._LHS_increment(input_sample) |
|
... |
... |
class DiceBox: |
226 |
173 |
#ё шайтан регенираци лэзьиз |
#ё шайтан регенираци лэзьиз |
227 |
174 |
bx._logger(msg='regeneration started') |
bx._logger(msg='regeneration started') |
228 |
175 |
bx._LHS_regen() |
bx._LHS_regen() |
229 |
|
bx._nsim = bx.nsim |
|
230 |
176 |
|
|
231 |
177 |
def _LHS_regen(bx): |
def _LHS_regen(bx): |
232 |
178 |
# pro LHS_like_correction |
# pro LHS_like_correction |
|
... |
... |
class DiceBox: |
291 |
237 |
|
|
292 |
238 |
|
|
293 |
239 |
# MinEnergyCensoredSampling |
# MinEnergyCensoredSampling |
294 |
|
#č Pokud nepletu, hlavní pointa třídy byla v použití konvexní obálky |
|
295 |
|
#č místo triangulaci pro zjíštění inside-outside. |
|
296 |
|
#č Tohle brutálně všechno zrychlilo, proto se třída dostala název Chrt. |
|
297 |
240 |
class Chrt(DiceBox): |
class Chrt(DiceBox): |
298 |
|
""" |
|
299 |
|
Chrt |
|
300 |
|
methods: |
|
301 |
|
*__init__(): |
|
302 |
|
DiceBox.__init__(): |
|
303 |
|
*regen(): |
|
304 |
|
>_LHS_regen() |
|
305 |
|
_regen_outside(): |
|
306 |
|
estimate_outside(): |
|
307 |
|
assess_candidates: |
|
308 |
|
_nominate |
|
309 |
|
_regen_inside(): |
|
310 |
|
get_events |
|
311 |
|
estimate_simplex(simplex) |
|
312 |
|
_former_candidates_recovering |
|
313 |
|
assess_candidates |
|
314 |
|
|
|
315 |
|
>add_sample(): |
|
316 |
|
*increment(): |
|
317 |
|
_LHS_increment() |
|
318 |
|
export_estimation() |
|
319 |
|
_handle_changed_triangulation(input_sample): |
|
320 |
|
get_events() |
|
321 |
|
estimate_simplex(simplex): |
|
322 |
|
_former_candidates_recovering |
|
323 |
|
assess_candidates |
|
324 |
|
_invalidate_simplex(simplex) |
|
325 |
|
|
|
326 |
|
_handle_changed_outside(input_sample) |
|
327 |
|
estimate_outside(): |
|
328 |
|
assess_candidates: |
|
329 |
|
_nominate |
|
330 |
|
|
|
331 |
|
_handle_candidates() |
|
332 |
|
_judge_unjudged |
|
333 |
|
assess_candidates: |
|
334 |
|
_nominate |
|
335 |
|
assess_candidates: |
|
336 |
|
_nominate |
|
337 |
|
|
|
338 |
|
*regen() |
|
339 |
|
|
|
340 |
|
*__call__(): |
|
341 |
|
>LHS_like_correction() |
|
342 |
|
|
|
343 |
|
get_pf_estimation() |
|
344 |
|
|
|
345 |
|
export_estimation(): |
|
346 |
|
get_pf_estimation() |
|
347 |
|
""" |
|
348 |
241 |
#č už mě to dědění nebaví |
#č už mě to dědění nebaví |
349 |
242 |
#ё без поллитры было не разобраться, что этот слоёный пирог делал |
#ё без поллитры было не разобраться, что этот слоёный пирог делал |
350 |
243 |
def __init__(bx, sample_object, tri_space='Rn', tree_space=None,\ |
def __init__(bx, sample_object, tri_space='Rn', tree_space=None,\ |
351 |
|
sampling_space=None, kechato_space='U', potential='psee',\ |
|
352 |
|
p_norm=2, budget=1000, simplex_budget=100, q=0.5,\ |
|
|
244 |
|
sampling_space=None, kechato_space='U', potencial='psee',\ |
|
245 |
|
p_norm=2, budget=1000, simplex_budget=100, \ |
353 |
246 |
LHS_correction=False, design=None): |
LHS_correction=False, design=None): |
354 |
247 |
|
|
355 |
248 |
|
|
|
... |
... |
class Chrt(DiceBox): |
369 |
262 |
bx.budget = budget |
bx.budget = budget |
370 |
263 |
bx.simplex_budget = simplex_budget |
bx.simplex_budget = simplex_budget |
371 |
264 |
bx.p_norm = p_norm |
bx.p_norm = p_norm |
372 |
|
bx.q = q |
|
373 |
|
bx.potential = potential |
|
|
265 |
|
bx.potencial = potencial |
374 |
266 |
bx.LHS_correction = LHS_correction |
bx.LHS_correction = LHS_correction |
375 |
267 |
bx.design = design |
bx.design = design |
376 |
268 |
|
|
|
... |
... |
class Chrt(DiceBox): |
392 |
284 |
return {'sample_object':bx.sample_box, \ |
return {'sample_object':bx.sample_box, \ |
393 |
285 |
'tri_space':bx.tri_space, 'tree_space':bx.tree_space,\ |
'tri_space':bx.tri_space, 'tree_space':bx.tree_space,\ |
394 |
286 |
'sampling_space':bx.sampling_space, 'kechato_space':bx.kechato_space,\ |
'sampling_space':bx.sampling_space, 'kechato_space':bx.kechato_space,\ |
395 |
|
'potential':bx.potential, 'p_norm':bx.p_norm, 'budget':bx.budget,\ |
|
|
287 |
|
'potencial':bx.potencial, 'p_norm':bx.p_norm, 'budget':bx.budget,\ |
396 |
288 |
'simplex_budget':bx.simplex_budget, \ |
'simplex_budget':bx.simplex_budget, \ |
397 |
289 |
'LHS_correction':bx.LHS_correction, 'design':str(bx.design)} |
'LHS_correction':bx.LHS_correction, 'design':str(bx.design)} |
398 |
290 |
|
|
|
... |
... |
class Chrt(DiceBox): |
430 |
322 |
bx._LHS_regen() |
bx._LHS_regen() |
431 |
323 |
|
|
432 |
324 |
# kind of interface to CandidatesWidget |
# kind of interface to CandidatesWidget |
433 |
|
bx.candidates_index = CacheDict(bx.potential) |
|
|
325 |
|
bx.candidates_index = dict() |
434 |
326 |
|
|
435 |
327 |
if bx.nsim > 0: |
if bx.nsim > 0: |
436 |
|
# needed for potential calculation |
|
|
328 |
|
# needed for potencial calculation |
437 |
329 |
sampled_plan_tree = getattr(bx.sample_box, bx.tree_space) |
sampled_plan_tree = getattr(bx.sample_box, bx.tree_space) |
438 |
330 |
bx.tree = spatial.cKDTree(sampled_plan_tree) |
bx.tree = spatial.cKDTree(sampled_plan_tree) |
439 |
331 |
bx.highest_bid = 0 |
bx.highest_bid = 0 |
440 |
332 |
|
|
441 |
333 |
bx._regen_outside() |
bx._regen_outside() |
442 |
334 |
bx._regen_inside() |
bx._regen_inside() |
443 |
|
bx._nsim = bx.nsim |
|
|
335 |
|
|
444 |
336 |
|
|
445 |
337 |
|
|
446 |
338 |
def _regen_outside(bx): |
def _regen_outside(bx): |
|
... |
... |
class Chrt(DiceBox): |
521 |
413 |
bx._LHS_increment(input_sample) |
bx._LHS_increment(input_sample) |
522 |
414 |
|
|
523 |
415 |
#č strom posuneme sem |
#č strom posuneme sem |
524 |
|
# cKDTree is used for potential calculation |
|
|
416 |
|
# cKDTree is used for potencial calculation |
525 |
417 |
# we need everytime regenerate it |
# we need everytime regenerate it |
526 |
418 |
sampled_plan_tree = getattr(bx.sample_box, bx.tree_space) |
sampled_plan_tree = getattr(bx.sample_box, bx.tree_space) |
527 |
419 |
bx.tree = spatial.cKDTree(sampled_plan_tree) |
bx.tree = spatial.cKDTree(sampled_plan_tree) |
|
... |
... |
class Chrt(DiceBox): |
629 |
521 |
#č A ještě... AUKCE, AUCTION |
#č A ještě... AUKCE, AUCTION |
630 |
522 |
# Election - selection |
# Election - selection |
631 |
523 |
for candidates in bx.candidates_index.values(): |
for candidates in bx.candidates_index.values(): |
632 |
|
bids = getattr(candidates, bx.potential) |
|
|
524 |
|
bids = getattr(candidates, bx.potencial) |
633 |
525 |
if len(bids): |
if len(bids): |
634 |
526 |
bid = np.nanmax(bids) |
bid = np.nanmax(bids) |
635 |
527 |
# side effect |
# side effect |
|
... |
... |
class Chrt(DiceBox): |
649 |
541 |
""" |
""" |
650 |
542 |
function should be only runned by .assess_candidates() |
function should be only runned by .assess_candidates() |
651 |
543 |
""" |
""" |
652 |
|
bids = getattr(candidates, bx.potential) |
|
|
544 |
|
bids = getattr(candidates, bx.potencial) |
653 |
545 |
# -1 = 'out', 0=success, 1=failure, 2=mix |
# -1 = 'out', 0=success, 1=failure, 2=mix |
654 |
546 |
#č -1 a 2 jsou samozrejmě disjunktní |
#č -1 a 2 jsou samozrejmě disjunktní |
655 |
547 |
bids *= (candidates.event_id == -1) + (candidates.event_id == 2) |
bids *= (candidates.event_id == -1) + (candidates.event_id == 2) |
|
... |
... |
class Chrt(DiceBox): |
683 |
575 |
|
|
684 |
576 |
if bx.nsim < 1: # je to legální |
if bx.nsim < 1: # je to legální |
685 |
577 |
bx._logger(msg="median first!") |
bx._logger(msg="median first!") |
686 |
|
node = bx.LHS_like_correction(bx.f_model(1)) |
|
687 |
|
return CandyBox(node, event_id=np.full(1, -1, dtype=np.int8)) |
|
|
578 |
|
return bx.LHS_like_correction(bx.f_model(1)) |
688 |
579 |
else: |
else: |
689 |
580 |
selected = bx.bidder |
selected = bx.bidder |
690 |
581 |
if bx.LHS_correction: |
if bx.LHS_correction: |
|
... |
... |
class Chrt(DiceBox): |
716 |
607 |
|
|
717 |
608 |
|
|
718 |
609 |
# potřebuju nová slovesa |
# potřebuju nová slovesa |
719 |
|
#ё пранк вышел из под контроля |
|
720 |
|
#č jako, byl to takovej vtipeček označit potenciál dvojitým e |
|
721 |
|
#č stala se z toho ale konvence, ne že by všal byla ničím nepodložena |
|
722 |
|
#č algorithmus se sestavá ze dvou částí: |
|
723 |
|
#č tri - triangulace, |
|
724 |
|
#č tree - stromovej cKDTree scipy algorithmus, který hledá nejblížší body |
|
725 |
|
#č (nechám bokem otázku, že ten asi hned na začátku nepřepiná do banalního brute force) |
|
726 |
|
#č Každopadně, davá smysl, že psee, chee atd. potenciály spadají do tree části. |
|
727 |
610 |
def assess_candidates(bx, candidates): |
def assess_candidates(bx, candidates): |
728 |
|
#č nikdo to nepouživá, ale se mi nějaký takový parameter libí. |
|
729 |
611 |
candidates.nsim_stamp = np.full(len(candidates), bx.nsim) |
candidates.nsim_stamp = np.full(len(candidates), bx.nsim) |
730 |
612 |
|
|
731 |
613 |
candidates_tree = getattr(candidates, bx.tree_space) |
candidates_tree = getattr(candidates, bx.tree_space) |
|
... |
... |
class Chrt(DiceBox): |
742 |
624 |
if np.any(mask): #č ať mě to neznervozňuje |
if np.any(mask): #č ať mě to neznervozňuje |
743 |
625 |
ii[mask] = bx.nsim - 1 |
ii[mask] = bx.nsim - 1 |
744 |
626 |
dd[mask] = 0 |
dd[mask] = 0 |
745 |
|
bx._logger(msg="cKDTree zlobí", orphan_candidates=candidates[mask], P=candidates[mask].P) |
|
|
627 |
|
bx._logger(msg="cKDTree zlobí", orphan_candidates=candidates[mask]) |
746 |
628 |
|
|
747 |
|
# the most agressive potential ever |
|
|
629 |
|
|
748 |
630 |
candidates.dd = dd |
candidates.dd = dd |
|
631 |
|
candidates.ii = ii |
749 |
632 |
|
|
750 |
|
if bx.potential in ('psee', 'fee', 'fee2'): |
|
|
633 |
|
|
|
634 |
|
if bx.potencial in ('psee', 'fee', 'fee2'): |
751 |
635 |
#оӵ кучапи |
#оӵ кучапи |
752 |
636 |
#č pejskovej potenciál |
#č pejskovej potenciál |
753 |
637 |
#č psí-kučapí není invariántní vůči lineárním transformácím |
#č psí-kučapí není invariántní vůči lineárním transformácím |
|
... |
... |
class Chrt(DiceBox): |
757 |
641 |
PDF = PDFs[ii] |
PDF = PDFs[ii] |
758 |
642 |
pdf = candidates.pdf(bx.tree_space) |
pdf = candidates.pdf(bx.tree_space) |
759 |
643 |
|
|
760 |
|
tree_Pdf_mean = (pdf+PDF)/2 |
|
761 |
|
tree_Pdf_gmean = np.sqrt(pdf*PDF) |
|
762 |
|
volume = np.power(dd, bx.nvar) |
|
763 |
|
candidates.psee = tree_Pdf_gmean * volume |
|
764 |
|
candidates.fee = tree_Pdf_mean * volume * np.power(pdf/PDF, 1/(bx.nvar+1)) |
|
765 |
|
candidates.fee2 = tree_Pdf_mean * volume * np.power(pdf/PDF, 1/(bx.nvar*2)) |
|
|
644 |
|
candidates.tree_PDF = PDF |
|
645 |
|
candidates.tree_pdf = pdf |
|
646 |
|
|
|
647 |
|
#sampled_tree = getattr(bx.sample_box, bx.tree_space) |
|
648 |
|
#mirrored_tree = candidates_tree*2 |
|
649 |
|
#mirrored_tree -= sampled_tree[ii] #č ušetříme aspoň kuseček paměti |
|
650 |
|
#candidates.mirror_pdf = candidates.sample_pdf(mirrored_tree, bx.tree_space) |
|
651 |
|
|
|
652 |
|
candidates.tree_Pdf_mean = (pdf+PDF)/2 |
|
653 |
|
candidates.tree_Pdf_gmean = np.sqrt(pdf*PDF) |
|
654 |
|
candidates.volume = np.power(dd , bx.nvar) |
|
655 |
|
candidates.psee = candidates.tree_Pdf_gmean * candidates.volume |
|
656 |
|
#candidates.density = PDF + 4*pdf + candidates.mirror_pdf #pdf**2/PDF |
|
657 |
|
candidates.fee = candidates.tree_Pdf_mean * candidates.volume * np.power(pdf/PDF, 1/(bx.nvar+1)) |
|
658 |
|
candidates.fee2 = candidates.tree_Pdf_mean * candidates.volume * np.power(pdf/PDF, 1/(bx.nvar*2)) |
766 |
659 |
|
|
767 |
|
elif bx.potential == 'ksee': # ksee |
|
|
660 |
|
elif bx.potencial == 'ksee': # ksee |
768 |
661 |
#оӵ кечато |
#оӵ кечато |
769 |
662 |
#č koťatko-káčátkovej potenciál |
#č koťatko-káčátkovej potenciál |
770 |
663 |
#č ksí-kěčató není invariántní vůčí rotacím |
#č ksí-kěčató není invariántní vůčí rotacím |
|
... |
... |
class Chrt(DiceBox): |
773 |
666 |
# doufám, že je to legální |
# doufám, že je to legální |
774 |
667 |
ksee[i==ii] = lk.kechato_potential(bx.f_model[i], candidates[i==ii], kechato_space=bx.kechato_space) |
ksee[i==ii] = lk.kechato_potential(bx.f_model[i], candidates[i==ii], kechato_space=bx.kechato_space) |
775 |
668 |
candidates.ksee = ksee |
candidates.ksee = ksee |
776 |
|
|
|
777 |
|
elif bx.potential == 'q_psee': #č kup si |
|
778 |
|
#оӵ кучапи |
|
779 |
|
#č pejskovej potenciál |
|
780 |
|
#č psí-kučapí není invariántní vůči lineárním transformácím |
|
781 |
|
|
|
782 |
|
PDFs = bx.sample_box.pdf(bx.tree_space) |
|
783 |
|
# teď máme hustoty kandidatů a prislušejicích jím vzorků |
|
784 |
|
PDF = PDFs[ii] |
|
785 |
|
pdf = candidates.pdf(bx.tree_space) |
|
786 |
|
|
|
787 |
|
volume = np.power(dd, bx.nvar) |
|
788 |
|
candidates.psee = np.sqrt(pdf*PDF) * volume |
|
789 |
|
candidates.q_psee = pdf**bx.q * PDF**(1-bx.q) * volume |
|
790 |
|
|
|
791 |
|
|
|
792 |
|
elif bx.potential in ('chee', 'chee2'): |
|
793 |
|
PDFs = bx.sample_box.pdf(bx.tree_space) |
|
794 |
|
# teď máme hustoty kandidatů a prislušejicích jím vzorků |
|
795 |
|
PDF = PDFs[ii] |
|
796 |
|
candidates.PDF = PDF |
|
797 |
|
pdf = candidates.pdf(bx.tree_space) |
|
798 |
|
candidates.Pdf = pdf #č pdf() je funkce f_modelu |
|
799 |
|
|
|
800 |
|
|
|
801 |
|
tree_Pdf_gmean = np.sqrt(pdf*PDF) |
|
802 |
|
volume = np.power(dd, bx.nvar) |
|
803 |
|
candidates.psee = tree_Pdf_gmean * volume |
|
804 |
|
|
|
805 |
|
sum_squares = np.sqrt(np.sum(np.square(candidates.G), axis=1)) |
|
806 |
|
chi2 = stats.chi2.pdf(sum_squares, bx.nvar) |
|
807 |
|
candidates.chi2 = chi2 |
|
808 |
|
candidates.chee2 = np.sqrt(chi2*PDF) * volume |
|
809 |
|
|
|
810 |
|
r = np.sqrt(sum_squares) |
|
811 |
|
candidates.r = r |
|
812 |
|
chi = stats.chi.pdf(r, bx.nvar) |
|
813 |
|
candidates.chi = chi |
|
814 |
|
candidates.chee = np.sqrt(chi*PDF) * volume |
|
815 |
|
|
|
816 |
|
|
|
817 |
669 |
|
|
818 |
670 |
# prepare to elections |
# prepare to elections |
819 |
671 |
bx._nominate(candidates) |
bx._nominate(candidates) |
|
... |
... |
class Chrt(DiceBox): |
832 |
684 |
#č vyhodnotíme |
#č vyhodnotíme |
833 |
685 |
bx.assess_candidates(candidates) |
bx.assess_candidates(candidates) |
834 |
686 |
#č půlku prýč |
#č půlku prýč |
835 |
|
bids = getattr(candidates, bx.potential) |
|
|
687 |
|
bids = getattr(candidates, bx.potencial) |
836 |
688 |
mask = np.isfinite(bids) |
mask = np.isfinite(bids) |
837 |
689 |
if np.any(mask): #č hmm... |
if np.any(mask): #č hmm... |
838 |
690 |
candidates = candidates[mask] |
candidates = candidates[mask] |
|
... |
... |
class Chrt(DiceBox): |
1012 |
864 |
|
|
1013 |
865 |
#оӵ ӧрӟи |
#оӵ ӧрӟи |
1014 |
866 |
#č RJ |
#č RJ |
1015 |
|
#č Třída nevytvaří triangulaci, dokud nejsou tečky oboje druhů. |
|
1016 |
|
#č Jakoby letí nad zemi jako orel (ӧрӟи), nebo jako vlaky RJ jede bez zastavek. |
|
1017 |
867 |
class Erjee(Chrt): |
class Erjee(Chrt): |
1018 |
868 |
def _regen_inside(bx): |
def _regen_inside(bx): |
1019 |
869 |
failsi = bx.failsi |
failsi = bx.failsi |
|
... |
... |
class Erjee(Chrt): |
1032 |
882 |
bx._LHS_increment(input_sample) |
bx._LHS_increment(input_sample) |
1033 |
883 |
|
|
1034 |
884 |
#č strom posuneme sem |
#č strom posuneme sem |
1035 |
|
# cKDTree is used for potential calculation |
|
|
885 |
|
# cKDTree is used for potencial calculation |
1036 |
886 |
# we need everytime regenerate it |
# we need everytime regenerate it |
1037 |
887 |
sampled_plan_tree = getattr(bx.sample_box, bx.tree_space) |
sampled_plan_tree = getattr(bx.sample_box, bx.tree_space) |
1038 |
888 |
bx.tree = spatial.cKDTree(sampled_plan_tree) |
bx.tree = spatial.cKDTree(sampled_plan_tree) |
|
... |
... |
class Erjee(Chrt): |
1068 |
918 |
|
|
1069 |
919 |
|
|
1070 |
920 |
|
|
1071 |
|
# use cubature formulas for simplex integration |
|
|
921 |
|
# MinEnergyCensoredSampling |
1072 |
922 |
class Razitko(Erjee): |
class Razitko(Erjee): |
1073 |
923 |
#č už mě to dědění nebaví |
#č už mě to dědění nebaví |
1074 |
924 |
#ё без поллитры было не разобраться, что этот слоёный пирог делал |
#ё без поллитры было не разобраться, что этот слоёный пирог делал |
1075 |
925 |
def __init__(bx, sample_object, scheme, tri_space='Rn', tree_space=None,\ |
def __init__(bx, sample_object, scheme, tri_space='Rn', tree_space=None,\ |
1076 |
|
sampling_space=None, kechato_space='U', potential='psee',\ |
|
1077 |
|
p_norm=2, q=0.5, budget=1000, \ |
|
|
926 |
|
sampling_space=None, kechato_space='U', potencial='psee',\ |
|
927 |
|
p_norm=2, budget=1000, \ |
1078 |
928 |
LHS_correction=False, design=None): |
LHS_correction=False, design=None): |
1079 |
929 |
|
|
1080 |
930 |
bx.scheme = scheme |
bx.scheme = scheme |
|
... |
... |
class Razitko(Erjee): |
1093 |
943 |
bx.kechato_space = kechato_space |
bx.kechato_space = kechato_space |
1094 |
944 |
bx.budget = budget |
bx.budget = budget |
1095 |
945 |
bx.p_norm = p_norm |
bx.p_norm = p_norm |
1096 |
|
bx.q = q |
|
1097 |
|
bx.potential = potential |
|
|
946 |
|
bx.potencial = potencial |
1098 |
947 |
bx.LHS_correction = LHS_correction |
bx.LHS_correction = LHS_correction |
1099 |
948 |
bx.design = design |
bx.design = design |
1100 |
949 |
|
|
|
... |
... |
class Razitko(Erjee): |
1123 |
972 |
return {'sample_object':bx.sample_box, 'scheme':bx.scheme.name,\ |
return {'sample_object':bx.sample_box, 'scheme':bx.scheme.name,\ |
1124 |
973 |
'tri_space':bx.tri_space, 'tree_space':bx.tree_space,\ |
'tri_space':bx.tri_space, 'tree_space':bx.tree_space,\ |
1125 |
974 |
'sampling_space':bx.sampling_space, 'kechato_space':bx.kechato_space,\ |
'sampling_space':bx.sampling_space, 'kechato_space':bx.kechato_space,\ |
1126 |
|
'potential':bx.potential, 'p_norm':bx.p_norm, 'budget':bx.budget,\ |
|
|
975 |
|
'potencial':bx.potencial, 'p_norm':bx.p_norm, 'budget':bx.budget,\ |
1127 |
976 |
'LHS_correction':bx.LHS_correction, 'design':str(bx.design)} |
'LHS_correction':bx.LHS_correction, 'design':str(bx.design)} |
1128 |
977 |
|
|
1129 |
978 |
def _regen_inside(bx): |
def _regen_inside(bx): |
|
... |
... |
class Razitko(Erjee): |
1234 |
1083 |
|
|
1235 |
1084 |
|
|
1236 |
1085 |
|
|
1237 |
|
#č Teď je třeba vytvoriť novou skříňku, která by místo Shull použivala Ghull. |
|
1238 |
|
#č Samozřejmě, že už ani já, ani čert nemůžeme se v tom vyznat |
|
1239 |
|
#č Takže radší zdědíme pouze bázový DiceBox |
|
1240 |
1086 |
|
|
1241 |
|
#č Je třeba dávát bacha na odlišnosti v (staré) Triangulation třídě a nové Ghull třídě. |
|
1242 |
|
#č Zatímco Triangulation drží starý stáv, dokud .update() není spustěn, |
|
1243 |
|
#č Ghull, ale hlavně, odpovídající modely konvexních obálek jíž žádný .update() nemájí, |
|
1244 |
|
#č nové tečky uvidí sami dřív než se naše skříňka probere. |
|
1245 |
|
#č Takže teď odhady nově budeme ukladať hned pri incrementu. |
|
1246 |
|
#č Triangulation používá i jínej kód, samotné třídy beztak zbytečně komplikováné, |
|
1247 |
|
#č nechci teď to toho lezt. |
|
1248 |
|
class Goal(DiceBox): |
|
1249 |
|
""" |
|
1250 |
|
Goal |
|
1251 |
|
methods: |
|
1252 |
|
*__init__(): |
|
1253 |
|
DiceBox.__init__(): |
|
1254 |
|
Chrt.regen(): |
|
1255 |
|
DiceBox._LHS_regen() |
|
1256 |
|
_regen_outside(): |
|
1257 |
|
estimate_outside(): |
|
1258 |
|
Chrt.assess_candidates: |
|
1259 |
|
Chrt._nominate |
|
1260 |
|
_regen_inside(): |
|
1261 |
|
__regen_inside: |
|
1262 |
|
**Triangulation magic**: |
|
1263 |
|
Razitko._on_add_simplex: |
|
1264 |
|
Chrt.assess_candidates: |
|
1265 |
|
Chrt._nominate |
|
1266 |
|
|
|
1267 |
|
>add_sample(): |
|
1268 |
|
*increment(): |
|
1269 |
|
>_LHS_increment() |
|
1270 |
|
export_estimation() |
|
1271 |
|
_handle_changed_triangulation(input_sample): |
|
1272 |
|
get_events() |
|
1273 |
|
estimate_simplex(simplex): |
|
1274 |
|
assess_candidates |
|
1275 |
|
_invalidate_simplex(simplex) |
|
1276 |
|
|
|
1277 |
|
_handle_changed_outside(input_sample) |
|
1278 |
|
estimate_outside(): |
|
1279 |
|
assess_candidates: |
|
1280 |
|
_nominate |
|
1281 |
|
|
|
1282 |
|
_handle_candidates() |
|
1283 |
|
assess_candidates: |
|
1284 |
|
_nominate |
|
1285 |
|
|
|
1286 |
|
*regen() |
|
1287 |
|
|
|
1288 |
|
Chrt.__call__(): |
|
1289 |
|
>LHS_like_correction() |
|
1290 |
|
|
|
1291 |
|
get_pf_estimation() |
|
1292 |
|
|
|
1293 |
|
export_estimation(): |
|
1294 |
|
get_pf_estimation() |
|
1295 |
|
""" |
|
1296 |
|
|
|
1297 |
|
#č praca s kandidatama moc sa nezměnila |
|
1298 |
|
#č funkce assess_candidates přířadí potenciál bodíkům |
|
1299 |
|
#č a zaroveň je nominuje na soutež. |
|
1300 |
|
#č na vstupu assess_candidates musí být CandyBox |
|
1301 |
|
#č s jíž nastaveným event_id |
|
1302 |
|
assess_candidates = Chrt.assess_candidates |
|
1303 |
|
_nominate = Chrt._nominate |
|
1304 |
|
|
|
1305 |
|
#č explicitně převezmu některé funkce |
|
1306 |
|
#č ať v budoucnu nelamame hlavu, co jestě potřebujeme, co už nikoliv |
|
1307 |
|
__repr__ = Chrt.__repr__ |
|
1308 |
|
__str__ = Chrt.__str__ |
|
1309 |
|
__call__ = Chrt.__call__ |
|
1310 |
|
regen = Chrt.regen |
|
1311 |
|
|
|
1312 |
|
_on_add_simplex = Razitko._on_add_simplex |
|
1313 |
|
_invalidate_simplex = Razitko._invalidate_simplex |
|
1314 |
|
|
|
1315 |
|
|
|
1316 |
|
#č míží nám sampling_space: Ghull umí vzorkovat outside pouze v G prostoru |
|
1317 |
|
#č quadpy umístí integráční bodíky v prostoru triangulace. |
|
1318 |
|
def __init__(bx, sample_object, scheme, tri_space='G', tree_space=None,\ |
|
1319 |
|
kechato_space='U', potential='q_psee', q=0.5,\ |
|
1320 |
|
p_norm=2, shell_budget=1000, outer_budget=100,\ |
|
1321 |
|
LHS_correction=False, stm_filename=None): |
|
1322 |
|
|
|
1323 |
|
bx.scheme = scheme |
|
1324 |
|
bx.tri_space = tri_space |
|
1325 |
|
if tree_space is None: |
|
1326 |
|
bx.tree_space = tri_space |
|
1327 |
|
else: |
|
1328 |
|
bx.tree_space = tree_space |
|
1329 |
|
|
|
1330 |
|
|
|
1331 |
|
bx.kechato_space = kechato_space |
|
1332 |
|
bx.shell_budget = shell_budget |
|
1333 |
|
bx.outer_budget = outer_budget |
|
1334 |
|
bx.p_norm = p_norm |
|
1335 |
|
bx.potential = potential |
|
1336 |
|
bx.q = q # used for q_psee potential only |
|
1337 |
|
bx.LHS_correction = LHS_correction |
|
1338 |
|
|
|
1339 |
|
bx.stm_filename = stm_filename |
|
1340 |
|
|
|
1341 |
|
DiceBox.__init__(bx, sample_object) |
|
1342 |
|
|
|
1343 |
|
|
|
1344 |
|
def init_parameters(bx): |
|
1345 |
|
""" |
|
1346 |
|
Returns dictionary of parameters the DiceBox was initialized with |
|
1347 |
|
""" |
|
1348 |
|
return {'sample_object':bx.sample_box, 'scheme':bx.scheme.name,\ |
|
1349 |
|
'tri_space':bx.tri_space, 'tree_space':bx.tree_space,\ |
|
1350 |
|
'kechato_space':bx.kechato_space, 'potential':bx.potential,\ |
|
1351 |
|
'p_norm':bx.p_norm, 'shell_budget':bx.shell_budget,\ |
|
1352 |
|
'outer_budget':bx.outer_budget, 'LHS_correction':bx.LHS_correction} |
|
1353 |
|
|
|
1354 |
1087 |
|
|
1355 |
|
|
|
1356 |
|
def _regen_outside(bx): |
|
1357 |
|
bx.convex_hull = khull.QHull(bx.f_model, space=bx.tri_space) # for gl_plot |
|
1358 |
|
bx.ghull = Ghull(bx.convex_hull) |
|
1359 |
|
bx._R = -1 # update outer under R>_R condition |
|
1360 |
|
bx._afacet = None |
|
1361 |
|
bx._bfacet = np.inf |
|
1362 |
|
#č konečně mám pořádnou stejtful třídu |
|
1363 |
|
#č pokud mám aspoň jednu tečku, tak už je mi šuma |
|
1364 |
|
#č zda se konvexní obálka vytvořila, či nikoliv |
|
1365 |
|
if bx.nsim > 0: |
|
1366 |
|
bx.estimate_outside() |
|
1367 |
|
|
|
1368 |
|
|
|
1369 |
|
def _regen_inside(bx): |
|
1370 |
|
failsi = bx.failsi |
|
1371 |
|
if np.any(failsi) and not np.all(failsi): |
|
1372 |
|
#bx._logger(msg="triangulation started") |
|
1373 |
|
bx.__regen_inside() |
|
1374 |
|
else: |
|
1375 |
|
#č jíž není nutný |
|
1376 |
|
bx._logger(msg="triangulation skipped") |
|
1377 |
|
|
|
1378 |
|
def __regen_inside(bx): |
|
1379 |
|
# create .tri triangulation |
|
1380 |
|
if bx.nsim > bx.nvar + 1: # incremental triangulation require one more point |
|
1381 |
|
try: |
|
1382 |
|
# I'll use tri_space as a weigthing space |
|
1383 |
|
# It could be separeted, but I am a little bit tired |
|
1384 |
|
# from so much different spaces over there |
|
1385 |
|
bx.Tri = sx.JustCubatureTriangulation(bx.samplebox, bx.scheme,\ |
|
1386 |
|
tri_space=bx.tri_space, issi=None, \ |
|
1387 |
|
weighting_space=bx.tri_space, incremental=True,\ |
|
1388 |
|
on_add_simplex=bx._on_add_simplex,\ |
|
1389 |
|
on_delete_simplex=bx._invalidate_simplex) |
|
1390 |
|
|
|
1391 |
|
bx.Tri.integrate() # nic nevrácí, všecko je přes kolbeky |
|
1392 |
|
#č tri - Deloneho triangulace |
|
1393 |
|
bx.tri = bx.Tri.tri #č všichní tam očekávajou QHull |
|
1394 |
|
bx._logger(msg="triangulation has been created") |
|
1395 |
|
|
|
1396 |
|
except BaseException as e: |
|
1397 |
|
#č chcu zachytit spadnuti QHull na začatku, |
|
1398 |
|
#č kdy ještě není dostatek teček. |
|
1399 |
|
#č Jinak je třeba nechat QHull spadnout |
|
1400 |
|
if bx.nsim > 2*bx.nvar + 3: |
|
1401 |
|
#č no to teda ne! |
|
1402 |
|
raise |
|
1403 |
|
else: |
|
1404 |
|
#č lze přípustit chybu triangulace |
|
1405 |
|
bx._logger(msg='triangulation failed') |
|
1406 |
|
|
|
1407 |
|
|
|
1408 |
|
|
|
1409 |
|
#č beží 99% času |
|
1410 |
|
def increment(bx, input_sample): |
|
1411 |
|
#ё ну нахрен это ваше наследование-расследование |
|
1412 |
|
|
|
1413 |
|
#č nechť bude, asi nikomu nevadí |
|
1414 |
|
bx._LHS_increment(input_sample) |
|
1415 |
|
|
|
1416 |
|
#č strom posuneme sem |
|
1417 |
|
# cKDTree is used for potential calculation |
|
1418 |
|
# we need everytime regenerate it |
|
1419 |
|
sampled_plan_tree = getattr(bx.sample_box, bx.tree_space) |
|
1420 |
|
bx.tree = spatial.cKDTree(sampled_plan_tree) |
|
1421 |
|
bx.highest_bid = 0 |
|
1422 |
|
|
|
|
1088 |
|
|
|
1089 |
|
|
|
1090 |
|
|
|
1091 |
|
|
|
1092 |
|
|
|
1093 |
|
|
|
1094 |
|
|
|
1095 |
|
|
|
1096 |
|
|
|
1097 |
|
|
|
1098 |
|
|
|
1099 |
|
class FullSimpleX: |
|
1100 |
|
def export_estimation(bx): |
|
1101 |
|
bx.siss.get_estimations() |
|
1102 |
|
simplices = np.array(tuple(bx.siss.estimations.keys())) |
|
1103 |
|
probabilities = np.array(tuple(bx.siss.estimations.values())) |
1423 |
1104 |
|
|
1424 |
|
#č logika se mění. Konvexní obálku máme vždycky. |
|
1425 |
|
#č jistě, že po incrementu máme alespoň jeden vzorek |
|
1426 |
|
#č takže hned od začátku můžeme trhat odhady |
|
|
1105 |
|
estimation = dict() |
|
1106 |
|
estimation[-1] = np.sum(probabilities[simplices == -1]) |
1427 |
1107 |
|
|
1428 |
|
#č tri - Deloneho triangulace |
|
1429 |
|
if "tri" in dir(bx): |
|
1430 |
|
bx._handle_changed_triangulation(input_sample) |
|
1431 |
|
else: |
|
1432 |
|
bx._regen_inside() |
|
1433 |
|
|
|
1434 |
|
|
|
1435 |
|
bx._handle_changed_outside(input_sample) |
|
1436 |
|
bx._handle_candidates() |
|
|
1108 |
|
# jevy aj klidně in-place (nerobím kopiju) |
|
1109 |
|
events = simplices[simplices != -1] |
|
1110 |
|
probabilities = probabilities[simplices != -1] |
1437 |
1111 |
|
|
1438 |
|
#č exportovať odhady jistě môžeme |
|
1439 |
|
#č teďkom to děláme hned po přídání vzorků |
|
1440 |
|
bx.export_estimation() |
|
|
1112 |
|
# zhruba - get_events() vrací pole s odpovidajícími čísly jevů pro každý simplex, počineje od nuly |
|
1113 |
|
# tím slajsingem my jakoby vybirame ke každemu nalezenemu simplexovi ten správnej mu odpovídajicí jev |
|
1114 |
|
events = bx.simplex_events[events] |
1441 |
1115 |
|
|
|
1116 |
|
for i in range(3): # kvůli 0,1,2 robiť cyklus? |
|
1117 |
|
estimation[i] = np.sum(probabilities[events == i]) |
1442 |
1118 |
|
|
1443 |
|
#č tato funkce běží 91% času |
|
1444 |
|
# bottleneck function |
|
1445 |
|
def _handle_changed_triangulation(bx, input_sample): |
|
|
1119 |
|
bx.guessbox.guess('TRI_overall_estimations', bx.tri.npoints, estimation) |
|
1120 |
|
|
|
1121 |
|
|
|
1122 |
|
def estimate_simplex(bx, simplex_id): |
1446 |
1123 |
""" |
""" |
1447 |
|
Triangulace zajistěně existuje |
|
|
1124 |
|
Delaunay triangulation |
1448 |
1125 |
""" |
""" |
1449 |
|
bx.Tri.update() |
|
1450 |
|
|
|
1451 |
|
|
|
1452 |
|
|
|
1453 |
|
|
|
1454 |
|
def _handle_changed_outside(bx, input_sample): |
|
1455 |
|
try: |
|
1456 |
|
#č kontrola korrektní i v případě NaN |
|
1457 |
|
test = input_sample.event_id > -1 |
|
1458 |
|
#оӵ эскером |
|
1459 |
|
if not test.all(): |
|
1460 |
|
bx.estimate_outside() |
|
1461 |
|
except BaseException as e: |
|
1462 |
|
msg = "input sample didn't provide correct 'event_id' attribute " |
|
1463 |
|
error_msg = bx.__class__.__name__ + ": " + msg + repr(e) |
|
1464 |
|
bx._logger(msg=error_msg) |
|
1465 |
|
bx.estimate_outside() |
|
1466 |
|
|
|
1467 |
|
|
|
1468 |
|
|
|
1469 |
|
|
|
1470 |
|
def _handle_candidates(bx): |
|
1471 |
|
#č A ještě... AUKCE, AUCTION |
|
1472 |
|
# Election - selection |
|
1473 |
|
for key, cached_bid in reversed(bx.candidates_index.cache.items()): |
|
1474 |
|
# side effect |
|
1475 |
|
if cached_bid > bx.highest_bid: |
|
1476 |
|
#č pokud neprovadíme optimalizaci v simplexech |
|
1477 |
|
#č tak nám stačí jednoduše assessovat |
|
1478 |
|
bx.assess_candidates(bx.candidates_index[key]) |
|
1479 |
|
#č tím se mi aktualizuje cache |
|
1480 |
|
bx.candidates_index[key] = bx.candidates_index[key] |
|
1481 |
|
|
|
1482 |
|
# probably, we shouldn't purge user candidates (if they are) |
|
1483 |
|
# just every time evaluate them |
|
1484 |
|
#č kdyby někdo chtěl mít užovatelské kandidaty... |
|
1485 |
|
# if len(bx.candidates) > 0: |
|
1486 |
|
# bx.judge_candidates(bx.candidates) |
|
1487 |
|
# bx.assess_candidates(bx.candidates) |
|
1488 |
|
|
|
1489 |
|
|
|
1490 |
|
|
|
1491 |
|
|
|
1492 |
|
def _ghull_outside_callback(bx, outside_nodes): |
|
1493 |
|
#č sice získáme filtrovaný outside, |
|
1494 |
|
#č musíme sami zabalit bodíky do CandyBoxu |
|
1495 |
|
# -2 = 'inside' -1 = 'outside' |
|
1496 |
|
event_ids = np.full(len(outside_nodes), -1, dtype=np.int8) |
|
1497 |
|
candidates = CandyBox(outside_nodes, event_id=event_ids) |
|
1498 |
|
bx.assess_candidates(candidates) |
|
|
1126 |
|
#bx._logger(msg="estimate simplex"+str(simplex_id)) |
1499 |
1127 |
|
|
1500 |
|
bids = getattr(candidates, bx.potential) |
|
1501 |
|
#č nie třeba kontrolovat jevy, tam je pouze outside |
|
1502 |
|
#bids *= (candidates.event_id == -1) + (candidates.event_id == 2) |
|
1503 |
|
bid = np.nanmax(bids) |
|
1504 |
|
if bid > bx._highest_outside: |
|
1505 |
|
#č uložíme varku bodíku pouze když |
|
1506 |
|
#č majú větší potenciál, |
|
1507 |
|
bx._highest_outside = bid |
|
1508 |
|
#č čo tam připadně bylo - přepíšeme |
|
1509 |
|
#č uložíme s indexem dle ghull_estimation: |
|
1510 |
|
# -22: inner, -21: shell inside, -12: shell outside, -11: outer |
|
1511 |
|
bx.candidates_index[-12] = candidates |
|
|
1128 |
|
simplex = bx.tri.simplices[simplex_id] |
|
1129 |
|
# чылкыт f_model |
|
1130 |
|
vertices = bx.f_model[simplex] |
1512 |
1131 |
|
|
1513 |
|
|
|
1514 |
|
def estimate_outside(bx): |
|
1515 |
|
#č konečně mám pořádnou stejtful třídu |
|
1516 |
|
#č pokud mám aspoň jednu tečku, tak už je mi šuma |
|
1517 |
|
#č zda se konvexní obálka vytvořila, či nikoliv |
|
1518 |
|
|
|
1519 |
|
#č Máme 2 úkoly: |
|
1520 |
|
#č 1. Získat odhady a uložit je, abychom nemuseli opakovaně integrovat, |
|
1521 |
|
#č dokud se neobjeví nějaký nový vzorek zvenku. |
|
1522 |
|
#č 2. Získat kandidaty. |
|
1523 |
|
#č a. z mezíkruží (-12) |
|
1524 |
|
#č b. fire, to co navrhne QHull (-1) |
|
1525 |
|
#č c. boom, doporuření QHull můžou i zklamat (-11) |
|
1526 |
|
#č cc. ze vdálenejších galaxí (-111) |
|
1527 |
|
|
|
1528 |
|
#č prace s tečkami v mezikruži se změnila |
|
1529 |
|
#č teď tečky dostávám přes kolbek po částech |
|
1530 |
|
#č a není předem známo, kolik těch částí bude. |
|
1531 |
|
#č Na začátku radší, pro jistotu, |
|
1532 |
|
#č vyhodíme stare bodíky z mezikruži (-12) |
|
1533 |
|
bx.candidates_index.pop(-12, "Nejsou? Nevadí...") # "Ӧвӧл-а мар-а?" |
|
1534 |
|
bx._highest_outside = 0 |
|
1535 |
1132 |
|
|
1536 |
|
# get candidates! |
|
1537 |
|
#č explicitně (pokažde) počtem teček zadavám přesnost integrace |
|
1538 |
|
#č takže změny bx.shell_budget budou při dálším spuštění aplikovány |
|
1539 |
|
data = bx.ghull.integrate(bx.shell_budget, \ |
|
1540 |
|
callback_outside=bx._ghull_outside_callback) |
|
1541 |
|
ghull_estimation, convex_hull_estimation, global_stats = data |
|
1542 |
|
#č uložíme. Не жалко. |
|
1543 |
|
#č první úkol máme splněný |
|
1544 |
|
bx.ghull_estimation = ghull_estimation |
|
1545 |
|
bx.convex_hull_estimation = convex_hull_estimation |
|
1546 |
|
bx.global_stats = global_stats |
|
1547 |
|
bx._logger(msg="outside estimation:", ghull_stats=global_stats) |
|
1548 |
|
|
|
1549 |
|
|
|
1550 |
|
|
|
1551 |
|
#č zde už nám jde pouze o kandidaty |
|
1552 |
|
|
|
1553 |
|
# fire |
|
1554 |
|
bx._fire() |
|
1555 |
|
# boom |
|
1556 |
|
|
|
1557 |
|
if global_stats['R'] > bx._R: |
|
1558 |
|
#č Projedeme Moravou |
|
1559 |
|
nodes = bx.ghull.boom(bx.outer_budget, use_MC=True) |
|
1560 |
|
#č tyhle funkce už vracej pouhý f_model |
|
1561 |
|
event_id = np.full(bx.outer_budget, -1, dtype=np.int8) |
|
1562 |
|
candidates = CandyBox(nodes, event_id=event_id) |
|
1563 |
|
bx.assess_candidates(candidates) # nic nevrácí, to je procedura |
|
1564 |
|
bx.candidates_index[-11] = candidates |
|
1565 |
|
|
|
1566 |
|
#č Už máte Mléčnou dráhu projdutou? |
|
1567 |
|
nodes = bx.ghull.boom(bx.outer_budget, use_MC=False) |
|
1568 |
|
#č tyhle funkce už vracej pouhý f_model |
|
1569 |
|
event_id = np.full(bx.outer_budget, -1, dtype=np.int8) |
|
1570 |
|
candidates = CandyBox(nodes, event_id=event_id) |
|
1571 |
|
bx.assess_candidates(candidates) # nic nevrácí, to je procedura |
|
1572 |
|
bx.candidates_index[-111] = candidates |
|
1573 |
|
|
|
1574 |
|
bx._R = global_stats['R'] |
|
1575 |
|
bx._logger(msg='boom!', _R=bx._R) |
|
1576 |
|
#č to je vše. Nic nevrácíme |
|
1577 |
|
|
|
1578 |
|
def _fire(bx): |
|
1579 |
|
qhull = bx.ghull.hull |
|
1580 |
|
if bx._afacet is None: |
|
1581 |
|
bx.__fire() |
|
1582 |
|
|
|
1583 |
|
#č podle mě sem nemusí dojít, |
|
1584 |
|
#č dokud se konvexní obálka ve skutku nevytvoří |
|
1585 |
|
#č b-čko u QHull pro nás má jakoby záporné vzdálenosti |
|
1586 |
|
elif np.all(bx._bfacet > qhull.b): |
|
1587 |
|
#č jasně, že musíme zapalit |
|
1588 |
|
bx.__fire() |
|
1589 |
|
elif np.any(bx._afacet != qhull.A[np.nanargmax(qhull.b)]): |
|
1590 |
|
#č "beta" se nezměnila, ale jen kvůli jinejm návrhovým bodům |
|
1591 |
|
bx.__fire() |
|
1592 |
|
|
|
1593 |
|
def __fire(bx): |
|
1594 |
|
qhull = bx.ghull.hull |
|
1595 |
|
nodes = qhull.fire(bx.outer_budget, use_MC=True) |
|
1596 |
|
if nodes is not None: |
|
1597 |
|
#č tyhle funkce už vracej pouhý f_model |
|
1598 |
|
event_id = np.full(bx.outer_budget, -1, dtype=np.int8) |
|
1599 |
|
candidates = CandyBox(nodes, event_id=event_id) |
|
1600 |
|
bx.assess_candidates(candidates) # nic nevrácí, to je procedura |
|
1601 |
|
bx.candidates_index[-1] = candidates |
|
1602 |
|
|
|
1603 |
|
arg = np.nanargmax(qhull.b) |
|
1604 |
|
bx._bfacet = b = qhull.b[arg] |
|
1605 |
|
bx._afacet = a = qhull.A[arg] |
|
1606 |
|
bx._logger(msg='fire!', a=a, b=b) |
|
1607 |
|
|
|
1608 |
|
|
|
1609 |
|
def get_pf_estimation(bx): |
|
1610 |
|
#č dle toho, čo vidím v kódu (spouští nás .increment()) |
|
1611 |
|
#č přinejmenším konvexní obálka musí |
|
1612 |
|
#č zajištěně existovat |
|
1613 |
|
# convex_hull_estimation -2: inside, -1: outside |
|
1614 |
|
pf_inside = bx.convex_hull_estimation[-2] |
|
1615 |
|
pf_outside = bx.convex_hull_estimation[-1] |
|
|
1133 |
|
# already divided by nsim in variance formule |
|
1134 |
|
# divide by /(nvar+1)/(nvar+2) from simplex inertia tensor solution |
|
1135 |
|
# multiply by simplex_volume, but it looks like it shouldn't be here |
|
1136 |
|
# for simplex: d = nvar+2 |
|
1137 |
|
# sice to má nazev h_plan, ale nese rozdělení a hustoty v f-ku |
|
1138 |
|
h_plan = IS_stat.IS_like(vertices, sampling_space=bx.sampling_space, nis=bx.simplex_budget, d=bx.nvar+2) |
1616 |
1139 |
|
|
1617 |
|
#č Ghull spouštíme sporadicky, |
|
1618 |
|
#č takže musíme sami lepit nové etikety |
|
1619 |
|
bx.global_stats['nsim'] = bx.nsim |
|
|
1140 |
|
# nejdřív vyfiltrujeme vzorky, pak budeme řešit hustoty |
|
1141 |
|
# |
|
1142 |
|
h_plan_model = getattr(h_plan, bx.tri_space) |
|
1143 |
|
vertices_model = getattr(vertices, bx.tri_space) |
1620 |
1144 |
|
|
1621 |
|
failsi = bx.failsi |
|
|
1145 |
|
# budeme pokažde sestavovat triangulaci z jedného simplexu |
|
1146 |
|
# a rešit jen zda naši bodíky "inside or outside" |
|
1147 |
|
# (s narustajícím nsim tohle brzy se stavá rychlejším než bežný dotaz) |
|
1148 |
|
found_simplices = spatial.Delaunay(vertices_model).find_simplex(h_plan_model) |
1622 |
1149 |
|
|
1623 |
|
if 'tri' in dir(bx): |
|
1624 |
|
#č Tri.get_pf_estimation() vrací: |
|
1625 |
|
# 'TRI_estimation': tri_estimation, 'global_stats': {mix, failure}, \ |
|
1626 |
|
#'vertex_estimation' : vertex_estimation, \ |
|
1627 |
|
#'weighted_vertex_estimation' : weighted_vertex_estimation, |
|
1628 |
|
#'coplanar':sx.tri.coplanar} |
|
1629 |
|
estimations = bx.Tri.get_pf_estimation() |
|
1630 |
|
# TRI-compatible estimation |
|
1631 |
|
# -1=outside, 0=success, 1=failure, 2=mix |
|
1632 |
|
#č to je JustTriangulation, |
|
1633 |
|
#č outside (-1), ani success (1) nebudou korektní |
|
1634 |
|
tri_estimation = estimations.pop('TRI_estimation') |
|
1635 |
|
tri_estimation[-1] = pf_outside |
|
1636 |
|
tri_estimation[0] = pf_inside - tri_estimation[1] - tri_estimation[2] |
|
1637 |
|
estimations['TRI_overall_estimations'] = tri_estimation |
|
1638 |
|
estimations['ghull_estimation'] = bx.ghull_estimation |
|
1639 |
|
|
|
1640 |
|
#č hrozně důležitý. Těšíme se na csv-čko. |
|
1641 |
|
bx.global_stats.update(estimations['global_stats']) |
|
1642 |
|
bx.global_stats['success_points'] = len(failsi[~failsi]) |
|
1643 |
|
bx.global_stats['failure_points'] = len(failsi[failsi]) |
|
1644 |
|
bx.global_stats['success'] = tri_estimation[0] |
|
1645 |
|
bx.global_stats['candidates_sets'] = len(bx.candidates_index) |
|
1646 |
|
estimations['global_stats'].update(bx.global_stats) |
|
1647 |
|
return estimations |
|
|
1150 |
|
# ten simplex nic neznamená, asi nebudu jej použivat |
|
1151 |
|
h_plan.simplex = found_simplices |
1648 |
1152 |
|
|
|
1153 |
|
# uvnitř simplexu - mel by tam bejt pouze jeden, "nulový" simplex |
|
1154 |
|
mask = found_simplices == 0 |
1649 |
1155 |
|
|
1650 |
|
#оӵ триангуляци ӧвӧл, иськем... |
|
1651 |
1156 |
|
|
1652 |
|
#č může se stát, že první dvě tečky už hned májí různé barvy, |
|
1653 |
|
#č ale žádnej simplex ještě nemáme. |
|
1654 |
|
#č takže celou skříňku prostě bereme jako simplex |
|
1655 |
|
event, event_id, fr, wfr = sx.get_simplex_event(bx, weighting_space=bx.tri_space) |
|
1656 |
|
# -1=outside, 0=success, 1=failure, 2=mix |
|
1657 |
|
tri_estimation = {-1:pf_outside, 0:0, 1:0, 2:0} |
|
1658 |
|
tri_estimation[event_id] = pf_inside |
|
1659 |
|
|
|
1660 |
|
vertex_estimation = pf_inside * fr |
|
1661 |
|
weighted_vertex_estimation = pf_inside * wfr |
|
1662 |
|
|
|
1663 |
|
global_stats = bx.global_stats |
|
1664 |
|
# outside dodá Ghull |
|
1665 |
|
global_stats['success_points'] = len(failsi[~failsi]) |
|
1666 |
|
global_stats['failure_points'] = len(failsi[failsi]) |
|
1667 |
|
global_stats['success'] = tri_estimation[0] |
|
1668 |
|
global_stats['failure'] = tri_estimation[1] |
|
1669 |
|
global_stats['mix'] = tri_estimation[2] |
|
1670 |
|
global_stats['vertex_estimation'] = vertex_estimation |
|
1671 |
|
global_stats['weighted_vertex_estimation'] = weighted_vertex_estimation |
|
1672 |
|
global_stats['nsimplex'] = 0 |
|
1673 |
|
global_stats['tn_scheme'] = bx.scheme.name |
|
1674 |
|
global_stats['tn_scheme_points'] = bx.scheme.points.shape[1] |
|
1675 |
|
global_stats['newly_invalidated'] = 0 |
|
1676 |
|
global_stats['newly_estimated'] = 0 |
|
1677 |
|
global_stats['simplex_stats'] = 0 |
|
1678 |
|
global_stats['candidates_sets'] = len(bx.candidates_index) |
|
1679 |
|
global_stats['ncoplanar'] = 0 |
|
|
1157 |
|
# necháme ISSI trapit sa pravděpodobnostma |
|
1158 |
|
bx.siss.add_single_event_data(h_plan.w[mask], event=simplex_id, nis=bx.simplex_budget) |
1680 |
1159 |
|
|
1681 |
|
return {'TRI_overall_estimations': tri_estimation, \ |
|
1682 |
|
'vertex_estimation' : vertex_estimation, \ |
|
1683 |
|
'weighted_vertex_estimation' : weighted_vertex_estimation, \ |
|
1684 |
|
'ghull_estimation' : bx.ghull_estimation} |
|
1685 |
1160 |
|
|
|
1161 |
|
# je nejvyšší čas zjistit čo to byl za utvar |
1686 |
1162 |
|
|
1687 |
|
def export_estimation(bx): |
|
1688 |
|
#č teď raději estimátory ukladáme hned |
|
1689 |
|
for key, value in bx.get_pf_estimation().items(): |
|
1690 |
|
bx.guessbox.guess(key, bx.nsim, value) |
|
1691 |
|
|
|
1692 |
|
# prepare export to csv |
|
1693 |
|
# All I Can Give You (Ashley Wallbridge Remix) |
|
1694 |
|
# but not sure about proxy |
|
1695 |
|
#č Ghull nabízí slušný stats |
|
|
1163 |
|
# fp like a failure points. Number of failure points |
|
1164 |
|
# intersect 2 times faster than setdiff (in my tests) |
|
1165 |
|
fp = len(np.intersect1d(simplex, bx.sample_box.failure_points, assume_unique=True)) |
|
1166 |
|
# -1 = 'out', 0=success, 1=failure, 2=mix |
|
1167 |
|
if fp == bx.nvar + 1: |
|
1168 |
|
pass |
|
1169 |
|
#event = 'failure' |
|
1170 |
|
#event_id = 0 |
|
1171 |
|
elif fp == 0: |
|
1172 |
|
pass |
|
1173 |
|
#event = 'success' |
|
1174 |
|
#event_id = 1 |
|
1175 |
|
else: |
|
1176 |
|
#event = 'mix' |
|
1177 |
|
event_id = 2 |
|
1178 |
|
|
|
1179 |
|
candidates = h_plan[mask] |
|
1180 |
|
candidates.event = np.full(len(candidates), event_id, dtype=np.int8) |
|
1181 |
|
# a vyhodnotíme je |
|
1182 |
|
bx.assess_candidates(candidates) |
|
1183 |
|
# vzorky je třeba přidát ke kandidatům |
|
1184 |
|
# jako, nic nepokazí, ale čo tam připadně bylo - přepíše |
|
1185 |
|
bx.candidates_index[simplex_id] = candidates |
|
1186 |
|
|
|
1187 |
|
|
1696 |
1188 |
|
|
1697 |
|
#č Teď je to hrozně křehký, musí být zajištěno |
|
1698 |
|
#č stejný počet a stejné pořádí estimátorů. |
|
1699 |
|
#č Musí být způštěn bx.get_pf_estimation() |
|
1700 |
|
#č který dopočíta zbývající estimátory z triangulaci. |
|
1701 |
|
#č bx.get_pf_estimation() spolehá na určíté pořádí |
|
1702 |
|
#č global_stats z Triangulation třídy, čímž jsme |
|
1703 |
|
#č porušujeme zásady SOLID |
|
1704 |
|
#č Ale zatím budíž. Až se to rozbíje, tak možná |
|
1705 |
|
#č necham třídu samostatně inicializovyvat svůj |
|
1706 |
|
#č vlastní slovník s pevným počtem a pevným pořadím složek. |
|
1707 |
|
if bx.stm_filename is not None: |
|
1708 |
|
reader.export_estimation(bx.stm_filename, bx.global_stats) |
|
|
1189 |
|
# vzorky je třeba přidát ke kandidatům |
|
1190 |
|
# zás nakladaní s odpadem... |
|
1191 |
|
bx.unjudged_candidates.append(h_plan[~mask]) |
1709 |
1192 |
|
|
1710 |
1193 |
|
|
|
1194 |
|
|
1711 |
1195 |
|
|
1712 |
1196 |
|
|
|
1197 |
|
def _handle_candidates(bx): |
|
1198 |
|
# pokud -2 jíž existuje, tak pro dnešek stačí |
|
1199 |
|
if (-2 not in bx.candidates_index) and (len(bx.former_candidates) > 0): |
|
1200 |
|
# nikdo nám neslibil, že u starých kandidatu |
|
1201 |
|
# třeba se nebude zvýšovat potanciál |
|
1202 |
|
# (je to prostě, opravdu ríct, jednodušší) |
|
1203 |
|
|
|
1204 |
|
# prohrabeme odpad |
|
1205 |
|
candidates = bx.former_candidates.pop() |
|
1206 |
|
for i in range(len(bx.former_candidates)): |
|
1207 |
|
candidates.add_sample(bx.former_candidates.pop()) |
|
1208 |
|
for i in range(len(bx.unjudged_candidates)): |
|
1209 |
|
candidates.add_sample(bx.unjudged_candidates.pop()) |
|
1210 |
|
|
|
1211 |
|
# hodil by se ještě nám? |
|
1212 |
|
bx.judge_candidates(candidates) |
|
1213 |
|
# profiltrujeme |
|
1214 |
|
# -1 = 'out', 0=success, 1=failure, 2=mix |
|
1215 |
|
candidates = candidates[candidates.event != 0] |
|
1216 |
|
candidates = candidates[candidates.event != 1] |
|
1217 |
|
# uvalíme pokutu |
|
1218 |
|
bx.assess_candidates(candidates) |
|
1219 |
|
#č někoho z tyto hromady dostaneme |
|
1220 |
|
for __ in range((bx.nvar+1)*2): |
|
1221 |
|
if len(candidates) > 0: |
|
1222 |
|
mc_id = collections.Counter(candidates.simplex).most_common()[0][0] |
|
1223 |
|
#č uložíme |
|
1224 |
|
bx.candidates_index[mc_id].add_sample(candidates[candidates.simplex == mc_id]) |
|
1225 |
|
#č a teď bacha - ty co jsem uložil do simplexu nechcu v -2 videt |
|
1226 |
|
candidates = candidates[candidates.simplex != mc_id] |
|
1227 |
|
else: |
|
1228 |
|
break |
|
1229 |
|
if len(candidates) > 0: |
|
1230 |
|
#č -2 je určen pro zbytky, кылем-мылем |
|
1231 |
|
bx.candidates_index[-2] = candidates |
1713 |
1232 |
|
|
1714 |
1233 |
|
|
1715 |
1234 |
|
|
|
... |
... |
class DiceSimpleX: |
1781 |
1300 |
# |
# |
1782 |
1301 |
highest_bid = 0 |
highest_bid = 0 |
1783 |
1302 |
for i, candidates in bx.candidates_index.items(): |
for i, candidates in bx.candidates_index.items(): |
1784 |
|
bids = getattr(candidates, bx.potential) |
|
|
1303 |
|
bids = getattr(candidates, bx.potencial) |
1785 |
1304 |
bid = np.max(bids) |
bid = np.max(bids) |
1786 |
1305 |
# side effect |
# side effect |
1787 |
1306 |
if bid > highest_bid: |
if bid > highest_bid: |
|
... |
... |
class DiceSimpleX: |
1793 |
1312 |
candidates = bx.candidates_index[i] |
candidates = bx.candidates_index[i] |
1794 |
1313 |
|
|
1795 |
1314 |
#š a eště ráz |
#š a eště ráz |
1796 |
|
bids = getattr(candidates, bx.potential) |
|
|
1315 |
|
bids = getattr(candidates, bx.potencial) |
1797 |
1316 |
bid = np.max(bids) |
bid = np.max(bids) |
1798 |
1317 |
if bid > highest_bid: |
if bid > highest_bid: |
1799 |
1318 |
highest_bid = bid |
highest_bid = bid |
|
... |
... |
class DiceSimpleX: |
1867 |
1386 |
Objective function for optimalization |
Objective function for optimalization |
1868 |
1387 |
""" |
""" |
1869 |
1388 |
|
|
1870 |
|
if (bx.tri_space == bx.tree_space) and (bx.potential == 'psee'): |
|
|
1389 |
|
if (bx.tri_space == bx.tree_space) and (bx.potencial == 'psee'): |
1871 |
1390 |
x_tree = x |
x_tree = x |
1872 |
1391 |
pdf = float(bx.f_model.sample_pdf(x, bx.tree_space)) |
pdf = float(bx.f_model.sample_pdf(x, bx.tree_space)) |
1873 |
1392 |
else:#č máme smulu |
else:#č máme smulu |
|
... |
... |
class DiceSimpleX: |
1882 |
1401 |
# teď máme hustoty kandidatů a prislušejicích jím vzorků |
# teď máme hustoty kandidatů a prislušejicích jím vzorků |
1883 |
1402 |
PDF = PDFs[ii] |
PDF = PDFs[ii] |
1884 |
1403 |
|
|
1885 |
|
if bx.potential == 'psee': |
|
|
1404 |
|
if bx.potencial == 'psee': |
1886 |
1405 |
#оӵ кучапи |
#оӵ кучапи |
1887 |
1406 |
#č pejskovej potenciál |
#č pejskovej potenciál |
1888 |
1407 |
#č psí-kučapí není invariántní vůči lineárním transformácím |
#č psí-kučapí není invariántní vůči lineárním transformácím |
|
... |
... |
class DiceSimpleX: |
1901 |
1420 |
|
|
1902 |
1421 |
|
|
1903 |
1422 |
|
|
1904 |
|
|
|
1905 |
|
class KickPointVoronoi(DiceBox): |
|
1906 |
|
""" |
|
1907 |
|
Goal |
|
1908 |
|
methods: |
|
1909 |
|
*__init__(): |
|
1910 |
|
DiceBox.__init__(): |
|
1911 |
|
Chrt.regen(): |
|
1912 |
|
DiceBox._LHS_regen() |
|
1913 |
|
_regen_outside(): |
|
1914 |
|
estimate_outside(): |
|
1915 |
|
Chrt.assess_candidates: |
|
1916 |
|
Chrt._nominate |
|
1917 |
|
_regen_inside(): |
|
1918 |
|
__regen_inside: |
|
1919 |
|
**Triangulation magic**: |
|
1920 |
|
Razitko._on_add_simplex: |
|
1921 |
|
Chrt.assess_candidates: |
|
1922 |
|
Chrt._nominate |
|
1923 |
|
|
|
1924 |
|
>add_sample(): |
|
1925 |
|
*increment(): |
|
1926 |
|
>_LHS_increment() |
|
1927 |
|
export_estimation() |
|
1928 |
|
_handle_changed_triangulation(input_sample): |
|
1929 |
|
get_events() |
|
1930 |
|
estimate_simplex(simplex): |
|
1931 |
|
assess_candidates |
|
1932 |
|
_invalidate_simplex(simplex) |
|
1933 |
|
|
|
1934 |
|
_handle_changed_outside(input_sample) |
|
1935 |
|
estimate_outside(): |
|
1936 |
|
assess_candidates: |
|
1937 |
|
_nominate |
|
1938 |
|
|
|
1939 |
|
_handle_candidates() |
|
1940 |
|
assess_candidates: |
|
1941 |
|
_nominate |
|
1942 |
|
|
|
1943 |
|
*regen() |
|
1944 |
|
|
|
1945 |
|
Chrt.__call__(): |
|
1946 |
|
>LHS_like_correction() |
|
1947 |
|
|
|
1948 |
|
get_pf_estimation() |
|
1949 |
|
|
|
1950 |
|
export_estimation(): |
|
1951 |
|
get_pf_estimation() |
|
1952 |
|
""" |
|
1953 |
|
|
|
1954 |
|
#č praca s kandidatama moc sa nezměnila |
|
1955 |
|
#č funkce assess_candidates přířadí potenciál bodíkům |
|
1956 |
|
#č a zaroveň je nominuje na soutež. |
|
1957 |
|
#č na vstupu assess_candidates musí být CandyBox |
|
1958 |
|
#č s jíž nastaveným event_id |
|
1959 |
|
#assess_candidates = Chrt.assess_candidates |
|
1960 |
|
_nominate = Chrt._nominate |
|
1961 |
|
|
|
1962 |
|
#č explicitně převezmu některé funkce |
|
1963 |
|
#č ať v budoucnu nelamame hlavu, co jestě potřebujeme, co už nikoliv |
|
1964 |
|
__repr__ = Chrt.__repr__ |
|
1965 |
|
__str__ = Chrt.__str__ |
|
1966 |
|
__call__ = Chrt.__call__ |
|
1967 |
|
regen = Chrt.regen |
|
1968 |
|
|
|
1969 |
|
_on_add_simplex = Razitko._on_add_simplex |
|
1970 |
|
_invalidate_simplex = Razitko._invalidate_simplex |
|
1971 |
|
|
|
1972 |
|
|
|
1973 |
|
def __init__(bx, sample_object, ghull, tri_space='G', tree_space=None,\ |
|
1974 |
|
sampling_space=None, kechato_space='U', potential='q_psee', q=0.5,\ |
|
1975 |
|
p_norm=2, shell_budget=1000, outer_budget=100,\ |
|
1976 |
|
LHS_correction=False, stm_filename=None): |
|
1977 |
|
|
|
1978 |
|
bx.ghull = ghull |
|
1979 |
|
bx.tri_space = tri_space |
|
1980 |
|
if tree_space is None: |
|
1981 |
|
bx.tree_space = tri_space |
|
1982 |
|
else: |
|
1983 |
|
bx.tree_space = tree_space |
|
1984 |
|
|
|
1985 |
|
if sampling_space is None: |
|
1986 |
|
bx.sampling_space = tri_space |
|
1987 |
|
else: |
|
1988 |
|
bx.sampling_space = sampling_space |
|
1989 |
|
|
|
1990 |
|
|
|
1991 |
|
bx.kechato_space = kechato_space |
|
1992 |
|
bx.shell_budget = shell_budget |
|
1993 |
|
bx.outer_budget = outer_budget |
|
1994 |
|
bx.p_norm = p_norm |
|
1995 |
|
bx.potential = potential |
|
1996 |
|
bx.q = q # used for q_psee potential only |
|
1997 |
|
bx.LHS_correction = LHS_correction |
|
1998 |
|
|
|
1999 |
|
bx.stm_filename = stm_filename |
|
2000 |
|
|
|
2001 |
|
DiceBox.__init__(bx, sample_object) |
|
2002 |
|
|
|
2003 |
|
|
|
2004 |
|
def init_parameters(bx): |
|
2005 |
|
""" |
|
2006 |
|
Returns dictionary of parameters the DiceBox was initialized with |
|
2007 |
|
""" |
|
2008 |
|
return {'sample_object':bx.sample_box, 'scheme':bx.scheme.name,\ |
|
2009 |
|
'tri_space':bx.tri_space, 'tree_space':bx.tree_space,\ |
|
2010 |
|
'kechato_space':bx.kechato_space, 'potential':bx.potential,\ |
|
2011 |
|
'p_norm':bx.p_norm, 'shell_budget':bx.shell_budget,\ |
|
2012 |
|
'outer_budget':bx.outer_budget, 'LHS_correction':bx.LHS_correction} |
|
2013 |
|
|
|
2014 |
|
|
|
2015 |
|
|
|
2016 |
|
def _regen_outside(bx): |
|
2017 |
|
bx.convex_hull = khull.QHull(bx.f_model, space=bx.tri_space) # for gl_plot |
|
2018 |
|
bx.ghull = Ghull(bx.convex_hull) |
|
2019 |
|
bx._R = -1 # update outer under R>_R condition |
|
2020 |
|
bx._afacet = None |
|
2021 |
|
bx._bfacet = np.inf |
|
2022 |
|
#č konečně mám pořádnou stejtful třídu |
|
2023 |
|
#č pokud mám aspoň jednu tečku, tak už je mi šuma |
|
2024 |
|
#č zda se konvexní obálka vytvořila, či nikoliv |
|
2025 |
|
if bx.nsim > 0: |
|
2026 |
|
bx.estimate_outside() |
|
2027 |
|
|
|
2028 |
|
|
|
2029 |
|
def _regen_inside(bx): |
|
2030 |
|
failsi = bx.failsi |
|
2031 |
|
if np.any(failsi) and not np.all(failsi): |
|
2032 |
|
#bx._logger(msg="triangulation started") |
|
2033 |
|
bx.__regen_inside() |
|
2034 |
|
else: |
|
2035 |
|
#č jíž není nutný |
|
2036 |
|
bx._logger(msg="triangulation skipped") |
|
2037 |
|
|
|
2038 |
|
def __regen_inside(bx): |
|
2039 |
|
# create .tri triangulation |
|
2040 |
|
if bx.nsim > bx.nvar + 1: # incremental triangulation require one more point |
|
2041 |
|
try: |
|
2042 |
|
# I'll use tri_space as a weigthing space |
|
2043 |
|
# It could be separeted, but I am a little bit tired |
|
2044 |
|
# from so much different spaces over there |
|
2045 |
|
bx.Tri = sx.JustCubatureTriangulation(bx.samplebox, bx.scheme,\ |
|
2046 |
|
tri_space=bx.tri_space, issi=None, \ |
|
2047 |
|
weighting_space=bx.tri_space, incremental=True,\ |
|
2048 |
|
on_add_simplex=bx._on_add_simplex,\ |
|
2049 |
|
on_delete_simplex=bx._invalidate_simplex) |
|
2050 |
|
|
|
2051 |
|
bx.Tri.integrate() # nic nevrácí, všecko je přes kolbeky |
|
2052 |
|
#č tri - Deloneho triangulace |
|
2053 |
|
bx.tri = bx.Tri.tri #č všichní tam očekávajou QHull |
|
2054 |
|
bx._logger(msg="triangulation has been created") |
|
2055 |
|
|
|
2056 |
|
except BaseException as e: |
|
2057 |
|
#č chcu zachytit spadnuti QHull na začatku, |
|
2058 |
|
#č kdy ještě není dostatek teček. |
|
2059 |
|
#č Jinak je třeba nechat QHull spadnout |
|
2060 |
|
if bx.nsim > 2*bx.nvar + 3: |
|
2061 |
|
#č no to teda ne! |
|
2062 |
|
raise |
|
2063 |
|
else: |
|
2064 |
|
#č lze přípustit chybu triangulace |
|
2065 |
|
bx._logger(msg='triangulation failed') |
|
2066 |
|
|
|
2067 |
|
|
|
2068 |
|
|
|
2069 |
|
#č beží 99% času |
|
2070 |
|
def increment(bx, input_sample): |
|
2071 |
|
#ё ну нахрен это ваше наследование-расследование |
|
2072 |
|
|
|
2073 |
|
#č nechť bude, asi nikomu nevadí |
|
2074 |
|
bx._LHS_increment(input_sample) |
|
2075 |
|
|
|
2076 |
|
#č strom posuneme sem |
|
2077 |
|
# cKDTree is used for potential calculation |
|
2078 |
|
# we need everytime regenerate it |
|
2079 |
|
sampled_plan_tree = getattr(bx.sample_box, bx.tree_space) |
|
2080 |
|
bx.tree = spatial.cKDTree(sampled_plan_tree) |
|
2081 |
|
bx.highest_bid = 0 |
|
2082 |
|
|
|
2083 |
|
|
|
2084 |
|
#č logika se mění. Konvexní obálku máme vždycky. |
|
2085 |
|
#č jistě, že po incrementu máme alespoň jeden vzorek |
|
2086 |
|
#č takže hned od začátku můžeme trhat odhady |
|
2087 |
|
|
|
2088 |
|
#č tri - Deloneho triangulace |
|
2089 |
|
if "tri" in dir(bx): |
|
2090 |
|
bx._handle_changed_triangulation(input_sample) |
|
2091 |
|
else: |
|
2092 |
|
bx._regen_inside() |
|
2093 |
|
|
|
2094 |
|
|
|
2095 |
|
bx._handle_changed_outside(input_sample) |
|
2096 |
|
bx._handle_candidates() |
|
2097 |
|
|
|
2098 |
|
#č exportovať odhady jistě môžeme |
|
2099 |
|
#č teďkom to děláme hned po přídání vzorků |
|
2100 |
|
bx.export_estimation() |
|
2101 |
|
|
|
2102 |
|
|
|
2103 |
|
#č tato funkce běží 91% času |
|
2104 |
|
# bottleneck function |
|
2105 |
|
def _handle_changed_triangulation(bx, input_sample): |
|
2106 |
|
""" |
|
2107 |
|
Triangulace zajistěně existuje |
|
2108 |
|
""" |
|
2109 |
|
bx.Tri.update() |
|
2110 |
|
|
|
2111 |
|
|
|
2112 |
|
|
|
2113 |
|
|
|
2114 |
|
def _handle_changed_outside(bx, input_sample): |
|
2115 |
|
try: |
|
2116 |
|
#č kontrola korrektní i v případě NaN |
|
2117 |
|
test = input_sample.event_id > -1 |
|
2118 |
|
#оӵ эскером |
|
2119 |
|
if not test.all(): |
|
2120 |
|
bx.estimate_outside() |
|
2121 |
|
except BaseException as e: |
|
2122 |
|
msg = "input sample didn't provide correct 'event_id' attribute " |
|
2123 |
|
error_msg = bx.__class__.__name__ + ": " + msg + repr(e) |
|
2124 |
|
bx._logger(msg=error_msg) |
|
2125 |
|
bx.estimate_outside() |
|
2126 |
|
|
|
2127 |
|
|
|
2128 |
|
|
|
2129 |
|
|
|
2130 |
|
def _handle_candidates(bx): |
|
2131 |
|
#č A ještě... AUKCE, AUCTION |
|
2132 |
|
# Election - selection |
|
2133 |
|
for candidates in bx.candidates_index.values(): |
|
2134 |
|
bids = getattr(candidates, bx.potential) |
|
2135 |
|
if len(bids): |
|
2136 |
|
bid = np.nanmax(bids) |
|
2137 |
|
# side effect |
|
2138 |
|
if bid > bx.highest_bid: |
|
2139 |
|
#č pokud neprovadíme optimalizaci v simplexech |
|
2140 |
|
#č tak nám stačí jednoduše assessovat |
|
2141 |
|
bx.assess_candidates(candidates) |
|
2142 |
|
|
|
2143 |
|
# probably, we shouldn't purge user candidates (if they are) |
|
2144 |
|
# just every time evaluate them |
|
2145 |
|
#č kdyby někdo chtěl mít užovatelské kandidaty... |
|
2146 |
|
# if len(bx.candidates) > 0: |
|
2147 |
|
# bx.judge_candidates(bx.candidates) |
|
2148 |
|
# bx.assess_candidates(bx.candidates) |
|
2149 |
|
|
|
2150 |
|
|
|
2151 |
|
def assess_candidates(bx, candidates): |
|
2152 |
|
#č nikdo to nepouživá, ale se mi nějaký takový parameter libí. |
|
2153 |
|
candidates.nsim_stamp = np.full(len(candidates), bx.nsim) |
|
2154 |
|
|
|
2155 |
|
candidates_tree = getattr(candidates, bx.tree_space) |
|
2156 |
|
dd, ii = bx.tree.query(candidates_tree, k=1, p=bx.p_norm) |
|
2157 |
|
|
|
2158 |
|
# from scipy documentation: |
|
2159 |
|
# [dd] Missing neighbors are indicated with infinite distances. |
|
2160 |
|
# [ii] Missing neighbors are indicated with self.n |
|
2161 |
|
|
|
2162 |
|
#č mǐ radši budeme předpokladat nulovou vzdálenost a třeba nulového souseda |
|
2163 |
|
#č ne, radší posledného souseda |
|
2164 |
|
#č pokud ten chytrej strom si myslí, že nějaký kandidat nemá spolubydlu |
|
2165 |
|
mask = ii == bx.nsim |
|
2166 |
|
if np.any(mask): #č ať mě to neznervozňuje |
|
2167 |
|
ii[mask] = bx.nsim - 1 |
|
2168 |
|
dd[mask] = 0 |
|
2169 |
|
bx._logger(msg="cKDTree zlobí", orphan_candidates=candidates[mask], P=candidates[mask].P) |
|
2170 |
|
|
|
2171 |
|
# the most agressive potential ever |
|
2172 |
|
candidates.dd = dd |
|
2173 |
|
|
|
2174 |
|
if bx.potential in ('psee', 'fee', 'fee2'): |
|
2175 |
|
#оӵ кучапи |
|
2176 |
|
#č pejskovej potenciál |
|
2177 |
|
#č psí-kučapí není invariántní vůči lineárním transformácím |
|
2178 |
|
|
|
2179 |
|
PDFs = bx.sample_box.pdf(bx.tree_space) |
|
2180 |
|
# teď máme hustoty kandidatů a prislušejicích jím vzorků |
|
2181 |
|
PDF = PDFs[ii] |
|
2182 |
|
pdf = candidates.pdf(bx.tree_space) |
|
2183 |
|
|
|
2184 |
|
tree_Pdf_mean = (pdf+PDF)/2 |
|
2185 |
|
tree_Pdf_gmean = np.sqrt(pdf*PDF) |
|
2186 |
|
volume = np.power(dd, bx.nvar) |
|
2187 |
|
candidates.psee = tree_Pdf_gmean * volume |
|
2188 |
|
candidates.fee = tree_Pdf_mean * volume * np.power(pdf/PDF, 1/(bx.nvar+1)) |
|
2189 |
|
candidates.fee2 = tree_Pdf_mean * volume * np.power(pdf/PDF, 1/(bx.nvar*2)) |
|
2190 |
|
|
|
2191 |
|
elif bx.potential == 'ksee': # ksee |
|
2192 |
|
#оӵ кечато |
|
2193 |
|
#č koťatko-káčátkovej potenciál |
|
2194 |
|
#č ksí-kěčató není invariántní vůčí rotacím |
|
2195 |
|
ksee = np.empty(len(candidates)) |
|
2196 |
|
for i in np.unique(ii): |
|
2197 |
|
# doufám, že je to legální |
|
2198 |
|
ksee[i==ii] = lk.kechato_potential(bx.f_model[i], candidates[i==ii], kechato_space=bx.kechato_space) |
|
2199 |
|
candidates.ksee = ksee |
|
2200 |
|
|
|
2201 |
|
elif bx.potential == 'q_psee': #č kup si |
|
2202 |
|
#оӵ кучапи |
|
2203 |
|
#č pejskovej potenciál |
|
2204 |
|
#č psí-kučapí není invariántní vůči lineárním transformácím |
|
2205 |
|
|
|
2206 |
|
PDFs = bx.sample_box.pdf(bx.tree_space) |
|
2207 |
|
# teď máme hustoty kandidatů a prislušejicích jím vzorků |
|
2208 |
|
PDF = PDFs[ii] |
|
2209 |
|
pdf = candidates.pdf(bx.tree_space) |
|
2210 |
|
|
|
2211 |
|
volume = np.power(dd, bx.nvar) |
|
2212 |
|
candidates.psee = np.sqrt(pdf*PDF) * volume |
|
2213 |
|
candidates.q_psee = pdf**bx.q * PDF**(1-bx.q) * volume |
|
2214 |
|
|
|
2215 |
|
|
|
2216 |
|
|
|
2217 |
|
# prepare to elections |
|
2218 |
|
bx._nominate(candidates) |
|
2219 |
|
|
|
2220 |
|
|
|
2221 |
|
def _ghull_outside_callback(bx, outside_nodes): |
|
2222 |
|
#č sice získáme filtrovaný outside, |
|
2223 |
|
#č musíme sami zabalit bodíky do CandyBoxu |
|
2224 |
|
# -2 = 'inside' -1 = 'outside' |
|
2225 |
|
event_ids = np.full(len(outside_nodes), -1, dtype=np.int8) |
|
2226 |
|
candidates = CandyBox(outside_nodes, event_id=event_ids) |
|
2227 |
|
bx.assess_candidates(candidates) |
|
2228 |
|
|
|
2229 |
|
bids = getattr(candidates, bx.potential) |
|
2230 |
|
#č nie třeba kontrolovat jevy, tam je pouze outside |
|
2231 |
|
#bids *= (candidates.event_id == -1) + (candidates.event_id == 2) |
|
2232 |
|
bid = np.nanmax(bids) |
|
2233 |
|
if bid > bx._highest_outside: |
|
2234 |
|
#č uložíme varku bodíku pouze když |
|
2235 |
|
#č majú větší potenciál, |
|
2236 |
|
bx._highest_outside = bid |
|
2237 |
|
#č čo tam připadně bylo - přepíšeme |
|
2238 |
|
#č uložíme s indexem dle ghull_estimation: |
|
2239 |
|
# -22: inner, -21: shell inside, -12: shell outside, -11: outer |
|
2240 |
|
bx.candidates_index[-12] = candidates |
|
2241 |
|
|
|
2242 |
|
|
|
2243 |
|
def estimate_outside(bx): |
|
2244 |
|
#č konečně mám pořádnou stejtful třídu |
|
2245 |
|
#č pokud mám aspoň jednu tečku, tak už je mi šuma |
|
2246 |
|
#č zda se konvexní obálka vytvořila, či nikoliv |
|
2247 |
|
|
|
2248 |
|
#č Máme 2 úkoly: |
|
2249 |
|
#č 1. Získat odhady a uložit je, abychom nemuseli opakovaně integrovat, |
|
2250 |
|
#č dokud se neobjeví nějaký nový vzorek zvenku. |
|
2251 |
|
#č 2. Získat kandidaty. |
|
2252 |
|
#č a. z mezíkruží (-12) |
|
2253 |
|
#č b. fire, to co navrhne QHull (-1) |
|
2254 |
|
#č c. boom, doporuření QHull můžou i zklamat (-11) |
|
2255 |
|
#č cc. ze vdálenejších galaxí (-111) |
|
2256 |
|
|
|
2257 |
|
#č prace s tečkami v mezikruži se změnila |
|
2258 |
|
#č teď tečky dostávám přes kolbek po částech |
|
2259 |
|
#č a není předem známo, kolik těch částí bude. |
|
2260 |
|
#č Na začátku radší, pro jistotu, |
|
2261 |
|
#č vyhodíme stare bodíky z mezikruži (-12) |
|
2262 |
|
bx.candidates_index.pop(-12, "Nejsou? Nevadí...") # "Ӧвӧл-а мар-а?" |
|
2263 |
|
bx._highest_outside = 0 |
|
2264 |
|
|
|
2265 |
|
# get candidates! |
|
2266 |
|
#č explicitně (pokažde) počtem teček zadavám přesnost integrace |
|
2267 |
|
#č takže změny bx.shell_budget budou při dálším spuštění aplikovány |
|
2268 |
|
data = bx.ghull.integrate(bx.shell_budget, \ |
|
2269 |
|
callback_outside=bx._ghull_outside_callback) |
|
2270 |
|
ghull_estimation, convex_hull_estimation, global_stats = data |
|
2271 |
|
#č uložíme. Не жалко. |
|
2272 |
|
#č první úkol máme splněný |
|
2273 |
|
bx.ghull_estimation = ghull_estimation |
|
2274 |
|
bx.convex_hull_estimation = convex_hull_estimation |
|
2275 |
|
bx.global_stats = global_stats |
|
2276 |
|
bx._logger(msg="outside estimation:", ghull_stats=global_stats) |
|
2277 |
|
|
|
2278 |
|
|
|
2279 |
|
|
|
2280 |
|
#č zde už nám jde pouze o kandidaty |
|
2281 |
|
|
|
2282 |
|
# fire |
|
2283 |
|
bx._fire() |
|
2284 |
|
# boom |
|
2285 |
|
|
|
2286 |
|
if global_stats['R'] > bx._R: |
|
2287 |
|
#č Projedeme Moravou |
|
2288 |
|
nodes = bx.ghull.boom(bx.outer_budget, use_MC=True) |
|
2289 |
|
#č tyhle funkce už vracej pouhý f_model |
|
2290 |
|
event_id = np.full(bx.outer_budget, -1, dtype=np.int8) |
|
2291 |
|
candidates = CandyBox(nodes, event_id=event_id) |
|
2292 |
|
bx.assess_candidates(candidates) # nic nevrácí, to je procedura |
|
2293 |
|
bx.candidates_index[-11] = candidates |
|
2294 |
|
|
|
2295 |
|
#č Už máte Mléčnou dráhu projdutou? |
|
2296 |
|
nodes = bx.ghull.boom(bx.outer_budget, use_MC=False) |
|
2297 |
|
#č tyhle funkce už vracej pouhý f_model |
|
2298 |
|
event_id = np.full(bx.outer_budget, -1, dtype=np.int8) |
|
2299 |
|
candidates = CandyBox(nodes, event_id=event_id) |
|
2300 |
|
bx.assess_candidates(candidates) # nic nevrácí, to je procedura |
|
2301 |
|
bx.candidates_index[-111] = candidates |
|
2302 |
|
|
|
2303 |
|
bx._R = global_stats['R'] |
|
2304 |
|
bx._logger(msg='boom!', _R=bx._R) |
|
2305 |
|
#č to je vše. Nic nevrácíme |
|
2306 |
|
|
|
2307 |
|
def _fire(bx): |
|
2308 |
|
qhull = bx.ghull.hull |
|
2309 |
|
if bx._afacet is None: |
|
2310 |
|
bx.__fire() |
|
2311 |
|
|
|
2312 |
|
#č podle mě sem nemusí dojít, |
|
2313 |
|
#č dokud se konvexní obálka ve skutku nevytvoří |
|
2314 |
|
#č b-čko u QHull pro nás má jakoby záporné vzdálenosti |
|
2315 |
|
elif np.all(bx._bfacet > qhull.b): |
|
2316 |
|
#č jasně, že musíme zapalit |
|
2317 |
|
bx.__fire() |
|
2318 |
|
elif np.any(bx._afacet != qhull.A[np.nanargmax(qhull.b)]): |
|
2319 |
|
#č "beta" se nezměnila, ale jen kvůli jinejm návrhovým bodům |
|
2320 |
|
bx.__fire() |
|
2321 |
|
|
|
2322 |
|
def __fire(bx): |
|
2323 |
|
qhull = bx.ghull.hull |
|
2324 |
|
nodes = qhull.fire(bx.outer_budget, use_MC=True) |
|
2325 |
|
if nodes is not None: |
|
2326 |
|
#č tyhle funkce už vracej pouhý f_model |
|
2327 |
|
event_id = np.full(bx.outer_budget, -1, dtype=np.int8) |
|
2328 |
|
candidates = CandyBox(nodes, event_id=event_id) |
|
2329 |
|
bx.assess_candidates(candidates) # nic nevrácí, to je procedura |
|
2330 |
|
bx.candidates_index[-1] = candidates |
|
2331 |
|
|
|
2332 |
|
arg = np.nanargmax(qhull.b) |
|
2333 |
|
bx._bfacet = b = qhull.b[arg] |
|
2334 |
|
bx._afacet = a = qhull.A[arg] |
|
2335 |
|
bx._logger(msg='fire!', a=a, b=b) |
|
2336 |
|
|
|
2337 |
|
|
|
2338 |
|
def get_pf_estimation(bx): |
|
2339 |
|
#č dle toho, čo vidím v kódu (spouští nás .increment()) |
|
2340 |
|
#č přinejmenším konvexní obálka musí |
|
2341 |
|
#č zajištěně existovat |
|
2342 |
|
# convex_hull_estimation -2: inside, -1: outside |
|
2343 |
|
pf_inside = bx.convex_hull_estimation[-2] |
|
2344 |
|
pf_outside = bx.convex_hull_estimation[-1] |
|
2345 |
|
|
|
2346 |
|
#č Ghull spouštíme sporadicky, |
|
2347 |
|
#č takže musíme sami lepit nové etikety |
|
2348 |
|
bx.global_stats['nsim'] = bx.nsim |
|
2349 |
|
|
|
2350 |
|
if 'tri' in dir(bx): |
|
2351 |
|
#č Tri.get_pf_estimation() vrací: |
|
2352 |
|
# 'TRI_estimation': tri_estimation, 'global_stats': {mix, failure}, \ |
|
2353 |
|
#'vertex_estimation' : vertex_estimation, \ |
|
2354 |
|
#'weighted_vertex_estimation' : weighted_vertex_estimation, |
|
2355 |
|
#'coplanar':sx.tri.coplanar} |
|
2356 |
|
estimations = bx.Tri.get_pf_estimation() |
|
2357 |
|
# TRI-compatible estimation |
|
2358 |
|
# -1=outside, 0=success, 1=failure, 2=mix |
|
2359 |
|
#č to je JustTriangulation, |
|
2360 |
|
#č outside (-1), ani success (1) nebudou korektní |
|
2361 |
|
tri_estimation = estimations.pop('TRI_estimation') |
|
2362 |
|
tri_estimation[-1] = pf_outside |
|
2363 |
|
tri_estimation[0] = pf_inside - tri_estimation[1] - tri_estimation[2] |
|
2364 |
|
estimations['TRI_overall_estimations'] = tri_estimation |
|
2365 |
|
estimations['ghull_estimation'] = bx.ghull_estimation |
|
2366 |
|
|
|
2367 |
|
#č hrozně důležitý. Těšíme se na csv-čko. |
|
2368 |
|
bx.global_stats = {**bx.global_stats, **estimations['global_stats']} |
|
2369 |
|
bx.global_stats['success'] = tri_estimation[0] |
|
2370 |
|
|
|
2371 |
|
return estimations |
|
2372 |
|
|
|
2373 |
|
|
|
2374 |
|
#оӵ триангуляци ӧвӧл, иськем... |
|
2375 |
|
|
|
2376 |
|
#č může se stát, že první dvě tečky už hned májí různé barvy, |
|
2377 |
|
#č ale žádnej simplex ještě nemáme. |
|
2378 |
|
#č takže celou skříňku prostě bereme jako simplex |
|
2379 |
|
event, event_id, fr, wfr = sx.get_simplex_event(bx, weighting_space=bx.tri_space) |
|
2380 |
|
# -1=outside, 0=success, 1=failure, 2=mix |
|
2381 |
|
tri_estimation = {-1:pf_outside, 0:0, 1:0, 2:0} |
|
2382 |
|
tri_estimation[event_id] = pf_inside |
|
2383 |
|
|
|
2384 |
|
vertex_estimation = pf_inside * fr |
|
2385 |
|
weighted_vertex_estimation = pf_inside * wfr |
|
2386 |
|
|
|
2387 |
|
global_stats = bx.global_stats |
|
2388 |
|
# outside dodá Ghull |
|
2389 |
|
global_stats['success'] = tri_estimation[0] |
|
2390 |
|
global_stats['failure'] = tri_estimation[1] |
|
2391 |
|
global_stats['mix'] = tri_estimation[2] |
|
2392 |
|
global_stats['vertex_estimation'] = vertex_estimation |
|
2393 |
|
global_stats['weighted_vertex_estimation'] = weighted_vertex_estimation |
|
2394 |
|
global_stats['nsimplex'] = 0 |
|
2395 |
|
global_stats['ncoplanar'] = 0 |
|
2396 |
|
|
|
2397 |
|
return {'TRI_overall_estimations': tri_estimation, \ |
|
2398 |
|
'vertex_estimation' : vertex_estimation, \ |
|
2399 |
|
'weighted_vertex_estimation' : weighted_vertex_estimation, \ |
|
2400 |
|
'ghull_estimation' : bx.ghull_estimation} |
|
2401 |
|
|
|
2402 |
|
|
|
2403 |
|
def export_estimation(bx): |
|
2404 |
|
#č teď raději estimátory ukladáme hned |
|
2405 |
|
for key, value in bx.get_pf_estimation().items(): |
|
2406 |
|
bx.guessbox.guess(key, bx.nsim, value) |
|
2407 |
|
|
|
2408 |
|
# prepare export to csv |
|
2409 |
|
# All I Can Give You (Ashley Wallbridge Remix) |
|
2410 |
|
# but not sure about proxy |
|
2411 |
|
#č Ghull nabízí slušný stats |
|
2412 |
|
|
|
2413 |
|
#č Teď je to hrozně křehký, musí být zajištěno |
|
2414 |
|
#č stejný počet a stejné pořádí estimátorů. |
|
2415 |
|
#č Musí být způštěn bx.get_pf_estimation() |
|
2416 |
|
#č který dopočíta zbývající estimátory z triangulaci. |
|
2417 |
|
#č bx.get_pf_estimation() spolehá na určíté pořádí |
|
2418 |
|
#č global_stats z Triangulation třídy, čímž jsme |
|
2419 |
|
#č porušujeme zásady SOLID |
|
2420 |
|
#č Ale zatím budíž. Až se to rozbíje, tak možná |
|
2421 |
|
#č necham třídu samostatně inicializovyvat svůj |
|
2422 |
|
#č vlastní slovník s pevným počtem a pevným pořadím složek. |
|
2423 |
|
if bx.stm_filename is not None: |
|
2424 |
|
reader.export_estimation(bx.stm_filename, bx.global_stats) |
|
2425 |
|
|
|
2426 |
|
|
|
2427 |
|
##č nie treba počítat odhady v každem kroku |
|
2428 |
|
##č ale zas, taky že chceme do článků davat nějaké diagramy |
|
2429 |
|
# |
|
2430 |
|
##č |
|
2431 |
|
##č po 2. kroku ghull.fire() nastaví vnější poloměr R a dovnitř už ani nesahne. |
|
2432 |
|
##č Je to v porádku, pokud rozhodování je v G prostoru, |
|
2433 |
|
##č protože v druhem kroku algoritmus nějak zvolil ten nejoptimálnější poloměr |
|
2434 |
|
##č a není v dalším kroku skutečně důvod pod ten poloměr jít. |
|
2435 |
|
##č Pokud rozhodujeme v R prostoru s nějakým divokým rozdělením - už je to trošku problem. |
|
2436 |
|
# |
|
2437 |
|
##č Jak poznam, že není dostatek teček, nebo je nějaký problem? Jakou chybu hodí Ghull? |
|
2438 |
|
|
|
2439 |
|
|
|
2440 |
1423 |
#♥♥♥♥♥♥ |
#♥♥♥♥♥♥ |
2441 |
1424 |
# DiceBoxiCheck |
# DiceBoxiCheck |
2442 |
1425 |
class KechatoLukiskon(DiceBox): |
class KechatoLukiskon(DiceBox): |