|
| 1 | +# Copyright 2017, Iain Dunning, Joey Huchette, Miles Lubin, and contributors #src |
| 2 | +# This Source Code Form is subject to the terms of the Mozilla Public License #src |
| 3 | +# v.2.0. If a copy of the MPL was not distributed with this file, You can #src |
| 4 | +# obtain one at https://mozilla.org/MPL/2.0/. #src |
| 5 | + |
| 6 | +# # Simple multi-objective examples |
| 7 | + |
| 8 | +# This tutorial contains a number of examples of multi-objective programs from |
| 9 | +# the literature. |
| 10 | + |
| 11 | +# ## Required packages |
| 12 | + |
| 13 | +# This tutorial requires the following packages: |
| 14 | + |
| 15 | +using JuMP |
| 16 | +import HiGHS |
| 17 | +import MultiObjectiveAlgorithms as MOA |
| 18 | + |
| 19 | +# ## Bi-objective linear problem |
| 20 | + |
| 21 | +# This is example is taken from Example 6.3 (from Steuer, 1985), page 154 of |
| 22 | +# Multicriteria Optimization (2nd ed), M. Ehrgott, Springer 2005. The code was |
| 23 | +# adapted from an example in [vOptGeneric](https://github.com/vOptSolver/vOptGeneric.jl) |
| 24 | +# by [@xgandibleux](https://github.com/xgandibleux). |
| 25 | + |
| 26 | +model = Model() |
| 27 | +set_silent(model) |
| 28 | +@variable(model, x1 >= 0) |
| 29 | +@variable(model, 0 <= x2 <= 3) |
| 30 | +@objective(model, Min, [3x1 + x2, -x1 - 2x2]) |
| 31 | +@constraint(model, 3x1 - x2 <= 6) |
| 32 | +set_optimizer(model, () -> MOA.Optimizer(HiGHS.Optimizer)) |
| 33 | +set_optimizer_attribute( |
| 34 | + model, |
| 35 | + MOA.Algorithm(), |
| 36 | + MOA.Lexicographic(; all_permutations = true), |
| 37 | +) |
| 38 | +optimize!(model) |
| 39 | +solution_summary(model) |
| 40 | + |
| 41 | +#- |
| 42 | + |
| 43 | +for i in 1:result_count(model) |
| 44 | + print(i, ": z = ", round.(Int, objective_value(model; result = i)), " | ") |
| 45 | + println("x = ", value.([x1, x2]; result = i)) |
| 46 | +end |
| 47 | + |
| 48 | +# ## Bi-objective linear assignment problem |
| 49 | + |
| 50 | +# This is example is taken from Example 9.38 (from Ulungu and Teghem, 1994), |
| 51 | +# page 255 of Multicriteria Optimization (2nd ed), M. Ehrgott, Springer 2005. |
| 52 | +# The code was adapted from an example in [vOptGeneric](https://github.com/vOptSolver/vOptGeneric.jl) |
| 53 | +# by [@xgandibleux](https://github.com/xgandibleux). |
| 54 | + |
| 55 | +C1 = [5 1 4 7; 6 2 2 6; 2 8 4 4; 3 5 7 1] |
| 56 | +C2 = [3 6 4 2; 1 3 8 3; 5 2 2 3; 4 2 3 5] |
| 57 | +n = size(C2, 1) |
| 58 | +model = Model() |
| 59 | +set_silent(model) |
| 60 | +@variable(model, x[1:n, 1:n], Bin) |
| 61 | +@objective(model, Min, [sum(C1 .* x), sum(C2 .* x)]) |
| 62 | +@constraint(model, [i = 1:n], sum(x[i, :]) == 1) |
| 63 | +@constraint(model, [j = 1:n], sum(x[:, j]) == 1) |
| 64 | +set_optimizer(model, () -> MOA.Optimizer(HiGHS.Optimizer)) |
| 65 | +set_optimizer_attribute(model, MOA.Algorithm(), MOA.EpsilonConstraint()) |
| 66 | +optimize!(model) |
| 67 | +solution_summary(model) |
| 68 | + |
| 69 | +#- |
| 70 | + |
| 71 | +for i in 1:result_count(model) |
| 72 | + print(i, ": z = ", round.(Int, objective_value(model; result = i)), " | ") |
| 73 | + println("x = ", round.(Int, value.(x; result = i))) |
| 74 | +end |
| 75 | + |
| 76 | +# ## Bi-objective shortest path problem |
| 77 | + |
| 78 | +# This is example is taken from Exercise 9.5 page 269 of Multicriteria |
| 79 | +# Optimization (2nd edt), M. Ehrgott, Springer 2005. The code was adapted from |
| 80 | +# an example in [vOptGeneric](https://github.com/vOptSolver/vOptGeneric.jl) by |
| 81 | +# [@xgandibleux](https://github.com/xgandibleux). |
| 82 | + |
| 83 | +M = 50 |
| 84 | +C1 = [ |
| 85 | + M 4 5 M M M |
| 86 | + M M 2 1 2 7 |
| 87 | + M M M 5 2 M |
| 88 | + M M 5 M M 3 |
| 89 | + M M M M M 4 |
| 90 | + M M M M M M |
| 91 | +] |
| 92 | +C2 = [ |
| 93 | + M 3 1 M M M |
| 94 | + M M 1 4 2 2 |
| 95 | + M M M 1 7 M |
| 96 | + M M 1 M M 2 |
| 97 | + M M M M M 2 |
| 98 | + M M M M M M |
| 99 | +] |
| 100 | +n = size(C2, 1) |
| 101 | +model = Model() |
| 102 | +set_silent(model) |
| 103 | +@variable(model, x[1:n, 1:n], Bin) |
| 104 | +@objective(model, Min, [sum(C1 .* x), sum(C2 .* x)]) |
| 105 | +@constraint(model, sum(x[1, :]) == 1) |
| 106 | +@constraint(model, sum(x[:, n]) == 1) |
| 107 | +@constraint(model, [i = 2:n-1], sum(x[i, :]) - sum(x[:, i]) == 0) |
| 108 | +set_optimizer(model, () -> MOA.Optimizer(HiGHS.Optimizer)) |
| 109 | +set_optimizer_attribute(model, MOA.Algorithm(), MOA.EpsilonConstraint()) |
| 110 | +optimize!(model) |
| 111 | +solution_summary(model) |
| 112 | + |
| 113 | +#- |
| 114 | + |
| 115 | +for i in 1:result_count(model) |
| 116 | + print(i, ": z = ", round.(Int, objective_value(model; result = i)), " | ") |
| 117 | + X = round.(Int, value.(x; result = i)) |
| 118 | + print("Path:") |
| 119 | + for ind in findall(val -> val ≈ 1, X) |
| 120 | + i, j = ind.I |
| 121 | + print(" $i->$j") |
| 122 | + end |
| 123 | + println() |
| 124 | +end |
0 commit comments