Search code examples
pythonmathematical-optimizationconstraint-programmingdocplexdocplexcloud

Adding multiple quadratic constraints in DOCPLEX


I am trying to add quadratic constraints to a docplex model however it is interpreting them as linear and giving me error messages.

below is a excerpt from the script

here is the quadratic constraint:

mdl.add_constraints((mdl.sum(Allocation[o,p] * Folds[o,p] for o in rangeOils) == proddf['Demand'][p] for p in rangeProd))

here is where it sits

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))

any help would be massively appreciated

here is the error

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'>

Solution

  • Not very nice syntax but the following should work:

    for p in rangeProd:
      mdl.add_constraint(mdl.sum(Allocation[o,p] * Folds[o,p] for o in rangeOils) == proddf['Demand'][p])
    

    Performance-wise this should be fine since at the engine level constraints are added one-by-one anyway.

    Apparently there is a problem with add_constraints() because in the code below some statements fail unexpectedly:

    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)])