简体   繁体   English

纸浆限制:至少一个团队满足最低标准

[英]PULP Constraint: At least one team to meet meet minimum criteria

I have a lineup generator where I'm trying to add a constraint where every lineup I generate has at least 3 players who belong to the same team.我有一个阵容生成器,我试图在其中添加一个约束,我生成的每个阵容至少有 3 名属于同一支球队的球员。

For example: The generator has a total of 60 players to choose from (10 individuals from 6 different teams), I don't want the line up it builds (each lineup has 9 players) to be valid unless one of the teams is represented in at least 3 of the 9 positions.例如:生成器共有 60 名球员可供选择(来自 6 个不同球队的 10 个人),我不希望它建立的阵容(每个阵容有 9 名球员)有效,除非其中一支球队被代表在 9 个位置中的至少 3 个。

I currently am able to ensure it does not allow more than 5 players from any team, but I'm unsure how to enforce the minimum from at least one team.我目前能够确保它不允许任何一支球队超过 5 名球员,但我不确定如何强制至少一支球队的最低限度。

My current max per team constraint is as follows:我目前每个团队的最大限制如下:

    used_team = [pulp.LpVariable("u{}".format(i + 1), cat="Binary") for i in range(self.num_teams)]
    for i in range(self.num_teams):
        prob += (used_team[i] <= pulp.lpSum(
            self.players_teams[k][i] * players_lineup[k] for k in range(self.num_players)))
        prob += (pulp.lpSum(self.players_teams[k][i] * players_lineup[k] for k in range(self.num_players)) <= max_per_team *
                    used_team[i])

The constraint you are giving is successfully forcing the used_team variable to be active when there are active players, bound these active players by the upper bound and forces used_team to be off when there are no active players.您给出的约束是在有活跃玩家时成功强制used_team变量处于活跃状态,将这些活跃玩家限制在上限,并在没有活跃玩家时强制used_team关闭。

To ensure your other constraint you will need to add an additional binary variable used_3_in_team for each team indicating whether the 3 players from that team are reached.为确保您的其他约束,您需要为每支球队添加一个额外的二进制变量used_3_in_team ,指示是否达到该球队的 3 名球员。 Then you need to do something like the following to add the constraints.然后,您需要执行以下操作来添加约束。

for i in range(self.num_teams):
        prob += (3*used_3_in_team[i] <= pulp.lpSum(
            self.players_teams[k][i] * players_lineup[k] for k in range(self.num_players)))
prob += pulp.lpSum(self.used_3_in_team) >= 1

The first set of inequalities forces used_3_in_team to be only active when there are at least 3 players chosen.第一组不等式强制used_3_in_team仅在选择了至少 3 名玩家时才有效。 And the last inequality ensures at least one of the teams has at least 3 players active.最后一个不等式确保至少有一支球队至少有 3 名球员活跃。

I hope this solves your problem.我希望这能解决你的问题。

As you did not provide a minimum working example I did not check my code, but I hope the idea is clear.由于您没有提供最低限度的工作示例,我没有检查我的代码,但我希望这个想法很清楚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM