# How to find Shadow Price in JuMP

**URL:** <https://discourse.julialang.org/t/how-to-find-shadow-price-in-jump/99887>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [June 5, 2023, 12:35pm UTC](https://discourse.julialang.org/t/how-to-find-shadow-price-in-jump/99887 "2023-06-05T12:35:59Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gagan\_Meena](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gagan_meena/32/49621_2.png) [@Gagan\_Meena](https://discourse.julialang.org/u/Gagan_Meena)\
**Post date:** [June 5, 2023, 12:35pm UTC](https://discourse.julialang.org/t/how-to-find-shadow-price-in-jump/99887/1 "2023-06-05T12:35:59Z")

</div>

How to find the shadow price for the equality constraint in the following code?

```julia
using JuMP, Gurobi
include("Data.jl")

Market_dt = PlanningData

function Market_Clearing(Market_dt)
    
    # Sets and Indices
    n = length(Market_dt.Bus_Garver.PG)
    r = length(Market_dt.ROW.from)

    bus = collect(1:n)
    rows = collect(1:r)

    # Bus Data 
    Pgmax = Market_dt.Bus_Garver.PG
    Pdmax = Market_dt.Bus_Garver.PL
    Offer = Market_dt.Bus_Garver.Offer
    Bid = Market_dt.Bus_Garver.Bid
    
    # Line Data
    from = Market_dt.ROW.from
    to = Market_dt.ROW.to
    f_max = Market_dt.ROW.f_max
    y = Market_dt.ROW.y
  
    # Model 
    Market = Model(Gurobi.Optimizer)
    set_attribute(Market, "OutputFlag", 1)
    set_attribute(Market, "FeasibilityTol", 1e-8)
    set_attribute(Market, "MIPGap", 1e-8)
    set_attribute(Market, "MIPGapAbs", 0)
    
    # Variables
  
    @variables(Market, begin
        th[bus]
        0 <= Pg[i =1:n] <= Pgmax[i]
        0 <= Pd[i =1:n] <= Pdmax[i]
    end)
    
    @expression(Market, Inj[i=1:n], Pg[i] - Pd[i])
    
    for i = 1:r
      f = from[i]
      t = to[i]
      Inj[f] = Inj[f] - y[i]*(th[f] - th[t])
      Inj[t] = Inj[t] + y[i]*(th[f] - th[t])
    end

    @constraints(Market, begin
        th[1]==0
        [i=1:n], Inj == 0
        [i=1:r], y[i]*(th[from[i]] - th[to[i]]) <= f_max[i]
        [i=1:r], y[i]*(th[from[i]] - th[to[i]]) >=- f_max[i]
    end)
   
    @expressions(Market, begin
      utility, sum(Bid[i]*Pd[i] for i in 1:n)
      gencost, sum(Offer[i]*Pg[i] for i in 1:n)
    end)
  
    println(Market)
    @objective(Market, Max, utility - gencost)
    optimize!(Market)
    println("Termination Status: ", termination_status(Market))
    println(value.(Pd))
    println(value.(Pg))
end

@time Market_Clearing(Market_dt)

```

---

<div class="post-metadata">

**Author:** ![merlion](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@merlion](https://discourse.julialang.org/u/merlion)\
**Post date:** [June 5, 2023, 12:54pm UTC](https://discourse.julialang.org/t/how-to-find-shadow-price-in-jump/99887/2 "2023-06-05T12:54:23Z")

</div>

Are you looking for the value of dual variables? If you give your constraints names as shown in the [JuMP documentation](https://jump.dev/JuMP.jl/stable/manual/solutions/#Dual-solutions), you can retrieve the dual solutions by running `dual(constraintname)`. (See the section “Dual Solutions” on the same documentation page.)

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [June 5, 2023, 1:16pm UTC](https://discourse.julialang.org/t/how-to-find-shadow-price-in-jump/99887/3 "2023-06-05T13:16:59Z")

</div>

There is actually a function called [`shadow_price`](https://jump.dev/JuMP.jl/stable/reference/solutions/#JuMP.shadow_price).

---

<div class="post-metadata">

**Author:** ![Gagan\_Meena](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gagan_meena/32/49621_2.png) [@Gagan\_Meena](https://discourse.julialang.org/u/Gagan_Meena)\
**Post date:** [June 8, 2023, 6:42am UTC](https://discourse.julialang.org/t/how-to-find-shadow-price-in-jump/99887/4 "2023-06-08T06:42:50Z")

</div>

Following code is giving error.

```julia
using JuMP, Gurobi
include("Data.jl")

Market_dt = PlanningData

function Market_Clearing(Market_dt)
    
    # Sets and Indices
    n = length(Market_dt.Bus_Garver.PG)
    r = length(Market_dt.ROW.from)

    bus = collect(1:n)
    rows = collect(1:r)

    # Bus Data 
    Pgmax = Market_dt.Bus_Garver.PG
    Pdmax = Market_dt.Bus_Garver.PL
    Offer = Market_dt.Bus_Garver.Offer
    Bid = Market_dt.Bus_Garver.Bid
    
    # Line Data
    from = Market_dt.ROW.from
    to = Market_dt.ROW.to
    f_max = Market_dt.ROW.f_max
    y = Market_dt.ROW.y
  
    # Model 
    Market = Model(Gurobi.Optimizer)
    set_attribute(Market, "OutputFlag", 1)
    set_attribute(Market, "FeasibilityTol", 1e-8)
    set_attribute(Market, "MIPGap", 1e-8)
    set_attribute(Market, "MIPGapAbs", 0)
    
    # Variables
  
    @variables(Market, begin
        th[bus]
        0 <= Pg[i =1:n] <= Pgmax[i]
        0 <= Pd[i =1:n] <= Pdmax[i]
    end)
    
    @expression(Market, Inj[i=1:n], Pg[i] - Pd[i])
    
    for i = 1:r
      f = from[i]
      t = to[i]
      Inj[f] = Inj[f] - y[i]*(th[f] - th[t])
      Inj[t] = Inj[t] + y[i]*(th[f] - th[t])
    end

    @constraints(Market, begin
        th[1]==0
        [i=1:r], y[i]*(th[from[i]] - th[to[i]]) <= f_max[i]
        [i=1:r], y[i]*(th[from[i]] - th[to[i]]) >=- f_max[i]
    end)
    @constraint(Market,[i=1:n], Inj == 0, base_name="injection")
    @expressions(Market, begin
      utility, sum(Bid[i]*Pd[i] for i in 1:n)
      gencost, sum(Offer[i]*Pg[i] for i in 1:n)
    end)
  
    
    @objective(Market, Max, utility - gencost)
    println(Market)
    optimize!(Market)
    println("Termination Status: ", termination_status(Market))
    println(value.(Pd))
    println(value.(Pg))
    println(shadow_price(injection[1]))
end
  
 @time Market_Clearing(Market_dt)

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [June 8, 2023, 7:35am UTC](https://discourse.julialang.org/t/how-to-find-shadow-price-in-jump/99887/5 "2023-06-08T07:35:15Z")

</div>

But what is the error? Please read

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

and turn your post into an MWE so others can help.

---

<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:** [June 8, 2023, 8:52pm UTC](https://discourse.julialang.org/t/how-to-find-shadow-price-in-jump/99887/6 "2023-06-08T20:52:04Z")

</div>

You need `@constraint(Market, injection[i=1:n], Inj[i] == 0)`, not `@constraint(Market,[i=1:n], Inj == 0, base_name="injection")`.

Using `base_name` just changes the name that JuMP displays when printing. My suggestion creates a new Julia variable `injection` that is a vector with one element for each constraint.

(Also, you have a typo, it should be `Inj[I]`.)

---

<div class="post-metadata">

**Author:** ![Gagan\_Meena](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gagan_meena/32/49621_2.png) [@Gagan\_Meena](https://discourse.julialang.org/u/Gagan_Meena)\
**Post date:** [June 9, 2023, 4:38am UTC](https://discourse.julialang.org/t/how-to-find-shadow-price-in-jump/99887/7 "2023-06-09T04:38:47Z")

</div>

Thank you very much.
