def test_simple_setter(): weights_1 = np.array([1, 2, 3]) simple_asf = SimpleASF(weights_1) weights_2 = np.array([1, 2, 4]) assert np.all(np.isclose(simple_asf.weights, weights_1)) assert not np.all(np.isclose(simple_asf.weights, weights_2)) simple_asf.weights = weights_2 assert np.all(np.isclose(simple_asf.weights, weights_2)) assert not np.all(np.isclose(simple_asf.weights, weights_1))
def test_change_asf_parameters(SimpleASFCylinderSolver): weights = np.array([1, 1, 1]) solver = SimpleASFCylinderSolver asf = SimpleASF(weights) solver.asf = asf before = solver.asf.weights asf.weights = np.array([2, 2, 2]) after = solver.asf.weights assert np.all(np.isclose(after, np.array([2, 2, 2]))) assert np.all(np.not_equal(before, after))
def test_simple_init(): weights_1 = np.array([1, 2, 3]) simple_asf = SimpleASF(weights_1) weights_tmp = weights_1 weights_1 = np.array([1, 2, 4]) assert np.all(np.isclose(simple_asf.weights, weights_tmp)) assert not np.all(np.isclose(simple_asf.weights, weights_1))
def test_simple_call(): weights = np.array([3, 4, 2]) simple_asf = SimpleASF(weights) objective = np.array([1, 1, 2.5]) reference = np.array([0.5, -2, 1.5]) res = simple_asf(objective, reference) assert res == approx(12.0)
def test_asf_solve_ones_weights(SimpleASFCylinderSolver): weights = np.ones(3) solver = SimpleASFCylinderSolver solver.asf = SimpleASF(weights) reference_point = np.array([500, 500, 500]) (variables, (objectives, constraints)) = solver.solve(reference_point) expected_variables = np.array([5.0, 10.0]) assert np.all( np.isclose(variables, expected_variables, rtol=0.0, atol=1.0e-1)) assert np.all(np.greater_equal(constraints, 0))
def test_asf(simple_data_problem): solver = ASFScalarSolver(simple_data_problem, DiscreteMinimizer()) solver.asf = SimpleASF([1, 1]) res1 = solver.solve(np.array([6, 3.4])) assert np.all(np.isclose(res1[0], [2, 2, 2])) assert np.all(np.isclose(res1[1], [6, 3.464101])) res2 = solver.solve(np.array([0, 0])) assert np.all(np.isclose(res2[0], [-1.05, -2.05, 3.1])) assert np.all(np.isclose(res2[1], [0, 3.861994]))
def test_asf_with_cons(simple_data_problem): solver = ASFScalarSolver(simple_data_problem, DiscreteMinimizer()) def fun1(xs, fs): return fs[:, 0] - 7 cons1 = ScalarConstraint("cons1", 3, 2, fun1) simple_data_problem.constraints = [cons1] solver.asf = SimpleASF([1, 1]) res1 = solver.solve(np.array([6, 3.4])) assert np.all(np.isclose(res1[0], [2.2, 3.3, 6.6])) assert np.all(np.isclose(res1[1], [12.1, 7.699999]))
def test_asf_evaluator_zero_weights( SimpleASFCylinderSolver, cylinder_good_decision_vectors, cylinder_bad_decision_vectors, ): solver = SimpleASFCylinderSolver weights = np.zeros(3) solver.asf = SimpleASF(weights) solver.reference_point = np.array([500, 500, 500]) res_good = solver._evaluator(cylinder_good_decision_vectors) res_bad = solver._evaluator(cylinder_bad_decision_vectors) assert np.all(res_good == approx(0)) assert np.all(res_bad == np.inf)
def test_asf_solve_reference_height(SimpleASFCylinderSolver): """Ignore all other objectives but the hieght difference. Results should therefore be the optimal height according to the 3rd objective which is 15, regardless of the reference value for the objective. """ weights = np.array([1, 1, 1]) solver = SimpleASFCylinderSolver solver.asf = SimpleASF(weights) reference_point = np.array([np.nan, np.nan, 6]) (variables, (objectives, constraints)) = solver.solve(reference_point) assert objectives[0][2] == approx(0, abs=1e-3) # Solution should always be feasible assert np.all(np.greater_equal(constraints, 0))
def test_asf_solve_reference_pareto(SimpleASFCylinderSolver): """Test a reference point residing very close to the pareto front. The resulting solution should be close to this point. """ weights = np.array([1, 1, 1]) solver = SimpleASFCylinderSolver solver.asf = SimpleASF(weights) reference_point = np.array([785.398, -471.239, 5.0]) (variables, (objectives, constraints)) = solver.solve(reference_point) assert objectives[0][0] == approx(reference_point[0], abs=5e-2) assert objectives[0][1] == approx(reference_point[1], abs=5e-2) assert variables[0] == approx(5.0, abs=1e-3) assert variables[1] == approx(10.0, abs=1e-3) assert np.all(np.greater_equal(constraints, 0))
def test_simple_non_matching_shapes(): weights = np.ones(3) simple_asf = SimpleASF(weights) objective = np.array([1, 1, 2.5]) reference = np.array([0.5, -2, 1.5]) objective2d = np.array([[1, 1, 1], [2, 2, 2]]) reference2d = np.array([[1, 1, 1], [2, 2, 2]]) with pytest.raises(ASFError): simple_asf(objective[:1], reference) with pytest.raises(ASFError): simple_asf(objective2d, reference) with pytest.raises(ASFError): simple_asf(objective, reference2d)
def test_asf_solve_reference_extreme_area_and_volume(SimpleASFCylinderSolver): """Test a reference point residing very close to the pareto front in the 2nd objective space. The 1st objective is set to uniquely to specify the variables to be r=12.5 and h=25. The third objective is ignored. """ weights = np.array([1, 1, 1]) solver = SimpleASFCylinderSolver solver.asf = SimpleASF(weights) reference_point = np.array([12271.8, -2945.25, np.nan]) (variables, (objectives, constraints)) = solver.solve(reference_point) assert objectives[0][0] == approx(reference_point[0], abs=5e-2) assert objectives[0][1] == approx(reference_point[1], abs=5e-2) assert variables[0] == approx(12.5, abs=1e-3) assert variables[1] == approx(25.0, abs=1e-3) assert np.all(np.greater_equal(constraints, 0))