|
| 1 | +# Copyright 2019, Oscar Dowson and contributors |
| 2 | +# This Source Code Form is subject to the terms of the Mozilla Public License, |
| 3 | +# v.2.0. If a copy of the MPL was not distributed with this file, You can |
| 4 | +# obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | + |
| 6 | +module TestChalmet |
| 7 | + |
| 8 | +using Test |
| 9 | + |
| 10 | +import HiGHS |
| 11 | +import MultiObjectiveAlgorithms as MOA |
| 12 | + |
| 13 | +const MOI = MOA.MOI |
| 14 | + |
| 15 | +function run_tests() |
| 16 | + for name in names(@__MODULE__; all = true) |
| 17 | + if startswith("$name", "test_") |
| 18 | + @testset "$name" begin |
| 19 | + getfield(@__MODULE__, name)() |
| 20 | + end |
| 21 | + end |
| 22 | + end |
| 23 | + return |
| 24 | +end |
| 25 | + |
| 26 | +function test_knapsack_min() |
| 27 | + n = 10 |
| 28 | + W = 2137.0 |
| 29 | + C = Float64[ |
| 30 | + 566 611 506 180 817 184 585 423 26 317 |
| 31 | + 62 84 977 979 874 54 269 93 881 563 |
| 32 | + ] |
| 33 | + w = Float64[557, 898, 148, 63, 78, 964, 246, 662, 386, 272] |
| 34 | + model = MOA.Optimizer(HiGHS.Optimizer) |
| 35 | + MOI.set(model, MOA.Algorithm(), MOA.Chalmet()) |
| 36 | + MOI.set(model, MOI.Silent(), true) |
| 37 | + x = MOI.add_variables(model, n) |
| 38 | + MOI.add_constraint.(model, x, MOI.ZeroOne()) |
| 39 | + MOI.add_constraint( |
| 40 | + model, |
| 41 | + MOI.ScalarAffineFunction( |
| 42 | + [MOI.ScalarAffineTerm(w[j], x[j]) for j in 1:n], |
| 43 | + 0.0, |
| 44 | + ), |
| 45 | + MOI.LessThan(W), |
| 46 | + ) |
| 47 | + f = MOI.VectorAffineFunction( |
| 48 | + [ |
| 49 | + MOI.VectorAffineTerm(i, MOI.ScalarAffineTerm(-C[i, j], x[j])) |
| 50 | + for i in 1:2 for j in 1:n |
| 51 | + ], |
| 52 | + [0.0, 0.0], |
| 53 | + ) |
| 54 | + MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE) |
| 55 | + MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f) |
| 56 | + MOI.optimize!(model) |
| 57 | + X_E = Float64[ |
| 58 | + 0 0 1 1 1 0 1 1 1 1 |
| 59 | + 1 0 1 1 1 0 1 1 0 1 |
| 60 | + 0 1 1 1 1 0 1 0 1 1 |
| 61 | + ] |
| 62 | + Y_N = Float64[ |
| 63 | + -2854 -4636 |
| 64 | + -3394 -3817 |
| 65 | + -3042 -4627 |
| 66 | + ] |
| 67 | + N = MOI.get(model, MOI.ResultCount()) |
| 68 | + x_sol = hcat([MOI.get(model, MOI.VariablePrimal(i), x) for i in 1:N]...) |
| 69 | + @test isapprox(x_sol, X_E'; atol = 1e-6) |
| 70 | + y_sol = hcat([MOI.get(model, MOI.ObjectiveValue(i)) for i in 1:N]...) |
| 71 | + @test isapprox(y_sol, Y_N'; atol = 1e-6) |
| 72 | + return |
| 73 | +end |
| 74 | + |
| 75 | +function test_knapsack_max() |
| 76 | + n = 10 |
| 77 | + W = 2137.0 |
| 78 | + C = Float64[ |
| 79 | + 566 611 506 180 817 184 585 423 26 317 |
| 80 | + 62 84 977 979 874 54 269 93 881 563 |
| 81 | + ] |
| 82 | + w = Float64[557, 898, 148, 63, 78, 964, 246, 662, 386, 272] |
| 83 | + model = MOA.Optimizer(HiGHS.Optimizer) |
| 84 | + MOI.set(model, MOA.Algorithm(), MOA.Chalmet()) |
| 85 | + MOI.set(model, MOI.Silent(), true) |
| 86 | + x = MOI.add_variables(model, n) |
| 87 | + MOI.add_constraint.(model, x, MOI.ZeroOne()) |
| 88 | + MOI.add_constraint( |
| 89 | + model, |
| 90 | + MOI.ScalarAffineFunction( |
| 91 | + [MOI.ScalarAffineTerm(w[j], x[j]) for j in 1:n], |
| 92 | + 0.0, |
| 93 | + ), |
| 94 | + MOI.LessThan(W), |
| 95 | + ) |
| 96 | + f = MOI.VectorAffineFunction( |
| 97 | + [ |
| 98 | + MOI.VectorAffineTerm(i, MOI.ScalarAffineTerm(C[i, j], x[j])) for |
| 99 | + i in 1:2 for j in 1:n |
| 100 | + ], |
| 101 | + [0.0, 0.0], |
| 102 | + ) |
| 103 | + MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE) |
| 104 | + MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f) |
| 105 | + MOI.optimize!(model) |
| 106 | + X_E = Float64[ |
| 107 | + 0 0 1 1 1 0 1 1 1 1 |
| 108 | + 1 0 1 1 1 0 1 1 0 1 |
| 109 | + 0 1 1 1 1 0 1 0 1 1 |
| 110 | + ] |
| 111 | + Y_N = Float64[ |
| 112 | + 2854 4636 |
| 113 | + 3394 3817 |
| 114 | + 3042 4627 |
| 115 | + ] |
| 116 | + N = MOI.get(model, MOI.ResultCount()) |
| 117 | + x_sol = hcat([MOI.get(model, MOI.VariablePrimal(i), x) for i in 1:N]...) |
| 118 | + @test isapprox(x_sol, X_E'; atol = 1e-6) |
| 119 | + y_sol = hcat([MOI.get(model, MOI.ObjectiveValue(i)) for i in 1:N]...) |
| 120 | + @test isapprox(y_sol, Y_N'; atol = 1e-6) |
| 121 | + return |
| 122 | +end |
| 123 | + |
| 124 | +function test_unbounded() |
| 125 | + model = MOA.Optimizer(HiGHS.Optimizer) |
| 126 | + MOI.set(model, MOA.Algorithm(), MOA.Chalmet()) |
| 127 | + MOI.set(model, MOI.Silent(), true) |
| 128 | + x = MOI.add_variables(model, 2) |
| 129 | + MOI.add_constraint.(model, x, MOI.GreaterThan(0.0)) |
| 130 | + f = MOI.Utilities.operate(vcat, Float64, 1.0 .* x...) |
| 131 | + MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f) |
| 132 | + MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE) |
| 133 | + MOI.optimize!(model) |
| 134 | + @test MOI.get(model, MOI.TerminationStatus()) == MOI.DUAL_INFEASIBLE |
| 135 | + @test MOI.get(model, MOI.PrimalStatus()) == MOI.NO_SOLUTION |
| 136 | + @test MOI.get(model, MOI.DualStatus()) == MOI.NO_SOLUTION |
| 137 | + return |
| 138 | +end |
| 139 | + |
| 140 | +function test_infeasible() |
| 141 | + model = MOA.Optimizer(HiGHS.Optimizer) |
| 142 | + MOI.set(model, MOA.Algorithm(), MOA.Chalmet()) |
| 143 | + MOI.set(model, MOI.Silent(), true) |
| 144 | + x = MOI.add_variables(model, 2) |
| 145 | + MOI.add_constraint.(model, x, MOI.GreaterThan(0.0)) |
| 146 | + MOI.add_constraint(model, 1.0 * x[1] + 1.0 * x[2], MOI.LessThan(-1.0)) |
| 147 | + f = MOI.Utilities.operate(vcat, Float64, 1.0 .* x...) |
| 148 | + MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f) |
| 149 | + MOI.optimize!(model) |
| 150 | + @test MOI.get(model, MOI.TerminationStatus()) == MOI.INFEASIBLE |
| 151 | + @test MOI.get(model, MOI.PrimalStatus()) == MOI.NO_SOLUTION |
| 152 | + @test MOI.get(model, MOI.DualStatus()) == MOI.NO_SOLUTION |
| 153 | + return |
| 154 | +end |
| 155 | + |
| 156 | +end |
| 157 | + |
| 158 | +TestChalmet.run_tests() |
0 commit comments