DataFrame的Google OR工具约束 [英] Google OR Tools constraints from DataFrame
问题描述
我想构建一个Google OR工具模型,以对 CBC_MIXED_INTEGER_PROGRAMMING
使用 linear_solver
。
在 Google教程之后,我学到了建立约束的热度问题... 是否需要手写每个约束?
我的意思是,我有以下包含系数的DataFrame df_constraint
ax + by <= c
形式的约束。
I would like to build a Google OR Tools model to use linear_solver
for a CBC_MIXED_INTEGER_PROGRAMMING
.
Following Google tutorial I learned hot to build the constraints but I have a question...is it necessary to hand write every constraint?
I mean, I have the following DataFrame df_constraint
which contain the coefficient of the constraints in the form of ax+by<=c
.
+---+---+---+
| A | B | C |
+---+---+---+
| 1 | 5 | 7 |
| 2 | 9 | 3 |
| 3 | 0 | 4 |
+---+---+---+
表可能是转换为以下约束
the table could be translated into the following contraints
# 1x+5y<=7
constraint1 = solver.Constraint(-solver.infinity(), 7)
constraint1.SetCoefficient(x, 1)
constraint1.SetCoefficient(y, 5)
# 2x+9y<=3
constraint2 = solver.Constraint(-solver.infinity(), 3)
constraint2.SetCoefficient(x, 2)
constraint2.SetCoefficient(y, 9)
# 3x<=4
constraint3 = solver.Constraint(-solver.infinity(), 4)
constraint3.SetCoefficient(x, 3)
我不想写每行,而是这样:
Instead of write every rows I would like something like this:
for index, row in df.iterrows():
constraint = solver.Constraint(-solver.infinity(), row['C'])
constraint.SetCoefficient(x, row['A'])
constraint.SetCoefficient(y, row['B'])
我的代码段无效,因为每个约束必须使用不同的名称(li ke constraint1
, constraint2
,...)。
My snippet won't work, as every constraint must have a different name (like constraint1
, constraint2
, ...).
推荐答案
实际上,OR-Tools不需要每个约束都具有唯一的名称。但是无论如何,以下内容为它们提供了唯一的名称。如上所述,如果需要存储约束,则可以按如下所示在数组中进行存储。在这里,我使用的是更常用的符号(A是约束系数,B是约束右侧,c是目标系数)。
In fact, OR-Tools doesn't require each constraint to have a unique name. But the following gives them unique names anyway. As mentioned above, if you need to store the constraints, you can do so in an array as follows. Here I'm using the more common notation (A is the constraint coefficients, B is the constraint right-hand sides, c is the objective coefficients). But it will adapt to your Pandas setup.
from ortools.linear_solver import pywraplp # adapted from one of the examples
inf = float("inf")
AB = [
[1, 0, 1], # x <= 1
[0, 1, 2], # y <= 2
[1, 1, 2], # x + y <= 2
[-1, -1, 0] # x + y >= 0
]
c = [3, 1]
def main():
solver = pywraplp.Solver('simple_lp_program',
pywraplp.Solver.GLOP_LINEAR_PROGRAMMING)
x = solver.NumVar(-inf, inf, 'x') # no UB or LB on x, y
y = solver.NumVar(-inf, inf, 'y')
cts = []
for i, (*a, b) in enumerate(AB):
ct = solver.Constraint(-inf, b, 'ct' + str(i))
ct.SetCoefficient(x, a[0])
ct.SetCoefficient(y, a[1])
cts.append(ct)
print('Number of constraints =', solver.NumConstraints())
objective = solver.Objective()
objective.SetCoefficient(x, c[0])
objective.SetCoefficient(y, c[1])
objective.SetMaximization()
solver.Solve()
print('Solution:')
print('Objective value =', objective.Value())
print('x =', x.solution_value())
print('y =', y.solution_value())
if __name__ == '__main__':
main()
这篇关于DataFrame的Google OR工具约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!