Decision Optimization

 View Only
  • 1.  Doxplex does not synchronize bounds before exporting model

    Posted Mon January 25, 2021 04:38 AM
    Hi,

    I've noticed that docplex does not synchronize the variable bounds with cplex for the variables that updated their bounds after creation when exporting the model as sav or mps.

    In my use case I build the model, save it and then load and solve it later. I've noticed the problem since building the model then solving it directly would produce a solution, while loading the saved model would not.

    Below it's a simple reproduction of this issue:
    from docplex.mp.model import Mode
    model = Model('example')
    v1 = model.continuous_var(-1, 1, 'v1')
    v2 = model.continuous_var(-1, 2, 'v2')
    model.add_constraint(v2>=v1)
    v2.ub = 10
    print(model.export_as_mps_string())
    This prints:
    * ENCODING=ISO-8859-1
    NAME          
    ROWS
     N  obj1    
     G  c1      
    COLUMNS
        v1        c1                             -1
        v2        c1                              1
    RHS
    BOUNDS
     LO bnd       v1                             -1
     UP bnd       v1                              1
     LO bnd       v2                             -1
     UP bnd       v2                              2
    ENDATA
    
    The variable v2 has the original upper bound of 2, not the correct one of 10.

    Updating the example to call the internal method "model._Model__engine.sync_cplex(model)" like done before a solve will produce the correct output:
    from docplex.mp.model import Model
    model = Model('example')
    v1 = model.continuous_var(-1, 1, 'v1')
    v2 = model.continuous_var(-1, 2, 'v2')
    model.add_constraint(v2>=v1)
    v2.ub = 10
    model._Model__engine.sync_cplex(model)
    print(model.export_as_mps_string())


    I've tested this on both docplex 2.19.202 and 2.15.194 and the result is the same.

    Is there a way to force synchronization of the model that's not using the internal method?
    I've noticed that solving the model, even without letting it complete, by setting a very small time limit, will make the synchronization happen allowing exporting the correct model.

    Exporting to LP does not have this problem, but the LP format is not optimal for my use case.

    Thank you,
    Best

    ------------------------------
    Federico Caselli
    ------------------------------

    #DecisionOptimization


  • 2.  RE: Doxplex does not synchronize bounds before exporting model

    Posted Tue January 26, 2021 04:56 AM
    Hi

    I confirm this is a bug.
    As you noticed, synchronization is performed before any solve-related operations (solve, populate, refine_conflict),
    so starting a solve with a small time limit (1s) will indeed  re-synchronize bounds before an export.
    There is a way to work around this without solving, but it involves undocumented methods;
    let me know if solving with a small time limit is not an option for you and I'll answer you in private with the workaround code.
    Thanks for taking the time reporting this in such a precise way.

                            Philippe.

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



  • 3.  RE: Doxplex does not synchronize bounds before exporting model

    Posted Tue January 26, 2021 08:35 AM
    Hi Philippe,

    Thanks for looking into it.
    In my use case starting a solve with a time limit is not the best workaround, but the one I mentioned above, calling "model._Model__engine.sync_cplex(model)", is currently working for my use case. If you have a more robust workaround code it would be useful, until this is hopefully fixed in a future version of docplex.

    Thanks
    Federico

    ------------------------------
    Federico Caselli
    ------------------------------



  • 4.  RE: Doxplex does not synchronize bounds before exporting model

    Posted Tue January 26, 2021 08:44 AM
    Hi Federico,

    I confirm the call to `sync_cplex()` you found is the only workaround for now, except for starting a solve;
    this code is non-documented and might well be changed in future versions.
    However, the bug has been fixed and the fix will be part of the next version of DOcplex.

         Thanks.

         Philippe.

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



  • 5.  RE: Doxplex does not synchronize bounds before exporting model

    Posted Tue January 26, 2021 08:50 AM
    Hi Philippe,

    That's great to hear.
    I'll look forward to the next version of DOcplex, and use the workaround only until that's released.

    Thanks for the quick reply.

    Best,
    Federico

    ------------------------------
    Federico Caselli
    ------------------------------



  • 6.  RE: Doxplex does not synchronize bounds before exporting model

    Posted Wed February 10, 2021 09:37 AM
    Hi Philippe,
    I've updated to version 2.20 of docplex and I confirm that it has been fixed.

    Thanks!

    Federico

    PS: At the same time I've also updated to version 1.20 of numpy and now importing the ConflictRefiner gives a warning: "DeprecationWarning: `np.bool` is a deprecated alias for the builtin `bool`..." that points to https://numpy.org/devdocs/release/1.20.0-notes.html#using-the-aliases-of-builtin-types-like-np-int-is-deprecated

    ------------------------------
    Federico Caselli
    ------------------------------



  • 7.  RE: Doxplex does not synchronize bounds before exporting model

    Posted Thu February 11, 2021 05:28 AM
    Hi Federico

    You're welcome. Thanks for the confirmation.

    About the 'np.bool' deprecation, I found no explicit mention of `np.bool` in conflict refiner;
    Does this warning happen only when importing conflict refiner or other Docplex modules?

    To help me reproduce, can you attach the output of `pip freeze` ?

       Thanks.

          Philippe

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



  • 8.  RE: Doxplex does not synchronize bounds before exporting model

    Posted Thu February 11, 2021 05:46 AM
    Hi Philippe,

    First of all, sorry, I should have included the strack trace obtained when running with the python flag "-W error".
    The warning comes from "docplex.mp.utils", see below:
    (env) PS C:\path\to\folder> python -W error
    Python 3.7.9 (default, Aug 31 2020, 17:10:11) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from docplex.mp.conflict_refiner import ConflictRefiner
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "C:\Users\path\to\folder\lib\site-packages\docplex\mp\conflict_refiner.py", line 9, in <module>
        from docplex.mp.utils import is_string
      File "C:\Users\path\to\folder\lib\site-packages\docplex\mp\utils.py", line 48, in <module>
        __int_types.add(numpy.bool)
      File "C:\Users\path\to\folder\lib\site-packages\numpy\__init__.py", line 287, in __getattr__
        warnings.warn(msg, DeprecationWarning, stacklevel=2)
    DeprecationWarning: `np.bool` is a deprecated alias for the builtin `bool`. To silence this warning, use `bool` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.bool_` here.
    Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations​


    Regarding packages, the relevant ones are "numpy==1.20.1" and "docplex==2.20.204" (but I think it may apply also to older docplex versions).

    Thanks for looking into it, I noticed the warning only because it's reported by our pytest runs.

    Federico



    ------------------------------
    Federico Caselli
    ------------------------------



  • 9.  RE: Doxplex does not synchronize bounds before exporting model

    Posted Thu February 11, 2021 05:52 AM
    Thanks for your quick answer, Federico.

    Now this is crystal clear. I'll register an issue and this will be fixed in the next release.

    Thanks again for spotting and reporting this. Such feedback help us to improve Docplex.


          Philippe.

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