如何使用 DOcplex (python) 向模型添加二次约束?

How to add quadratic constraints to the model by using DOcplex (python)?

以此二次约束为例:

(-x1^2 + x2^2 + x3^2 <= 0) 

请注意,在 CPLEX Python API 中,上述约束形式化如下:

m.quadratic_constraints.add(
    quad_expr=[["x1", "x2", "x3"], ["x1", "x2", "x3"], [-1,   1,    1]],
    sense='L', rhs=0, name="q1"
)

如何使用 DOcplex 而不是 CPLEX 将上述二次约束添加到模型中 Python API?

让我稍微更改一下我在 cpleqp equivalent in docplex

中分享的示例
from docplex.mp.model import Model  

mdl = Model(name='qpex1')

#decision variables
x = {b: mdl.continuous_var(0,40,name="x"+str(b)) for b in range(0,3)}


# Constraint
mdl.add_constraint( - x[0] +     x[1] + x[2] <= 20, 'ct1')
mdl.add_constraint(x[0] - 3 * x[1] + x[2] <= 30,'ct2')

mdl.add_constraint(x[0] * x[0] <= 30,'quad')


# Objective
mdl.maximize(x[0] + 2 * x[1] + 3 * x[2]-\
             0.5 * ( 33*x[0]*x[0] + 22*x[1]*x[1] + 11*x[2]*x[2] -\
                     12*x[0]*x[1] - 23*x[1]*x[2] ))

msol=mdl.solve()

# Dislay solution
for v in mdl.iter_continuous_vars():
   print(v," = ",v.solution_value)

print("objective : ",msol.get_objective_value() ) 

mdl.add_constraint(x[0] * x[0] <= 30,'quad')

是二次约束

Docplex 允许您将变量与标准“*”运算符相乘以构建二次表达式,如:

x * y 或 x * x

还可以使用“**”(幂)运算符对变量求平方,如

m.add(x**2 + y**2 <= 1)