# Time varying constrained optimization

**URL:** <https://discourse.julialang.org/t/time-varying-constrained-optimization/10926>\
**Category:** Optimization (Mathematical)\
**Tags:** question\
**Created:** [May 16, 2018, 12:01pm UTC](https://discourse.julialang.org/t/time-varying-constrained-optimization/10926 "2018-05-16T12:01:50Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![tfk17lstm](https://avatars.discourse-cdn.com/v4/letter/t/f0a364/32.png) [@tfk17lstm](https://discourse.julialang.org/u/tfk17lstm)\
**Post date:** [May 16, 2018, 12:01pm UTC](https://discourse.julialang.org/t/time-varying-constrained-optimization/10926/1 "2018-05-16T12:01:50Z")

</div>

Dear Julia community,

This is Edgar a completely newbie in Julia that has been looking for an alternative to R and Matlab for solving optimization problems. After reading some examples available at JuliaOpt I think that is the right tool for solving my problem. However I did not fin anything relating with my specific question mentioned in the title.

Giving some context I am trying to solve the unit commitment problem . Lets denote G as the set containing generator g=1,2… and T the set containing the number of periods t=1,2… of the optimization.

My problem is that despite of having defined my objetive function (which is basically the fuel costs + the startup/shutdown costs) and the prediction of all my constrains (which basically are the power demand + the maximum available capacity depending on the weather conditions) I do not know how to implement that my lower and upper bounds of the problem (min and max capacity) are changing over the time. Basically I want to implement that:

lb\_i \leq x\_it \leq ub\_i

Will become:

lb\_it \leq x\_it \leq ub\_it

Has anyone tried something like that before?

Thank you in advance

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [May 16, 2018, 12:36pm UTC](https://discourse.julialang.org/t/time-varying-constrained-optimization/10926/2 "2018-05-16T12:36:44Z")

</div>

Welcome Edgar! I can only offer this: Put your latex equations into $ to render them.

---

<div class="post-metadata">

**Author:** ![mbesancon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbesancon/32/6528_2.png) [@mbesancon](https://discourse.julialang.org/u/mbesancon)\
**Post date:** [May 16, 2018, 2:47pm UTC](https://discourse.julialang.org/t/time-varying-constrained-optimization/10926/3 "2018-05-16T14:47:04Z")

</div>

Hi,  
I think [JuMP](http://www.juliaopt.org/JuMP.jl/0.18/) might be a better tool for the Unit Commitment problem, you’re going to use binary variables and will want to define your constraints in a convenient way. Give it a try with a MILP solver like Cbc or GLPK.

```julia
# add the required packages
Pkg.add("JuMP")
Pkg.add("Cbc")
# import into working environment
using JuMP
using Cbc: CbcSolver
# start playing 

```

See [the doc](http://www.juliaopt.org/JuMP.jl/0.18/refexpr.html) for how to express constraints

---

<div class="post-metadata">

**Author:** ![pkofod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pkofod/32/2179_2.png) [@pkofod](https://discourse.julialang.org/u/pkofod)\
**Post date:** [May 16, 2018, 8:04pm UTC](https://discourse.julialang.org/t/time-varying-constrained-optimization/10926/4 "2018-05-16T20:04:59Z")

</div>

> [@mbesancon](#):
>
> might be a better tool

better than what? 🙂 He was quoting JuliaOpt, the home of JuMP.

---

<div class="post-metadata">

**Author:** ![mbesancon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbesancon/32/6528_2.png) [@mbesancon](https://discourse.julialang.org/u/mbesancon)\
**Post date:** [May 16, 2018, 8:27pm UTC](https://discourse.julialang.org/t/time-varying-constrained-optimization/10926/5 "2018-05-16T20:27:39Z")

</div>

Oh that’s on me, my poor brain parsed “Optim” instead of JuliaOpt

---

<div class="post-metadata">

**Author:** ![mbesancon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbesancon/32/6528_2.png) [@mbesancon](https://discourse.julialang.org/u/mbesancon)\
**Post date:** [May 16, 2018, 8:34pm UTC](https://discourse.julialang.org/t/time-varying-constrained-optimization/10926/6 "2018-05-16T20:34:56Z")

</div>

```julia
m = Model(solver = ClpSolver())
# x[i,t]
@variable(m, x[1:2,1:3] >= 0)
upper_bounds = [10.0 12.5 13.5;14.0 15.0 16.0]
lower_bounds = [5.0 2.5 3.5; 4.0 1.0 1.0]
@constraint(m, [i=1:2,t=1:3], x[i,t] >= lower_bounds[i,t])
@constraint(m, [i=1:2,t=1:3], x[i,t] <= upper_bounds[i,t])

```

---

<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:** [May 16, 2018, 9:14pm UTC](https://discourse.julialang.org/t/time-varying-constrained-optimization/10926/7 "2018-05-16T21:14:52Z")

</div>

An even simpler way is just:

```julia
m = Model()
@variable(m, lower_bounds[i,t] <= x[i=1:2, t=1:3] <= upper_bounds[i,t])

```

---

<div class="post-metadata">

**Author:** ![tfk17lstm](https://avatars.discourse-cdn.com/v4/letter/t/f0a364/32.png) [@tfk17lstm](https://discourse.julialang.org/u/tfk17lstm)\
**Post date:** [May 17, 2018, 9:00am UTC](https://discourse.julialang.org/t/time-varying-constrained-optimization/10926/8 "2018-05-17T09:00:49Z")

</div>

Sorry Mauro! I forgot to write the $, my fault 😅

---

<div class="post-metadata">

**Author:** ![tfk17lstm](https://avatars.discourse-cdn.com/v4/letter/t/f0a364/32.png) [@tfk17lstm](https://discourse.julialang.org/u/tfk17lstm)\
**Post date:** [May 17, 2018, 9:00am UTC](https://discourse.julialang.org/t/time-varying-constrained-optimization/10926/9 "2018-05-17T09:00:51Z")

</div>

I did not express myself correctly. I was saying that Julia will be a better tool than R. For the moment all the pre-built optimization packages that I have tried (NLoptr, ROI, NlcOptim etc) did not allow these options. My original idea was to implement the lagrangian relaxation myselft (and I did it) but the program is quite slow and I have to select a duality gap to big.
