我偶然发现了一个我希望从Julia转换为Python的软件(对Julia没有多少经验)。我遇到的主要问题是我不明白我在标注的部分中发生了什么变化#PROBLEM BELOW / ABOVE
skaters_teams是一个180 x 10矩阵(180名选手和10支球队),球队存储为二进制数组,其中skaters_teams [0]给出了球员0 ex的数组:[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]
此外,我找不到有关使用@addConstraint
的3个参数的任何文档。第一个是你要添加它的问题,第三个是你要添加的约束。第二个是什么?
@addConstraint(m, constr[i=1:num_teams], sum{skaters_teams[t, i]*skaters_lineup[t], t=1:num_skaters} <= 6*used_team[i])
答案 0 :(得分:1)
在玩了一段时间之后,我找到了解决方案。如果有人在纸浆和跳跃方面有类似的问题,这就是我使用的。
for i in range(num_teams):
m += sum(x * st[i] for x,st in zip (skaters_lineup, skaters_teams[:])) >= used_team[i]
m += sum(x * st[i] for x,st in zip (skaters_lineup, skaters_teams[:])) <= 6*used_team[i]