def test_compare_maxweight_vs_lp_maxweight_double_reentrant_line_model( self): seed = 42 np.random.seed(seed) initial_state = 50 * np.ones((5, 1)) env = examples.double_reentrant_line_model( alpha=1, mu1=4, mu2=3, mu3=2, mu4=3, mu5=4, cost_per_buffer=np.array([1, 1, 1, 1, 1])[:, None], initial_state=initial_state, capacity=np.ones((5, 1)) * np.inf, job_conservation_flag=True, job_gen_seed=seed, max_episode_length=None) mw_agent = mw.MaxWeightAgent(env) mw_lp_agent = mw_lp.MaxWeightLpAgent(env) states = np.random.randint(50, size=(400, 5)) for s in states: z_star_mw_lp, _ = mw_lp_agent.max_weight_policy(s[:, None]) z_star_mw = mw_agent.max_weight_policy(s[:, None]) self.assert_both_mw_policies_are_equivalent( s, env.cost_per_buffer, env.job_generator.buffer_processing_matrix, z_star_mw_lp, z_star_mw)
def perform_test_max_weight_binary_policy(state, env, z_star_theory, method, binary_action): agent = mw_lp.MaxWeightLpAgent(env, method=method, binary_action=binary_action) z_star, _ = agent.max_weight_policy(state) equal = 0 for z in z_star_theory: if np.allclose(z, z_star): equal = equal + 1 assert equal == 1
def test_compare_maxweight_vs_maxweight_scheduling(self): seed = 42 np.random.seed(seed) initial_state = 50 * np.ones((5, 1)) env = examples.double_reentrant_line_model(initial_state=initial_state, job_gen_seed=seed) sch_mw_agent = sch_mw.SchedulingMaxWeightAgent(env) mw_agent = mw_lp.MaxWeightLpAgent(env) states = np.random.randint(50, size=(400, 5)) for s in states: z_star_mw, _ = mw_agent.max_weight_policy(s[:, None]) z_star_sch_mw = sch_mw_agent.scheduling_max_weight_policy(s[:, None]) self.assert_both_mw_policies_are_equivalent( s, env.cost_per_buffer, env.job_generator.buffer_processing_matrix, z_star_mw, z_star_sch_mw)
def test_integration_double_reentrant_line_model(self): seed = 42 np.random.seed(seed) initial_state = 50 * np.ones((5, 1)) env = examples.double_reentrant_line_model( alpha=1, mu1=4, mu2=3, mu3=2, mu4=3, mu5=4, cost_per_buffer=np.array([1, 1, 1, 1, 1])[:, None], initial_state=initial_state, capacity=np.ones((5, 1)) * np.inf, job_conservation_flag=True, job_gen_seed=seed, max_episode_length=None) agent = mw_lp.MaxWeightLpAgent(env) simulator = ps.SncSimulator(env, agent, discount_factor=0.95) data = simulator.run(num_simulation_steps=1000) assert np.all(data['state'][-1] < initial_state)
def test_compare_maxweight_vs_lp_maxweight_simple_routing_model(self): seed = 42 np.random.seed(seed) env = examples.simple_routing_model(alpha_r=0.2, mu1=0.13, mu2=0.07, mu_r=0.2, cost_per_buffer=np.ones((3, 1)), initial_state=(1, 1, 1), capacity=np.ones((3, 1)) * np.inf, job_conservation_flag=True, job_gen_seed=seed, max_episode_length=None) mw_agent = mw.MaxWeightAgent(env) mw_lp_agent = mw_lp.MaxWeightLpAgent(env) states = np.random.randint(50, size=(400, 3)) for s in states: z_star_mw_lp, _ = mw_lp_agent.max_weight_policy(s[:, None]) z_star_mw = mw_agent.max_weight_policy(s[:, None]) self.assert_both_mw_policies_are_equivalent( s, env.cost_per_buffer, env.job_generator.buffer_processing_matrix, z_star_mw_lp, z_star_mw)
def perform_test_max_weight_policy(state, env, z_star_theory): agent = mw_lp.MaxWeightLpAgent(env) z_star, _ = agent.max_weight_policy(state) np.testing.assert_almost_equal(z_star, z_star_theory)