如何“为所有人"在Julia/JuMP中的总和符号 [英] How to do "for all" in sum notation in Julia/JuMP
问题描述
我正在尝试使用JuMP在Julia中为线性优化问题添加约束.我正在使用sum{}
函数,但是在某些约束方面遇到了麻烦.有谁知道如何在JuMP中写所有人"(颠倒的A)?这是我到目前为止的代码:
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
我想要得到的是:
我正在尝试使用for循环代替从1到5的所有i",但是我不断出错.还有另一种方法吗?
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?
推荐答案
用数学符号表示,您对i
求和,并对每个j
求和.
在Julia/JuMP中,您可以将∀"视为一个for
循环(所有人"),
和Σ"为sum{ }
:
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
这篇关于如何“为所有人"在Julia/JuMP中的总和符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!