Пример #1
0
    def dump_data(self, path: str or Path) -> str:
        
        if check_path(path, create_if_file=True) != 0:
            raise Exception(f"Invalid simulation data dump path:\n\t{path}...")

        write_json(vars(self), path)

        self.clear()
Пример #2
0
def run_simulation(config: Config, bn: OpenBooleanNetwork) -> dict:

    # Save model (inside or outside of the config? mumble rumble)
    write_json(bn.to_json(), config.bn_model_path)  # BN Model
    write_json(config.to_json(), config.sim_config_path,
               indent=True)  # Simulation Configuration

    # Run Webots
    proc_closure = subprocess.run([
        str(config.webots_path), *config.webots_launch_opts,
        str(config.webots_world_path)
    ])

    return proc_closure
Пример #3
0
def save_subopt_model(path: Path,
                      score: object,
                      it: int,
                      bn: dict,
                      save_subopt=False):

    bn.update({'stats': dict(score=score, it=it)})

    model_dir = get_dir(path)

    if save_subopt:
        # Save only if <sd_save_suboptimal_models> >= score
        write_json(
            bn, model_dir / GLOBALS.app['subopt_model_name'].format(
                date=FROZEN_DATE, it=GLOBALS.app['it_suffix'].format(it=it)))

    # Always save the last suboptimal model (overwrite)
    write_json(
        bn,
        model_dir / GLOBALS.app['last_model_name'].format(date=FROZEN_DATE))
Пример #4
0
def generate_or_load_bn(params: BNParams, path: Path, save_virgin=False):

    __bn = None

    if check_path(path, create_if_dir=True):

        generator = template_behaviour_generator(*params)

        __bn = generate_rbn(generator.new_obn, force_consistency=True)

        if save_virgin:
            p = path / 'virgin_bn_{date}.json'.format(date=FROZEN_DATE)

            write_json(__bn.to_json(), p)

            logger.info(f'Virgin BN saved to {p}.')

    else:
        __bn = OpenBooleanNetwork.from_json(read_json(path))
        logger.info(f'BN loaded from {path}.')

    return __bn
Пример #5
0
 def to_file(self, fp: Path or str):
     write_json(self.to_json(), fp, indent=True)
Пример #6
0
    @property
    def bn_params(self):
        return BNParams(
            N=self.bn_n,
            K=self.bn_k,
            P=self.bn_p,
            Q=self.bn_q,
            I=self.bn_n_inputs,
            O=self.bn_n_outputs
        )
    
    @bn_params.setter
    def bn_params(self, params: BNParams or tuple):
        
        N, K, P, Q, I, O = params

        self.bn_n = N
        self.bn_k = K
        self.bn_p = P
        self.bn_q = Q
        self.bn_n_inputs = I 
        self.bn_n_outputs = O

if __name__ == "__main__":
    
    import os

    print(os.getcwd())

    write_json(Config(), os.getcwd() + '/template.json', indent=True)
Пример #7
0
    ### BN Generation / Loading ####################################################

    bn = generate_or_load_bn(params=GLOBALS.bn_params,
                             path=GLOBALS.bn_model_path,
                             save_virgin=True)

    ### Launch search algorithm ##############################################

    if not GLOBALS.train_generate_only:

        t = time.perf_counter()

        bn, ctx = GLOBALS.app_core_function(bn)

        logger.info(f"Search time: {time.perf_counter() - t}s")

        logger.info(ctx)

        savepath = GLOBALS.bn_model_path / 'behavioural_bn_{date}.json'.format(
            mode=GLOBALS.app['mode'], date=FROZEN_DATE)

        write_json(bn, savepath)

        logger.info(f'Output model saved to {savepath}.')

    logger.info('Closing...')

    logger.flush()

    exit(1)
Пример #8
0
        while bn is None or not bn.attractors_input_map or None in bn.attractors_input_map:
            logger.info('Failure. Retrying...')
            t = time.perf_counter()
            bn = GLOBALS.app_core_function()
            logger.info(time.perf_counter() - t)

        logger.info(dict(**bn.attractors_input_map))
        logger.info(dict(**bn.atm.dtableau))
        logger.info(dict(**bn.atm.dattractors))

        path = FOLDER / f'selective_bn_{iso8106(ms=3)}.json'

        bnjson = bn.to_json()

        bnjson['gen_params'] = dict(
            list(zip(
                ["N", "K", "P", "Q", "I", "O", "tTau", "nRho"],
                [N, K, P, Q, I, O, tTau, nRho]
            ))
        )

        write_json(bnjson, path, indent=True)

        logger.info(f'BN Selector saved in {path}.\n')

    logger.flush()
    
    exit(1)


Пример #9
0
            it = max(map(len, bn.atm.attractors)) * len(bn) * 20

            c1 = constraints.test_attractors_number(bn, nA)
            c2 = constraints.test_attractors_transitions(
                bn, tTau_map)  # if len(bn.atm.attractors) > 1 else False
            c2no1 = constraints.test_attractors_transitions(
                bn, tTau_map) if len(bn.atm.attractors) > 1 else False
            c3 = constraints.test_bn_state_space_homogeneity(bn, it, nRho)
            c4 = bool(constraints.test_attraction_basins(bn, isPhi))

            files.append(
                mpath /
                f'{i}_bns_n{N}_k{K}_nrho{int(nRho*100)}_ttau{int(tTau*100)}_iphi{isPhi}_{FROZEN_DATE}.json'
            )

            write_json(bn.to_json(), files[-1])

            c1s.append(c1)
            c2s.append(c2)
            c2no1s.append(c2no1)
            c3s.append(c3)
            c4s.append(c4)

        DataFrame({
            'bn': list(map(lambda p: p.name, files)),
            'c1': c1s,
            'c2': c2s,
            'c2no1': c2no1s,
            'c3': c3s,
            'c4': c4s
        }).to_json(