限制 google OR-Tools 上的系数值
Limit the coefficient valule on google OR-Tools
我遇到的问题与 The Stigler diet
非常相似
我已经复制了结果
在下面的代码中使用我的数据:
data = [
['f1', 10, 15, 17, 10],
['f2', 2, 11, 12, 14],
['f3', 5, 17, 16, 13],
['f4', 8, 12, 8, 16]
]
nutrients = [
["protein",15.5],
["carbohydrates",12.3]]
TM = 10
nutrients = [
["protein",15.5*TM],
["carbohydrates",12.3*TM]]
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, solver.infinity(), data[i][0])
objective.SetCoefficient(food[i], 4)
objective.SetMinimization()
# Create the constraints, one per nutrient.
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])
status = 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.')
哪个returns这个结果:
f1 = 0.770492 f3 = 8.868852 Optimal price: .64
这很好,除了我对每种食物都有上限:
例如:
f1 = 4
f2 = 6
f3 = 5
f4 =2
如何将这部分添加到约束中?
您必须将它们设置为决策变量(在本例中似乎是吃哪种食物的多少)并为描述其范围的每种食物添加上限。
所以在这种情况下:
upper_bounds = [4, 6, 5, 2]
# Objective: minimize the sum of (price-normalized) foods.
objective = solver.Objective()
for i in range(0, len(data)):
food[i] = solver.NumVar(0.0, upper_bounds[i], data[i][0])
objective.SetCoefficient(food[i], 4)
objective.SetMinimization()
请注意,solver.NumVar
有 3 个参数,第一个是下限,第二个是上限,最后是它的名字。希望这可以帮助。
我遇到的问题与 The Stigler diet
非常相似我已经复制了结果 在下面的代码中使用我的数据:
data = [
['f1', 10, 15, 17, 10],
['f2', 2, 11, 12, 14],
['f3', 5, 17, 16, 13],
['f4', 8, 12, 8, 16]
]
nutrients = [
["protein",15.5],
["carbohydrates",12.3]]
TM = 10
nutrients = [
["protein",15.5*TM],
["carbohydrates",12.3*TM]]
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, solver.infinity(), data[i][0])
objective.SetCoefficient(food[i], 4)
objective.SetMinimization()
# Create the constraints, one per nutrient.
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])
status = 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.')
哪个returns这个结果:
f1 = 0.770492 f3 = 8.868852 Optimal price: .64
这很好,除了我对每种食物都有上限: 例如:
f1 = 4
f2 = 6
f3 = 5
f4 =2
如何将这部分添加到约束中?
您必须将它们设置为决策变量(在本例中似乎是吃哪种食物的多少)并为描述其范围的每种食物添加上限。
所以在这种情况下:
upper_bounds = [4, 6, 5, 2]
# Objective: minimize the sum of (price-normalized) foods.
objective = solver.Objective()
for i in range(0, len(data)):
food[i] = solver.NumVar(0.0, upper_bounds[i], data[i][0])
objective.SetCoefficient(food[i], 4)
objective.SetMinimization()
请注意,solver.NumVar
有 3 个参数,第一个是下限,第二个是上限,最后是它的名字。希望这可以帮助。