Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.

 View Only
Expand all | Collapse all

Model problems with docplex and cplex on python

  • 1.  Model problems with docplex and cplex on python

    Posted Mon September 28, 2020 08:56 AM
    Hello,
    I have been working with cplex on python for a few months now. 
    I made my model with docplex but then I used a solution pool with cplex for solving it (I don't use docplex to solve it). In my model ,with the help of some other developers, I found out when my model is being built with docplex some of the docplex specific constraints( for example: if_then and logical_and) are not being implemented in the model. Therefor, when I solve the model with cplex I don't have some of the constraints implemented in my solutions.
    What can I do to make cplex wrap up my whole model before I send it to my solution pool?

    ------------------------------
    Sana NZ
    ------------------------------

    #DecisionOptimization


  • 2.  RE: Model problems with docplex and cplex on python

    Posted Mon September 28, 2020 10:07 AM
    Hello Sana,

      Thanks for your explanations, I now understand better. If you solve directly at the "cplex" level, then some parts of the model have not been synchronized yet,
    among them logical equivalences, so this might well explain your issues. I am attaching a small function, to be inserted just before calling `populate`, which should
    perform this missing synchronization. This code uses undocumented methods.
    The good news is, the next version of DOcplex will support solution pools directly , removing the need for these workarounds.
    Let me know if this works.

          Philippe.


    def sync_cplex(mdl): # call this method before cplex.populate() print(f"-- synchronize cplex parameters") mdl.apply_parameters() eng = mdl.get_engine() # wrap all syncs print(f"-- synchronize variable bounds ") eng._sync_var_bounds() eng._sync_annotations(mdl) print(f"-- synchronize equivalence cts") eng._sync_equivalence_cts(mdl)


    ------------------------------
    Philippe Couronne
    ------------------------------



  • 3.  RE: Model problems with docplex and cplex on python

    Posted Tue September 29, 2020 10:57 AM
    Thanks , I used your code but I got this error:
    AttributeError: 'CplexEngine' object has no attribute '_sync_equivalence_cts'

    ------------------------------
    sana naz
    ------------------------------



  • 4.  RE: Model problems with docplex and cplex on python

    Posted Tue September 29, 2020 11:02 AM
    Which version of DOcplex are you using? is it the latest version (2.15.194 of last July), if not can you update?

    ------------------------------
    Philippe Couronne
    ------------------------------



  • 5.  RE: Model problems with docplex and cplex on python

    Posted Tue September 29, 2020 12:00 PM
    I have version 2.0 but I did not install it directly. I installed cplex studio with the academic licence. I don't know how can i upgrade that since there is no new version of cplex studio (latest I have is 12.10) and my system doesn't recognize docplex by itself in order to upgrade. 
    Is it OK if I install docplex again with conda and upgrade?

    ------------------------------
    sana naz
    ------------------------------



  • 6.  RE: Model problems with docplex and cplex on python

    Posted Wed September 30, 2020 05:28 AM
    DOcplex is a pure python module, so it has a faster release cycle than Cplex Studio.
    Before going further can you provide the following informations:

    - what is your platform? (windows, Linux, MacOs)?
    - what is your version of python? (output of `python --version`)
    - what is your version of anaconda?
    - what version of Cplex Studio are you using?

    And finally, what is the output of the `pip freeze` command?\
    You should see one line with `docplex=2.xxx` in it.

    From there, I can advise a safe course of actions to upgrade DOcplex to the latest version.


         Philippe.

    ------------------------------
    Philippe Couronne
    ------------------------------



  • 7.  RE: Model problems with docplex and cplex on python

    Posted Wed September 30, 2020 06:24 AM
    - I have windows 10
    - python =3.6
    - using anaconda3 -anaconda-navigator==1.9.12
    -  Cplex Studio version 12.10.0
    - docplex==2.11.176

    ------------------------------
    sana naz
    ------------------------------



  • 8.  RE: Model problems with docplex and cplex on python

    Posted Wed September 30, 2020 08:29 AM
    Ok, let's try the following command, from where you ran `pip freeze`:


    > pip install --upgrade docplex

    This should uninstall the old 2.11 and replace it with 2.15, . You can rerun `pip freeze to check that it worked.
    Then the code workaround I sent should work; no surprise it didn't work with 2.11.
    Let me know if this works.

       Philippe.

    ------------------------------
    Philippe Couronne
    ------------------------------



  • 9.  RE: Model problems with docplex and cplex on python

    Posted Fri October 09, 2020 05:10 AM
    Hi,
    I tried your solution on two different machines and got this error:

    ERROR: Cannot uninstall 'docplex'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.

    ------------------------------
    Sana Nazari
    ------------------------------



  • 10.  RE: Model problems with docplex and cplex on python

    Posted Mon October 12, 2020 04:11 AM
    Since 2.11 , the way to build distributions of DOcplex has changed. Still, there is a way to force the upgrade, using:

       pip install --upgrade --force-reinstall docplex

    The "--force-reinstall" should force Python to upgrade DOcplex, even if the build method has changed. Running `pip freeze` afterwards, you should see 2.15 installed.

               PHilippe.



    ------------------------------
    Philippe Couronne
    ------------------------------



  • 11.  RE: Model problems with docplex and cplex on python

    Posted Tue October 13, 2020 10:28 AM
    Hi again.
    I tried the new command but still facing the same error. Is there another way to resolve this issue?

    ------------------------------
    Sana Nazari
    ------------------------------



  • 12.  RE: Model problems with docplex and cplex on python

    Posted Wed October 14, 2020 04:58 AM
    Hi,

      It seems your version of "pip" should be upgraded too. So, let's proceed in two phases:

    1. upgrade "pip" command, run the following command:

    python -m pip install --upgrade pip

    This should install the latest version of pip. You can check with with "pip --version"

    2. Do not try uninstall, force a "brutal" installation of DOcplex with this command:

    pip install --ignore-installed -U docplex

    By this command, we force an installation of docplex, regardless of what was installed before, without trying to uninistall. The "-U" is for upgrade.

    At the end of the day, you should have 2.15 installed (check with pip freeze)


    Let me know if this works.

            Philippe.





    ------------------------------
    Philippe Couronne
    ------------------------------



  • 13.  RE: Model problems with docplex and cplex on python

    Posted Wed October 21, 2020 05:50 AM
    Thank you Philippe. Both of your methods worked!

    ------------------------------
    Sana Nazari
    ------------------------------



  • 14.  RE: Model problems with docplex and cplex on python

    Posted Wed October 14, 2020 07:55 AM
    Hi there,

    Older versions of pip just copied the files without keeping track of what is installed, and newer versions of pip just raise this error to be sure.
    In the case of docplex, there is only one directory installed so you are safe to ignore what was previously installed.

    If I were you, I would do this. First update pip:

    python -m pip install --upgrade pip
    Then install docplex, ignoring previous install: 
    pip install --ignore-installed -U docplex


    ------------------------------
    Viu Long Kong
    ------------------------------