问题
I am trying to add constraints to a linear optimization problem in Julia using JuMP. I am using the sum{}
function however, I am having trouble with some of the constraints. Does anyone know how to write "for all" in JuMP (the upside down A)? Here is the code I have so far:
using JuMP
m = Model()
c= [3 5 2 ; 4 3 5 ; 4 5 3 ; 5 4 3 ; 3 5 4]
@variable(m, x[i=1:5,j=1:3] >= 0)
@objective(m,Min,sum{c[i,j]*x[i,j],i=1:5,j=1:3})
for i=1:5
@constraint(m, sum{x[i,j],i,j=1:3} <= 480)
end
What I am trying to get is this:
I am trying to use the for loop as a substitute of "for all i from 1 to 5" however I keep getting errors. Is there another way to do this?
回答1:
In mathematical notation, you sum across i
, and do so for each j
.
In Julia/JuMP, you can think of "∀" as being a for
loop ("for all"),
and a "Σ" as being a sum{ }
:
using JuMP
m = Model()
c= [3 5 2;
4 3 5;
4 5 3;
5 4 3;
3 5 4]
# x_ij >= 0 ∀ i = 1,...,5, j = 1,...,3
@variable(m, x[i=1:5,j=1:3] >= 0)
@objective(m,Min,sum{c[i,j]*x[i,j],i=1:5,j=1:3})
# ∀j = 1,...,3
for j in 1:3
@constraint(m, sum{x[i,j],i=1:5} <= 480)
end
来源:https://stackoverflow.com/questions/31944602/how-to-do-for-all-in-sum-notation-in-julia-jump