Scope of variables in optimisation

It is possible in JuMP to create a variable with a name, and then retrieve it from a model using that name. One way to do that is to index the model with a Symbol

julia> m = Model();

julia> @variable(m, x[1:3]);

julia> m[:x]
3-element Array{VariableRef,1}:
 x[1]
 x[2]
 x[3]

As for updating the constraints, if you are not deleting the previous constraints, or if your new threshold is not less than or equal to your previous threshold, then you are not actually updating the problem in any way. So you can

  1. iterate through the thresholds from largest to smallest (so that y <= Threshold is always a tighter constraint than previously) or
  2. delete constraints of the form y <= Threshold each time (probably JuMP does this under the hood anyway if you follow the first case.). Or
  3. change the right hand side using set_normalized_rhs

You can name (and access by indexing) constraints just like variables in order to modify or delete them. If you didn’t want to do that, you could also return them at the end of the function and keep track of them manually (@constraint returns a ConstraintRef).

Also, just a small note on julia style that maybe you are already aware of: the widely adopted style convention is to give function names and variables lowercase names (with underscores between words if necessary) like build_model rather than BuildModel. TitleCase is reserved for types and modules, and global constants are often declared in all caps.

Hope this helps!

1 Like