# Alternatives to @expression for complex model constriction

**URL:** <https://discourse.julialang.org/t/alternatives-to-expression-for-complex-model-constriction/1829>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [February 1, 2017, 8:58pm UTC](https://discourse.julialang.org/t/alternatives-to-expression-for-complex-model-constriction/1829 "2017-02-01T20:58:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![adowling2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adowling2/32/539_2.png) [@adowling2](https://discourse.julialang.org/u/adowling2)\
**Post date:** [February 1, 2017, 8:58pm UTC](https://discourse.julialang.org/t/alternatives-to-expression-for-complex-model-constriction/1829/1 "2017-02-01T20:58:05Z")

</div>

I have created a rather complex set of Julia functions to assemble versions of an optimization problem. With a certain set of user inputs, I need to define “x” in my problem. For other inputs, I know that “x” can be removed from my model. Because x can involve thousands of elements, I’d prefer to not declare it in my model unless it is needed.

Now assume I have a bunch of constraints that involve “x”. One option is to declare these constraints in an if loop structure. One version for if “x” is defined and another for the case it is not. This gets tedious. Below is my current work around:

```julia
if( #= some logical condition =#)
    @variable(m, x[i=I, j=J])
else
    @expression(m, x[i=I, j=J], 0.0)
end

```

Two concerns:

- Does this use more memory than needed? Is there another way to set x = 0 but still allow x[i,j] to be used in a constraint?
- The above expression syntax works fine if I and J are type `UnitRange{Int64}`. If one is type `Array{Int64,1}`, I get an error:

`no method matching setindex!(::JuMP.JuMPArray{JuMP.GenericAffExpr{Float64,JuMP.Variable},2,Tuple{UnitRange{Int64},Array{Int64,1}}}, ::Float64, ::Int64, ::Int64)`

---

<div class="post-metadata">

**Author:** ![adowling2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adowling2/32/539_2.png) [@adowling2](https://discourse.julialang.org/u/adowling2)\
**Post date:** [February 1, 2017, 9:06pm UTC](https://discourse.julialang.org/t/alternatives-to-expression-for-complex-model-constriction/1829/2 "2017-02-01T21:06:49Z")

</div>

I reported concern 2 as an issue on Github: [https://github.com/JuliaOpt/JuMP.jl/issues/955](https://github.com/JuliaOpt/JuMP.jl/issues/955)

---

<div class="post-metadata">

**Author:** ![miles.lubin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miles.lubin/32/279_2.png) [@miles.lubin](https://discourse.julialang.org/u/miles.lubin)\
**Post date:** [February 5, 2017, 10:40pm UTC](https://discourse.julialang.org/t/alternatives-to-expression-for-complex-model-constriction/1829/3 "2017-02-05T22:40:58Z")

</div>

> [@adowling2](#):
>
> - Does this use more memory than needed? Is there another way to set x = 0 but still allow x[i,j] to be used in a constraint?

You’re free to define and use your own Julia custom type that returns zero on any `getindex` operation and doesn’t use any storage.

---

<div class="post-metadata">

**Author:** ![adowling2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adowling2/32/539_2.png) [@adowling2](https://discourse.julialang.org/u/adowling2)\
**Post date:** [February 6, 2017, 10:39pm UTC](https://discourse.julialang.org/t/alternatives-to-expression-for-complex-model-constriction/1829/4 "2017-02-06T22:39:24Z")

</div>

Miles - Excellent suggestion. Where should I start looking in the JuMP src as I attempt to implement this?

---

<div class="post-metadata">

**Author:** ![miles.lubin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miles.lubin/32/279_2.png) [@miles.lubin](https://discourse.julialang.org/u/miles.lubin)\
**Post date:** [February 7, 2017, 12:24am UTC](https://discourse.julialang.org/t/alternatives-to-expression-for-complex-model-constriction/1829/5 "2017-02-07T00:24:17Z")

</div>

Nothing to do with JuMP really.

```julia
type DummyVariable
end
Base.getindex(::DummyVariable, idx...) = 0.0

x = DummyVariable()
@show x[1,2]

```
