2

Take this quadratic constraint as example:

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

Note that in the CPLEX Python API, the above constraint is formalated as follows:

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

How to add the aforementioned quadratic constraint into the model by using DOcplex, not CPLEX Python API?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
aniuniu
  • 23
  • 3

2 Answers2

3

let me change a bit the example I shared in 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() ) 

and

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

is a quadratic constraint

Alex Fleischer
  • 9,276
  • 2
  • 12
  • 15
1

Docplex lets you multiply variables with the standard '*' operator to build quadratic expressions, as in:

x * y or x * x

but also take the square of a variable using the '**' (power) operator, as in

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

Philippe Couronne
  • 826
  • 1
  • 5
  • 6