I have a semi-continuous optimization problem reformulated as a MIQP optimization problem.

My objective has a quadratic form $x^{T}Qx$ and my $x_{i}$ are such as $x_{i} \in [m,M] \cup \{0\}$. Therefore, I introduce a vector of binary variables $y$ such as $y_{i} \in \{0,1\}$ and consider the following constraint on the $x_{i}$ : $m * y_{i} \leq x_{i} \leq M * y_{i}$.

My optimization problem is hence defined by the objective function, the ranged constraint on the $x_{i}$ and the binary constraint on $y_{i}$.

I'm trying to solve my problem with CPLEX but I'm having trouble specifying the ranged constraint on the $x_{i}$. Here's what I have so far for the constraints :


#define the variables
names_amounts=["amounts " + str(i) for i in range(50))]
names_binary=["binary " + str(i) for i in range(50))]
for i in range(50):
    myProblem.variables.set_types("amounts " + str(i), myProblem.variables.type.continuous)
    myProblem.variables.set_types("binary " + str(i), myProblem.variables.type.integer)

#define the constraints

I need to add the ranged constraint but can't figure out how. I know my senses attribute will become ['E'] + ['R' for i in range(50)] but what about the lin_expr and rhs attributes ? How to specify the dependance of the rhs attribute with the binary variables ?

  • $\begingroup$ I am not a CPLEX user, but I suspect that a range constraint requires constant lower and upper bounds and that here you should instead declare two separate inequality constraints. $\endgroup$
    – RobPratt
    Sep 2, 2020 at 14:56
  • $\begingroup$ Yes sorry maybe my question was unclear. CPLEX has the particularity to only consider constraints one by one, and therefore I can't just put an inequality between two vectors (especially since the bound vectors depend on the binary variables of the problem). At least it's the feeling I have. If anyone could confirm or not what I say and show me how to do it I'd be very grateful. $\endgroup$
    – FredNgu
    Sep 2, 2020 at 15:06

1 Answer 1


that s quite easy to do with docplex python API:

let me change




into semicontinuous

from docplex.mp.model import Model

# original model

mdl = Model(name='buses')
nbbus40 = mdl.semicontinuous_var(4,20,name='nbBus40')
nbbus30 = mdl.semicontinuous_var(4,20,name='nbBus30')
mdl.add_constraint(nbbus40*40 + nbbus30*30 >= 300, 'kids')
mdl.minimize(nbbus40*500 + nbbus30*400)


for v in mdl.iter_semicontinuous_vars():
    print(v," = ",v.solution_value
  • $\begingroup$ EDIT : it works great thank you ! I don't know why I didn't hear of docplex before, it's way easier to use than other solvers. What are the advantages of cplex compared to docplex then ? $\endgroup$
    – FredNgu
    Sep 2, 2020 at 15:45
  • 1
    $\begingroup$ Well to me OPL is even easier than docplex python API linkedin.com/pulse/… but for sure docplex is much easier to use than the cplex python matrix API. The cplex matrix pytho API has some features that are not yet in docplex like sensitivity analysis but then you can call the matrix api from docplex lise shown at stackoverflow.com/questions/62475139/… $\endgroup$ Sep 2, 2020 at 16:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.