File wellmet/dicebox/goal.py renamed from wellmet/dicebox.py (similarity 95%) (mode: 100644) (index 750fd88..b686600) |
... |
... |
import numpy as np |
15 |
15 |
from scipy import spatial |
from scipy import spatial |
16 |
16 |
from scipy import stats |
from scipy import stats |
17 |
17 |
import pickle |
import pickle |
18 |
|
from . import IS_stat |
|
19 |
|
from .candybox import CandyBox |
|
|
18 |
|
from .. import IS_stat |
|
19 |
|
from ..candybox import CandyBox |
20 |
20 |
from scipy import optimize # for BlackSimpleX |
from scipy import optimize # for BlackSimpleX |
21 |
21 |
|
|
22 |
22 |
|
|
23 |
|
from .samplebox import SampleBox # for candidates packing |
|
24 |
|
from .ghull import Ghull |
|
25 |
|
from . import simplex as sx |
|
26 |
|
from . import convex_hull as khull |
|
27 |
|
from . import lukiskon as lk |
|
28 |
|
from . import reader # for Goal |
|
29 |
|
from . import estimation as stm # for KechatoLukiskon |
|
|
23 |
|
from ..samplebox import SampleBox # for candidates packing |
|
24 |
|
from ..ghull import Ghull |
|
25 |
|
from .. import simplex as sx |
|
26 |
|
from .. import convex_hull as khull |
|
27 |
|
from .. import lukiskon as lk |
|
28 |
|
from .. import reader # for Goal |
|
29 |
|
from .. import estimation as stm # for KechatoLukiskon |
30 |
30 |
import collections #E for Counter in MinEnergyCensoredSampling |
import collections #E for Counter in MinEnergyCensoredSampling |
31 |
31 |
|
|
32 |
32 |
|
|
|
... |
... |
class DiceSimpleX: |
1902 |
1902 |
|
|
1903 |
1903 |
|
|
1904 |
1904 |
|
|
|
1905 |
|
|
|
1906 |
|
|
|
1907 |
|
|
|
1908 |
|
|
|
1909 |
|
|
|
1910 |
|
|
|
1911 |
|
|
|
1912 |
|
|
1905 |
1913 |
class KickPointVoronoi(DiceBox): |
class KickPointVoronoi(DiceBox): |
1906 |
1914 |
""" |
""" |
1907 |
1915 |
Goal |
Goal |
|
... |
... |
class KickPointVoronoi(DiceBox): |
1956 |
1964 |
#č a zaroveň je nominuje na soutež. |
#č a zaroveň je nominuje na soutež. |
1957 |
1965 |
#č na vstupu assess_candidates musí být CandyBox |
#č na vstupu assess_candidates musí být CandyBox |
1958 |
1966 |
#č s jíž nastaveným event_id |
#č s jíž nastaveným event_id |
1959 |
|
#assess_candidates = Chrt.assess_candidates |
|
|
1967 |
|
assess_candidates = Chrt.assess_candidates |
1960 |
1968 |
_nominate = Chrt._nominate |
_nominate = Chrt._nominate |
1961 |
1969 |
|
|
1962 |
1970 |
#č explicitně převezmu některé funkce |
#č explicitně převezmu některé funkce |
|
... |
... |
class KickPointVoronoi(DiceBox): |
1970 |
1978 |
_invalidate_simplex = Razitko._invalidate_simplex |
_invalidate_simplex = Razitko._invalidate_simplex |
1971 |
1979 |
|
|
1972 |
1980 |
|
|
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,\ |
|
|
1981 |
|
#č míží nám sampling_space: Ghull umí vzorkovat outside pouze v G prostoru |
|
1982 |
|
#č quadpy umístí integráční bodíky v prostoru triangulace. |
|
1983 |
|
def __init__(bx, sample_object, scheme, tri_space='G', tree_space=None,\ |
|
1984 |
|
kechato_space='U', potential='q_psee', q=0.5,\ |
1975 |
1985 |
p_norm=2, shell_budget=1000, outer_budget=100,\ |
p_norm=2, shell_budget=1000, outer_budget=100,\ |
1976 |
1986 |
LHS_correction=False, stm_filename=None): |
LHS_correction=False, stm_filename=None): |
1977 |
1987 |
|
|
1978 |
|
bx.ghull = ghull |
|
|
1988 |
|
bx.scheme = scheme |
1979 |
1989 |
bx.tri_space = tri_space |
bx.tri_space = tri_space |
1980 |
1990 |
if tree_space is None: |
if tree_space is None: |
1981 |
1991 |
bx.tree_space = tri_space |
bx.tree_space = tri_space |
1982 |
1992 |
else: |
else: |
1983 |
1993 |
bx.tree_space = tree_space |
bx.tree_space = tree_space |
1984 |
1994 |
|
|
1985 |
|
if sampling_space is None: |
|
1986 |
|
bx.sampling_space = tri_space |
|
1987 |
|
else: |
|
1988 |
|
bx.sampling_space = sampling_space |
|
1989 |
|
|
|
1990 |
1995 |
|
|
1991 |
1996 |
bx.kechato_space = kechato_space |
bx.kechato_space = kechato_space |
1992 |
1997 |
bx.shell_budget = shell_budget |
bx.shell_budget = shell_budget |
|
... |
... |
class KickPointVoronoi(DiceBox): |
2130 |
2135 |
def _handle_candidates(bx): |
def _handle_candidates(bx): |
2131 |
2136 |
#č A ještě... AUKCE, AUCTION |
#č A ještě... AUKCE, AUCTION |
2132 |
2137 |
# Election - selection |
# 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) |
|
|
2138 |
|
for key, cached_bid in reversed(bx.candidates_index.cache.items()): |
|
2139 |
|
# side effect |
|
2140 |
|
if cached_bid > bx.highest_bid: |
|
2141 |
|
#č pokud neprovadíme optimalizaci v simplexech |
|
2142 |
|
#č tak nám stačí jednoduše assessovat |
|
2143 |
|
bx.assess_candidates(bx.candidates_index[key]) |
|
2144 |
|
#č tím se mi aktualizuje cache |
|
2145 |
|
bx.candidates_index[key] = bx.candidates_index[key] |
2142 |
2146 |
|
|
2143 |
2147 |
# probably, we shouldn't purge user candidates (if they are) |
# probably, we shouldn't purge user candidates (if they are) |
2144 |
2148 |
# just every time evaluate them |
# just every time evaluate them |
|
... |
... |
class KickPointVoronoi(DiceBox): |
2148 |
2152 |
# bx.assess_candidates(bx.candidates) |
# bx.assess_candidates(bx.candidates) |
2149 |
2153 |
|
|
2150 |
2154 |
|
|
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 |
2155 |
|
|
2220 |
2156 |
|
|
2221 |
2157 |
def _ghull_outside_callback(bx, outside_nodes): |
def _ghull_outside_callback(bx, outside_nodes): |
|
... |
... |
class KickPointVoronoi(DiceBox): |
2347 |
2283 |
#č takže musíme sami lepit nové etikety |
#č takže musíme sami lepit nové etikety |
2348 |
2284 |
bx.global_stats['nsim'] = bx.nsim |
bx.global_stats['nsim'] = bx.nsim |
2349 |
2285 |
|
|
|
2286 |
|
failsi = bx.failsi |
|
2287 |
|
|
2350 |
2288 |
if 'tri' in dir(bx): |
if 'tri' in dir(bx): |
2351 |
2289 |
#č Tri.get_pf_estimation() vrací: |
#č Tri.get_pf_estimation() vrací: |
2352 |
2290 |
# 'TRI_estimation': tri_estimation, 'global_stats': {mix, failure}, \ |
# 'TRI_estimation': tri_estimation, 'global_stats': {mix, failure}, \ |
|
... |
... |
class KickPointVoronoi(DiceBox): |
2365 |
2303 |
estimations['ghull_estimation'] = bx.ghull_estimation |
estimations['ghull_estimation'] = bx.ghull_estimation |
2366 |
2304 |
|
|
2367 |
2305 |
#č hrozně důležitý. Těšíme se na csv-čko. |
#č hrozně důležitý. Těšíme se na csv-čko. |
2368 |
|
bx.global_stats = {**bx.global_stats, **estimations['global_stats']} |
|
|
2306 |
|
bx.global_stats.update(estimations['global_stats']) |
|
2307 |
|
bx.global_stats['success_points'] = len(failsi[~failsi]) |
|
2308 |
|
bx.global_stats['failure_points'] = len(failsi[failsi]) |
2369 |
2309 |
bx.global_stats['success'] = tri_estimation[0] |
bx.global_stats['success'] = tri_estimation[0] |
2370 |
|
|
|
|
2310 |
|
bx.global_stats['candidates_sets'] = len(bx.candidates_index) |
|
2311 |
|
estimations['global_stats'].update(bx.global_stats) |
2371 |
2312 |
return estimations |
return estimations |
2372 |
2313 |
|
|
2373 |
2314 |
|
|
|
... |
... |
class KickPointVoronoi(DiceBox): |
2386 |
2327 |
|
|
2387 |
2328 |
global_stats = bx.global_stats |
global_stats = bx.global_stats |
2388 |
2329 |
# outside dodá Ghull |
# outside dodá Ghull |
|
2330 |
|
global_stats['success_points'] = len(failsi[~failsi]) |
|
2331 |
|
global_stats['failure_points'] = len(failsi[failsi]) |
2389 |
2332 |
global_stats['success'] = tri_estimation[0] |
global_stats['success'] = tri_estimation[0] |
2390 |
2333 |
global_stats['failure'] = tri_estimation[1] |
global_stats['failure'] = tri_estimation[1] |
2391 |
2334 |
global_stats['mix'] = tri_estimation[2] |
global_stats['mix'] = tri_estimation[2] |
2392 |
2335 |
global_stats['vertex_estimation'] = vertex_estimation |
global_stats['vertex_estimation'] = vertex_estimation |
2393 |
2336 |
global_stats['weighted_vertex_estimation'] = weighted_vertex_estimation |
global_stats['weighted_vertex_estimation'] = weighted_vertex_estimation |
2394 |
2337 |
global_stats['nsimplex'] = 0 |
global_stats['nsimplex'] = 0 |
|
2338 |
|
global_stats['tn_scheme'] = bx.scheme.name |
|
2339 |
|
global_stats['tn_scheme_points'] = bx.scheme.points.shape[1] |
|
2340 |
|
global_stats['newly_invalidated'] = 0 |
|
2341 |
|
global_stats['newly_estimated'] = 0 |
|
2342 |
|
global_stats['simplex_stats'] = 0 |
|
2343 |
|
global_stats['candidates_sets'] = len(bx.candidates_index) |
2395 |
2344 |
global_stats['ncoplanar'] = 0 |
global_stats['ncoplanar'] = 0 |
2396 |
2345 |
|
|
2397 |
2346 |
return {'TRI_overall_estimations': tri_estimation, \ |
return {'TRI_overall_estimations': tri_estimation, \ |
|
... |
... |
class KickPointVoronoi(DiceBox): |
2424 |
2373 |
reader.export_estimation(bx.stm_filename, bx.global_stats) |
reader.export_estimation(bx.stm_filename, bx.global_stats) |
2425 |
2374 |
|
|
2426 |
2375 |
|
|
|
2376 |
|
#č boom-body klidně můžou spadat dovnitř orth obálky. |
|
2377 |
|
#č Ale to nevadí, kandidati za vnější kryžnicí nejsou špatní, že? |
|
2378 |
|
def boom(self, ns): |
|
2379 |
|
# rand_dir: prepare ns random directions on a unit d-sphere |
|
2380 |
|
rand_dir = sball.get_random_directions(ns, self.sample.nvar) #random directions |
|
2381 |
|
|
|
2382 |
|
#č deme od vnější .get_R() kružnici směrem ven |
|
2383 |
|
r = self.get_R() |
|
2384 |
|
|
|
2385 |
|
if r < max_R_ever: |
|
2386 |
|
R = max_R_ever |
|
2387 |
|
else: |
|
2388 |
|
R = r + 10 |
|
2389 |
|
r = np.linspace(self.get_R(), max_R_ever, ns, endpoint=True) |
|
2390 |
|
nodes_G = rand_dir*r[:,None] |
|
2391 |
|
|
|
2392 |
|
nodes = self.sample.f_model.new_sample(nodes_G, space='G') |
|
2393 |
|
return nodes |
|
2394 |
|
|
|
2395 |
|
|
|
2396 |
|
|
|
2397 |
|
|
|
2398 |
|
def get_R(self): |
|
2399 |
|
return self._R |
|
2400 |
|
#sum_squared = np.sum(np.square(self.sample.G), axis=1) |
|
2401 |
|
#index = np.argmax(sum_squared) |
|
2402 |
|
#return np.sqrt(np.nanmax(sum_squared)) |
|
2403 |
|
|
|
2404 |
|
|
2427 |
2405 |
##č nie treba počítat odhady v každem kroku |
##č nie treba počítat odhady v každem kroku |
2428 |
2406 |
##č ale zas, taky že chceme do článků davat nějaké diagramy |
##č ale zas, taky že chceme do článků davat nějaké diagramy |
2429 |
2407 |
# |
# |