File dicebox.py copied from file blackbox.py (similarity 97%) (mode: 100644) (index 251ec1a..ecdf0b0) |
2 |
2 |
# coding: utf-8 |
# coding: utf-8 |
3 |
3 |
|
|
4 |
4 |
""" |
""" |
5 |
|
Zde leží BlackBox (tuším, bude jeden) |
|
6 |
|
BlackBox pěčlivě ukladá věškerá data, |
|
|
5 |
|
|
|
6 |
|
Zde leží DiceBox (tuším, bude jeden) |
|
7 |
|
DiceBox pěčlivě ukladá věškerá data, |
7 |
8 |
věškeré sady vzorků, průběžné odhady a tak. |
věškeré sady vzorků, průběžné odhady a tak. |
8 |
|
Nejsem už jistý, zda BlackBox je šťastný nazev, neboť |
|
|
9 |
|
Nejsem už jistý, zda DiceBox je šťastný nazev, neboť |
9 |
10 |
teďkom je to spíše jen krabička pro krámy |
teďkom je to spíše jen krabička pro krámy |
10 |
11 |
""" |
""" |
11 |
12 |
|
|
|
... |
... |
from scipy import optimize # for BlackSimpleX |
24 |
25 |
import inspect # for ._log() function |
import inspect # for ._log() function |
25 |
26 |
|
|
26 |
27 |
from .samplebox import SampleBox # for candidates packing |
from .samplebox import SampleBox # for candidates packing |
27 |
|
from . import simplex as six # probably calling it "six" wasn't so good idea |
|
28 |
28 |
from . import simplex as sx |
from . import simplex as sx |
29 |
29 |
from . import lukiskon as lk |
from . import lukiskon as lk |
30 |
30 |
|
|
|
... |
... |
class GuessBox: |
102 |
102 |
print('GuessBox is in air mode') |
print('GuessBox is in air mode') |
103 |
103 |
|
|
104 |
104 |
|
|
105 |
|
class BlackBox: |
|
|
105 |
|
class DiceBox: |
106 |
106 |
""" |
""" |
107 |
|
BlackBox pěčlivě ukladá věškerá data, |
|
|
107 |
|
DiceBox pěčlivě ukladá věškerá data, |
108 |
108 |
věškeré sady vzorků (well, no yet), průběžné odhady a tak. |
věškeré sady vzorků (well, no yet), průběžné odhady a tak. |
109 |
|
Nejsem už jistý, zda BlackBox je šťastný nazev, neboť |
|
|
109 |
|
Nejsem už jistý, zda DiceBox je šťastný nazev, neboť |
110 |
110 |
teďkom je to spíše jen krabička pro krámy |
teďkom je to spíše jen krabička pro krámy |
111 |
|
|
|
112 |
|
.sampled_plan object |
|
113 |
|
.Z = g_values |
|
114 |
|
.failsi |
|
115 |
|
|
|
116 |
|
Souřadnice primárně z prostoru modelu, ty co jsme rovnou |
|
117 |
|
posilali do g_modelu! |
|
118 |
|
|
|
119 |
111 |
""" |
""" |
120 |
112 |
def __init__(bx, sample_box): |
def __init__(bx, sample_box): |
121 |
113 |
bx.sample_box = sample_box |
bx.sample_box = sample_box |
|
... |
... |
class BlackBox: |
127 |
119 |
bx.candidates = sample_box.sampled_plan() |
bx.candidates = sample_box.sampled_plan() |
128 |
120 |
|
|
129 |
121 |
# nové uložiště odhadů zadám explicitně, aby se pak |
# nové uložiště odhadů zadám explicitně, aby se pak |
130 |
|
# odhady v stm kodu přířazovaly zprávné krabice |
|
|
122 |
|
# odhady v stm kodu přířazovaly správné krabičce |
131 |
123 |
bx.estimations = [] |
bx.estimations = [] |
132 |
|
# má bejt GuessBox součástí BlackBoxu? |
|
|
124 |
|
# má bejt GuessBox součástí DiceBoxu? |
133 |
125 |
try: |
try: |
134 |
126 |
bx.guessbox = GuessBox(sample_box.filename, flush=20) |
bx.guessbox = GuessBox(sample_box.filename, flush=20) |
135 |
127 |
except: |
except: |
|
... |
... |
class BlackBox: |
137 |
129 |
bx.regen() |
bx.regen() |
138 |
130 |
|
|
139 |
131 |
def __repr__(bx): |
def __repr__(bx): |
140 |
|
return "%s(%s)"%('BlackBox', repr(bx.sample_box)) |
|
|
132 |
|
return "%s(%s)"%('DiceBox', repr(bx.sample_box)) |
141 |
133 |
|
|
142 |
134 |
def __str__(bx): |
def __str__(bx): |
143 |
|
return str('BlackBox ' + bx.sample_box) |
|
|
135 |
|
return str('DiceBox ' + bx.sample_box) |
144 |
136 |
|
|
145 |
137 |
def __len__(bx): |
def __len__(bx): |
146 |
138 |
return bx.sample_box.nsim |
return bx.sample_box.nsim |
|
... |
... |
class BlackBox: |
157 |
149 |
# stačí vratit sample_box |
# stačí vratit sample_box |
158 |
150 |
return bx.sample_box[slice] |
return bx.sample_box[slice] |
159 |
151 |
|
|
160 |
|
def __getattr__(bx, attr): |
|
161 |
|
if attr == 'blackbox': |
|
162 |
|
return bx |
|
|
152 |
|
def __getattr__(dx, attr): |
|
153 |
|
if attr == 'dicebox': |
|
154 |
|
return dx |
163 |
155 |
|
|
164 |
156 |
# branime sa rekurzii |
# branime sa rekurzii |
165 |
157 |
# defend against recursion |
# defend against recursion |
|
... |
... |
class BlackBox: |
239 |
231 |
print("\t"*indent, inspect.currentframe().f_back.f_code.co_name + ":", *msg) |
print("\t"*indent, inspect.currentframe().f_back.f_code.co_name + ":", *msg) |
240 |
232 |
|
|
241 |
233 |
|
|
242 |
|
# The BlackBox Observer |
|
|
234 |
|
# The DiceBox Observer |
243 |
235 |
def _logger(self, *args, msg="", indent=0, **kwargs): |
def _logger(self, *args, msg="", indent=0, **kwargs): |
244 |
236 |
print(self.__class__.__name__ + ":" + msg, *args, kwargs) |
print(self.__class__.__name__ + ":" + msg, *args, kwargs) |
245 |
237 |
|
|
|
... |
... |
class BlackBox: |
255 |
247 |
|
|
256 |
248 |
|
|
257 |
249 |
|
|
258 |
|
class Censoring(BlackBox): |
|
|
250 |
|
class Censoring(DiceBox): |
259 |
251 |
def __init__(bx, sample_object, tri_space='Rn'): |
def __init__(bx, sample_object, tri_space='Rn'): |
260 |
252 |
bx.tri_space = tri_space |
bx.tri_space = tri_space |
261 |
253 |
super().__init__(sample_object) |
super().__init__(sample_object) |
|
... |
... |
class MinEnergyCensoredSampling(Censoring): |
736 |
728 |
# už mě to dědění nebaví |
# už mě to dědění nebaví |
737 |
729 |
# без поллитры не разберёшься, что этот слоёный пирог делает |
# без поллитры не разберёшься, что этот слоёный пирог делает |
738 |
730 |
def __init__(bx, sample_object, tri_space='Rn', tree_space=None,\ |
def __init__(bx, sample_object, tri_space='Rn', tree_space=None,\ |
739 |
|
sampling_space=None, kechato_space='U', \ |
|
740 |
|
potencial='ksee', p_norm=2, budget=1000, simplex_budget=100): |
|
|
731 |
|
sampling_space=None, kechato_space='U', potencial='psee',\ |
|
732 |
|
p_norm=2, budget=1000, simplex_budget=100, LHS_correction=False): |
|
733 |
|
|
741 |
734 |
|
|
742 |
|
bx.sball = sball.Sball(sample_object.nvar) |
|
743 |
|
bx.base_r = bx.sball.get_r(0.5) |
|
744 |
735 |
|
|
745 |
736 |
if tree_space is None: |
if tree_space is None: |
746 |
737 |
bx.tree_space = tri_space |
bx.tree_space = tri_space |
|
... |
... |
class MinEnergyCensoredSampling(Censoring): |
751 |
742 |
bx.sampling_space = tri_space |
bx.sampling_space = tri_space |
752 |
743 |
else: |
else: |
753 |
744 |
bx.sampling_space = sampling_space |
bx.sampling_space = sampling_space |
|
745 |
|
|
|
746 |
|
|
754 |
747 |
|
|
755 |
748 |
bx.kechato_space = kechato_space |
bx.kechato_space = kechato_space |
756 |
749 |
# pro simplexy. Chcu ukladat jejich míry |
# pro simplexy. Chcu ukladat jejich míry |
|
... |
... |
class MinEnergyCensoredSampling(Censoring): |
760 |
753 |
bx.simplex_budget = simplex_budget |
bx.simplex_budget = simplex_budget |
761 |
754 |
bx.p_norm = p_norm |
bx.p_norm = p_norm |
762 |
755 |
bx.potencial = potencial |
bx.potencial = potencial |
|
756 |
|
bx.LHS_correction = LHS_correction |
763 |
757 |
|
|
764 |
758 |
# for current candidates |
# for current candidates |
765 |
759 |
# kandidaty musí být 'judged' a 'assessed' |
# kandidaty musí být 'judged' a 'assessed' |
|
... |
... |
class MinEnergyCensoredSampling(Censoring): |
783 |
777 |
""" |
""" |
784 |
778 |
# super TRYES to create .tri triangulation |
# super TRYES to create .tri triangulation |
785 |
779 |
super().regen() |
super().regen() |
786 |
|
# zás mám vyhodit odhady? |
|
787 |
|
bx.siss = IS_stat.ISSI() |
|
|
780 |
|
|
788 |
781 |
# kind of interface to CandidatesWidget |
# kind of interface to CandidatesWidget |
789 |
782 |
bx.candidates_index = dict() |
bx.candidates_index = dict() |
790 |
783 |
|
|
791 |
|
# needed for potencial calculation |
|
792 |
|
sampled_plan_tree = getattr(bx.sample_box, bx.tree_space) |
|
793 |
|
bx.tree = spatial.cKDTree(sampled_plan_tree) |
|
794 |
784 |
|
|
|
785 |
|
if bx.nsim > 0: |
|
786 |
|
# needed for potencial calculation |
|
787 |
|
sampled_plan_tree = getattr(bx.sample_box, bx.tree_space) |
|
788 |
|
bx.tree = spatial.cKDTree(sampled_plan_tree) |
795 |
789 |
|
|
796 |
790 |
bx._regen_outside() |
bx._regen_outside() |
797 |
791 |
bx._regen_inside() |
bx._regen_inside() |
|
... |
... |
class MinEnergyCensoredSampling(Censoring): |
800 |
794 |
|
|
801 |
795 |
def _regen_outside(bx): |
def _regen_outside(bx): |
802 |
796 |
# function reserved for childs |
# function reserved for childs |
803 |
|
pass |
|
|
797 |
|
pass |
|
798 |
|
|
804 |
799 |
|
|
805 |
800 |
|
|
806 |
801 |
def _regen_inside(bx): |
def _regen_inside(bx): |
807 |
802 |
if "tri" in dir(bx): |
if "tri" in dir(bx): |
808 |
|
# tri - Deloneho triangulace |
|
|
803 |
|
#č tri - Deloneho triangulace |
|
804 |
|
bx.sball = sball.Sball(bx.nvar) |
|
805 |
|
bx.base_r = bx.sball.get_r(0.5) |
|
806 |
|
|
|
807 |
|
#č odhady |
|
808 |
|
bx.siss = IS_stat.ISSI() |
|
809 |
|
|
809 |
810 |
bx.simplex_events = bx.get_events() |
bx.simplex_events = bx.get_events() |
810 |
811 |
bx.estimate_outside() |
bx.estimate_outside() |
811 |
812 |
for simplex_id in range(bx.tri.nsimplex): |
for simplex_id in range(bx.tri.nsimplex): |
|
... |
... |
class MinEnergyCensoredSampling(Censoring): |
819 |
820 |
bx.assess_candidates(candidates) |
bx.assess_candidates(candidates) |
820 |
821 |
# uložíme |
# uložíme |
821 |
822 |
bx.candidates_index[-1] = candidates |
bx.candidates_index[-1] = candidates |
822 |
|
|
|
|
823 |
|
|
823 |
824 |
|
|
824 |
825 |
|
|
825 |
826 |
|
|
|
... |
... |
class OptimizedCensoredSampling(MinEnergyCensoredSampling): |
1254 |
1255 |
def _regen_outside(bx): |
def _regen_outside(bx): |
1255 |
1256 |
if bx.nsim >= bx.nvar + 1: |
if bx.nsim >= bx.nvar + 1: |
1256 |
1257 |
try: |
try: |
1257 |
|
bx.convex_hull = spatial.ConvexHull(getattr(bx.sampled_plan, bx.tri_space), incremental=True) |
|
|
1258 |
|
bx.shull = sx.Shull(bx.f_model, bx.budget, bx.tri_space, bx.sampling_space, powerset_correction=False) |
|
1259 |
|
bx.siss = bx.shull.oiss |
1258 |
1260 |
bx.estimate_outside() |
bx.estimate_outside() |
1259 |
1261 |
#č a máme hotovo |
#č a máme hotovo |
1260 |
1262 |
return # Ok, go away |
return # Ok, go away |
|
... |
... |
class OptimizedCensoredSampling(MinEnergyCensoredSampling): |
1417 |
1419 |
|
|
1418 |
1420 |
|
|
1419 |
1421 |
#č řeším problém, že při načítaní ze souboru |
#č řeším problém, že při načítaní ze souboru |
1420 |
|
#č blackbox nemá adekvatní odhad Brno-venkova |
|
|
1422 |
|
#č DiceBox nemá adekvatní odhad Brno-venkova |
1421 |
1423 |
while len(candidates) < 2: |
while len(candidates) < 2: |
1422 |
1424 |
p_out = p_out/2 |
p_out = p_out/2 |
1423 |
1425 |
bx._logger(msg="suppose p_out="+str(p_out)) |
bx._logger(msg="suppose p_out="+str(p_out)) |
|
... |
... |
class FastSimpleX(OptimizedCensoredSampling): |
1703 |
1705 |
|
|
1704 |
1706 |
#č alternativní název je |
#č alternativní název je |
1705 |
1707 |
#:) UltimateFastOptimizationOptimizedMinDistanceEnergyAdaptiveCensoringSampling |
#:) UltimateFastOptimizationOptimizedMinDistanceEnergyAdaptiveCensoringSampling |
1706 |
|
class BlackSimpleX(OptimizedCensoredSampling): |
|
|
1708 |
|
class DiceSimpleX(OptimizedCensoredSampling): |
1707 |
1709 |
|
|
1708 |
1710 |
#č rozdil je v chybejicích odhadéch |
#č rozdil je v chybejicích odhadéch |
1709 |
1711 |
def _handle_changed_triangulation(bx, input_sample): |
def _handle_changed_triangulation(bx, input_sample): |
|
... |
... |
class BlackSimpleX(OptimizedCensoredSampling): |
1890 |
1892 |
|
|
1891 |
1893 |
|
|
1892 |
1894 |
#♥♥♥♥♥♥ |
#♥♥♥♥♥♥ |
1893 |
|
# BlackBoxiCheck |
|
1894 |
|
class KechatoLukiskon(BlackBox): |
|
|
1895 |
|
# DiceBoxiCheck |
|
1896 |
|
class KechatoLukiskon(DiceBox): |
1895 |
1897 |
def __init__(kl, sample_object, model_space='Rn', sampling_space=None, kechato_space='Rn', p_norm=1, gradient=None, budget=20000): |
def __init__(kl, sample_object, model_space='Rn', sampling_space=None, kechato_space='Rn', p_norm=1, gradient=None, budget=20000): |
1896 |
1898 |
kl.model_space = model_space |
kl.model_space = model_space |
1897 |
1899 |
kl.sampling_space = sampling_space |
kl.sampling_space = sampling_space |
|
... |
... |
class KechatoLukiskon(BlackBox): |
1955 |
1957 |
|
|
1956 |
1958 |
|
|
1957 |
1959 |
#♥♥♥♥♥♥♥♥♥♥♥♥ |
#♥♥♥♥♥♥♥♥♥♥♥♥ |
1958 |
|
class KechatoTwoPointLukiskon(BlackBox): |
|
|
1960 |
|
class KechatoTwoPointLukiskon(DiceBox): |
1959 |
1961 |
def __init__(kl, sample_object, model_space='Rn', sampling_space=None, kechato_space='Rn', p_norm=1, gradient=None, budget=20000): |
def __init__(kl, sample_object, model_space='Rn', sampling_space=None, kechato_space='Rn', p_norm=1, gradient=None, budget=20000): |
1960 |
1962 |
kl.model_space = model_space |
kl.model_space = model_space |
1961 |
1963 |
kl.sampling_space = sampling_space |
kl.sampling_space = sampling_space |