def __mixed_compare(a, b, strat): ''' Comparator able to match numerics and tuples. It check equality for each positional element and apply strat to the first mismatching elements or the last elements. ''' # Transform inputs to sequence if a is None or b is None: return cmp.none_cmp(a, b) a = flat([a]) b = flat([b]) def __fill(a, b): return a[:len(b) + 1], type(b)([*b, 0.0]) if len(a) > len(b): a, b = __fill(a, b) elif len(a) < len(b): b, a = __fill(b, a) while a[0] == b[0] and len(a) == len(b) and len(b) > 1: a = a[-len(a) + 1:] b = b[-len(b) + 1:] return strat(a[0], b[0])
def generate_test_params(spawn_points: dict): return map( lambda t: flat(t, to=tuple, exclude=Point3D), itertools.product( spawn_points['light_spawn_points'], zip(spawn_points['agent_spawn_points'], spawn_points['agent_yrots'])))
def train_behavioural_bn(bn: OpenBooleanNetwork) -> (OpenBooleanNetwork, VNSOutput): spawn_points = GLOBALS.generate_spawn_points() terminal_nodes = get_terminal_nodes(bn) to_never_flip = set(map(hash, bn.input_nodes + terminal_nodes)) train_scores_cmp = GLOBALS.eval_cmp_function train_eval_fn = lambda bn, ct: pt_evaluation_for_train(bn, ct, spawn_points) train_scramble_strategy = lambda bn, nf, e: bn_scramble_strategy(bn, nf, e.union(to_never_flip)) train_tidy_strategy = edit_boolean_network pvns = VNS( sol_evaluator=train_eval_fn, sols_comparator=train_scores_cmp, sol_scrambler=train_scramble_strategy, sol_tidier=train_tidy_strategy ) params = VNSParameters( target_score=flat((GLOBALS.sd_target_score, float('+inf')), to=tuple), min_flips=GLOBALS.sd_min_flips, max_flips=sum(2**n.arity for n in bn.nodes if hash(n.label) not in to_never_flip), max_iters=GLOBALS.sd_max_iters, max_stalls=GLOBALS.sd_max_stalls, max_stagnation=GLOBALS.sd_max_stagnation ) return pvns(bn, params)
def scatter4d(y: list, x: list, z: list, w: list, k: list, window: str, title: str, xlabel: str, ylabel: str, zlabel: str, zlims: list): cmap = get_weighted_cmap(flat(w), name='rainbow') fig = plotter.figure(num=window, figsize=(19, 11), dpi=138) ax = fig.gca(projection='3d') ax.set_title(title) ax.set_xlabel(xlabel) ax.set_ylabel(ylabel) ax.set_zlabel(zlabel) ax.set_zlim3d(zlims) ax.ticklabel_format(axis='z', style='sci', scilimits=(0, 0)) for i, _k in enumerate(k): ax.scatter(xs=x[i], ys=y[i], zs=z[i], c=w[i], color=cmap.to_rgba(w[i]), label=_k) ax.legend(prop={'size': LEG_LABEL_SIZE}) return fig, ax
def test_bnselector_atm_similarity(self): N, K, P, Q, I, O = 5, 2, 0.5, 0.0, 1, 0 bnsg = template_selector_generator(N, K, P, Q, I, O) atms = [] for _ in range(1000): s = bnsg.new_selector() if constraints.test_attractors_number(s, 2): atms.append(s.atm.tableau) # print(s.atm.tableau) flatatm = [flat(list(a), to=tuple) for a in atms] # print(len(set(flatatm))) self.assertTrue( all([ sum(a == b for b in atms) <= 1 + int(len(atms) / 2) for a in atms ])) ############################################################ atms2 = [] for _ in range(1000): s = bnsg.new_selector() if constraints.test_attractors_number(s, 2): atms2.append(s.atm.tableau) # print(s.atm.tableau) flatatm2 = [flat(list(a), to=tuple) for a in atms2] # print(len(set(flatatm2))) self.assertFalse(flatatm == flatatm2)
def r_point3d(O=Point3D(0.0, 0.0, 0.0), R=1.0, axis=Axis.NONE, quadrant=Quadrant.ANY): R = R if isinstance(R, (list, tuple)) and len(R) > 1 else flat([0.0, R]) r = min(R) + abs(R[0] - R[1]) * np.random.uniform(0, 1.0) if axis is Axis.NONE: el = math.acos(1.0 - np.random.uniform(0.0, 2.0)) # better distribution but fails for Z | X == 0 else: el = np.random.uniform(0.0, 1.0) * 2 * math.pi az = np.random.uniform(0.0, 1.0) * 2 * math.pi return O + quadrant(axis(r, el, az))
def plot_data(data: dict, infos: dict, positives_threshold: float, light_intensity: float, phase_len: int): thresholds = [t * light_intensity for t in flat([positives_threshold])] LEAST_OPT_WPT_SCORE = round(sum(thresholds) / len(thresholds), 7) # round(max(thresholds) * 1.0 / 3.0, 7) # LEAST_OPT_WAPT_SCORE = round(max(thresholds) * 3.0 / 1.0, 7) simple_keys = set( map( lambda x: fu.get_simple_fname( x, fu.FNAME_PATTERN, parts=['%s', '%s', '%s'], uniqueness=2), data.keys())) model = 'all' if len(simple_keys) > 1 else simple_keys.pop() ####################################################################################### cols = [ 'bn', 'pt_score', 'apt_score', 'wpt', 'wapt', # 'pt_idist', 'pt_fdist', 'apt_idist', 'apt_fdist', # 'pt_yrot', 'apt_yrot', 'apt_fyrot', 'n1a0p', 'n1a1p', 'n2a0p', 'n2a1p', 'n1rd', 'n2rd', 's1', 's2', 's3', 's4', 's5', 's6', 's7', 's8', 'ss', 'sr', 'a0len', 'a1len', 'pt_apos_x', 'pt_apos_z', 'apt_apos_x', 'apt_apos_z', ] ds = aggregate_data(data, infos, phase_len, light_intensity, LEAST_OPT_WPT_SCORE) # ds = round_data(ds) ds.set_index([ds.index, 'bn']) data.clear() means = DataFrame(ds[cols]).groupby(by=['bn'], as_index=False).mean() means = round_data(means) ranks = DataFrame({ 'bn': means['bn'].values, 'wpt': means['wpt'].rank(axis=0, method='dense', numeric_only=True, ascending=True).values, 'wapt': means['wapt'].rank(axis=0, method='dense', numeric_only=True, ascending=False).values, 'n1rd': means['n1rd'].rank(axis=0, method='dense', numeric_only=True, ascending=True).values, 'n2rd': means['n2rd'].rank(axis=0, method='dense', numeric_only=True, ascending=True).values, 'ss': means['ss'].rank(axis=0, method='dense', numeric_only=True, ascending=False).values, 'sr': means['sr'].rank(axis=0, method='dense', numeric_only=True, ascending=False).values, }) ranks['rscore'] = ranks.sum(axis=1) ranks['frank'] = ranks['rscore'].rank(axis=0, method='dense', numeric_only=True, ascending=True) ranks.sort_values(by=['frank'], inplace=True) ds['mrscore'] = float('+inf') ds['mfrank'] = float('+inf') means['rscore'] = float('+inf') means['frank'] = float('+inf') for k, r in ranks[['bn', 'frank']].values: means.loc[means['bn'] == k, ['frank']] = r ds.loc[ds['bn'] == k, ['mfrank']] = r for k, r in ranks[['bn', 'rscore']].values: means.loc[means['bn'] == k, ['rscore']] = r ds.loc[ds['bn'] == k, ['mrscore']] = r means.sort_values(by=['frank'], inplace=True) ds.sort_values(by=['mfrank'], inplace=True) print(means) # exit(1) b0fig, b0ax = plot.htwin_bars( x1=[*reversed(means[['sr']].values)], x2=[*reversed(means[['ss']].values)], y=[*reversed(means['bn'].values)], window=f'test_score_bars_{model}', title= 'Model Tests -- Satisfactions (Wider is better) -- by Rank (Upper-most is Better)', ylabel='Model', x1label='Satisfaction Rate (%)', x2label='Satisfaction Score', x1lims=[0, 1.1], x2lims=[0, 1.1], legend_labels=['Sat. Rate (%)', 'Sat. Score']) b1fig, b1ax = plot.bars( y=means[['s1', 's2', 's3', 's4', 's5', 's6', 's7', 's8']].values, x=means['bn'].values, window=f'test_constraints_bars_{model}', title= 'Model Tests -- Test <i> Satisfaction (%) (Higher is better) -- by Rank (Left-most is Better)', xlabel='Model', ylabel='Test Satisfaction (%)', ylims=[0.0, 1.1], legend_labels=[ 'noise phase 1 -- | a[i] - mean(atm[i]) | < 0.15', 'noise phase 2 -- | a[i] - mean(atm[i]) | < 0.15', 'pt -- fdist - idist < 0.35', 'apt -- fdist - idist > 0.35', f'wpt <= {LEAST_OPT_WPT_SCORE}', f'wapt >= wpt', '|APT| / |PT| < 0.15 (pt)', '|PT| / |APT| < 0.15 (apt)', ]) b2fig, b2ax = plot.bars( y=means[['a0len', 'a1len']].values, x=means['bn'].values, window=f'test_attr_length_bars_{model}', title= 'Model Tests -- Attractors Length -- by Rank (Left-most is Better)', xlabel='Model', ylabel='Attractor <i> length', ylims=[], legend_labels=['a0', 'a1']) # # Model Tests -- Scores distribution # keys = ds['bn'].unique() c = [ 'pt_score', 'apt_score', 'wpt', 'wapt', 'pt_idist', 'apt_idist', 'pt_fdist', 'apt_fdist', 'pt_yrot', 'apt_yrot', 'apt_fyrot', 'pt_apos_x', 'pt_apos_z', 'apt_apos_x', 'apt_apos_z', ] ( pt, apt, wpt, wapt, ptidist, aptidist, ptfdist, aptfdist, ptyrot, aptyrot, aptfyrot, pt_apos_x, pt_apos_z, apt_apos_x, apt_apos_z, ) = ds[['bn'] + c].groupby(by=['bn'], sort=False)[c].agg(list).T.values bp11fig, bp11ax = plot.boxplot( y=wpt, x=keys, window=f'test_wpt_score_boxplot_{model}', title='Model Tests -- wPT scores distribution (Lower is Better)', xlabel='Model', ylabel='wPT Score', ylims=[-0.1e-05, 2.5e-05]) bp12fig, bp12ax = plot.boxplot( y=pt, x=keys, window=f'test_pt_score_boxplot_{model}', title='Model Tests -- PT scores distribution (Lower is Better)', xlabel='Model', ylabel='PT Score', ylims=[-0.1e-05, 2.5e-05]) bp21fig, bp21ax = plot.boxplot( y=wapt, x=keys, window=f'test_wapt_score_boxplot_{model}', title='Model Tests -- wAPT scores distribution (High is Better)', xlabel='Model', ylabel='wAPT Score', ylims=[-0.1e-05, 5e-05]) bp22fig, bp22ax = plot.boxplot( y=apt, x=keys, window=f'test_apt_score_boxplot_{model}', title='Model Tests -- APT scores distribution (High is Better)', xlabel='Model', ylabel='APT Score', ylims=[-0.1e-05, 2.5e-05]) bp31fig, bp31ax = plot.boxplot( y=[[a / b for a, b in zip(__apt, __pt)] for __apt, __pt in zip(apt, pt)], x=keys, window=f'test_apt_pt_score_ratio_boxplot_{model}', title= 'Model Tests -- APT/PT scores ratio distribution (High is Better)', xlabel='Model', ylabel='APT/PT Score Ratio', ylims=[0, 3]) bp32fig, bp32ax = plot.boxplot( y=[[wa / wb for wa, wb in zip(__wapt, __wpt)] for __wapt, __wpt in zip(wapt, wpt)], x=keys, window=f'test_wapt_wpt_score_ratio_boxplot_{model}', title= 'Model Tests -- wAPT/wPT scores ratio distribution (High is Better)', xlabel='Model', ylabel='wAPT/wPT Score Ratio', ylims=[0, 100]) # Model Tests -- rDist distribution # # plotter.show() # # # Model Tests -- Scores | Initial Distance distribution # sp1fig, sp1ax = plot.scatter2d( pt, ptidist, keys, window=f'test_pt_scores_by_iDist_scatter_{model}', title=f'Model Tests -- PT Scores | Initial Distance (m) distribution', xlabel='Initial Distance (m)', ylabel='PT Score', ylims=[-0.1e-05, 2.5e-05]) sp11fig, sp11ax = plot.scatter2d( wpt, ptidist, keys, window=f'test_wpt_scores_by_iDist_scatter_{model}', title=f'Model Tests -- wPT Scores | Initial Distance (m) distribution', xlabel='Initial Distance (m)', ylabel='wPT Score', ylims=[-0.1e-05, 2.5e-05]) # interactive_legend(sp1ax).show() sp2fig, sp2ax = plot.scatter2d( apt, aptidist, keys, window=f'test_apt_scores_by_iDist_scatter_{model}', title=f'Model Tests -- APT Scores | Initial Distance (m) distribution', xlabel='Initial Distance (m)', ylabel='APT Score', ylims=[-0.1e-05, 5e-05]) sp21fig, sp21ax = plot.scatter2d( wapt, aptidist, keys, window=f'test_wapt_scores_by_iDist_scatter_{model}', title=f'Model Tests -- wAPT Scores | Initial Distance (m) distribution', xlabel='Initial Distance (m)', ylabel='wAPT Score', ylims=[-0.1e-05, 5e-05]) # interactive_legend(sp2ax).show() sp3fig, sp3ax = plot.scatter2d( wapt, aptfdist, keys, window=f'test_wapt_scores_by_fDist_scatter_{model}', title=f'Model Tests -- wAPT Scores | Final Distance (m) distribution', xlabel='Final Distance (m)', ylabel='APT Score', ylims=[-0.1e-05, 5e-05]) # interactive_legend(sp3ax).show() # # Model Tests -- Scores | Initial Distance / yRot Distribution # sp3d11fig, sp3d11ax = plot.scatter3d( ptyrot, ptidist, pt, keys, window=f'test_pt_scores_by_iDist_yRot_scatter3d_{model}', title= f'Model Tests -- PT Scores | Initial Distance (m) | yRot (°) Distribution', xlabel='Initial Distance (m)', ylabel='yRot (°)', zlabel='PT Score', zlims=[0, 2.5e-05]) sp3d14fig, sp3d14ax = plot.scatter3d( ptyrot, ptidist, wpt, keys, window=f'test_wpt_scores_by_iDist_yRot_scatter3d_{model}', title= f'Model Tests -- wPT Scores | Initial Distance (m) | yRot (°) Distribution', xlabel='Initial Distance (m)', ylabel='yRot (°)', zlabel='wPT Score', zlims=[0, 2.5e-05]) # interactive_legend(sp3d14ax).show() sp3d12fig, sp3d12ax = plot.scatter3d( ptfdist, ptidist, pt, keys, window=f'test_pt_scores_by_iDist_fDist_scatter3d_{model}', title= f'Model Tests -- PT Scores | Initial Distance (m) | Final Distance (m) Distribution', xlabel='Initial Distance (m)', ylabel='Final Distance (m)', zlabel='PT Score', zlims=[0, 2.5e-05]) # interactive_legend(sp3d12ax).show() sp3d13fig, sp3d13ax = plot.scatter3d( ptfdist, ptidist, wpt, keys, window=f'test_wpt_scores_by_iDist_fDist_scatter3d_{model}', title= f'Model Tests -- wPT Scores | Initial Distance (m) | Final Distance (m) Distribution', xlabel='Initial Distance (m)', ylabel='Final Distance (m)', zlabel='wPT Score', zlims=[0, 2.5e-05]) # interactive_legend(sp3d13ax).show() sp3d21fig, sp3d21ax = plot.scatter3d( aptfdist, aptidist, apt, keys, window=f'test_apt_scores_by_iDist_yrot_scatter3d_{model}', title= f'Model Tests -- APT Scores | Initial Distance (m) | Final Distance (m) Distribution', xlabel='Initial Distance (m)', ylabel='Final Distance (m)', zlabel='APT Score', zlims=[0, 2.5e-05]) sp3d23fig, sp3d23ax = plot.scatter3d( aptyrot, aptidist, apt, keys, window=f'test_apt_scores_by_iDist_fDist_scatter3d_{model}', title= f'Model Tests -- APT Scores | Initial Distance (m) | yRot (°) Distribution', xlabel='Initial Distance (m)', ylabel='yRot (°)', zlabel='APT Score', zlims=[0, 2.5e-05]) # interactive_legend(sp3d21ax).show() sp3d22fig, sp3d22ax = plot.scatter3d( y=aptfdist, x=aptidist, z=wapt, k=keys, window=f'test_wapt_scores_by_iDist_fDist_scatter3d_{model}', title= f'Model Tests -- wAPT Scores | Initial Distance (m) | Final Distance (m) Distribution', xlabel='Initial Distance (m)', ylabel='Final Distance (m)', zlabel='wAPT Score', zlims=[0, 5e-05]) sp3d24fig, sp3d24ax = plot.scatter3d( y=aptyrot, x=aptidist, z=wapt, k=keys, window=f'test_wapt_scores_by_iDist_yrot_scatter3d_{model}', title= f'Model Tests -- wAPT Scores | Initial Distance (m) | yRot (°) Distribution', xlabel='Initial Distance (m)', ylabel='yRot (°)', zlabel='wAPT Score', zlims=[0, 5e-05]) sp3d25fig, sp3d25ax = plot.scatter3d( y=aptfyrot, x=aptfdist, z=wapt, k=keys, window=f'test_wapt_scores_by_fDist_fyrot_scatter3d_{model}', title= f'Model Tests -- wAPT Scores | Final Distance (m) | Final yRot (°) Distribution', xlabel='Final Distance (m)', ylabel='Final yRot (°)', zlabel='wAPT Score', zlims=[0, 5e-05]) # sp4d1fig, sp4d1ax = plot.scatter3d( # y=pt_apos_z, # x=pt_apos_x, # z=ptyrot, # # w=wpt, # k=keys, # window=f'test_wpt_scores_by_idist_fDist_yrot_scatter4d_{model}', # title=f'Model Tests -- wPT Scores | Initial Distance (m) | Final Distance (m) | yRot (°) Distribution', # xlabel='Initial Distance (m)', # ylabel='yRot (°)', # zlabel='Final Distance', # zlims=[0, 360] # ) # interactive_legend(sp4d1ax).show() # plotter.show() # exit(1) return model, [ (b0fig, b0ax), (b1fig, b1ax), (b2fig, b2ax), (bp11fig, bp11ax), (bp12fig, bp12ax), (bp21fig, bp21ax), (bp22fig, bp22ax), (bp31fig, bp31ax), (bp32fig, bp32ax), (sp1fig, sp1ax), (sp11fig, sp11ax), (sp2fig, sp2ax), (sp21fig, sp21ax), (sp3fig, sp3ax), (sp3d11fig, sp3d11ax), (sp3d12fig, sp3d12ax), (sp3d13fig, sp3d13ax), (sp3d14fig, sp3d14ax), (sp3d21fig, sp3d21ax), (sp3d22fig, sp3d22ax), (sp3d23fig, sp3d23ax), (sp3d24fig, sp3d24ax), (sp3d25fig, sp3d25ax), # (sp4d1fig, sp4d1ax), ]
def to_deg(x): __x = flat([x]) m = sum(__x) / max(1, len(__x)) return phase_zero_360(np.rad2deg(x))
############################################################## NP = cpu_count() pool = Pool(processes=NP) mapper = lambda f, p: pool.imap_unordered(f, p, chunksize=2*NP) ############################################################## toiter = lambda x: x if isinstance(x, (list, tuple)) else list([x]) Ns, Ks, Ps, Qs, Is, Os = tuple(map(toiter, GLOBALS.bn_params)) aNs = flat(toiter(GLOBALS.slct_target_n_attractors)) tTaus = flat(toiter(GLOBALS.slct_target_transition_tau)) nRhos = flat(toiter(GLOBALS.slct_noise_rho)) iPhis = flat(toiter(GLOBALS.slct_input_steps_phi)) params = itertools.product(Ns, Ks, Ps, Qs, Is, Os, aNs, tTaus, nRhos, iPhis) FOLDER = get_dir(GLOBALS.bn_model_path, create_if_dir=True) for N, K, P, Q, I, O, aN, tTau, nRho, iPhi in params: GLOBALS.bn_params = N, K, P, Q, I, O if not isinstance(tTau, dict): GLOBALS.slct_target_transition_tau = { "a0": {"a0": tTau, "a1": tTau},