具有动态约束的 Python Pulp 整数线性规划

Cen*_*tAu 4 python linear-programming integer-programming pulp

我想求解具有以下目标函数的混合整数线性规划:

J = 最大化 (f1(x) + f2(x)) 受约束:成本(x) <= 阈值

其中 x 是选定变量的集合,f1 和 f2 是两个评分函数,cost 是成本函数。

f2 是基于所选变量之间相似性的函数。我不知道如何在纸浆中制定这个功能。

这是我的最小工作示例,其中函数 f2 是两种成分之间的相似度,我想添加similarity[i][j]到目标函数 ifj已经在选定的变量中,但不知道该怎么做。

import numpy as np
import pulp
threshold = 200
model = pulp.LpProblem('selection', pulp.LpMaximize)
similarity = np.array([[1., 0.08333333, 0.1, 0., 0., 0.0625],
                       [0.08333333, 1., 0.33333333,
                           0., 0.11111111, 0.07692308],
                       [0.1, 0.33333333, 1., 0.2, 0., 0.09090909],
                       [0., 0., 0.2, 1., 0., 0.],
                       [0., 0.11111111, 0., 0., 1., 0.27272727],
                       [0.0625, 0.07692308, 0.09090909, 0., 0.27272727, 1.]])
ingredients = ['var_%d' % i for i in range(6)]
scores = np.random.randint(1, 3, size=len(ingredients))
costs = np.random.randint(20, 60, len(ingredients))
scores = dict(zip(ingredients, scores))
costs = dict(zip(ingredients, costs))
x = pulp.LpVariable.dict(
    'x_%s', ingredients, lowBound=0, upBound=1, cat=pulp.LpInteger)
model += sum([scores[i] * x[i] for i in ingredients])
model += sum([costs[i] * x[i] for i in ingredients]) <= threshold
solver = pulp.solvers.PULP_CBC_CMD()
model.solve(solver)
Run Code Online (Sandbox Code Playgroud)

该代码基本上只考虑静态成本(以成本变量编码)。如何动态添加变量的相似性成本similarity?

Ioa*_*nis 5

我相信您想要做的是添加一个交互项,该项本质上是说当选择两种成分i和时,会产生与和j的存在相关的额外成本,这在矩阵中进行了描述。我将假设(正如您的情况一样)这是一个对称矩阵,因为和的顺序并不重要(只有两者都被选择与否才重要)。ijsimilaritysimilarityij

一个天真的表述是将这个术语添加selected[i, j] * x[i] * x[j]到目标中。这将使问题变得非线性,尽管其结构并不是非常困难,但有一个常见的建模技巧可以保持模型的线性。这里是。

我们定义了一组新变量,仅当 和都参与解决方案时才y_{ij}相等。请注意,我们可以这样定义它们,因为我们并不真正关心顺序。我们施加限制:1iji>jj<i

y_{ij} <= x_i
y_{ij} <= x_j
y_{ij} >= x_i + x_j - 1
Run Code Online (Sandbox Code Playgroud)

这组限制保证只有当和equal时才y_{ij}等于,这就是我们想要的。1x_ix_j1

您的代码的实现:

import numpy as np
import pulp
from itertools import product
threshold = 200
model = pulp.LpProblem('selection', pulp.LpMaximize)
similarity = np.array([[1., 0.08333333, 0.1, 0., 0., 0.0625],
                       [0.08333333, 1., 0.33333333,
                           0., 0.11111111, 0.07692308],
                       [0.1, 0.33333333, 1., 0.2, 0., 0.09090909],
                       [0., 0., 0.2, 1., 0., 0.],
                       [0., 0.11111111, 0., 0., 1., 0.27272727],
                       [0.0625, 0.07692308, 0.09090909, 0., 0.27272727, 1.]])
ingredients = ['var_%d' % i for i in range(6)]

ingredient_pairs = ['var_{}_{}'.format(
    ingredients.index(var[0]), ingredients.index(var[1])) 
    for var in product(ingredients, ingredients) 
    if ingredients.index(var[0]) > ingredients.index(var[1])]  
# Flatten the similarity array
indices = np.triu_indices_from(similarity)
similarity = similarity[indices]

scores = np.random.randint(1, 3, size=len(ingredients))
costs = np.random.randint(20, 60, len(ingredients))
scores = dict(zip(ingredients, scores))
costs = dict(zip(ingredients, costs))
similarity = dict(zip(ingredient_pairs, similarity))
x = pulp.LpVariable.dict(
    'x_%s', ingredients, lowBound=0, upBound=1, cat=pulp.LpInteger)
y = pulp.LpVariable.dict(
    'y_%s', ingredient_pairs, lowBound=0, upBound=1, cat=pulp.LpInteger)
model += sum([scores[i] * x[i] for i in ingredients]) + sum([
    similarity[i] * y[i] for i in ingredient_pairs])
model += sum([costs[i] * x[i] for i in ingredients]) <= threshold
for pair in ingredient_pairs:
    indexes = pair.split('_')[1:]
    for index in indexes:
        # y_{ij} <= x_i and y_{ij} <= x_j Q
        model += y[pair] <= x['var_{}'.format(index)]
    # y_{ij} >= x_i + x_j - 1
    model += y[pair] >= sum(x['var_{}'.format(i)] for i in indexes) - 1
solver = pulp.solvers.PULP_CBC_CMD()
model.solve(solver)
model.writeLP('similarity.lp')
print 'Objective: {}'.format(pulp.value(model.objective))
for v in model.variables():
    if v.varValue > 10e-4:
        print v.name, v.varValue
Run Code Online (Sandbox Code Playgroud)

我希望这有帮助。