File wellmet/convex_hull.py changed (mode: 100644) (index d12f786..6c21f9a) |
... |
... |
class BrickHull: #č nebo BoundingBrick |
424 |
424 |
def get_exploration_vector(hull): |
def get_exploration_vector(hull): |
425 |
425 |
hull._update() |
hull._update() |
426 |
426 |
to_fire = np.nanargmax(hull.b) |
to_fire = np.nanargmax(hull.b) |
427 |
|
return hull.A[to_fire], hull.b[to_fire] |
|
|
427 |
|
return hull.A[to_fire], hull.b[to_fire], hull.sample |
428 |
428 |
|
|
429 |
429 |
# shortcut for Ghull |
# shortcut for Ghull |
430 |
430 |
# valid only if space==G |
# valid only if space==G |
|
... |
... |
class DirectHull: |
668 |
668 |
def get_exploration_vector(hull): |
def get_exploration_vector(hull): |
669 |
669 |
hull._update() |
hull._update() |
670 |
670 |
to_fire = np.nanargmax(hull.b) |
to_fire = np.nanargmax(hull.b) |
671 |
|
return hull.A[to_fire], hull.b[to_fire] |
|
|
671 |
|
return hull.A[to_fire], hull.b[to_fire], hull.sample |
672 |
672 |
|
|
673 |
673 |
# shortcut for Ghull |
# shortcut for Ghull |
674 |
674 |
# valid only if space==G |
# valid only if space==G |
|
... |
... |
class QHull: |
992 |
992 |
def get_exploration_vector(hull): |
def get_exploration_vector(hull): |
993 |
993 |
hull._update() |
hull._update() |
994 |
994 |
if hull.enough_points: |
if hull.enough_points: |
995 |
|
to_fire = np.nanargmax(hull.b) |
|
996 |
|
return hull.A[to_fire], hull.b[to_fire] |
|
|
995 |
|
to_sample = np.nanargmax(hull.b) |
|
996 |
|
sample = hull.sample[hull.simplices[to_sample]] |
|
997 |
|
return hull.A[to_sample], hull.b[to_sample], sample |
997 |
998 |
else: |
else: |
998 |
|
return DirectHull(self.sample, self.fallback_plan, self.space).get_exploration_vector() |
|
|
999 |
|
return DirectHull(hull.sample, hull.fallback_plan, hull.space).get_exploration_vector() |
999 |
1000 |
|
|
1000 |
1001 |
|
|
1001 |
1002 |
# shortcut for Ghull |
# shortcut for Ghull |
|
... |
... |
class Grick: |
1429 |
1430 |
def get_exploration_vector(hull): |
def get_exploration_vector(hull): |
1430 |
1431 |
hull._update() |
hull._update() |
1431 |
1432 |
if hull.bm[0] > (-hull.bp[0]): |
if hull.bm[0] > (-hull.bp[0]): |
1432 |
|
return -hull.orth_basis[0], hull.bm[0] |
|
|
1433 |
|
return -hull.orth_basis[0], hull.bm[0], hull.sample |
1433 |
1434 |
else: |
else: |
1434 |
|
return hull.orth_basis[0], -hull.bp[0] |
|
|
1435 |
|
return hull.orth_basis[0], -hull.bp[0], hull.sample |
1435 |
1436 |
|
|
1436 |
1437 |
|
|
1437 |
1438 |
|
|
File wellmet/dicebox/_exploration.py changed (mode: 100644) (index 41e448f..5c80972) |
... |
... |
import numpy as np |
6 |
6 |
import quadpy |
import quadpy |
7 |
7 |
|
|
8 |
8 |
from ..ghull import Ghull |
from ..ghull import Ghull |
9 |
|
from ..convex_hull import Grick |
|
|
9 |
|
from .. import convex_hull as khull |
10 |
10 |
from .. import simplex as sx |
from .. import simplex as sx |
11 |
11 |
from ..reader import Store |
from ..reader import Store |
12 |
12 |
from .. import sball |
from .. import sball |
13 |
13 |
|
|
|
14 |
|
from scipy import stats |
|
15 |
|
from scipy import spatial |
14 |
16 |
|
|
15 |
17 |
from collections import namedtuple |
from collections import namedtuple |
16 |
18 |
from sortedcollections import ValueSortedDict |
from sortedcollections import ValueSortedDict |
|
... |
... |
from sortedcollections import ValueSortedDict |
24 |
26 |
|
|
25 |
27 |
class _Exploration: |
class _Exploration: |
26 |
28 |
|
|
|
29 |
|
outer_budget = 100 |
|
30 |
|
psi_q = 0.5 |
27 |
31 |
|
|
28 |
32 |
def __call__(bx): |
def __call__(bx): |
29 |
33 |
if bx.nsim < 1: # je to legální |
if bx.nsim < 1: # je to legální |
|
... |
... |
class _Exploration: |
40 |
44 |
|
|
41 |
45 |
|
|
42 |
46 |
def explore(bx): |
def explore(bx): |
|
47 |
|
# empirical rule to get desired behavior |
43 |
48 |
p_desired = np.exp(-np.sqrt(bx.q * bx.nsim)) |
p_desired = np.exp(-np.sqrt(bx.q * bx.nsim)) |
|
49 |
|
|
|
50 |
|
# get matematically clean radius of it |
44 |
51 |
r = bx.sball.get_r(p_desired) |
r = bx.sball.get_r(p_desired) |
45 |
|
if bx.nsim > len(bx.direct_plan): |
|
46 |
|
a, b = bx.convex_hull.get_exploration_vector() |
|
47 |
|
R = -b |
|
48 |
|
else: |
|
49 |
|
a = bx.direct_plan[bx.nsim-1] |
|
50 |
|
R = bx.ghull.get_R() |
|
|
52 |
|
|
|
53 |
|
#č konkretně tato třída je pevně napojena na G prostor |
|
54 |
|
#č ale bacha, kbyby se to změnilo... |
|
55 |
|
assert bx.convex_hull.space == 'G' |
|
56 |
|
|
|
57 |
|
a, b, _sample = bx.convex_hull.get_exploration_vector() |
|
58 |
|
R = -b |
51 |
59 |
|
|
52 |
60 |
if r < R: |
if r < R: |
53 |
61 |
bx._logger(msg='refine (fallbacked)!') |
bx._logger(msg='refine (fallbacked)!') |
54 |
62 |
return bx.refine() |
return bx.refine() |
55 |
63 |
|
|
56 |
|
sample_G = a * r |
|
57 |
|
bx._logger(msg='explore!') |
|
58 |
|
return bx.f_model.new_sample(sample_G, space='G') |
|
59 |
|
|
|
60 |
|
|
|
61 |
|
|
|
|
64 |
|
orth_nodes_T = np.random.randn(len(a), bx.outer_budget) # len(a) == ndim |
|
65 |
|
orth_basis = khull.get_orth_basis(a) |
|
66 |
|
sample_from = stats.norm.sf(r) |
|
67 |
|
t = np.linspace(sample_from, 0, bx.outer_budget, endpoint=False) |
|
68 |
|
orth_nodes_T[0] = stats.norm.isf(t) |
|
69 |
|
|
|
70 |
|
outside_nodes_G = (orth_basis.T @ orth_nodes_T).T |
|
71 |
|
outside_nodes = bx.f_model.new_sample(outside_nodes_G, space='G') |
62 |
72 |
|
|
|
73 |
|
tree = spatial.KDTree(bx.G, compact_nodes=True, balanced_tree=False) |
|
74 |
|
d1, i1 = tree.query(outside_nodes_G, k=1) |
63 |
75 |
|
|
|
76 |
|
PDF = bx.pdf('G') |
64 |
77 |
|
|
|
78 |
|
nodes_pdf = outside_nodes.pdf('G') |
|
79 |
|
node_potentials = d1**bx.nvar * nodes_pdf**bx.psi_q * PDF[i1]**(1-bx.psi_q) |
65 |
80 |
|
|
|
81 |
|
max_node = np.argmax(node_potentials) |
|
82 |
|
|
|
83 |
|
bx._logger(msg='explore!') |
|
84 |
|
return outside_nodes[max_node] |
|
85 |
|
|
66 |
86 |
|
|
67 |
87 |
|
|
68 |
88 |
|
|
|
... |
... |
class DumbExploration(_Exploration): |
98 |
118 |
|
|
99 |
119 |
bx.direct_plan = quadpy.un.mysovskikh_1(bx.nvar).points |
bx.direct_plan = quadpy.un.mysovskikh_1(bx.nvar).points |
100 |
120 |
|
|
101 |
|
bx.convex_hull = Grick(bx.f_model, bx.direct_plan, nrandom=50) |
|
|
121 |
|
bx.convex_hull = khull.Grick(bx.f_model, bx.direct_plan, nrandom=50) |
102 |
122 |
bx.q = q |
bx.q = q |
103 |
123 |
|
|
104 |
124 |
bx.ghull = Ghull(bx.convex_hull) |
bx.ghull = Ghull(bx.convex_hull) |