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 typeArray{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)