# JuMP: Sensitivity of Objective to Constraints

**URL:** <https://discourse.julialang.org/t/jump-sensitivity-of-objective-to-constraints/77361>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [March 3, 2022, 2:38pm UTC](https://discourse.julialang.org/t/jump-sensitivity-of-objective-to-constraints/77361 "2022-03-03T14:38:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alec\_Loudenback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alec_loudenback/32/278_2.png) [@Alec\_Loudenback](https://discourse.julialang.org/u/Alec_Loudenback)\
**Post date:** [March 3, 2022, 2:38pm UTC](https://discourse.julialang.org/t/jump-sensitivity-of-objective-to-constraints/77361/1 "2022-03-03T14:38:27Z")

</div>

Is there a recommended way to perform sensitivity analysis of the optimization target to different constraints?

What I mean by that is a way to determine which constraints are most limiting to the objective function. For example, in this MWE I’d like to be able to ascertain which constraint could be relaxed to achieve the most benefit to the optimization target:

```julia

using JuMP
using GLPK

function optimizer(limitall,limitone)
	model = Model(GLPK.Optimizer)
	@variable(model, x[1:5])
	@constraint(model, x .<= limitall)
	@constraint(model, x[1] .<= limitone)
	vals = [1:5...]

	@objective(model, Max, sum(x .* vals) )
	optimize!(model)
	return model
end

m = optimizer(10,5)

objective_value(m) # 145
value.(m[:x]) # [5.0, 10.0, 10.0, 10.0, 10.0]

```

In this case, if `limitall` went from `10` to `11` then my objective would improve by `4`, while `limitone` going from `5` to `6` would improve it by `1`.

In practice, I have a model like this that has 100+ constraints and I’m trying to determine which are most binding.

I have seen the function `JuMP.lp_sensitivity_report(m)` - what it seems to tell me is a different question: how much the constraints can change without affecting the objective value.

I have also tried wrapping the `optimizer` in a function `f` that takes the two limits and returns the objective value. Zygote did not differentiate the function with the error:

```julia
Compiling Tuple{typeof(MathOptInterface.add_variable), MathOptInterface.Utilities.CachingOptimizer{MathOptInterface.Bridges.LazyBridgeOptimizer{GLPK.Optimizer}, MathOptInterface.Utilities.UniversalFallback{MathOptInterface.Utilities.Model{Float64}}}}: try/catch is not supported.

```

---

<div class="post-metadata">

**Author:** ![cvanaret](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cvanaret/32/11594_2.png) [@cvanaret](https://discourse.julialang.org/u/cvanaret)\
**Post date:** [March 3, 2022, 2:55pm UTC](https://discourse.julialang.org/t/jump-sensitivity-of-objective-to-constraints/77361/2 "2022-03-03T14:55:21Z")

</div>

You want to look at the Lagrange multipliers (aka dual variables): [Solutions · JuMP](https://jump.dev/JuMP.jl/stable/manual/solutions/#Dual-solutions)

---

<div class="post-metadata">

**Author:** ![Alec\_Loudenback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alec_loudenback/32/278_2.png) [@Alec\_Loudenback](https://discourse.julialang.org/u/Alec_Loudenback)\
**Post date:** [March 3, 2022, 3:14pm UTC](https://discourse.julialang.org/t/jump-sensitivity-of-objective-to-constraints/77361/3 "2022-03-03T15:14:26Z")

</div>

Thank you for the pointer!

Can you help me understand the output here? I updated the constraints to include identifiers(?):

```julia
@constraint(model,c_all, x .<= limitall)
@constraint(model,c_one, x[1] .<= limitone)

```

and got the dual solution:

```julia
dual.(m[:c_all]) # [-0.0, -2.0, -3.0, -4.0, -5.0]
dual.(m[:c_one]) # -1.0

```

I tried playing around with the model but it hasn’t clicked what this is telling me. E.g. if I change the `c_one` constraint to `@constraint(model,c_one, x[1] .<= limitone/2)` then `dual.(m[:c_one])` remains `-1.0`.

---

<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:** [March 4, 2022, 1:51am UTC](https://discourse.julialang.org/t/jump-sensitivity-of-objective-to-constraints/77361/4 "2022-03-04T01:51:52Z")

</div>

> Is there a recommended way to perform sensitivity analysis of the optimization target to different constraints?

You should look up “duality” in any textbook in linear programming. Another terms to search is “shadow price”. JuMP’s `shadow_price(constraint)` will return the change in the objective if you relaxed the right-hand side of the constraint. So you just need to pick the constraint with the largest shadow price.

Docs:

- [Shadow prices in linear programming - Mathematics Stack Exchange](https://math.stackexchange.com/questions/91504/shadow-prices-in-linear-programming)
- [https://jump.dev/JuMP.jl/stable/reference/solutions/#JuMP.shadow\_price](https://jump.dev/JuMP.jl/stable/reference/solutions/#JuMP.shadow_price)
- [Constraints · JuMP](https://jump.dev/JuMP.jl/stable/manual/constraints/#constraint_duality)
