Alternatives to @expression for complex model constriction

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:

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)

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

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.

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

Nothing to do with JuMP really.

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

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