Bre*_*hio 9 python linear-programming combinatorics cplex pulp
在Python PuLP中,线性编程约束可以变成弹性子问题.
http://www.coin-or.org/PuLP/pulp.html?highlight=lpsum#elastic-constraints
解决子问题可以优化距目标值的距离.
当然,目标值是这个子问题的最佳解决方案,但弹性化的全部意义在于我们认为这种解决方案可能是不可行的.
如何将子问题纳入整体问题?我尝试按照添加约束的方式将其添加到问题中,这会引发类型错误.我尝试将它放在目标函数中,这也不起作用.
我在上面的文档或此处托管的示例中找不到任何内容:
https://code.google.com/p/pulp-or/wiki/OptimisationWithPuLP?tm=6
这是我制定的子问题:
capacity = LpConstraint(e=lpSum([ x[m][n] * len(n.items) for n in N ]),
sense=-1, rhs=30, name=str(random.random()))
stretch_proportion = 30/50
elasticCapacity = capacity.makeElasticSubProblem(penalty=50,
proportionFreeBoundList=[1,stretch_proportion])
Run Code Online (Sandbox Code Playgroud)
以下是我认为必须将其纳入LP目标的最接近的事情:
def sub(m):
capacity = LpConstraint(e=lpSum([ x[m][n] * len(n.items) for n in N ]),
sense=-1, rhs=30, name=str(random.random()))
stretch_proportion = 30/50
elasticCapacity = capacity.makeElasticSubProblem(penalty=50,
proportionFreeBoundList=[1,stretch_proportion])
elasticCapacity.solve()
return elasticCapacity.isViolated()
Run Code Online (Sandbox Code Playgroud)
...
prob += lpSum( [ x[m][n] * reduce(op.add, map(D2, [i.l for i in n.items], [j.l for j in n.items]))\
for n in N for m in M ] ) + 50 * sub(m)
Run Code Online (Sandbox Code Playgroud)
这是一个简短的答案,以工作示例的草图形式:
创建问题,并添加硬约束和目标.
prob = LpProblem("My Problem", LpMinimize)
....
Run Code Online (Sandbox Code Playgroud)
完成之后,定义软(弹性)约束并将其添加到问题中pulp.prob.extend(),如下所示:
c_e_LHS = LpAffineExpression([(var1,coeff1), (var2,coeff2)]) # example left-hand-side expression
c_e_RHS = 30 # example right-hand-side value
c_e_pre = LpConstraint(e=el_constr_LHS, sense=-1, name='pre-elastic', rhs=c_e_RHS) # The constraint LHS = RHS
c_e = c_e_pre.makeElasticSubProblem(penalty=100, proportionFreeBoundList=[.02,.02]) # The definition of the elasticized constraint
prob.extend(c_e) # Adding the constraint to the problem
Run Code Online (Sandbox Code Playgroud)
此时问题已被修改为包含软(弹性)约束,您可以解决它.$\$ QED.
这是一个更长的答案:这个问题在添加弹性约束的纸浆或讨论Google小组中得到了回答.为了我自己的目的,我创建了以下示例,基于该讨论以及PuLP文档网站上混合问题的较长公式.
首先你创建问题:
from pulp import *
prob = LpProblem("The Whiskas Problem", LpMinimize)
Run Code Online (Sandbox Code Playgroud)
创建成分列表:
Ingredients = ['CHICKEN', 'BEEF', 'MUTTON', 'RICE', 'WHEAT', 'GEL']
Run Code Online (Sandbox Code Playgroud)
创建每种成分的成本字典:
costs = {'CHICKEN': 0.013,
'BEEF': 0.008,
'MUTTON': 0.010,
'RICE': 0.002,
'WHEAT': 0.005,
'GEL': 0.001}
Run Code Online (Sandbox Code Playgroud)
创建每种成分中蛋白质百分比的字典:
proteinPercent = {'CHICKEN': 0.100,
'BEEF': 0.200,
'MUTTON': 0.150,
'RICE': 0.000,
'WHEAT': 0.040,
'GEL': 0.000}
Run Code Online (Sandbox Code Playgroud)
创建每种成分中脂肪百分比的字典:
fatPercent = {'CHICKEN': 0.080,
'BEEF': 0.100,
'MUTTON': 0.110,
'RICE': 0.010,
'WHEAT': 0.010,
'GEL': 0.000}
Run Code Online (Sandbox Code Playgroud)
创建每种成分中纤维百分比的字典:
fibrePercent = {'CHICKEN': 0.001,
'BEEF': 0.005,
'MUTTON': 0.003,
'RICE': 0.100,
'WHEAT': 0.150,
'GEL': 0.000}
Run Code Online (Sandbox Code Playgroud)
创建每种成分中盐百分比的字典:
saltPercent = {'CHICKEN': 0.002,
'BEEF': 0.005,
'MUTTON': 0.007,
'RICE': 0.002,
'WHEAT': 0.008,
'GEL': 0.000}
Run Code Online (Sandbox Code Playgroud)
创建'prob'变量以包含问题数据:
prob = LpProblem("The Whiskas Problem", LpMinimize)
Run Code Online (Sandbox Code Playgroud)
创建一个名为'ingredient_vars'的字典以包含引用的变量:
ingredient_vars = LpVariable.dicts("Ingr",Ingredients,0)
Run Code Online (Sandbox Code Playgroud)
添加目标:
prob += lpSum([costs[i]*ingredient_vars[i] for i in Ingredients]), "Total Cost of Ingredients per can"
Run Code Online (Sandbox Code Playgroud)
创建硬约束(这是我的示例开始偏离文档中的那个):
c1 = lpSum([ingredient_vars[i] for i in Ingredients]) == 100, "PercentagesSum"
c2 = lpSum([proteinPercent[i] * ingredient_vars[i] for i in Ingredients]) >= 8.0, "ProteinRequirement"
c3 = lpSum([fatPercent[i] * ingredient_vars[i] for i in Ingredients]) >= 6.0, "FatRequirement"
c4 = lpSum([fibrePercent[i] * ingredient_vars[i] for i in Ingredients]) <= 2.0, "FibreRequirement"
c5 = lpSum([saltPercent[i] * ingredient_vars[i] for i in Ingredients]) <= 0.4, "SaltRequirement"
Run Code Online (Sandbox Code Playgroud)
添加硬约束:
for con in [c1,c2,c3,c4,c5]:
prob += con
Run Code Online (Sandbox Code Playgroud)
定义要弹性化的约束的左侧表达式:
c6_LHS = LpAffineExpression([(ingredient_vars['GEL'],1), (ingredient_vars['BEEF'],1)])
Run Code Online (Sandbox Code Playgroud)
定义要弹性的约束:凝胶和牛肉总量小于30%:
c6= LpConstraint(e=c6_LHS, sense=-1, name='GelBeefTotal', rhs=30)
Run Code Online (Sandbox Code Playgroud)
定义弹性约束:
c6_elastic = c6.makeElasticSubProblem(penalty = 100, proportionFreeBoundList = [.02,.02])
Run Code Online (Sandbox Code Playgroud)
而这是一个弹性(即软)约束添加到该问题的方法:
prob.extend(c6_elastic)
Run Code Online (Sandbox Code Playgroud)
解决这个问题:
prob.writeLP("WhiskasModel.lp")
prob.solve()
Run Code Online (Sandbox Code Playgroud)
输出最佳解决方案:
for v in prob.variables():
print v.name, "=", v.varValue
Run Code Online (Sandbox Code Playgroud)
如果您使用惩罚和界限,您可以验证它是否像宣传的那样有效.
PS,我的理解是问题的标题可能会产生误导.添加弹性子问题相当于向目标添加一些成本项,对应于"软约束".软约束不是约束 - 它是目标中一组成本项的简写.