def test_return_workload_vectors_based_on_medium_load_threshold_simple_routing_model(): """Example 6.2.2. from CTCN online, Example 6.2.4 from printed version. Returns workload vectors that correspond with threshold > 0.6.""" alpha_r = 0.18 mu1 = 0.13 mu2 = 0.07 mu_r = 0.3 env = envex.simple_routing_model(alpha_r, mu1, mu2, mu_r) # Compute load, workload and sensitivity vectors sorted by load load_threshold = 0.6 load, workload, nu = wl.compute_load_workload_matrix(env, load_threshold=load_threshold) # Compare results with theoretical result from CTCN book for load >= value. load_theory = np.array([alpha_r / (mu1 + mu2), alpha_r / mu_r]) workload_theory = np.vstack((1 / (mu1 + mu2) * np.ones(env.num_buffers), [0, 0, 1/mu_r])) nu_theory = np.array([[mu1 / (mu1 + mu2), mu2 / (mu1 + mu2), 0], [0, 0, 1]]) np.testing.assert_almost_equal(load, load_theory, decimal=6) np.testing.assert_almost_equal(workload, workload_theory, decimal=6) np.testing.assert_almost_equal(nu, nu_theory, decimal=6)
def test_compare_maxweight_vs_maxweight_scheduling_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) smw_agent = smw.SchedulingMaxWeightAgent(env) mw_agent = mw.MaxWeightAgent(env) states = np.random.randint(50, size=(400, 3)) for s in states: z_star_mw = mw_agent.max_weight_policy(s[:, None]) z_star_smw = smw_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_smw)
def test_physical_resources_to_workload_simple_routing_model(): # For this model, there should be a pooled resource 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=42) nu = wl.compute_load_workload_matrix(env=env, num_wl_vec=None, load_threshold=None, feasible_tol=1e-10).nu phys_resources_to_wl_index = validation_utils.workload_to_physical_resources_index(nu) assert np.all(np.array([[2], [0, 1], [1], [0]]) == phys_resources_to_wl_index)
def test_compute_network_load_and_workload_simple_routing_model(): """Example 6.2.2. from CTCN online, Example 6.2.4 from printed version.""" alpha_r = 0.18 mu1 = 0.13 mu2 = 0.07 mu_r = 0.3 env = envex.simple_routing_model(alpha_r, mu1, mu2, mu_r) # Compute load, workload and sensitivity vectors sorted by load load, workload, nu = wl.compute_load_workload_matrix(env) # Compute network load and associated workload and sensitivity vectors. Useful to validate that # we obtain the same results with two different methods. network_load, xi_bottleneck, nu_bottleneck, constraints \ = alt_methods_test.compute_network_load_and_bottleneck_workload(env) # Third method of computing the network load: network_load_bis = alt_methods_test.compute_network_load(env) # Compare the two methods of obtaining the network load, directly and as the highest load. np.testing.assert_almost_equal(network_load, network_load_bis, decimal=6) np.testing.assert_almost_equal(network_load, load[0], decimal=6) np.testing.assert_almost_equal(xi_bottleneck.value, np.reshape(workload[0, :], (env.num_buffers, 1)), decimal=6) # Compare results with theoretical result from CTCN book, for the 4 resources. load_theory = np.array([alpha_r / (mu1 + mu2), alpha_r / mu_r, 0, 0]) workload_theory = np.vstack((1 / (mu1 + mu2) * np.ones(env.num_buffers), [0, 0, 1/mu_r], [0, 1 / mu2, 0], [1/mu1, 0, 0])) nu_theory = np.array([[mu1 / (mu1 + mu2), mu2 / (mu1 + mu2), 0], [0, 0, 1], [0, 1, 0], [1, 0, 0]]) # Due to numerical noise, different computers can obtain the barc vectors in different order. # So we will compare sets instead of ndarrays. np.around(workload, decimals=6, out=workload) np.around(workload_theory, decimals=6, out=workload_theory) np.around(nu, decimals=6, out=nu) np.around(nu_theory, decimals=6, out=nu_theory) workload_set = set(map(tuple, workload)) workload_theory_set = set(map(tuple, workload_theory)) nu_set = set(map(tuple, nu)) nu_theory_set = set(map(tuple, nu_theory)) np.testing.assert_almost_equal(load, load_theory, decimal=6) assert workload_set == workload_theory_set assert nu_set == nu_theory_set
def test_return_no_workload_vectors_due_on_too_high_load_threshold_simple_routing_model(): """Example 6.2.2. from CTCN online, Example 6.2.4 from printed version. Returns workload vectors that correspond with threshold > 0.99, but there are none.""" alpha_r = 0.18 mu1 = 0.13 mu2 = 0.07 mu_r = 0.3 env = envex.simple_routing_model(alpha_r, mu1, mu2, mu_r) # Compute load, workload and sensitivity vectors sorted by load load_threshold = 0.99 load, workload, nu = wl.compute_load_workload_matrix(env, load_threshold=load_threshold) assert load.size == workload.size == nu.size == 0
def test_simple_routing_model_buffer3_all_positive_and_equal_theta_j(self): state = np.array([[1], [1], [5]]) z_star_theory = np.array([[1], [1], [1 / 2], [1 / 2]]) self.perform_test( state, 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=None, max_episode_length=None), z_star_theory)
def test_simple_routing_model_buffer3_all_negative_theta_j_binary(self): state = np.array([[8], [5], [1]]) z_star_theory = np.array([[[1], [1], [0], [0]]]) self.perform_test_max_weight_binary_policy( state, 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=None, max_episode_length=None), z_star_theory, method='cvx.ECOS_BB', binary_action=True)