How to find Shadow Price in JuMP

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

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)

Are you looking for the value of dual variables? If you give your constraints names as shown in the JuMP documentation, you can retrieve the dual solutions by running dual(constraintname). (See the section “Dual Solutions” on the same documentation page.)

1 Like

There is actually a function called shadow_price.

2 Likes

Following code is giving error.

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)

But what is the error? Please read

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

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].)

Thank you very much.

1 Like