Exemplo n.º 1
0
def append_configs(sim_configs={},
                   initial_state={},
                   seeds={},
                   raw_exogenous_states={},
                   env_processes={},
                   partial_state_update_blocks={},
                   policy_ops=[lambda a, b: a + b],
                   _exo_update_per_ts: bool = True) -> None:
    if _exo_update_per_ts is True:
        exogenous_states = exo_update_per_ts(raw_exogenous_states)
    else:
        exogenous_states = raw_exogenous_states

    if isinstance(sim_configs, dict):
        sim_configs = [sim_configs]

    for sim_config in sim_configs:
        config = Configuration(
            sim_config=sim_config,
            initial_state=initial_state,
            seeds=seeds,
            exogenous_states=exogenous_states,
            env_processes=env_processes,
            partial_state_update_blocks=partial_state_update_blocks,
            policy_ops=policy_ops)
        configs.append(config)
Exemplo n.º 2
0
def append_configs(
        user_id='cadCAD_user',
        session_id=0,  #ToDo: change to string
        sim_configs={},
        initial_state={},
        seeds={},
        raw_exogenous_states={},
        env_processes={},
        partial_state_update_blocks={},
        policy_ops=[lambda a, b: a + b],
        _exo_update_per_ts: bool = True,
        config_list=configs) -> None:
    if _exo_update_per_ts is True:
        exogenous_states = exo_update_per_ts(raw_exogenous_states)
    else:
        exogenous_states = raw_exogenous_states

    if isinstance(sim_configs, dict):
        sim_configs = [sim_configs]

    simulation_id = 0
    if len(config_list) > 0:
        last_config = config_list[-1]
        simulation_id = last_config.simulation_id + 1

    sim_cnt = 0
    new_sim_configs = []
    for t in list(zip(sim_configs, list(range(len(sim_configs))))):
        sim_config = t[0]
        N = sim_config['N']

        if N > 1:
            for n in range(N):
                sim_config['simulation_id'] = simulation_id + sim_cnt
                sim_config['run_id'] = n
                sim_config['N'] = 1
                # sim_config['N'] = n + 1
                new_sim_configs.append(deepcopy(sim_config))
            del sim_config
        else:
            sim_config['simulation_id'] = simulation_id
            sim_config['run_id'] = 0
            new_sim_configs.append(deepcopy(sim_config))

        sim_cnt += 1

    # for sim_config in sim_configs:
    for sim_config in new_sim_configs:
        config = Configuration(
            sim_config=sim_config,
            initial_state=initial_state,
            seeds=seeds,
            exogenous_states=exogenous_states,
            env_processes=env_processes,
            partial_state_update_blocks=partial_state_update_blocks,
            policy_ops=policy_ops,

            # session_id=session_id,
            user_id=user_id,
            session_id=
            f"{user_id}={sim_config['simulation_id']}_{sim_config['run_id']}",
            simulation_id=sim_config['simulation_id'],
            run_id=sim_config['run_id'])
        configs.append(config)
Exemplo n.º 3
0
    def append_configs(self,
                       user_id='cadCAD_user',
                       sim_configs={},
                       initial_state={},
                       seeds={},
                       raw_exogenous_states={},
                       env_processes={},
                       partial_state_update_blocks={},
                       policy_ops=[lambda a, b: a + b],
                       _exo_update_per_ts: bool = True,
                       config_list=configs) -> None:

        try:
            max_runs = sim_configs[0]['N']
        except KeyError:
            max_runs = sim_configs['N']

        if _exo_update_per_ts is True:
            exogenous_states = exo_update_per_ts(raw_exogenous_states)
        else:
            exogenous_states = raw_exogenous_states

        if isinstance(sim_configs, dict):
            sim_configs = [sim_configs]

        simulation_id = 0
        if len(config_list) > 0:
            last_config = config_list[-1]
            simulation_id = last_config.simulation_id + 1

        sim_cnt = 0
        new_sim_configs = []
        for subset_id, t in enumerate(
                list(zip(sim_configs, list(range(len(sim_configs)))))):
            sim_config = t[0]
            sim_config['subset_id'] = subset_id
            sim_config['subset_window'] = self.subset_window
            N = sim_config['N']
            if N > 1:
                for n in range(N):
                    sim_config['simulation_id'] = simulation_id + sim_cnt
                    sim_config['run_id'] = n
                    sim_config['N'] = 1
                    # sim_config['N'] = n + 1
                    new_sim_configs.append(deepcopy(sim_config))
                del sim_config
            else:
                sim_config['simulation_id'] = simulation_id
                sim_config['run_id'] = 0
                new_sim_configs.append(deepcopy(sim_config))
                # del sim_config

            sim_cnt += 1

        run_id = 0
        for sim_config in new_sim_configs:
            subset_id = sim_config['subset_id']
            sim_config['N'] = run_id + 1
            if max_runs == 1:
                sim_config['run_id'] = run_id
            elif max_runs >= 1:
                if run_id >= max_runs:
                    sim_config['N'] = run_id - (max_runs - 1)

            self.exp_window = deepcopy(self.exp_window)
            config = Configuration(
                sim_config=sim_config,
                initial_state=initial_state,
                seeds=seeds,
                exogenous_states=exogenous_states,
                env_processes=env_processes,
                partial_state_update_blocks=partial_state_update_blocks,
                policy_ops=policy_ops,

                # session_id=session_id,
                user_id=user_id,
                session_id=
                f"{user_id}={sim_config['simulation_id']}_{sim_config['run_id']}",
                simulation_id=sim_config['simulation_id'],
                run_id=sim_config['run_id'],
                experiment_id=self.exp_id,
                exp_window=self.exp_window,
                subset_id=subset_id,
                subset_window=self.subset_window)
            configs.append(config)
            run_id += 1
        self.exp_id += 1
        self.exp_window.appendleft(self.exp_id)
