纸浆写适当的约束以产生可行的解决方案

时间:2019-11-17 19:14:11

标签: python pulp integer-programming

我正在尝试为15个玩家选择特定数量的灯具模型。我的LpProblem由2个二进制变量播放器和固件组成。

choices = LpVariable.dicts(
            "Choices", (fixtures, constraints["player"]), 0, 1, LpBinary)

我想使用此约束来限制一组固定装置的玩家选择数量(这很糟糕-它不计算使用的所有玩家数量):

prob += lpSum([choices[f][p] for f in fixtures for p in constraints["player"]]
                      ) <= player_count + len(fixtures) - 1, "Transfers limit"

我还设置了一个约束条件,以便为每个装置精确选择15名球员:

for fixture in fixtures:
            prob += lpSum([choices[fixture][p]
                           for p in constraints["player"]]) == player_count, str(fixture) + " Total of " + str(player_count) + " players"

我的目标是从固定装置中选取15个少量的变更,但是出于某种原因,这些约束产生了不可行的问题。例如,如果我搜索fixtures = [0,1,2],则当我将传输限制设置为45(15 * 3)时,该问题就变得可行了。我不确定如何制定转移限制约束以实现我的目标。

示例:

players = [1, 2, 3, 4, 5, 6]
fixtures = [1, 2, 3]

prob = LpProblem(
    "Fantasy football selection", LpMaximize)

choices = LpVariable.dicts(
    "Players", (fixtures, players), 0, 1, LpBinary)

# objective function
prob += lpSum([predict_score(f, p) * choices[f][p]
               for p in players for f in fixtures]), "Total predicted score"

# constraints
for f in fixtures:
    # total players for each fixture
    prob += lpSum([choices[f][p] for p in players]) == 2, ""
    if f != fixtures[0]:
        # max of 1 change between fixtures
        prob += lpSum([1 if choices[f-1][p] != choices[f]
                       [p] else 0 for p in players]) <= 2, ""

prob.solve()
print("Status: ", LpStatus[prob.status])

1 个答案:

答案 0 :(得分:1)

我建议引入其他二进制变量,这些变量可用于跟踪是否在f和夹具f-1之间进行了更改。然后,您可以在允许的更改数量上应用约束。

在下面的示例代码中,如果注释掉最后一个约束,则会发现可以实现更高的目标,但需要付出更多的更改。还要注意,我为在目标函数中具有非零的changes变量添加了一个小小的惩罚-这是在不进行更改的情况下将其强制为零-这种方法不需要这个小的惩罚但是可能会更容易了解发生了什么。

没有最后一个约束应该获得118的目标值,但只有109的目标值。

from pulp import *
import random

players = [1, 2, 3, 4, 5]
fixtures = [1, 2, 3, 4]
random.seed(42)

score_dict ={(f, p):random.randint(0,20) for f in fixtures for p in players}

def predict_score(f,p):
    return score_dict[(f,p)]

prob = LpProblem(
    "Fantasy football selection", LpMaximize)

# Does fixture f include player p
choices = LpVariable.dicts(
    "choices", (fixtures, players), 0, 1, LpBinary)

changes = LpVariable.dicts(
    "changes", (fixtures[1:], players), 0, 1, LpBinary)

# objective function
prob += lpSum([predict_score(f, p) * choices[f][p]
               for p in players for f in fixtures]
              ) - lpSum([[changes[f][p] for f in fixtures[1:]] for p in players])/1.0e15, "Total predicted score"

# constraints
for f in fixtures:
    # Two players for each fixture
    prob += lpSum([choices[f][p] for p in players]) == 2, ""

    if f != fixtures[0]:
        for p in players:
            # Assign change constraints, force to one if player
            # is subbed in or out
            prob += changes[f][p] >= (choices[f][p] - choices[f-1][p])
            prob += changes[f][p] >= (choices[f-1][p] - choices[f][p])

        # Enforce only one sub-in + one sub-out per fixture (i.e. at most one change)
        # prob += lpSum([changes[f][p] for p in players]) <= 2

prob.solve()
print("Status: ", LpStatus[prob.status])

print("Objective Value: ", prob.objective.value())

choices_soln = [[choices[f][p].value() for p in players] for f in fixtures]
print("choices_soln")
print(choices_soln)

changes_soln = [[changes[f][p].value() for p in players] for f in fixtures[1:]]
print("changes_soln")
print(changes_soln)