|
| 1 | +@testset "Test generate alternatives with HSJ as modeling_method." begin |
| 2 | + @testset "Test regular run with one alternative." begin |
| 3 | + optimizer = Ipopt.Optimizer |
| 4 | + model = JuMP.Model(optimizer) |
| 5 | + |
| 6 | + # Initialise simple `square` JuMP model |
| 7 | + @variable(model, 0 ≤ x_1 ≤ 1) |
| 8 | + @variable(model, 0 ≤ x_2 ≤ 1) |
| 9 | + @objective(model, Max, x_1 + x_2) |
| 10 | + JuMP.optimize!(model) |
| 11 | + |
| 12 | + results = NearOptimalAlternatives.generate_alternatives!( |
| 13 | + model, |
| 14 | + 0.1, |
| 15 | + all_variables(model), |
| 16 | + 1; |
| 17 | + modeling_method = :HSJ, |
| 18 | + ) |
| 19 | + |
| 20 | + # Test that `results` contains one solution with 2 variables, and an objective value between 1.8 and 2.0. |
| 21 | + @test length(results.solutions) == 1 && |
| 22 | + length(results.solutions[1]) == 2 && |
| 23 | + length(results.objective_values) == 1 && |
| 24 | + ( |
| 25 | + results.objective_values[1] ≥ 1.8 || |
| 26 | + isapprox(results.objective_values[1], 1.8) |
| 27 | + ) && |
| 28 | + ( |
| 29 | + results.objective_values[1] ≤ 2.0 || |
| 30 | + isapprox(results.objective_values[1], 2.0) |
| 31 | + ) |
| 32 | + end |
| 33 | + |
| 34 | + @testset "Test regular run with one alternative with one fixed variable." begin |
| 35 | + optimizer = Ipopt.Optimizer |
| 36 | + model = JuMP.Model(optimizer) |
| 37 | + |
| 38 | + # Initialise simple `square` JuMP model |
| 39 | + @variable(model, 0 ≤ x_1 ≤ 1) |
| 40 | + @variable(model, 0 ≤ x_2 ≤ 1) |
| 41 | + @objective(model, Max, x_1 + x_2) |
| 42 | + JuMP.optimize!(model) |
| 43 | + |
| 44 | + results = NearOptimalAlternatives.generate_alternatives!( |
| 45 | + model, |
| 46 | + 0.1, |
| 47 | + all_variables(model), |
| 48 | + 1; |
| 49 | + fixed_variables = [x_2], |
| 50 | + modeling_method = :HSJ, |
| 51 | + ) |
| 52 | + |
| 53 | + # Test that `results` contains one solution with 2 variables, and an objective value between 1.8 and 2.0. Also, `x_2` should remain around 1.0 and `x_1` should be between 0.8 and 1.0. |
| 54 | + @test length(results.solutions) == 1 && |
| 55 | + length(results.solutions[1]) == 2 && |
| 56 | + length(results.objective_values) == 1 && |
| 57 | + ( |
| 58 | + results.objective_values[1] ≥ 1.8 || |
| 59 | + isapprox(results.objective_values[1], 1.8) |
| 60 | + ) && |
| 61 | + ( |
| 62 | + results.objective_values[1] ≤ 2.0 || |
| 63 | + isapprox(results.objective_values[1], 2.0) |
| 64 | + ) && |
| 65 | + ( |
| 66 | + results.solutions[1][x_1] ≥ 0.8 || |
| 67 | + isapprox(results.solutions[1][x_1], 0.8) |
| 68 | + ) && |
| 69 | + ( |
| 70 | + results.solutions[1][x_1] ≤ 1.0 || |
| 71 | + isapprox(results.solutions[1][x_1], 1.0) |
| 72 | + ) && |
| 73 | + isapprox(results.solutions[1][x_2], 1.0) |
| 74 | + end |
| 75 | + |
| 76 | + @testset "Test regular run with two alternatives." begin |
| 77 | + optimizer = Ipopt.Optimizer |
| 78 | + model = JuMP.Model(optimizer) |
| 79 | + |
| 80 | + # Initialise simple `square` JuMP model |
| 81 | + @variable(model, 0 ≤ x_1 ≤ 1) |
| 82 | + @variable(model, 0 ≤ x_2 ≤ 1) |
| 83 | + @objective(model, Max, x_1 + x_2) |
| 84 | + JuMP.optimize!(model) |
| 85 | + |
| 86 | + results = NearOptimalAlternatives.generate_alternatives!( |
| 87 | + model, |
| 88 | + 0.1, |
| 89 | + all_variables(model), |
| 90 | + 2; |
| 91 | + modeling_method = :HSJ, |
| 92 | + ) |
| 93 | + |
| 94 | + # Test that `results` contains 2 solutions with two variables each, where the objective values of both solutions are between 1.8 and 2.0. |
| 95 | + @test length(results.solutions) == 2 && |
| 96 | + length(results.solutions[2]) == 2 && |
| 97 | + length(results.objective_values) == 2 && |
| 98 | + ( |
| 99 | + results.objective_values[1] ≥ 1.8 || |
| 100 | + isapprox(results.objective_values[1], 1.8) |
| 101 | + ) && |
| 102 | + ( |
| 103 | + results.objective_values[1] ≤ 2.0 || |
| 104 | + isapprox(results.objective_values[1], 2.0) |
| 105 | + ) && |
| 106 | + ( |
| 107 | + results.objective_values[2] ≥ 1.8 || |
| 108 | + isapprox(results.objective_values[2], 1.8) |
| 109 | + ) && |
| 110 | + ( |
| 111 | + results.objective_values[2] ≤ 2.0 || |
| 112 | + isapprox(results.objective_values[2], 2.0) |
| 113 | + ) |
| 114 | + end |
| 115 | +end |
0 commit comments