# Rotated Second-Order Cone Constraints in docplex

I have programmed the model in here using Gurobi 9.0 in Python 3.7. To ensure playing within the boundaries of the licensing, I am trying to program it using CPLEX where we have broader flexibility under a commercial license. I have two short questions.

First, is DoCplex the same as Cplex in Python, if not can you please refer to an example implementation?

Second, how can I introduce the rotated second-order cone constraints in this coding? I checked Docplex functions and couldn't see something relevant.

### BEGIN DECLARING THE MODEL ###
m= Model('Some_model')
### END DECLARING THE MODEL ###

### BEGIN ADDING VARIABLES INTO THE MODEL m ###
freq = m.continuous_var_dict(Pattern, lb=1, ub=15, name ='freq')
invfreq = m.continuous_var_dict(Pattern, lb=1, ub=15, name ='invfreq')
### END ADDING VARIABLES INTO THE MODEL m ###

### BEGIN INTRODUCING THE OBJECTIVE FUNCTION INTO m ###
m.maximize(m.sum(0.5*crt_ridership[pat,s]*(30-30*invfreq[pat])
for (pat,s) in PatternStop))
### END INTRODUCING THE OBJECTIVE FUNCTION ###

### BEGIN ADDING CONSTRAINTS INTO m ###
m.add_constraint(m.sum(unit_cost[pat]*triptime[pat]*freq[pat]/60 for pat in Pattern)
- m.sum(FixedFare*crt_ridership[pat,s] for (pat,s) in PatternStop) <= S)

m.add_constraints(m.sum(triptimesurrogate[pat]*freq[pat]/30
for pat in Pattern if(pattern_time_index[pat]==p and pattern_bus_cap[pat]==82))
<= FleetAvailSmall[p] for p in Period)
m.add_constraints(m.sum(triptimesurrogate[pat]*freq[pat]/30
for pat in Pattern if(pattern_time_index[pat]==p and pattern_bus_cap[pat]==107))
<= FleetAvailLarge[p] for p in Period)
m.add_constraints(m.sum(triptimesurrogate[pat]*freq[pat]/30
for pat in Pattern if(pattern_time_index[pat]==p and pattern_bus_cap[pat]==246))
<= FleetAvail2Car[p] for p in Period)
m.add_constraints(m.sum(triptimesurrogate[pat]*freq[pat]/30
for pat in Pattern if(pattern_time_index[pat]==p and pattern_bus_cap[pat]==492))
<= FleetAvail4Car[p] for p in Period)
m.add_constraints(m.sum(triptimesurrogate[pat]*freq[pat]/30
for pat in Pattern if(pattern_time_index[pat]==p and pattern_bus_cap[pat]==738))
<= FleetAvail6Car[p] for p in Period)
m.add_constraints(m.sum(triptimesurrogate[pat]*freq[pat]/30
for pat in Pattern if(pattern_time_index[pat]==p and pattern_bus_cap[pat]==984))
<= FleetAvail8Car[p] for p in Period)

m.add_constraints(crt_flow[pat,s] <= pattern_bus_cap[pat]*freq[pat] for (pat,s) in PatternStop)

m.add(freq[pat]*invfreq[pat] >= abs(1) for pat in Pattern)
### END ADDING CONSTRAINTS INTO m ###

solution = m.solve(log_output=True)
print(solution)

• In general, you don't have to do anything special for (rotated) second order cone constraints in docplex. For CPLEX/docplex, a (rotated) second order cone constraint is just a special kind of quadratic constraint. See ibm.com/support/knowledgecenter/SSSA5P_12.10.0/… for some more details. This being said, what is the error you get? I tried to run your code but it failed because Pattern is not defined. Could you augment your question so that the code can be run? Mar 31, 2020 at 6:36
• If you have a CPLEX syntax question which is not adequately addressed on this forum, I suggest posting at the CPLEX Forum ibm.com/developerworks/community/forums/html/… Mar 31, 2020 at 12:08

## 2 Answers

For your first question: DOCplex instead is an object-oriented modeling API. It builds either on the CPLEX Python API (for local solves) or on the DOcplexcloud service, for remote solves. It provides object-oriented modeling which is more convenient for some people. This library is composed of 2 modules:

• Mathematical Programming Modeling for Python using docplex.mp (DOcplex.MP)
• Constraint Programming Modeling for Python using docplex.cp (DOcplex.CP)

in this link you can find plenty of examples in which problems modeled using DOcplex in python.

For the second question: I did a search and found that may be Mosek can be used for rotated second-order cone constraints (this link)

• I thought Cplex was working in Python. But, all my search navigated me to Docplex. We have a Cplex license and don't want to invest in another one, like Mosek. Here [or.stackexchange.com/questions/3745/… ], Mark mentioned that Cplex can handle it. But, I couldn't find any example problem implementing such similar problem. Would changing the programming language to C++ help to solve this with Cplex? Mar 31, 2020 at 1:16
• You can still use your CPLEX license with DOcplex. There is an example of SOCP in the documents of CPLEX for python named "socpex1.py" it may help you. Mar 31, 2020 at 1:45
• In case you want to use the CPLEX Python API (not docplex) then you can find examples for this in the distribution in files qcpex1.py, qcpdual.py, socpex1.py. They illustrate how to submit constraints that involve products of variables. At the API level, CPLEX does not distinguish between second order cone constraints, rotated cone constraints, or general quadratic constraints. The former are just special cases to the former. At the API level everything is a quadratic constraint to CPLEX (same for docplex). Mar 31, 2020 at 13:10
• @DanielJunglas, I see that CPLEX is not really user-friendly. Coding a 1/x non-linearity should not be this complicated. Doing it in Gurobi took me no time. In particular, which one of these examples best fit into my problem? Does it matter which example is followed? Mar 31, 2020 at 13:27

For future reference, I solved this issue by using Pyomo modeling tool. In the end, I could solve the problem using Cplex, Gurobi, and ipopt by only stating the name of the solver.

• Mosek (mosek.com) will also these conic quadratic problems when using Pyomo. Apr 3, 2020 at 6:06