Exemplo n.º 4
0
    def append_model(
            self,
            user_id='cadCAD_user',
            model_id=None,
            sim_configs={}, initial_state={}, seeds={}, raw_exogenous_states={}, env_processes={},
            partial_state_update_blocks={}, policy_ops=[lambda a, b: a + b], _exo_update_per_ts: bool = True, **kwargs
            # config_list=deepcopy(global_configs)
    ) -> None:
        _sim_configs = deepcopy(sim_configs)
        # self.configs = config_list
        self.simulation_id += 1

        try:
            max_runs = _sim_configs[0]['N']
        except KeyError:
            max_runs = _sim_configs['N']

        if _exo_update_per_ts is True:
            exogenous_states = exo_update_per_ts(raw_exogenous_states)
        else:
            exogenous_states = raw_exogenous_states

        if isinstance(_sim_configs, dict):
            _sim_configs = [_sim_configs]

        sim_cnt_local = 0
        new_sim_configs = []
        for subset_id, t in enumerate(list(zip(_sim_configs, list(range(len(_sim_configs)))))):
            sim_config = t[0]
            sim_config['subset_id'] = subset_id
            sim_config['subset_window'] = self.subset_window
            N = sim_config['N']
            if N > 1:
                for n in range(N):
                    sim_config['simulation_id'] = self.simulation_id
                    sim_config['run_id'] = n
                    sim_config['N'] = 1
                    new_sim_configs.append(deepcopy(sim_config))
                del sim_config
            else:
                sim_config['simulation_id'] = self.simulation_id
                sim_config['run_id'] = 0
                new_sim_configs.append(deepcopy(sim_config))

            sim_cnt_local += 1

        if model_id == None:
            new_model_id = str(len(self.model_ids))
            if new_model_id in self.model_ids:
                model_id = f"model@{len(self.model_ids)}"
            else:
                model_id = str(new_model_id)
        elif model_id != None:
            if model_id in self.model_ids:
                model_id = f"{model_id}@{len(self.model_ids)}"
            else:
                model_id = str(model_id)

        run_id = 0
        new_model_ids, new_configs = [], []
        for sim_config in new_sim_configs:
            subset_id = sim_config['subset_id']
            sim_config['N'] = run_id + 1
            if max_runs == 1:
                sim_config['run_id'] = run_id
            elif max_runs >= 1:
                if run_id >= max_runs:
                    sim_config['N'] = run_id - (max_runs - 1)

            self.exp_window = deepcopy(self.exp_window)
            config = Configuration(
                exp_creation_ts=self.exp_creation_ts,

                sim_config=sim_config,
                initial_state=initial_state,
                seeds=seeds,
                exogenous_states=exogenous_states,
                env_processes=env_processes,
                partial_state_update_blocks=partial_state_update_blocks,
                policy_ops=policy_ops,

                # session_id=session_id,
                user_id=user_id,
                model_id=model_id,
                session_id=f"{user_id}={sim_config['simulation_id']}_{sim_config['run_id']}",

                experiment_id=self.exp_id,
                simulation_id=self.simulation_id,
                subset_id=subset_id,
                run_id=sim_config['run_id'],

                exp_window=self.exp_window,
                subset_window=self.subset_window
            )

            # self.configs.append(config)
            new_configs.append(config)
            new_model_ids.append(model_id)
            run_id += 1
        self.configs += new_configs
        self.model_id_queue += new_model_ids
        self.exp_id += 1
        self.exp_window.appendleft(self.exp_id)
        self.sys_configs += configs_as_objs(new_configs)

        unique_new_model_ids = list(set(new_model_ids))
        new_model_job_list = [(model_id, []) for model_id in unique_new_model_ids]
        for model_id, v in new_model_job_list:
            if model_id not in self.model_ids:
                self.model_job_map[model_id] = v
                self.model_ids.append(model_id)
            else:
                except_str = f"""
                    Error: Duplicate model_id in Experiment - \'{model_id}\' in {self.model_ids}
                    -- Specify unique model_id for each use of `.append_model` per `Experiment()`
                """
                raise Exception(except_str)

        for model_id, job in list(zip(new_model_ids, new_configs)):
            self.model_job_map[model_id].append(job)

        self.model_job_counts = dict([(k, len(v)) for k, v in self.model_job_map.items()])