# Efficiently reusing/updating Julia's Convex constraints

**URL:** <https://discourse.julialang.org/t/efficiently-reusing-updating-julias-convex-constraints/5811>\
**Category:** Optimization (Mathematical)\
**Tags:** question\
**Created:** [September 10, 2017, 7:47pm UTC](https://discourse.julialang.org/t/efficiently-reusing-updating-julias-convex-constraints/5811 "2017-09-10T19:47:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jacob-roth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacob-roth/32/1862_2.png) [@jacob-roth](https://discourse.julialang.org/u/jacob-roth)\
**Post date:** [September 10, 2017, 7:47pm UTC](https://discourse.julialang.org/t/efficiently-reusing-updating-julias-convex-constraints/5811/1 "2017-09-10T19:47:00Z")

</div>

Not sure if this is the appropriate place to ask my question or if [StackOverflow](https://stackoverflow.com/questions/46144461/efficiently-reusing-updating-julia-convex-constraints) is better. Anyway, my question is related to reusing constraints in the Convex package.

I have something like the following simplified structure:

```julia
using Convex
x = Variable()
obj = square(x)
for sim_number = 1:100
    z = rand(7)
    p = minimize(obj)
    for j = 1:7
        p.constraints += [x >= z[j]]
    end
    solve!(p)
end

```

Is there a way to initialize the structure of the 7 constraints `x >= random_val[j]` outside of the `sim_number` loop so that I can reuse / update the RHS of the constraints only? For the real problem I have, setting up the constraints takes a long time, but solving is quick, so I’m seeking a way to reuse the constraint structure.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [September 10, 2017, 8:00pm UTC](https://discourse.julialang.org/t/efficiently-reusing-updating-julias-convex-constraints/5811/2 "2017-09-10T20:00:54Z")

</div>

Can’t you use `minimize(objective, constraints)` instead of iteratively adding constraints?

Compilation time in Convex.jl can definitely be an issue inside of loops. If you cannot find an alternative formulation that saves you from this bottleneck, try using JuMP.jl instead.

---

<div class="post-metadata">

**Author:** ![KrishnaKanhaiya](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krishnakanhaiya/32/443_2.png) [@KrishnaKanhaiya](https://discourse.julialang.org/u/KrishnaKanhaiya)\
**Post date:** [September 10, 2017, 8:33pm UTC](https://discourse.julialang.org/t/efficiently-reusing-updating-julias-convex-constraints/5811/3 "2017-09-10T20:33:33Z")

</div>

Try setting the RHS as :

```julia
for j = 1:7
        p.constraints[j].rhs = z[j]
        solve!(p)
end

```

---

<div class="post-metadata">

**Author:** ![jacob-roth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacob-roth/32/1862_2.png) [@jacob-roth](https://discourse.julialang.org/u/jacob-roth)\
**Post date:** [September 11, 2017, 12:24pm UTC](https://discourse.julialang.org/t/efficiently-reusing-updating-julias-convex-constraints/5811/4 "2017-09-11T12:24:38Z")

</div>

Thank you! This works well (for posterity, I’ve included an example below at the bottom..)

* * *

Anyway, I have a quick follow up. The issue I’m working through deals with adding constraints that have `Variable()`s (or expressions) on both sides in a vectorized way. For example, I have

```julia-auto
# variables
t = Variable(1)
v = Variable(7)
x = Variable(1)

# problem
p = minimize(x)
p.constraints += [t >= 0] # initialize constraint 1

# initializing first simulation
z = rand(7) # randomness
c = Vector(7) # initialize holder for constraints
for j = 1:7
    cc = t + z[j] # constraint component
    c[j] = max(cc,0)
end
p.constraints += [c <= v] # initialize constraint 2

# remaining simulations
for sim = 2:nsims
    z = rand(7) # randomness
    c = Vector(7) # initialize holder for constraints
    for j = 1:7
        cc = t + z[j]
        c[j] = max(cc,0)
    end
    p.constraints[2] = [c <= v] # update constraint 2
end

```

and I am trying efficiently impose the 7 randomness constraints in a vectorized way (later I will have many more than 7). I also have

```julia-auto
typeof(c) = Array{Any,1}
typeof(c[1]) = Convex.MaxAtom
typeof(v) = Convex.Variable
typeof(v[1]) = Convex.IndexAtom

```

but am unable to match operators (`ERROR: LoadError: MethodError: no method matching isless(::Complex{Int64}, ::Int64`). Is there a better way of enforcing these constraints in a vectorized way?

## Example (based on @KrishnaKanhaiya’s post)

* * *

```julia-auto
## example ##
using Convex

# setup
x = Variable()
obj = square(x)
sim_number = 1
z = rand(7)

# first simulation
p = minimize(obj)
for j = 1:7
    p.constraints += [x >= z[j]]
end
solve!(p)
print("==== $sim_number solved ===\n")

# remaining simulations
for sim_number = 2:100
    z = rand(7)
    for j = 1:7
        p.constraints[j].rhs = z[j]
    end
    solve!(p)
    print("==== $sim_number solved ===\n")
end

```

* * *
