我要解决的混合整数线性规划具有以下目标函数:Python的纸浆整数线性规划与动态约束
J =最大化(F1(x)+ F2(x)的) 受约束:成本(x)< =阈值
其中x是所选变量的集合,f1和f2是两个评分函数,成本是成本函数。
f2是基于所选变量之间的相似性的函数。我不知道如何在纸浆中制定这个功能。
这是我最小的工作示例中,函数f2是两种成分之间的相似性,我想补充similarity[i][j]
目标函数,如果j
已经在选定的变量,但不知道该怎么做。
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)
此代码基本上只考虑静态成本(编码成本变量)。我如何动态添加变量的相似性成本?
相似度数组表示的是什么 – dassouki
它基本上是一个返回元素之间相似度的矩阵。那就是这个矩阵的“(i,j)”元素是成分“i”和成分“j”之间的相似性。 @dassouki – CentAu