# JuMP dual inside loop

**URL:** https://discourse.julialang.org/t/jump-dual-inside-loop/76895
**Category:** Optimization (Mathematical)
**Tags:** question, jump
**Created:** [February 22, 2022, 9:36am UTC](https://discourse.julialang.org/t/jump-dual-inside-loop/76895 "2022-02-22T09:36:19Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Mariana](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mariana/32/15064_2.png) [@Mariana](https://discourse.julialang.org/u/Mariana)
#### Post date: [February 22, 2022, 9:36am UTC](https://discourse.julialang.org/t/jump-dual-inside-loop/76895/1 "2022-02-22T09:36:19Z")

</div>

Hello!

I am trying to run a code using the `dual` function from JuMP inside a loop, but I’m not getting the results I expected.

The code is a bit extensive, so I’ll try to put here the most relevant parts of the model for my question:

```julia
xx = [@variable(sub, [1:T, 1:nGen, 1:K+1]) for i = 1:3]

@constraints(sub, begin 
    DualFisherg, xx[1] .== xk[1] 
    DualFisherup, xx[2] .== xk[2] 
    DualFisherdn, xx[3] .== xk[3] 
end)

for ω =2:S
    for t=1:T
        JuMP.set_normalized_rhs(eq35_bal3[t,3], d_hat[t, ω]) 
        JuMP.set_normalized_rhs(eq36_bal3[t,3], d[t, ω])     
        for i=1:nGen 
            for k=1:K
                if t > 1
                    JuMP.set_normalized_coefficient(eqld1[t,i,k], xx[1][t, i, k], -d[t-k, ω]) 
                    JuMP.set_normalized_coefficient(eqld2[t,i,k], xx[2][t, i, k], -d[t-k, ω]) 
                    JuMP.set_normalized_coefficient(eqld3[t,i,k], xx[3][t, i, k], -d[t-k, ω]) 
                end
            end
        end
    end
  status = optimize!(sub)
  Q += JuMP.objective_value(sub)/S
  λg += JuMP.dual.(DualFisherg)./S 
  λup += JuMP.dual.(DualFisherup)./S 
  λdn += JuMP.dual.(DualFisherdn)./S 
end

```

In this code, `S` is the number of scenarios I’m using. So, when I have more than one scenario, I change the right-hand side and the coefficients of some constraints, optimize the model, and update the objective value and the duals of three constraints.

The values of `Q` and `λg` in this code are being updated correctly when I have more than one scenario, but the other values (`λup` and `λdn`) are not. Any ideas why this happens?

---

<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: [February 22, 2022, 5:45pm UTC](https://discourse.julialang.org/t/jump-dual-inside-loop/76895/2 "2022-02-22T17:45:52Z")

</div>

Please provide a reproducible example. I can’t run your code so I can’t see the problem.

What is the error? Just a wrong result?

---

<div class="post-metadata">

### Author: ![Mariana](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mariana/32/15064_2.png) [@Mariana](https://discourse.julialang.org/u/Mariana)
#### Post date: [February 23, 2022, 7:35pm UTC](https://discourse.julialang.org/t/jump-dual-inside-loop/76895/3 "2022-02-23T19:35:19Z")

</div>

It was just a wrong result, but I’ve already fixed it. Thanks!
