m = Model()
@defVar(m, 0 <= x <= 1)
@defVar(m, 0 <= y <= 1)
@setObjective(m, Max, 5x + 1y)
@addConstraint(m, con, x + y <= 1)
solve(m) # x = 1, y = 0while(someCondition) #someCondition is true when the current solution is sub-optimal
@defVar(m, 0 <= newVariable <= 1, newObjCoef , [con], [newValues]) # The variable names have to be unique every time the macro is executed
# ----------- ---------- --------- newObjCoef and newValues are obtained from the sub-problem
solve(m)
endNow suppose we have to keep adding new variables while some condition is true:while(someCondition) #someCondition is true when the current solution is sub-optimal
@defVar(m, 0 <= newVariable <= 1, newObjCoef , [con], [newValues]) # The variable names have to be unique every time the macro is executed
# ----------- ---------- --------- newObjCoef and newValues are obtained from the sub-problem
solve(m)
endNow for this to work, unique variable names has to be generated every time @defVar is executed inside the loop. I am having trouble implementing this. Can you please give any suggestion on how to do this?
@defVar(m, x, ...)x = Variable(m, ...)newColumns = Variable[] # collect the new columns here
while(someCondition) #someCondition is true when the current solution is sub-optimal
@defVar(m, 0 <= newVariable <= 1, newObjCoef , [con], [newValues])
push!(newColumns, newVariable)
setName(newVariable, string("newColumn",length(newColumns))) # only needed so that new columns display differently when calling print(m)
solve(m)
endnewColumns = Variable[] # collect the new columns here
while(someCondition) #someCondition is true when the current solution is sub-optimal
newVariable = Variable(m, 0, 1, :Cont, newObjCoef, [con], [newValues], name=string("newColumn",length(newColumns)))
push!(newColumns, newVariable)
solve(m)
endusing JuMPusing CPLEX
m = Model()
@defVar(m, 0 <= x <= 1)@defVar(m, 0 <= y <= 1)@setObjective(m, Max, 5x + 1y)con = @addConstraint(m, x + y <= 1)
solve(m) # x = 1, y = 0
# New Variables: Both With The Same Names z#------------------------------------------@defVar(m, 0 <= z <= 1, 10.0, [con], [1.0]) # The first z has optimal value 1
@defVar(m, 0 <= z <= 1, -3.0, [con], [-2.0]) # The second z has optimal value 0.5
print(m)
solve(m)
#print("The first z is ", getValue(z1), "and the second z is ", getValue(z2)) # How do I access the differnt z sprint("z= ", getValue(z)) # This seems to give the second z
# How do I access the first one?Is there a way I can access both of the zs and may be getting some information regarding their chronological order?
...
# New Variables: Both With The Same Names z
#------------------------------------------
@defVar(m, 0 <= z <= 1, 10.0, [con], [1.0]) # The first z has optimal value 1
z1 = z
@defVar(m, 0 <= z <= 1, -3.0, [con], [-2.0]) # The second z has optimal value 0.5
z2 = z
print(m)
solve(m)In[1]:
using JuMP
using CPLEX
m = Model()
@defVar(m, 0 <= x <= 1)
@defVar(m, 0 <= y <= 1)
@setObjective(m, Max, 5x + 1y)
@defConstrRef con[1:2]
for i in [1:2]
con[i]=@addConstraint(m, i* x + y <= i+5)
end
print(m)
solve(m) # x = 1, y = 0
println("x= ", getValue(x), " y= ", getValue(y))
@defVar(m, 0 <= z <= 1, 10.0, con, [1.0;-2.00]) # This does not work
# Surprisingly
#@defVar(m, 0 <= z <= 1, 10.0, [con[1]; con[2]], [1.0;-2.00])
#works but
#@defVar(m, 0 <= z <= 1, 10.0, [con[i] for i in [1:2]], [1.0;-2.00])
# does not !
solve(m) # z = 1
println("x= ", getValue(x), " y= ", getValue(y), " z= ", getValue(z))
Out[1]:
Max 5 x + y
Subject to
x + y <= 6
2 x + y <= 7
0 <= x <= 1
0 <= y <= 1
Tried aggregator 1 time.
LP Presolve eliminated 2 rows and 2 columns.
All rows and columns eliminated.
Presolve time = -0.00 sec. (0.00 ticks)
x= 1.0 y= 1.0
`Variable` has no method matching Variable(::Model, ::Int64, ::Int64, ::Symbol, ::Float64, ::JuMPArray##8734{ConstraintRef{T<:JuMPConstraint}}, ::Array{Float64,1})
while loading In[1], in expression starting on line 548@defVar(m, 0 <= z <= 1, 10.0, ConstraintRef{LinearConstraint}[con[i] for i in [1:2]], [1.0;-2.00])
In[3]:using JuMPusing CPLEXm = Model()@defVar(m, 0 <= x <= 1)@defVar(m, 0 <= y <= 1)@setObjective(m, Max, 5x + 1y)@defConstrRef con[1:2]for i in [1:2] con[i]=@addConstraint(m, i*x + y <= i+5)endprint(m)solve(m) # x = 1, y = 1println("x= ", getValue(x), " y= ", getValue(y))
# The following works: # --------------------@defVar(m, 0 <= z <= 1, 10.0, ConstraintRef{LinearConstraint}[con[i] for i in [1:2]], [1.0;-2.00]) #---------------------
# @defVar(m, 0 <= z <= 1, 10.0, con, [1.0;-2.00])# does not work# Surprisingly # @defVar(m, 0 <= z <= 1, 10.0, [con[1]; con[2]], [1.0;-2.00]) # works# But # @defVar(m, 0 <= z <= 1, 10.0, [con[i] for i in [1:2]], [1.0;-2.00])# does not work
print(m)solve(m) # z = 1println("x= ", getValue(x), " y= ", getValue(y), " z= ", getValue(z))
Out[3]:Max 5 x + ySubject to x + y <= 6 2 x + y <= 7 0 <= x <= 1 0 <= y <= 1Tried aggregator 1 time.LP Presolve eliminated 2 rows and 2 columns.All rows and columns eliminated.Presolve time = -0.00 sec. (0.00 ticks)x= 1.0 y= 1.0Max 5 x + y + 10 zSubject to x + y + z <= 6 2 x + y - 2 z <= 7 0 <= x <= 1 0 <= y <= 1 0 <= z <= 1
Iteration log . . .Iteration: 1 Objective = 16.000000x= 1.0 y= 1.0 z= 1.0
In[4]:
typeof([con[1]; con[2]])
Out[4]:
Array{ConstraintRef{GenericRangeConstraint{GenericAffExpr{Float64,Variable}}},1}In[5]:
ConstraintRef{GenericRangeConstraint{GenericAffExpr{Float64,Variable}}}[con[i] for i in [1:2]]
Out[5]:
GenericRangeConstraint not defined
while loading In[114], in expression starting on line 2
in anonymous at no file
In[6]:
ConstraintRef{LinearConstraint}[con[i] for i in [1:2]]
Out[6]:
2-element Array{ConstraintRef{GenericRangeConstraint{GenericAffExpr{Float64,Variable}}},1}:
x + y + z <= 6
2 x + y - 2 z <= 7