# Querying lower level objective value in BilevelJuMP.jl

**URL:** <https://discourse.julialang.org/t/querying-lower-level-objective-value-in-bileveljump-jl/80084>\
**Category:** Optimization (Mathematical)\
**Tags:** question, package\
**Created:** [April 26, 2022, 5:50pm UTC](https://discourse.julialang.org/t/querying-lower-level-objective-value-in-bileveljump-jl/80084 "2022-04-26T17:50:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![math\_opt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/math_opt/32/25317_2.png) [@math\_opt](https://discourse.julialang.org/u/math_opt)\
**Post date:** [April 26, 2022, 5:50pm UTC](https://discourse.julialang.org/t/querying-lower-level-objective-value-in-bileveljump-jl/80084/1 "2022-04-26T17:50:24Z")

</div>

Is there an inbuilt function I can use to query the lower level objective value when using BilevelJuMP.jl?  
For the following example:

```julia
using JuMP, BilevelJuMP, Gurobi

model = BilevelModel(Gurobi.Optimizer, mode = BilevelJuMP.SOS1Mode())

@variable(Lower(model), x)
@variable(Upper(model), y)

@objective(Upper(model), Min, 3x + y)
@constraints(Upper(model), begin
    x <= 5
    y <= 8
    y >= 0
end)

@objective(Lower(model), Min, -x)
@constraints(Lower(model), begin
     x + y <= 8
    4x + y >= 8
    2x + y <= 13
    2x - 7y <= 0
end)

optimize!(model)

```

To access the objective values, I use the following function:

```julia
upper_level_objective = objective_value(model)

```

Then, I see that both upper and lower level model are of type `JuMP.AbstractModel`, so I tried:

```julia
upper_level_objective = objective_value(Upper(model))
lower_level_objective = objective_value(Lower(model))

```

The above function returns me the correct objective values for both levels. So my questions are (in general):

1. Is `objective_value(model) == objective_value(Upper(model))` always?
2. Is `objective_value(Lower(model))` the correct way to access the lower level objective value?

Thank you!

---

<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:** [April 27, 2022, 11:51pm UTC](https://discourse.julialang.org/t/querying-lower-level-objective-value-in-bileveljump-jl/80084/2 "2022-04-27T23:51:18Z")

</div>

cc @joaquimg

I think the answer to both questions is probably “Yes”.

---

<div class="post-metadata">

**Author:** ![joaquimg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joaquimg/32/223_2.png) [@joaquimg](https://discourse.julialang.org/u/joaquimg)\
**Post date:** [April 28, 2022, 12:54am UTC](https://discourse.julialang.org/t/querying-lower-level-objective-value-in-bileveljump-jl/80084/3 "2022-04-28T00:54:41Z")

</div>

Exactly, yes for both questions.

---

<div class="post-metadata">

**Author:** ![math\_opt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/math_opt/32/25317_2.png) [@math\_opt](https://discourse.julialang.org/u/math_opt)\
**Post date:** [April 28, 2022, 1:06am UTC](https://discourse.julialang.org/t/querying-lower-level-objective-value-in-bileveljump-jl/80084/4 "2022-04-28T01:06:38Z")

</div>

Thanks for confirming.
