# How to model a multiobjective program

**URL:** <https://discourse.julialang.org/t/how-to-model-a-multiobjective-program/106561>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump, objective-function\
**Created:** [November 22, 2023, 8:07am UTC](https://discourse.julialang.org/t/how-to-model-a-multiobjective-program/106561 "2023-11-22T08:07:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [November 22, 2023, 8:07am UTC](https://discourse.julialang.org/t/how-to-model-a-multiobjective-program/106561/1 "2023-11-22T08:07:47Z")

</div>

In the following code, of which I do not report a long series of complicated constraints, I would like to be able to specify in the objective function also the fact that, **together** with the `Min, sum(x[2:nr,:,"G/\\N"])`, I want the `Max, sum(x[1,:,"G/\\N"])`.  
It’s possible?  
How can it be done?

Could something like this work?  
`Min, sum(x[2:nr,:,"G/\\N"])+1/sum(x[1,:,"G/\\N"])`

But I would like to know how it is possible in general to set up an objective function that has many conditions on different groups of variables.

```julia
m, M = getdata(path,H[1],H[2],H[3]) # m Matrix, M Dict

using JuMP
import HiGHS

model = Model(HiGHS.Optimizer)
# set_silent(model)
@variable(model, x[i in 1:nr, j in 1:ng, k in ["G", "N", "G/\\N", "G\\/N"]], Bin)
@objective(model, Min, sum(x[2:nr,:,"G/\\N"]))
@constraints(model, begin
    # total of G and N for row
    opcom[k in ["G", "N"], r in 1:nr], sum(x[r,:,k]) == M[k][r]
    # each day must have exactly one G (k==1) and one N (k==2)
    daycom[k in ["G", "N"], g in 1:ng], sum(x[:,g,k]) == 1
...
...
end)

function init_model(m)
    fixm(i,j, k,b)= begin fix(x[i, j, k], b; force = true); m[i,j]=replace(m[i,j], r"[N|G]"=>"") end
    for i in 1:nr, j in 1:ng
        m[i, j] == "xx" ? fixm.(i, j, ["G", "N"], 0) :
        m[i, j] == "xg" ? fixm(i, j, "G", 0) :
        m[i, j] == "xn" ? fixm(i, j, "N", 0) :
        m[i,j]∈["G","N"] ? fixm(i, j, m[i,j], 1) :    
        m[i, j] == "xnG" ? fixm.(i, j, ["N","G"], [0,1]) :
        m[i, j] == "xgN" ? fixm.(i, j, ["G","N"], [0,1]) :
        m[i, j] == "GN" ? fixm(i, j, "G/\\N", 1) :
        nothing
    end
end

init_model(m)

optimize!(model)

```

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [November 22, 2023, 8:44am UTC](https://discourse.julialang.org/t/how-to-model-a-multiobjective-program/106561/2 "2023-11-22T08:44:14Z")

</div>

> [@rocco\_sprmnt21](#):
>
> `Min, sum(x[2:nr,:,"G/\\N"])`, I want the `Max, sum(x[1,:,"G/\\N"])`.

To clarify, do you want to minimize `sum(x[2:nr,:,"G/\\N"])` and maximize `sum(x[1,:,"G/\\N"])`?

The first step is to convert everything to minimization or maximization, which you can do by multiplying maximization objectives by `-1` to create `minimize -sum(x[1,:,"G/\\N"])`.

Then you need to decide a trade-off: do you want to optimize `Min, sum(x[2:nr,:,"G/\\N"]) - sum(x[1,:,"G/\\N"])`, or do you want to solve a bi-objective problem.

See

- [Multi-objective knapsack · JuMP](https://jump.dev/JuMP.jl/stable/tutorials/linear/multi_objective_knapsack/)
- [Simple multi-objective examples · JuMP](https://jump.dev/JuMP.jl/stable/tutorials/linear/multi_objective_examples/)

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [November 22, 2023, 12:50pm UTC](https://discourse.julialang.org/t/how-to-model-a-multiobjective-program/106561/3 "2023-11-22T12:50:05Z")

</div>

Great question.  
I hadn’t thought about the situation enough (It’s a real problem of resource allocation, subject to a myriad of constraints. The requirements were posed to me by my wife in colloquial language and I try to interpret them as best I can).  
I have to think about it carefully, but I think the multi-objective algorithm is the most suitable one.  
You could therefore change the title of the discussion to make it more relevant.  
I ask you again, if you can, to explain to me in a (possibly) simple way the logic of the algorithm.  
That is, if you want to simultaneously minimize two functions, how do you regulate, for example, when you have to “sacrifice” a bit of the best of one to “improve” the other?

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [November 22, 2023, 5:34pm UTC](https://discourse.julialang.org/t/how-to-model-a-multiobjective-program/106561/4 "2023-11-22T17:34:11Z")

</div>

Follow the tutorials. The MOA package implements a number of algorithms that can do the trade off and return a set of candidate solutions. Picking the “best” is up to you. There is no right answer.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [November 26, 2023, 10:42am UTC](https://discourse.julialang.org/t/how-to-model-a-multiobjective-program/106561/5 "2023-11-26T10:42:04Z")

</div>

Thanks again for the prompt and comprehensive answers!

I did some tests and found solutions that were preferable to those obtained with the single objective algorithm.  
I would like to have clarified a curiosity (among many) that arise when reading the examples in the tutorial.  
It concerns the algorithms that can be used in the set\_attributes() function.  
For example the following:

```julia
set_attribute(model, MOA.Algorithm(), MOA.EpsilonConstraint())

set_attribute(model, MOA.Algorithm(), MOA.Lexicographic())

```

What are the specifics? When is one better than another?  
Where can I possibly read explanations?

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [November 26, 2023, 7:16pm UTC](https://discourse.julialang.org/t/how-to-model-a-multiobjective-program/106561/6 "2023-11-26T19:16:08Z")

</div>

> When is one better than another?

There is no “right” answer, it depends on what you want.

> Where can I possibly read explanations?

The docstring of each algorithm has some more details, although not much:

> <https://github.com/jump-dev/MultiObjectiveAlgorithms.jl/blob/bc0239de470cd487f03ee0a60fdb4b88a833bfd8/src/algorithms/Lexicographic.jl#L6-L10>

> <https://github.com/jump-dev/MultiObjectiveAlgorithms.jl/blob/bc0239de470cd487f03ee0a60fdb4b88a833bfd8/src/algorithms/EpsilonConstraint.jl#L6-L10>

We could expand.

The main reason I didn’t write more is that if you Google ‘lexicographic multiobjective’ and ‘epsilon-constraint method multiobjective’ you’ll find quite a large literature on these methods.
