我偶然发现了一个我想从 Julia 转换为 Python 的软件(对 Julia 没有太多经验)。我遇到的主要问题是我不明白在我用评论#PROBLEM BELOW/ABOVE 标记的部分中到底发生了什么
滑板队是一个 180 x 10 矩阵(180 名滑板手和 10 支球队),球队存储为二进制数组,其中滑板队 [0] 给出球员 0 的数组,例如:[1, 0, 0, 0, 0, 0, 0 , 0, 0, 0]。
m = Model(solver=GLPKSolverMIP())
# Variable for skaters in lineup
@defVar(m, skaters_lineup[i=1:num_skaters], Bin)
# Variable for goalie in lineup
@defVar(m, goalies_lineup[i=1:num_goalies], Bin)
# One goalie constraint
@addConstraint(m, sum{goalies_lineup[i], i=1:num_goalies} == 1)
# Eight Skaters constraint
@addConstraint(m, sum{skaters_lineup[i], i=1:num_skaters} == 8)
# between 2 and 3 centers
@addConstraint(m, sum{centers[i]*skaters_lineup[i], i=1:num_skaters} <= 3)
@addConstraint(m, 2 <= sum{centers[i]*skaters_lineup[i], i=1:num_skaters})
# between 3 and 4 wingers
@addConstraint(m, sum{wingers[i]*skaters_lineup[i], i=1:num_skaters} <= 4)
@addConstraint(m, 3<=sum{wingers[i]*skaters_lineup[i], i=1:num_skaters})
# between 2 and 3 defenders
@addConstraint(m, 2 <= sum{defenders[i]*skaters_lineup[i], i=1:num_skaters})
@addConstraint(m, sum{defenders[i]*skaters_lineup[i], i=1:num_skaters} <= 3)
# Financial Constraint
@addConstraint(m, sum{skaters[i,:Salary]*skaters_lineup[i], i=1:num_skaters} + sum{goalies[i,:Salary]*goalies_lineup[i], i=1:num_goalies} <= 50000)
# exactly 3 different teams for the 8 skaters constraint
@defVar(m, used_team[i=1:num_teams], Bin)
#PROBLEM BELOW
@addConstraint(m, constr[i=1:num_teams], used_team[i] <= sum{skaters_teams[t, i]*skaters_lineup[t], t=1:num_skaters})
@addConstraint(m, constr[i=1:num_teams], sum{skaters_teams[t, i]*skaters_lineup[t], t=1:num_skaters} <= 6*used_team[i])
#PROBLEM ABOVE
@addConstraint(m, sum{used_team[i], i=1:num_teams} == 3)
是否像这样循环:
for i in range(num_teams):
for t in range(num_skaters):
m += sum(skaters_teams[i][t]*skaters_lineup[t]) >=ut[i]
m += sum(skaters_teams[i][t]*skaters_lineup[t]) <=6*ut[i]
我也找不到任何关于使用 3 个参数的文档@addConstraint
。第一个是您要添加的问题,第三个是您要添加的约束。第二个是什么?
@addConstraint(m, constr[i=1:num_teams], sum{skaters_teams[t, i]*skaters_lineup[t], t=1:num_skaters} <= 6*used_team[i])