3
$\begingroup$

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)
$\endgroup$
  • 2
    $\begingroup$ 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? $\endgroup$ – Daniel Junglas Mar 31 at 6:36
  • $\begingroup$ 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/… $\endgroup$ – Mark L. Stone Mar 31 at 12:08
3
$\begingroup$

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)

| improve this answer | |
$\endgroup$
  • $\begingroup$ 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? $\endgroup$ – Taner Cokyasar Mar 31 at 1:16
  • $\begingroup$ 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. $\endgroup$ – Oguz Toragay Mar 31 at 1:45
  • 2
    $\begingroup$ 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). $\endgroup$ – Daniel Junglas Mar 31 at 13:10
  • $\begingroup$ @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? $\endgroup$ – Taner Cokyasar Mar 31 at 13:27
1
$\begingroup$

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.

| improve this answer | |
$\endgroup$
  • 2
    $\begingroup$ Mosek (mosek.com) will also these conic quadratic problems when using Pyomo. $\endgroup$ – ErlingMOSEK Apr 3 at 6:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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