我有一张下表,从中必须创建一个具有特定蛋白质和碳水化合物价值的配方。
并使用or-tools解决此问题,到目前为止,我有:
格式化的数据
data = [
['f1', 10, 15, 17, 10],
['f2', 2, 11, 12, 14],
['f3', 6.5, 17, 16, 13],
['f4', 8, 12, 8, 16]
]
营养素的约束:
营养素= [ [“蛋白质”,15.5], [“碳水化合物”,12.3]]目标函数,其中上限“数据i”是该特定元素的库存。
food = [[]] * len(data)
# Objective: minimize the sum of (price-normalized) foods.
objective = solver.Objective()
for i in range(0, len(data)):
food[i] = solver.NumVar(0.0, data[i][1], data[i][0])
objective.SetCoefficient(food[i], 4)
objective.SetMinimization()
我还约束了每种营养素的要求值:
constraints = [0] * (len(nutrients))
for i in range(0, len(nutrients)):
constraints[i] = solver.Constraint(nutrients[i][1], solver.infinity())
for j in range(0, len(data)):
constraints[i].SetCoefficient(food[j], data[j][i+3])
最后是求解器:
状态= Solver.Solve()
if status == solver.OPTIMAL:
# Display the amounts (in dollars) to purchase of each food.
price = 0
num_nutrients = len(data[i]) - 3
nutrients = [0] * (len(data[i]) - 3)
for i in range(0, len(data)):
price += food[i].solution_value()
for nutrient in range(0, num_nutrients):
nutrients[nutrient] += data[i][nutrient+3] * food[i].solution_value()
if food[i].solution_value() > 0:
print ("%s = %f" % (data[i][0], food[i].solution_value()))
print ('Optimal price: $%.2f' % (price))
else: # No optimal solution was found.
if status == solver.FEASIBLE:
print ('A potentially suboptimal solution was found.')
else:
print ('The solver could not solve the problem.')
到这部分工作正常,我得到的结果如下:
f1 = 0.077049
f3 = 0.886885
Optimal price: $0.96
知道我还需要添加将要制造的公斤数的约束,这些约束也必须满足之前的约束。
我的第一个猜测是要增加营养素需求量
factor = 10
nutrients = [
["protein",15.5*factor],
["carbohydrates",12.3*factor]]
我会多吃10倍的食物,但后来我意识到这是不对的,因为我需要的是浓缩食品。
我需要10公斤蛋白质和15.5蛋白质/公斤的碳水化合物 我需要的约束是这样的:
(f1*W + f2*X + f3*Y + f4*Z)/(W+X+Y+Z) = 10kg with 15.5 protein/kg and 12.3 carbohydrates/kg
Where W, X, Y and Z are the kg of each food
如何将此约束添加到求解器?
答案 0 :(得分:2)
(f1*W + f2*X + f3*Y + f4*Z)/(W+X+Y+Z) = 10
与
相同f1*W + f2*X + f3*Y + f4*Z = 10*(W+X+Y+Z)
现在是线性的。
并且,如果我们错过了一些数学课,我们可以将其写为标准的LP约束:
(f1-10)*W + (f2-10)*X + (f3-10)*Y + (f4-10)*Z = 0