def run(self): # Workload selected_workload: EDefaultYCSBWorkload = EDefaultYCSBWorkload.READ_10000_100000_LATEST_LARGE if self._config.light: selected_workload: EDefaultYCSBWorkload = EDefaultYCSBWorkload.READ_10000_100000_LATEST # Config registry_config = os.path.realpath(self._config.config) """Path to MDDE Registry configuration file.""" # Temp dir mdde_temp_dir = os.path.realpath(self._config.env_temp_dir) """Path to a folder where MDDE should store its temporary files.""" os.makedirs(os.path.abspath(mdde_temp_dir), exist_ok=True) # Result paths result_dir_path_root = Path(self._config.result_dir).resolve() result_dir_path_mdde_obj = result_dir_path_root.joinpath("mdde") result_dir_path_mdde_obj.mkdir(parents=True, exist_ok=True) result_dir_path_mdde = str(result_dir_path_mdde_obj) """Path to the folder where MDDE would output the result.""" mdde_config = ConfigEnvironment(tmp_dir=mdde_temp_dir, result_dir=result_dir_path_mdde) # Registry configuration config_container = ConfigRegistry() config_container.read(registry_config) # Create agents agents = list() idx = 0 for node in config_container.get_nodes(): agents.append( SingleNodeDefaultAgent( agent_name=node.id, agent_id=idx, data_node_id=node.id, write_stats=True, allow_do_nothing=self._config.do_nothing)) idx += 1 # Create scenario num_fragments: int = self._config.n_frags write_stats: bool = True if self._config.sim: scenario = DefaultScenarioSimulation( num_fragments=num_fragments, num_steps_before_bench=self._config.bench_psteps, agents=agents, benchmark_clients=self._config.bench_clients, data_gen_workload=selected_workload, bench_workload=selected_workload, write_stats=write_stats) # Number of YCSB threads else: scenario = DefaultScenario( num_fragments=num_fragments, num_steps_before_bench=self._config.bench_psteps, agents=agents, benchmark_clients=self._config.bench_clients, data_gen_workload=selected_workload, bench_workload=selected_workload, write_stats=write_stats) # Number of YCSB threads # Set multiplier to the sore related term of the default reward function scenario.set_storage_importance(self._config.store_m) # Set ho much do-nothing worth scenario.set_do_nothing_worth(self._config.dn_worth) def obs_shaper_2d_box(obs): """Reshapes the environment into a form suitable for 2D box. Example 1. Note: Guaranteed to work only with the Default agent - Default scenario combination.""" # Resulted shape (Example for default scenario and default single-node agent: 2 agents, 5 fragments): # a_1: [0-4(allocation) 5-9(popularity) 10-14(ownership binary flag)] # a_2: [0-4(allocation) 5-9(popularity) 10-14(ownership binary flag)] # Hint: 2D array where rows are agents, and attributes in columns are as shown above. return obs.reshape((obs.shape[0], obs.shape[1] * obs.shape[2]), order='F') def obs_shaper_flat_box(obs): """Reshapes the environment into a form suitable for 2D 'flat' box. Example 2. Note: Guaranteed to work only with the Default agent - Default scenario combination.""" # Resulted shape (Example for default scenario and default single-node agent: 2 agents, 5 fragments): # [0-4(a_1: allocation) 5-9(a_1: popularity) 10-14(a_1: ownership binary flag) # 15-19(a_2: allocation) 20-24(a_2: popularity) 25-29(a_2: ownership binary flag)] return obs.reshape((obs.shape[0], obs.shape[1] * obs.shape[2]), order='F')\ .reshape((obs.shape[0] * obs.shape[1] * obs.shape[2]), order='C') maac_run(config=self._config, mdde_config=mdde_config, scenario=scenario, write_stats=True, observation_shaper=obs_shaper_flat_box)
def test_initialization(self): config_container = ConfigRegistry() config_container.read(self.TEST_CONFIG_FILE) for node in config_container.get_nodes(): print("{} | {}".format(node.id, node.default))
def make_env(host: str, port: int, reg_config: str, env_config: ConfigEnvironment, write_stats: bool, initial_benchmark: bool = False) -> Environment: """ Configure MDDE environment to run default. :param host: MDDE registry host or IP. :param port: MDDE registry control port. :param reg_config: Path to MDDE registry config. :param env_config: Environment configuration object. :param write_stats: True to write additional analytics info. :param initial_benchmark: Execute benchmark immediately upon execution. :param do_nothing: Enable or disable the agents' "do_nothing" action. :return: MDDE Environment. """ # Ray is peculiar in the way it handles environments, passing a pre-configured environment might cause # unexpected behavior. Customize the code of this extension if more complex environment are needed # Create Registry client tcp_client = RegistryClientTCP(host, port, keep_open=True) read_client: PRegistryReadClient = tcp_client write_client: PRegistryWriteClient = tcp_client ctrl_client: PRegistryControlClient = tcp_client # Registry configuration config_container = ConfigRegistry() config_container.read(reg_config) # Create agents agents = list() idx = 0 for node in config_container.get_nodes(): agents.append( SingleNodeDefaultAgent(agent_name=node.id, agent_id=idx, data_node_id=node.id, write_stats=write_stats, allow_do_nothing=True)) idx += 1 # Create scenario scenario = DefaultScenarioSimulation( num_fragments=self.NUM_FRAGMENTS, num_steps_before_bench=config.bench_psteps, agents=agents, data_gen_workload=workload, bench_workload=workload, benchmark_clients=config.bench_clients, write_stats=write_stats) # Number of YCSB threads # Create environment environment = Environment(config=env_config, scenario=scenario, registry_ctrl=ctrl_client, registry_write=write_client, registry_read=read_client, write_stats=write_stats, write_obs_at_bench=False) # Re-generate data environment.initialize_registry(with_benchmark=initial_benchmark) return environment
def test_initialization(self): # Scenario config agents_path = '../../debug/agents' mdde_config = ConfigEnvironment(agents_path) os.makedirs(os.path.abspath(agents_path), exist_ok=True) # Create Registry client tcp_client = RegistryClientTCP(self.REGISTRY_HOST, self.REGISTRY_PORT, keep_open=False) read_client: PRegistryReadClient = tcp_client write_client: PRegistryWriteClient = tcp_client ctrl_client: PRegistryControlClient = tcp_client # Create agents config_container = ConfigRegistry() config_container.read(self.TEST_CONFIG_FILE) agents = list() idx = 0 for node in config_container.get_nodes(): agents.append(SingleNodeDefaultAgent(node.id, idx, node.id)) idx += 1 # Create scenario scenario = DefaultScenario( num_fragments=20, num_steps_before_bench=5, agents=agents, benchmark_clients=5, data_gen_workload=EDefaultYCSBWorkload.READ_10000_1000_LATEST, bench_workload=EDefaultYCSBWorkload.READ_10000_1000_LATEST) env_config = ConfigEnvironment(tmp_dir='../../debug/debug/temp', result_dir='../../debug/debug/result') # Create environment environment = Environment(config=env_config, scenario=scenario, registry_ctrl=ctrl_client, registry_write=write_client, registry_read=read_client, write_stats=True) # Re-generate data environment.initialize_registry(with_benchmark=True) # Reset observations = environment.reset() # Retrieve observation and action spaces osb, act_l = environment.observation_space act: Dict[int, int] = environment.action_space # Run benchmark environment.benchmark() # Make few totally legal steps: for i in range(0, 10): osb, act_l = environment.observation_space action_n = {} for k, v in act_l.items(): legal_idx_pool = [] for l_idx, l_act in np.ndenumerate(v): if l_act > 0: legal_idx_pool.append(l_idx[0]) action_n[k] = random.choice(legal_idx_pool) obs_s, reward_s, done, act_l = environment.step(action_n=action_n) # Make steps for i in range(0, 1000): osb, act_l = environment.observation_space action_n = {} for k, v in act.items(): action_n[k] = random.randrange(0, v - 1) obs_s, reward_s, done, act_l = environment.step(action_n=action_n) # Run benchmark environment.benchmark() # Reset reset = environment.reset() # Run benchmark environment.benchmark() # Make steps for i in range(0, 1000): osb = environment.observation_space action_n = {} for k, v in act.items(): action_n[k] = random.randrange(0, v - 1) obs_s, reward_s, done, act_l = environment.step(action_n=action_n) # Run benchmark environment.benchmark()