File wellmet/dicebox/_exploration.py added (mode: 100644) (index 0000000..784631e) |
|
1 |
|
#!/usr/bin/env python |
|
2 |
|
# coding: utf-8 |
|
3 |
|
|
|
4 |
|
|
|
5 |
|
import numpy as np |
|
6 |
|
import quadpy |
|
7 |
|
|
|
8 |
|
from ..ghull import Ghull |
|
9 |
|
from ..convex_hull import Grick |
|
10 |
|
from .. import simplex as sx |
|
11 |
|
from ..reader import Store |
|
12 |
|
from .. import sball |
|
13 |
|
|
|
14 |
|
|
|
15 |
|
from collections import namedtuple |
|
16 |
|
from sortedcollections import ValueSortedDict |
|
17 |
|
|
|
18 |
|
|
|
19 |
|
|
|
20 |
|
|
|
21 |
|
|
|
22 |
|
|
|
23 |
|
|
|
24 |
|
|
|
25 |
|
class _Exploration: |
|
26 |
|
|
|
27 |
|
|
|
28 |
|
def __call__(bx): |
|
29 |
|
if bx.nsim < 1: # je to legální |
|
30 |
|
return bx.f_model.new_sample([], space='G', extend=True) |
|
31 |
|
elif np.random.random() > bx.exploration_ratio: |
|
32 |
|
bx._logger(bx.exploration_ratio, msg='refine!') |
|
33 |
|
return bx.refine() |
|
34 |
|
else: |
|
35 |
|
return bx.explore() |
|
36 |
|
|
|
37 |
|
|
|
38 |
|
|
|
39 |
|
def explore(bx): |
|
40 |
|
p_desired = np.exp(-np.sqrt(bx.q * bx.nsim)) |
|
41 |
|
r = bx.sball.get_r(p_desired) |
|
42 |
|
if bx.nsim > len(bx.direct_plan): |
|
43 |
|
a, b = bx.convex_hull.get_exploration_vector() |
|
44 |
|
R = -b |
|
45 |
|
else: |
|
46 |
|
a = bx.direct_plan[bx.nsim-1] |
|
47 |
|
R = bx.ghull.get_R() |
|
48 |
|
|
|
49 |
|
if r < R: |
|
50 |
|
bx._logger(bx.exploration_ratio, msg='refine (fallbacked)!') |
|
51 |
|
return bx.refine() |
|
52 |
|
|
|
53 |
|
sample_G = a * r |
|
54 |
|
bx._logger(bx.exploration_ratio, msg='explore!') |
|
55 |
|
return bx.f_model.new_sample(sample_G, space='G') |
|
56 |
|
|
|
57 |
|
|
|
58 |
|
|
|
59 |
|
|
|
60 |
|
|
|
61 |
|
|
|
62 |
|
|
|
63 |
|
|
|
64 |
|
|
|
65 |
|
|
|
66 |
|
ExplorationLog = namedtuple('ExplorationLog', ( |
|
67 |
|
"nsim", |
|
68 |
|
"nvar", |
|
69 |
|
"nfacets", |
|
70 |
|
"r","R", |
|
71 |
|
"inner", |
|
72 |
|
"shell", |
|
73 |
|
"outer", |
|
74 |
|
"FORM_outside", |
|
75 |
|
"TwoFORM_outside", |
|
76 |
|
"orth_outside", |
|
77 |
|
"inside", |
|
78 |
|
"outside", |
|
79 |
|
"success_points", |
|
80 |
|
"failure_points", |
|
81 |
|
"success", |
|
82 |
|
"failure", |
|
83 |
|
"mix", |
|
84 |
|
"vertex_estimation", |
|
85 |
|
"weighted_vertex_estimation" |
|
86 |
|
)) |
|
87 |
|
|
|
88 |
|
|
|
89 |
|
class DumbExploration(_Exploration): |
|
90 |
|
|
|
91 |
|
#č hull musí být v G prostoru s auto_update=True |
|
92 |
|
def __init__(bx, sample_box, q=10): |
|
93 |
|
bx.sample_box = sample_box |
|
94 |
|
|
|
95 |
|
bx.direct_plan = quadpy.un.mysovskikh_1(bx.nvar).points |
|
96 |
|
|
|
97 |
|
bx.convex_hull = Grick(bx.f_model, bx.direct_plan, nrandom=50) |
|
98 |
|
bx.q = q |
|
99 |
|
|
|
100 |
|
bx.ghull = Ghull(bx.convex_hull) |
|
101 |
|
bx.sball = sball.Sball(bx.nvar) |
|
102 |
|
|
|
103 |
|
bx.exploration_ratio = 2 # never refine |
|
104 |
|
|
|
105 |
|
#č přece ponechame složku pro uživatelské odhady |
|
106 |
|
#č stm kód může semka něco ukladat |
|
107 |
|
bx.estimations = [] |
|
108 |
|
|
|
109 |
|
#č vítejte nové uložiště odhadů. |
|
110 |
|
#č Odhady z stm kódu už ale nemají na tohle sahat |
|
111 |
|
if hasattr(bx, 'filename'): |
|
112 |
|
bx.box_estimations = Store.create(bx.filename + "_exp", ExplorationLog) |
|
113 |
|
else: |
|
114 |
|
bx.box_estimations = [] |
|
115 |
|
|
|
116 |
|
bx.regen() |
|
117 |
|
|
|
118 |
|
|
|
119 |
|
def init_parameters(bx): |
|
120 |
|
""" |
|
121 |
|
Returns dictionary of parameters the DiceBox was initialized with |
|
122 |
|
""" |
|
123 |
|
return {'sample_box':bx.sample_box, 'hull':bx.hull, 'q':bx.q} |
|
124 |
|
|
|
125 |
|
|
|
126 |
|
def __repr__(bx): |
|
127 |
|
return "%s(**%s)"%(bx.__class__.__name__, repr(bx.init_parameters())) |
|
128 |
|
|
|
129 |
|
def __str__(bx): |
|
130 |
|
return "%s(%s)"%(bx.__class__.__name__, str(bx.init_parameters())) |
|
131 |
|
|
|
132 |
|
|
|
133 |
|
|
|
134 |
|
|
|
135 |
|
def regen(bx): |
|
136 |
|
if bx.nsim > 0: |
|
137 |
|
bx.estimate_outside() |
|
138 |
|
bx._nsim = bx.nsim |
|
139 |
|
|
|
140 |
|
|
|
141 |
|
|
|
142 |
|
|
|
143 |
|
|
|
144 |
|
def __len__(bx): |
|
145 |
|
return bx.sample_box.nsim |
|
146 |
|
|
|
147 |
|
def __getitem__(bx, slice): |
|
148 |
|
#č stačí vratit sample_box |
|
149 |
|
return bx.sample_box[slice] |
|
150 |
|
|
|
151 |
|
def __getattr__(dx, attr): |
|
152 |
|
if attr == 'dicebox': |
|
153 |
|
return dx |
|
154 |
|
|
|
155 |
|
#č branime sa rekurzii |
|
156 |
|
# defend against recursion |
|
157 |
|
#оӵ рекурсилы пезьдэт! |
|
158 |
|
if attr == 'sample_box': |
|
159 |
|
raise AttributeError |
|
160 |
|
|
|
161 |
|
#ё По всем вопросам обращайтесь |
|
162 |
|
#ё на нашу горячую линию |
|
163 |
|
else: |
|
164 |
|
return getattr(dx.sample_box, attr) |
|
165 |
|
|
|
166 |
|
|
|
167 |
|
# The DiceBox Observer |
|
168 |
|
def _logger(self, *args, msg="", indent=0, **kwargs): |
|
169 |
|
if not kwargs: |
|
170 |
|
kwargs = "" #č ať se nám prázdné závorky nezobrazujou |
|
171 |
|
print(self.__class__.__name__ + ":", msg, *args, kwargs) |
|
172 |
|
|
|
173 |
|
|
|
174 |
|
# inspired by Qt |
|
175 |
|
def connect(self, slot): self._logger = slot |
|
176 |
|
def disconnect(self): del(self._logger) |
|
177 |
|
|
|
178 |
|
|
|
179 |
|
# přidávání vzorků musí bejt explicitní! |
|
180 |
|
def add_sample(bx, input_sample): |
|
181 |
|
bx.sample_box.add_sample(input_sample) |
|
182 |
|
bx.increment(bx.sample_box[bx._nsim:]) |
|
183 |
|
bx._nsim = bx.nsim |
|
184 |
|
|
|
185 |
|
|
|
186 |
|
|
|
187 |
|
|
|
188 |
|
def increment(bx, input_sample): |
|
189 |
|
bx.estimate_outside() |
|
190 |
|
|
|
191 |
|
bx.box_estimations.append(bx.get_pf_estimation()) |
|
192 |
|
|
|
193 |
|
|
|
194 |
|
|
|
195 |
|
|
|
196 |
|
|
|
197 |
|
|
|
198 |
|
|
|
199 |
|
|
|
200 |
|
|
|
201 |
|
def estimate_outside(bx): |
|
202 |
|
#č konečně mám pořádnou stejtful třídu |
|
203 |
|
#č pokud mám aspoň jednu tečku, tak už je mi šuma |
|
204 |
|
#č zda se konvexní obálka vytvořila, či nikoliv |
|
205 |
|
|
|
206 |
|
|
|
207 |
|
shell_estimation, global_stats = bx.ghull.get_shell_estimation() |
|
208 |
|
|
|
209 |
|
# shell_estimation -22: inner, -3: shell, -11: outer |
|
210 |
|
bx.shell_estimation = shell_estimation |
|
211 |
|
|
|
212 |
|
#č kvůli názvu neleze do namedtuple |
|
213 |
|
global_stats['TwoFORM_outside'] = global_stats.pop('2FORM_outside') |
|
214 |
|
#č ndim se nelibí pandas |
|
215 |
|
global_stats['nvar'] = global_stats.pop('ndim') |
|
216 |
|
|
|
217 |
|
bx.global_stats = global_stats |
|
218 |
|
|
|
219 |
|
|
|
220 |
|
|
|
221 |
|
|
|
222 |
|
|
|
223 |
|
def get_pf_estimation(bx): |
|
224 |
|
global_stats = bx.global_stats |
|
225 |
|
orth_outside = global_stats['outside'] = global_stats['orth_outside'] |
|
226 |
|
pf_inside = global_stats['inside'] = 1 - orth_outside |
|
227 |
|
|
|
228 |
|
#č takže celou skříňku prostě bereme jako simplex |
|
229 |
|
event, event_id, fr, wfr = sx.get_simplex_event(bx, weighting_space='G') |
|
230 |
|
# -1=outside, 0=success, 1=failure, 2=mix |
|
231 |
|
tri_estimation = {0:0, 1:0, 2:0} |
|
232 |
|
tri_estimation[event_id] = pf_inside |
|
233 |
|
|
|
234 |
|
vertex_estimation = pf_inside * fr |
|
235 |
|
weighted_vertex_estimation = pf_inside * wfr |
|
236 |
|
|
|
237 |
|
|
|
238 |
|
failsi = bx.failsi |
|
239 |
|
global_stats['success_points'] = len(failsi[~failsi]) |
|
240 |
|
global_stats['failure_points'] = len(failsi[failsi]) |
|
241 |
|
global_stats['success'] = tri_estimation[0] |
|
242 |
|
global_stats['failure'] = tri_estimation[1] |
|
243 |
|
global_stats['mix'] = tri_estimation[2] |
|
244 |
|
global_stats['vertex_estimation'] = vertex_estimation |
|
245 |
|
global_stats['weighted_vertex_estimation'] = weighted_vertex_estimation |
|
246 |
|
|
|
247 |
|
return ExplorationLog(**global_stats) |
|
248 |
|
|