def test_optimize_works_for_simple_three_model_ex(optimizer_three_model, target_cost, factor): sample_array_expected = np.array([[1 * factor, 1, 1, 0, 0, 0], [2 * factor, 0, 0, 1, 1, 0], [8 * factor, 0, 0, 0, 0, 1]]) var_expected = 1.5 / float(factor) cost_expected = target_cost opt_result = optimizer_three_model.optimize(algorithm="mlmc", target_cost=target_cost) assert_opt_result_equal(opt_result, cost_expected, var_expected, sample_array_expected)
def test_optimize_for_noninteger_sample_nums(optimizer_three_model): sample_array_expected = np.array([[1, 1, 1, 0, 0, 0], [3, 0, 0, 1, 1, 0], [12, 0, 0, 0, 0, 1]]) target_cost = 36 var_expected = (1 / 2. + 1 / 3. + 1 / 3.) cost_expected = 32 opt_result = optimizer_three_model.optimize(algorithm="mlmc", target_cost=target_cost) assert_opt_result_equal(opt_result, cost_expected, var_expected, sample_array_expected)
def test_optimize_works_for_simple_two_model_ex(optimizer_two_model, target_cost, factor): sample_array_expected = np.array([[1 * factor, 1, 1, 0], [4 * factor, 0, 0, 1]]) variance_expected = 2 / float(factor) cost_expected = target_cost opt_result = optimizer_two_model.optimize(algorithm="mlmc", target_cost=target_cost) assert isinstance(opt_result.allocation, MLMCSampleAllocation) assert_opt_result_equal(opt_result, cost_expected, variance_expected, sample_array_expected)
def test_mfmc_with_model_selection_hifi_fastest(): covariance = np.array([[1, 0.0], [0.0, 1]]) model_costs = np.array([1, 2]) optimizer = Optimizer(model_costs, covariance) opt_result = optimizer.optimize(algorithm="mfmc", target_cost=30, auto_model_selection=True) expected_cost = 30 expected_variance = 1 / 30 expected_sample_array = np.array([[30, 1, 0, 0]], dtype=int) assert isinstance(opt_result.allocation, ACVSampleAllocation) assert_opt_result_equal(opt_result, expected_cost, expected_variance, expected_sample_array)
def test_case_mfmc(target_cost_multiplier, covariance_multiplier): covariance = np.array([[1, 0.5], [0.5, 1]]) * covariance_multiplier model_costs = np.array([4800, 4]) optimizer = Optimizer(model_costs, covariance) target_cost = 14640 * target_cost_multiplier opt_result = optimizer.optimize(algorithm="mfmc", target_cost=target_cost) expected_cost = 14640 * target_cost_multiplier expected_variance = 61 / 240 * covariance_multiplier \ / target_cost_multiplier expected_sample_array = np.array([[3 * target_cost_multiplier, 1, 1, 1], [57 * target_cost_multiplier, 0, 0, 1]], dtype=int) assert_opt_result_equal(opt_result, expected_cost, expected_variance, expected_sample_array)
def test_three_models_out_of_order(): target_cost = 24 cov_matrix = np.array([[1.5, dummy_var, 1], [dummy_var, 4, 2], [1, 2, 1]]) model_costs = np.array([5, 1, 3]) optimizer = Optimizer(model_costs=model_costs, covariance=cov_matrix) sample_array_expected = np.array([[1, 1, 0, 0, 1, 0], [2, 0, 1, 0, 0, 1], [8, 0, 0, 1, 0, 0]]) var_expected = 1.5 cost_expected = target_cost opt_result = optimizer.optimize(algorithm="mlmc", target_cost=target_cost) assert_opt_result_equal(opt_result, cost_expected, var_expected, sample_array_expected)
def test_mfmc_with_model_selection_no_best_result(mocker): mocker.patch('mxmc.optimizer.AutoModelSelection.' + '_get_subsets_of_model_indices', return_value=[]) covariance = np.array([[1, 0.0], [0.0, 1]]) model_costs = np.array([1, 2]) optimizer = Optimizer(model_costs, covariance) opt_result = optimizer.optimize(algorithm="mfmc", target_cost=30, auto_model_selection=True) expected_cost = 0 expected_variance = np.inf expected_sample_array = np.array([[1, 1, 0, 0]], dtype=int) assert_opt_result_equal(opt_result, expected_cost, expected_variance, expected_sample_array)
def test_mlmc_with_model_selection(): model_costs = np.array([3, 2, 1]) cov_matrix = np.array([[8, 6, 11/2.], [6, 6, 7/2.], [11/2., 7/2., 4.]]) optimizer = Optimizer(model_costs, covariance=cov_matrix) target_cost = 17 sample_array_expected = np.array([[2, 1, 0, 0, 1, 0], [8, 0, 0, 0, 0, 1]]) variance_expected = 1. cost_expected = 16 opt_result = optimizer.optimize(algorithm="mlmc", target_cost=target_cost, auto_model_selection=True) assert_opt_result_equal(opt_result, cost_expected, variance_expected, sample_array_expected)
def test_acvmfmc_known_solution(cost_factor, covariance_factor, mocker): covariance = np.array([[1, 0.5], [0.5, 1]]) * covariance_factor model_costs = np.array([4800, 4]) optimizer = Optimizer(model_costs, covariance) ratios_for_opt = np.array([20]) mocker.patch.object(Optimizer.get_algorithm("acvmfmc"), '_solve_opt_problem', return_value=ratios_for_opt) cost_ref = 14640 * cost_factor var_ref = 61 / 240 * covariance_factor / cost_factor allocation_ref = np.array( [[3 * cost_factor, 1, 1, 1], [57 * cost_factor, 0, 0, 1]], dtype=int) target_cost = 14640 * cost_factor opt_result = optimizer.optimize(algorithm="acvmfmc", target_cost=target_cost) assert_opt_result_equal(opt_result, cost_ref, var_ref, allocation_ref)
def test_four_models_out_of_order(): target_cost = 64 model_costs = np.array([11, 1, 5, 3]) cov_matrix = np.array([[0.75, dummy_var, 1, dummy_var], [dummy_var, 4, dummy_var, 2], [1, dummy_var, 1.5, 1], [dummy_var, 2, 1, 1]]) optimizer = Optimizer(model_costs=model_costs, covariance=cov_matrix) sample_array_expected = np.array([[1, 1, 0, 0, 1, 0, 0, 0], [2, 0, 0, 0, 0, 1, 1, 0], [4, 0, 1, 0, 0, 0, 0, 1], [16, 0, 0, 1, 0, 0, 0, 0]]) var_expected = 1. cost_expected = target_cost opt_result = optimizer.optimize(algorithm="mlmc", target_cost=target_cost) assert_opt_result_equal(opt_result, cost_expected, var_expected, sample_array_expected)
def test_mlmc_with_model_selection_zero_q(): model_costs = np.array([3, 2, 1]) cov_matrix = np.array([[8, 6, 11/2.], [6, 6, 7/2.], [11/2., 7/2., 4.]]) optimizer = Optimizer(model_costs, covariance=cov_matrix) target_cost = 8 sample_array_expected = np.array([[1, 1, 0, 0, 1, 0], [4, 0, 0, 0, 0, 1]]) variance_expected = 2. cost_expected = 8. with warnings.catch_warnings(record=True) as warning_log: opt_result = optimizer.optimize(algorithm="mlmc", target_cost=target_cost, auto_model_selection=True) assert_opt_result_equal(opt_result, cost_expected, variance_expected, sample_array_expected) assert len(warning_log) == 1 assert issubclass(warning_log[-1].category, UserWarning)
def test_acvmf_three_models_known_solution(cost_factor, covariance_factor, mocker): covariance = np.array([[1, 0.75, 0.25], [0.75, 1., 0.5], [0.25, 0.5, 1.] ]) * covariance_factor model_costs = np.array([3, 2, 1]) optimizer = Optimizer(model_costs, covariance) ratios_for_opt = np.array([1, 2]) mocker.patch.object(Optimizer.get_algorithm("wrdiff"), '_solve_opt_problem', return_value=ratios_for_opt) cost_ref = 10. * cost_factor var_ref = 0.6931818181818182 * covariance_factor / cost_factor allocation_ref = np.array( [[1 * cost_factor, 1, 1, 0, 0, 0], [1 * cost_factor, 0, 0, 1, 1, 0], [2 * cost_factor, 0, 0, 0, 0, 1]], dtype=int) target_cost = 10 * cost_factor opt_result = optimizer.optimize(algorithm="wrdiff", target_cost=target_cost) assert_opt_result_equal(opt_result, cost_ref, var_ref, allocation_ref)
def test_target_cost_too_low_to_run_models(ui_optimizer, algorithm, target_cost): opt_result = ui_optimizer.optimize(algorithm=algorithm, target_cost=target_cost) assert_opt_result_equal(opt_result, 0, np.inf, np.array([[1, 1, 0, 0]]))