Decision Optimization

Decision Optimization

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

 View Only
  • 1.  Issues in docplex.cp with context and log files and plotly and cpo_config.py

    Posted Mon November 16, 2020 06:32 PM

    There are a couple of issues that I would like to report with respect to docplex CPO that cost me hours of debugging time.  I was not getting log files. 

    PROBLEM 1:
    It turns out that my program does this:

    import plotly.figure_factory as ff

    That causes the `ipykernel` module to get imported, so that this test in `utils.py` makes the program think it is in a notebook:


    # Indicator that program is running inside a notebook
    IS_IN_NOTEBOOK = 'ipykernel' in sys.modules

    Then the default local context never prints any log file.

    I am using plotly to generate a plot that is offline (in a file), so you need to find another test to determine if it is being run in  a notebook.  

    PROBLEM 2:

    The docs indicate that you can create a file `cpo_config.py` to override the default configuration.  I placed that file in the top level of my project, which by default, is automatically on the PYTHONPATH.  The file is never found.   In config.py, you have:

    def _eval_file(file):
        """ If exists, evaluate the content of a python module in this module.
        Args:
        file: Python file to evaluate
        """
        for f in filter(os.path.isfile, [dir + "/" + file for dir in sys.path]):
    

    The default `sys.path` (this was on Linux) has the normal libraries and '' (an empty string) in it.  So the file `cpo_config.py` is never found, because it searches for "/cpo_config.py", which would be in the root of the file system.

    If the `cpo_config.py` file is in the root level of the project, then it should be found.  You need to fix that.



    ------------------------------
    Irv Lustig
    Optimization Principal
    Princeton Consultants
    ------------------------------

    #DecisionOptimization


  • 2.  RE: Issues in docplex.cp with context and log files and plotly and cpo_config.py

    Posted Tue November 17, 2020 03:47 AM
    Hello Irv,

    For the first problem, I don't know any other way to detect that Python is running inside a notebook.
    Problem when the log is activated in this context is that it generally kills the notebook that can not support huge log data.
    I have updated the documentation to give more details on this default setting.

    Anyway, this is just a default setting that can be overwritten by:
    - setting context.solver.trace_log=True in your cpo_config.py (the best way to proceed, and then we go to your second problem)
    - setting it explicitly in your code: config.context.solver.trace_log=True
    - setting the parameter in your call to solver, for example: mdl.solve(trace_log=True)

    For your second problem, your project root should be in sys.path if it is in the PYTHONPATH.
    I will study to update the code to:
    - Add '.' as directory in sys.path (frequently IDE are setting the default directory to the project root)
    - Also go over calling stack to find the root (main) module and add its directory.
    In the meantime, you can explicitly update your PYTHONPATH with a place where you put your cpo_config.py.

    Thanks to have report these problems, and sorry for the inconvenience.

    ------------------------------
    Olivier Oudot
    ------------------------------



  • 3.  RE: Issues in docplex.cp with context and log files and plotly and cpo_config.py

    Posted Tue November 17, 2020 09:11 AM

    Olivier:

    Regarding the test for being in a notebook, see this:

    https://stackoverflow.com/a/39662359/1970354



    ------------------------------
    Irv Lustig
    Optimization Principal
    Princeton Consultants
    ------------------------------



  • 4.  RE: Issues in docplex.cp with context and log files and plotly and cpo_config.py

    Posted Tue November 17, 2020 12:24 PM

    Thanks Irv,

    It seems to be a complex problem that sounds very controversial !

    I will investigate on this base, to find, if exists, a solution that works for all Python versions and all OS ....



    ------------------------------
    Olivier Oudot
    ------------------------------