Hello, folks! I am new to JuMP, so my apologies if it sounds a naive question.
I wrap an optimisation problem in a function and then run it by changing input data in a loop. I define the variables as global so that these can be called in a loop; however, I understand that global variables should be avoided. I have given a snapshot of the code.
I would like to pick your brains on:
a. Can the problem below be defined without use of global variables?
b. If I have to vary the range for which variable
y <= Threshold is valid, I change these values in a loop as shown in the code, though I am not sure if it is a recommended way of doing it. Reading JuMP documentation, my understanding is that modifying right hand side of constraint is possible; however, here it concerns modifying limits on the variable. Do I need to remove the limits on the variable y and instead define it as a separate constraint? (see the way I have done it below).
I will appreciate any help.
function BuildModel(MXData,Threshold) model = Model(CPLEX.Optimizer) @variable(model,x[1:3]) global x @variable(model,y <= Threshold) global y @objective(model,Max,sum(MXData[i]*x[i] for i=1:3) @contraint(model, x+y<=6) return model end Threshold =3 # Run optimisation by updating data for k = 1:6 MXData = # read data from files Generator = BuildModel(MXData,Threshold) optimize!(Generator) FirstVariable = JuMP.value.(x) SecondVariable = JuMP.value.(y) end # Vary the limit of variable y MXData = # read data from the relevant file for Threshold = -0.2:0.01:3 Generator = BuildModel(MXData,Threshold) optimize!(Generator) FirstVariable = JuMP.value.(x) SecondVariable = JuMP.value.(y) end