File wellmet/dicebox/circumtri.py changed (mode: 100644) (index d69f288..c733b99) |
... |
... |
TriEstimation = namedtuple('TriEstimation', ( |
28 |
28 |
*shell.ShellEstimation._fields, |
*shell.ShellEstimation._fields, |
29 |
29 |
'success_points', |
'success_points', |
30 |
30 |
'failure_points', |
'failure_points', |
31 |
|
'success', |
|
32 |
|
*sx.FastCubatureEstimation._fields[2:], |
|
|
31 |
|
*sx.CubatureEstimation._fields[2:], |
|
32 |
|
'p_sum', |
33 |
33 |
'max_potential' |
'max_potential' |
34 |
34 |
)) |
)) |
35 |
35 |
|
|
36 |
36 |
|
|
37 |
|
class CircumTri(_Exploration): |
|
|
37 |
|
class FullCircumTri(_Exploration): |
38 |
38 |
|
|
39 |
39 |
#č míží nám sampling_space: Ghull umí vzorkovat outside pouze v G prostoru |
#č míží nám sampling_space: Ghull umí vzorkovat outside pouze v G prostoru |
40 |
40 |
#č quadpy umístí integráční bodíky v prostoru triangulace. |
#č quadpy umístí integráční bodíky v prostoru triangulace. |
41 |
41 |
def __init__(bx, sample_box, scheme, q=10, psi_q=0.5, circumcenters_only=True, |
def __init__(bx, sample_box, scheme, q=10, psi_q=0.5, circumcenters_only=True, |
42 |
|
store_candidates_metainformation=False, shell_budget=1000): |
|
|
42 |
|
holydays=10, store_candidates_metainformation=False, shell_budget=1000): |
43 |
43 |
|
|
44 |
44 |
bx.sample_box = sample_box |
bx.sample_box = sample_box |
45 |
45 |
bx.scheme = scheme |
bx.scheme = scheme |
|
... |
... |
class CircumTri(_Exploration): |
54 |
54 |
bx.circumcenters_only = circumcenters_only |
bx.circumcenters_only = circumcenters_only |
55 |
55 |
bx.q = q |
bx.q = q |
56 |
56 |
bx.psi_q = psi_q |
bx.psi_q = psi_q |
57 |
|
|
|
|
57 |
|
bx.holydays = holydays |
58 |
58 |
|
|
59 |
59 |
bx.direct_plan = quadpy.un.mysovskikh_1(bx.nvar).points |
bx.direct_plan = quadpy.un.mysovskikh_1(bx.nvar).points |
60 |
60 |
bx.sball = sball.Sball(bx.nvar) |
bx.sball = sball.Sball(bx.nvar) |
|
... |
... |
class CircumTri(_Exploration): |
76 |
76 |
# kind of interface to CandidatesWidget |
# kind of interface to CandidatesWidget |
77 |
77 |
bx.candidates_index = {} |
bx.candidates_index = {} |
78 |
78 |
bx.potential_index = ValueSortedDict() |
bx.potential_index = ValueSortedDict() |
|
79 |
|
bx.global_potential_index = ValueSortedDict() |
79 |
80 |
|
|
80 |
81 |
bx.regen() |
bx.regen() |
81 |
82 |
|
|
|
... |
... |
class CircumTri(_Exploration): |
87 |
88 |
return {'sample_box':bx.sample_box, 'scheme':bx.scheme.name, |
return {'sample_box':bx.sample_box, 'scheme':bx.scheme.name, |
88 |
89 |
'shell_budget':bx.shell_budget, |
'shell_budget':bx.shell_budget, |
89 |
90 |
'circumcenters_only':bx.circumcenters_only, |
'circumcenters_only':bx.circumcenters_only, |
90 |
|
'q':bx.q, 'psi_q':bx.psi_q} |
|
|
91 |
|
'q':bx.q, 'psi_q':bx.psi_q, 'holydays':bx.holydays} |
91 |
92 |
|
|
92 |
93 |
def __repr__(bx): |
def __repr__(bx): |
93 |
94 |
return "%s(**%s)"%(bx.__class__.__name__, repr(bx.init_parameters())) |
return "%s(**%s)"%(bx.__class__.__name__, repr(bx.init_parameters())) |
|
... |
... |
class CircumTri(_Exploration): |
106 |
107 |
return bx.f_model(1) |
return bx.f_model(1) |
107 |
108 |
|
|
108 |
109 |
|
|
|
110 |
|
def holyday(bx): |
|
111 |
|
if len(bx.candidates_index): |
|
112 |
|
# return node with the greatest potential |
|
113 |
|
key, value = bx.global_potential_index.peekitem() |
|
114 |
|
return bx.candidates_index[key] |
|
115 |
|
else: |
|
116 |
|
bx._logger(msg='triangulation does not exist yet. Fallback to random sample') |
|
117 |
|
return bx.f_model(1) |
|
118 |
|
|
109 |
119 |
def regen(bx): |
def regen(bx): |
110 |
120 |
""" |
""" |
111 |
121 |
regen() recreates data structures of the box. |
regen() recreates data structures of the box. |
|
... |
... |
class CircumTri(_Exploration): |
117 |
127 |
|
|
118 |
128 |
bx.candidates_index.clear() |
bx.candidates_index.clear() |
119 |
129 |
bx.potential_index.clear() |
bx.potential_index.clear() |
|
130 |
|
bx.global_potential_index.clear() |
120 |
131 |
|
|
121 |
132 |
bx._regen_outside() |
bx._regen_outside() |
122 |
133 |
bx._regen_inside() |
bx._regen_inside() |
|
... |
... |
class CircumTri(_Exploration): |
183 |
194 |
|
|
184 |
195 |
|
|
185 |
196 |
|
|
|
197 |
|
#č teď je to kolbek, který volá Triangulation |
|
198 |
|
def _on_safe_added(bx, simplex, indices, vertices_model, nodes, vol): |
|
199 |
|
|
|
200 |
|
try: |
|
201 |
|
circum_center = bx.CC.get_circumcenter(vertices_model) |
|
202 |
|
except BaseException as e: |
|
203 |
|
bx._logger(repr(e), msg='CircumCenter error') |
|
204 |
|
return |
|
205 |
|
r = distance.euclidean(circum_center, vertices_model[0]) |
|
206 |
|
circum_node = bx.f_model.new_sample(circum_center, space=bx.tri_space) |
|
207 |
|
#č můžeme nechat numpy pole z jednoho prvku. Můžeme zredukovat na float |
|
208 |
|
#circum_pdf = circum_node.pdf(bx.tri_space) |
|
209 |
|
circum_pdf = float(circum_node.pdf(bx.tri_space)) |
|
210 |
|
|
|
211 |
|
|
|
212 |
|
PDF = bx.Tri.PDF[indices] |
|
213 |
|
max_PDF = np.max(PDF) |
|
214 |
|
|
|
215 |
|
circum_potential = r**bx.nvar * circum_pdf**bx.psi_q * max_PDF**(1-bx.psi_q) |
|
216 |
|
|
|
217 |
|
if not np.isfinite(circum_potential): |
|
218 |
|
return |
|
219 |
|
|
|
220 |
|
result_node = circum_node |
|
221 |
|
result_potential = circum_potential |
|
222 |
|
|
|
223 |
|
if bx.store_candidates_metainformation: |
|
224 |
|
result_node = CandyNodes(result_node) |
|
225 |
|
result_node.potential = result_potential |
|
226 |
|
|
|
227 |
|
bx.candidates_index[simplex] = result_node |
|
228 |
|
bx.global_potential_index[simplex] = result_potential |
|
229 |
|
|
|
230 |
|
|
|
231 |
|
|
186 |
232 |
#č teď je to kolbek, který volá Triangulation |
#č teď je to kolbek, který volá Triangulation |
187 |
233 |
def _on_mixed_added(bx, simplex, indices, vertices_model, nodes, _vol, fr, wfr, _mfpdf): |
def _on_mixed_added(bx, simplex, indices, vertices_model, nodes, _vol, fr, wfr, _mfpdf): |
188 |
234 |
|
|
|
... |
... |
class CircumTri(_Exploration): |
246 |
292 |
|
|
247 |
293 |
bx.candidates_index[simplex] = result_node |
bx.candidates_index[simplex] = result_node |
248 |
294 |
bx.potential_index[simplex] = result_potential |
bx.potential_index[simplex] = result_potential |
|
295 |
|
bx.global_potential_index[simplex] = result_potential |
249 |
296 |
|
|
250 |
297 |
|
|
251 |
298 |
|
|
|
... |
... |
class CircumTri(_Exploration): |
255 |
302 |
#č sx.on_delete_simplex(indices=indices) |
#č sx.on_delete_simplex(indices=indices) |
256 |
303 |
def _invalidate_simplex(bx, simplex): |
def _invalidate_simplex(bx, simplex): |
257 |
304 |
bx.potential_index.pop(simplex, None) |
bx.potential_index.pop(simplex, None) |
|
305 |
|
bx.global_potential_index.pop(simplex, None) |
258 |
306 |
bx.candidates_index.pop(simplex, None) |
bx.candidates_index.pop(simplex, None) |
259 |
307 |
|
|
260 |
308 |
|
|
|
... |
... |
class CircumTri(_Exploration): |
285 |
333 |
|
|
286 |
334 |
def __regen_inside(bx): |
def __regen_inside(bx): |
287 |
335 |
try: |
try: |
288 |
|
bx.Tri = sx.CubatureIntegration(bx.samplebox, bx.scheme,\ |
|
|
336 |
|
if bx.holydays: |
|
337 |
|
Integrator = sx.FullCubatureIntegration |
|
338 |
|
else: |
|
339 |
|
Integrator = sx.FastCubatureIntegration |
|
340 |
|
|
|
341 |
|
bx.Tri = Integrator(bx.samplebox, bx.scheme,\ |
289 |
342 |
tri_space=bx.tri_space, incremental=True,\ |
tri_space=bx.tri_space, incremental=True,\ |
|
343 |
|
on_safe_added=bx._on_safe_added,\ |
290 |
344 |
on_mixed_added=bx._on_mixed_added,\ |
on_mixed_added=bx._on_mixed_added,\ |
291 |
345 |
on_delete_simplex=bx._invalidate_simplex) |
on_delete_simplex=bx._invalidate_simplex) |
292 |
346 |
|
|
|
... |
... |
class CircumTri(_Exploration): |
351 |
405 |
try: |
try: |
352 |
406 |
mask = bx.is_mixed(bx.convex_hull.simplices[i]) |
mask = bx.is_mixed(bx.convex_hull.simplices[i]) |
353 |
407 |
if not np.any(mask): |
if not np.any(mask): |
354 |
|
return |
|
|
408 |
|
return |
355 |
409 |
outside_nodes = outside_nodes[mask] |
outside_nodes = outside_nodes[mask] |
356 |
410 |
d = d[mask] |
d = d[mask] |
357 |
411 |
|
|
|
... |
... |
class CircumTri(_Exploration): |
381 |
435 |
bx.candidates_index[-1] = outside_node |
bx.candidates_index[-1] = outside_node |
382 |
436 |
bx.potential_index.pop(-1) |
bx.potential_index.pop(-1) |
383 |
437 |
bx.potential_index[-1] = max_node_potential |
bx.potential_index[-1] = max_node_potential |
|
438 |
|
bx.global_potential_index.pop(-1) |
|
439 |
|
bx.global_potential_index[-1] = max_node_potential |
384 |
440 |
|
|
385 |
441 |
|
|
386 |
442 |
|
|
|
... |
... |
class CircumTri(_Exploration): |
401 |
457 |
bx.candidates_index.pop(-1, None) |
bx.candidates_index.pop(-1, None) |
402 |
458 |
#č těch kastomných slovníků se bojím... |
#č těch kastomných slovníků se bojím... |
403 |
459 |
bx.potential_index.pop(-1, None) |
bx.potential_index.pop(-1, None) |
|
460 |
|
bx.global_potential_index.pop(-1, None) |
404 |
461 |
#bx.rating_index.pop(-1, None) |
#bx.rating_index.pop(-1, None) |
405 |
462 |
bx.potential_index[-1] = 0 #č pro callback |
bx.potential_index[-1] = 0 #č pro callback |
|
463 |
|
bx.global_potential_index[-1] = 0 #č pro callback |
406 |
464 |
|
|
407 |
465 |
bx.shell_stats = bx.ghull.get_shell_estimation() |
bx.shell_stats = bx.ghull.get_shell_estimation() |
408 |
466 |
|
|
|
... |
... |
class CircumTri(_Exploration): |
416 |
474 |
|
|
417 |
475 |
|
|
418 |
476 |
|
|
|
477 |
|
def get_pf_estimation(bx): |
|
478 |
|
failsi = bx.failsi |
|
479 |
|
|
|
480 |
|
success_points = len(failsi[~failsi]) |
|
481 |
|
failure_points = len(failsi[failsi]) |
|
482 |
|
|
|
483 |
|
if len(bx.candidates_index): |
|
484 |
|
__key, max_potential = bx.potential_index.peekitem() |
|
485 |
|
else: |
|
486 |
|
max_potential = -1 |
|
487 |
|
|
|
488 |
|
if 'tri' in dir(bx): |
|
489 |
|
tri_estimation = bx.Tri.get_pf_estimation() |
|
490 |
|
|
|
491 |
|
bx.p_mixed = tri_estimation.mix |
|
492 |
|
outside = bx.shell_estimation.outside |
|
493 |
|
p_sum = outside + tri_estimation.failure + bx.p_mixed + tri_estimation.success |
|
494 |
|
|
|
495 |
|
return TriEstimation(bx.nsim, #č musíme sami lepit nové etikety, neboť |
|
496 |
|
*bx.shell_stats[1:], #č Ghull spouštíme sporadicky |
|
497 |
|
*bx.shell_estimation, |
|
498 |
|
success_points, |
|
499 |
|
failure_points, |
|
500 |
|
*tri_estimation[2:], |
|
501 |
|
p_sum, |
|
502 |
|
max_potential |
|
503 |
|
) |
|
504 |
|
|
|
505 |
|
|
|
506 |
|
#оӵ триангуляци ӧвӧл, иськем... |
|
507 |
|
inside = bx.shell_estimation.inside |
|
508 |
|
|
|
509 |
|
#č může se stát, že první dvě tečky už hned májí různé barvy, |
|
510 |
|
#č ale žádnej simplex ještě nemáme. |
|
511 |
|
#č takže celou skříňku prostě bereme jako simplex |
|
512 |
|
event, event_id, fr, wfr = sx.get_simplex_event(bx, weighting_space=bx.tri_space) |
|
513 |
|
# -1=outside, 0=success, 1=failure, 2=mix |
|
514 |
|
tri_estimation = {0:0, 1:0, 2:0} |
|
515 |
|
tri_estimation[event_id] = inside |
|
516 |
|
|
|
517 |
|
success = tri_estimation[0] |
|
518 |
|
failure = tri_estimation[1] |
|
519 |
|
bx.p_mixed = mixed = tri_estimation[2] |
|
520 |
|
|
|
521 |
|
return TriEstimation(bx.nsim, #č musíme sami lepit nové etikety, neboť |
|
522 |
|
*bx.shell_stats[1:], #č Ghull spouštíme sporadicky |
|
523 |
|
*bx.shell_estimation, |
|
524 |
|
success_points, |
|
525 |
|
failure_points, |
|
526 |
|
|
|
527 |
|
success, |
|
528 |
|
failure, |
|
529 |
|
mixed, |
|
530 |
|
inside * fr, |
|
531 |
|
inside * wfr, |
|
532 |
|
inside * wfr, |
|
533 |
|
0, # nsimplex |
|
534 |
|
#sx.tn_scheme.name, |
|
535 |
|
bx.scheme.points.shape[1], |
|
536 |
|
0, #sx.newly_invalidated, |
|
537 |
|
0, #sx.newly_estimated, |
|
538 |
|
0, #len(sx.failure_simplices), |
|
539 |
|
0, #len(sx.mixed_simplices), |
|
540 |
|
0, #len(sx.tri.coplanar), |
|
541 |
|
|
|
542 |
|
1, |
|
543 |
|
max_potential |
|
544 |
|
) |
|
545 |
|
|
|
546 |
|
|
|
547 |
|
|
|
548 |
|
class FastCircumTri(FullCircumTri): |
|
549 |
|
|
419 |
550 |
def get_pf_estimation(bx): |
def get_pf_estimation(bx): |
420 |
551 |
failsi = bx.failsi |
failsi = bx.failsi |
421 |
552 |
|
|
|
... |
... |
class CircumTri(_Exploration): |
442 |
573 |
success_points, |
success_points, |
443 |
574 |
failure_points, |
failure_points, |
444 |
575 |
success, |
success, |
445 |
|
*tri_estimation[2:], |
|
|
576 |
|
*tri_estimation[3:], |
|
577 |
|
1, |
446 |
578 |
max_potential |
max_potential |
447 |
579 |
) |
) |
448 |
580 |
|
|
|
... |
... |
class CircumTri(_Exploration): |
466 |
598 |
*bx.shell_estimation, |
*bx.shell_estimation, |
467 |
599 |
success_points, |
success_points, |
468 |
600 |
failure_points, |
failure_points, |
469 |
|
success, |
|
470 |
601 |
|
|
|
602 |
|
success, |
471 |
603 |
failure, |
failure, |
472 |
604 |
mixed, |
mixed, |
473 |
605 |
inside * fr, |
inside * fr, |
|
... |
... |
class CircumTri(_Exploration): |
482 |
614 |
0, #len(sx.mixed_simplices), |
0, #len(sx.mixed_simplices), |
483 |
615 |
0, #len(sx.tri.coplanar), |
0, #len(sx.tri.coplanar), |
484 |
616 |
|
|
|
617 |
|
1, |
485 |
618 |
max_potential |
max_potential |
486 |
619 |
) |
) |
487 |
620 |
|
|
File wellmet/qt_gui/qt_dicebox.py changed (mode: 100644) (index 496e068..f8e3f97) |
... |
... |
import numpy as np |
3 |
3 |
from .. import schemes |
from .. import schemes |
4 |
4 |
from ..candybox import CandyBox |
from ..candybox import CandyBox |
5 |
5 |
from ..dicebox.goal import Goal, Razitko, DiceBox |
from ..dicebox.goal import Goal, Razitko, DiceBox |
6 |
|
from ..dicebox.circumtri import CircumTri |
|
|
6 |
|
from ..dicebox.circumtri import FullCircumTri, FastCircumTri |
7 |
7 |
from ..dicebox._exploration import DumbExploration |
from ..dicebox._exploration import DumbExploration |
8 |
8 |
import pyqtgraph as pg |
import pyqtgraph as pg |
9 |
9 |
from pyqtgraph.Qt import QtCore, QtWidgets |
from pyqtgraph.Qt import QtCore, QtWidgets |
|
... |
... |
class CircumTriWidget(pg.parametertree.ParameterTree): |
96 |
96 |
q = np.log(self.box.nvar) / self.box.nvar**2 |
q = np.log(self.box.nvar) / self.box.nvar**2 |
97 |
97 |
params.append({'name': 'psi_q', 'type': 'float', \ |
params.append({'name': 'psi_q', 'type': 'float', \ |
98 |
98 |
'title': "psi_q", \ |
'title': "psi_q", \ |
99 |
|
'limits': (0, 1), 'value': q, 'default': q,\ |
|
|
99 |
|
'limits': (0, 1), 'value': 0.5, 'default': q,\ |
100 |
100 |
'tip': "Used for psi potential calculation"}) |
'tip': "Used for psi potential calculation"}) |
101 |
101 |
|
|
102 |
102 |
params.append({'name': 'shell_budget', 'type': 'int', \ |
params.append({'name': 'shell_budget', 'type': 'int', \ |
|
... |
... |
class CircumTriWidget(pg.parametertree.ParameterTree): |
104 |
104 |
'limits': (1, float('inf')), 'value': 1000, 'default': 1000,\ |
'limits': (1, float('inf')), 'value': 1000, 'default': 1000,\ |
105 |
105 |
'tip': "Number of annulus candidates"}) |
'tip': "Number of annulus candidates"}) |
106 |
106 |
|
|
|
107 |
|
params.append({'name': 'holydays', 'type': 'int', \ |
|
108 |
|
'title': "holydays", \ |
|
109 |
|
'limits': (0, float('inf')), 'value': 10, 'default': 10,\ |
|
110 |
|
'tip': "Every n-th point is sacrificed to the greatest safe simplex. 0 to switch off"}) |
|
111 |
|
|
107 |
112 |
|
|
108 |
113 |
### Create tree of Parameter objects |
### Create tree of Parameter objects |
109 |
114 |
# I don't know why that signals do not work for me |
# I don't know why that signals do not work for me |
|
... |
... |
class CircumTriWidget(pg.parametertree.ParameterTree): |
141 |
146 |
|
|
142 |
147 |
scheme = schemes.get_tn_scheme(self.scheme, self.box.nvar, self.degree) |
scheme = schemes.get_tn_scheme(self.scheme, self.box.nvar, self.degree) |
143 |
148 |
|
|
144 |
|
self.box.sample_box = CircumTri( |
|
|
149 |
|
if self.holydays: |
|
150 |
|
Box = FullCircumTri |
|
151 |
|
else: |
|
152 |
|
Box = FastCircumTri |
|
153 |
|
|
|
154 |
|
self.box.sample_box = Box( |
145 |
155 |
self.box.sample_box, #č rekurze) |
self.box.sample_box, #č rekurze) |
146 |
156 |
scheme, |
scheme, |
147 |
157 |
#potential_mode=self.potential_mode, |
#potential_mode=self.potential_mode, |
|
... |
... |
class CircumTriWidget(pg.parametertree.ParameterTree): |
149 |
159 |
q=self.q, |
q=self.q, |
150 |
160 |
psi_q=self.psi_q, |
psi_q=self.psi_q, |
151 |
161 |
shell_budget=self.shell_budget, |
shell_budget=self.shell_budget, |
152 |
|
circumcenters_only=self.circumcenters_only |
|
153 |
|
#weighted_entropy=self.weighted_entropy |
|
|
162 |
|
circumcenters_only=self.circumcenters_only, |
|
163 |
|
holydays=self.holydays |
154 |
164 |
) |
) |
155 |
165 |
|
|
156 |
166 |
|
|