def generate_eval_envs(eval_env: OnlineFlexibleResourceAllocationEnv, num_evals: int, folder: str, overwrite: bool = False) -> List[str]: """ Generates and saves the evaluation environment used for evaluating training of the agents Args: eval_env: The evaluation environment used to generate the files num_evals: The number of environments generated folder: The folder where the environments are generated overwrite: If to overwrite previous environments saved Returns: A list of environment file paths """ if not os.path.exists(folder): os.makedirs(folder) eval_files = [] for eval_num in range(num_evals): eval_file = f'{folder}/eval_{eval_num}.env' eval_files.append(eval_file) if overwrite or not os.path.exists(eval_file): eval_env.reset() eval_env.save_env(eval_file) return eval_files
def test_env_save_load(): # TODO add comments env = OnlineFlexibleResourceAllocationEnv('env/settings/basic.env') state = env.reset() random_task_pricing = RandomTaskPricingAgent(0) random_resource_weighting = RandomResourceWeightingAgent(0) for _ in range(40): if state.auction_task is not None: actions = { server: random_task_pricing.bid(state.auction_task, tasks, server, state.time_step) for server, tasks in state.server_tasks.items() } else: actions = { server: random_resource_weighting.weight(tasks, server, state.time_step) for server, tasks in state.server_tasks.items() } state, rewards, done, info = env.step(actions) env.save_env('env/settings/tmp/save.env') loaded_env, loaded_env_state = env.load_env('env/settings/tmp/save.env') assert state.auction_task == loaded_env_state.auction_task assert len(env._unallocated_tasks) == len(loaded_env._unallocated_tasks) for task, loaded_task in zip(env._unallocated_tasks, loaded_env._unallocated_tasks): assert task == loaded_task for server, tasks in state.server_tasks.items(): loaded_server, loaded_tasks = next(((loaded_server, loaded_tasks) for loaded_server, loaded_tasks in state.server_tasks.items() if loaded_server.name == server.name), (None, None)) assert loaded_server is not None and loaded_tasks is not None assert server.name == loaded_server.name and server.storage_cap == loaded_server.storage_cap and \ server.computational_cap == loaded_server.computational_cap and \ server.bandwidth_cap == loaded_server.bandwidth_cap for task, loaded_task in zip(tasks, loaded_tasks): assert task.name == loaded_task.name and task.required_storage == loaded_task.required_storage and \ task.required_computation == loaded_task.required_computation and \ task.required_results_data == loaded_task.required_results_data and \ task.auction_time == loaded_task.auction_time and task.deadline == loaded_task.deadline and \ task.stage is loaded_task.stage and task.loading_progress == loaded_task.loading_progress and \ task.compute_progress == loaded_task.compute_progress and \ task.sending_progress == loaded_task.sending_progress and task.price == loaded_task.price task.assert_valid() loaded_env.save_env('env/settings/tmp/loaded_save.env') with open('env/settings/tmp/save.env') as env_file: env_file_data = env_file.read() with open('env/settings/tmp/loaded_save.env') as loaded_env_file: loaded_env_file_data = loaded_env_file.read() assert env_file_data == loaded_env_file_data