在 DOCPLEX 中添加多个二次约束
Adding multiple quadratic constraints in DOCPLEX
我正在尝试向 docplex 模型添加二次约束,但它会将它们解释为线性并给我错误消息。
以下是脚本的节选
这里是二次约束:
mdl.add_constraints((mdl.sum(Allocation[o,p] * Folds[o,p] for o in rangeOils) == proddf['Demand'][p] for p in rangeProd))
这是它所在的位置
rangeOils = range(nb_Oils)
rangeProd = range(nb_Products)
#this is our decision variable it containts a matrix of the oils and the products
Allocation = mdl.continuous_var_matrix(keys1=nb_Oils, keys2=nb_Products, lb=0)
#this is another decision variable it contains a matrix of the folding an oil needs in a product
Folds = mdl.continuous_var_matrix(keys1=nb_Oils, keys2=nb_Products, lb=1)
#adding demand constraint - demand must be met even after a product is folded
mdl.add_constraints((mdl.sum(Allocation[o,p] * Folds[o,p] for o in rangeOils) == proddf['Demand'][p] for p in rangeProd))
任何帮助将不胜感激
这里是错误
DOcplexException: Expecting linear constraint, got: x1*x155+x3*x157+x5*x159+x7*x161+x9*x163+x11*x165+x13*x167+x15*x169+x17*x171+x19*x173+x21*x175+x23*x177+x25*x179+x27*x181+x29*x183+x31*x185+x33*x187+x35*x189+x37*x191+x39*x193+x41*x195+x43*x197+x45*x199+x47*x201+x49*x203+x51*x205+x53*x207+x55*x209+x57*x211+x59*x213+x61*x215+x63*x217+x65*x219+x67*x221+x69*x223+x71*x225+x73*x227+x75*x229+x77*x231+x79*x233+x81*x235+x83*x237+x85*x239+x87*x241+x89*x243+x91*x245+x93*x247+x95*x249+x97*x251+x99*x253+x101*x255+x103*x257+x105*x259+x107*x261+x109*x263+x111*x265+x113*x267+x115*x269+x117*x271+x119*x273+x121*x275+x123*x277+x125*x279+x127*x281+x129*x283+x131*x285+x133*x287+x135*x289+x137*x291+x139*x293+x141*x295+x143*x297+x145*x299+x147*x301+x149*x303+x151*x305+x153*x307 == 500000 with type: <class 'docplex.mp.constr.QuadraticConstraint'>
不是很好的语法,但以下应该可以工作:
for p in rangeProd:
mdl.add_constraint(mdl.sum(Allocation[o,p] * Folds[o,p] for o in rangeOils) == proddf['Demand'][p])
Performance-wise 这应该没问题,因为无论如何在引擎级别添加约束 one-by-one。
显然 add_constraints()
有问题,因为在下面的代码中一些语句意外失败:
with Model() as m:
x = m.continuous_var()
y = m.continuous_var()
# This works
m.add_constraint(m.sum([x * y]) <= 1)
# This works
for i in range(3):
m.add_constraint(m.sum([x * y]) <= i)
# This fails
m.add_constraints((m.sum([x * y]) <= i for i in range(3)))
# This fails
m.add_constraints([m.sum([x * y]) <= i for i in range(3)])
我正在尝试向 docplex 模型添加二次约束,但它会将它们解释为线性并给我错误消息。
以下是脚本的节选
这里是二次约束:
mdl.add_constraints((mdl.sum(Allocation[o,p] * Folds[o,p] for o in rangeOils) == proddf['Demand'][p] for p in rangeProd))
这是它所在的位置
rangeOils = range(nb_Oils)
rangeProd = range(nb_Products)
#this is our decision variable it containts a matrix of the oils and the products
Allocation = mdl.continuous_var_matrix(keys1=nb_Oils, keys2=nb_Products, lb=0)
#this is another decision variable it contains a matrix of the folding an oil needs in a product
Folds = mdl.continuous_var_matrix(keys1=nb_Oils, keys2=nb_Products, lb=1)
#adding demand constraint - demand must be met even after a product is folded
mdl.add_constraints((mdl.sum(Allocation[o,p] * Folds[o,p] for o in rangeOils) == proddf['Demand'][p] for p in rangeProd))
任何帮助将不胜感激
这里是错误
DOcplexException: Expecting linear constraint, got: x1*x155+x3*x157+x5*x159+x7*x161+x9*x163+x11*x165+x13*x167+x15*x169+x17*x171+x19*x173+x21*x175+x23*x177+x25*x179+x27*x181+x29*x183+x31*x185+x33*x187+x35*x189+x37*x191+x39*x193+x41*x195+x43*x197+x45*x199+x47*x201+x49*x203+x51*x205+x53*x207+x55*x209+x57*x211+x59*x213+x61*x215+x63*x217+x65*x219+x67*x221+x69*x223+x71*x225+x73*x227+x75*x229+x77*x231+x79*x233+x81*x235+x83*x237+x85*x239+x87*x241+x89*x243+x91*x245+x93*x247+x95*x249+x97*x251+x99*x253+x101*x255+x103*x257+x105*x259+x107*x261+x109*x263+x111*x265+x113*x267+x115*x269+x117*x271+x119*x273+x121*x275+x123*x277+x125*x279+x127*x281+x129*x283+x131*x285+x133*x287+x135*x289+x137*x291+x139*x293+x141*x295+x143*x297+x145*x299+x147*x301+x149*x303+x151*x305+x153*x307 == 500000 with type: <class 'docplex.mp.constr.QuadraticConstraint'>
不是很好的语法,但以下应该可以工作:
for p in rangeProd:
mdl.add_constraint(mdl.sum(Allocation[o,p] * Folds[o,p] for o in rangeOils) == proddf['Demand'][p])
Performance-wise 这应该没问题,因为无论如何在引擎级别添加约束 one-by-one。
显然 add_constraints()
有问题,因为在下面的代码中一些语句意外失败:
with Model() as m:
x = m.continuous_var()
y = m.continuous_var()
# This works
m.add_constraint(m.sum([x * y]) <= 1)
# This works
for i in range(3):
m.add_constraint(m.sum([x * y]) <= i)
# This fails
m.add_constraints((m.sum([x * y]) <= i for i in range(3)))
# This fails
m.add_constraints([m.sum([x * y]) <= i for i in range(3)])