Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Python API error

    Posted 01/03/19 01:20 PM

    Originally posted by: open_ball


    Hi,

     

    I am beginner in Python and was trying to use the example provided at http://ibmdecisionoptimization.github.io/docplex-doc/mp/creating_model.html

     

    When I run the model, I receive some errors and could not figure out the reason. 

     

    C:\Users\PycharmProjects\Excel_Read\venv\Scripts\python.exe C:/Users/mcamu/PycharmProjects/cplex_example.py
    Traceback (most recent call last):
      File "C:\Users\PycharmProjects\Excel_Read\venv\lib\site-packages\docplex\mp\mfactory.py", line 301, in _expand_bounds
        _computed_bounds = [var_bound(k) for k in keys]
      File "C:\Users\PycharmProjects\Excel_Read\venv\lib\site-packages\docplex\mp\mfactory.py", line 301, in <listcomp>
        _computed_bounds = [var_bound(k) for k in keys]
    TypeError: 'str' object is not callable

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "C:/Users/mcamu/PycharmProjects/cplex_example.py", line 17, in <module>
        x = model.binary_var_dict(idx, "X")
      File "C:\UsersPycharmProjects\Excel_Read\venv\lib\site-packages\docplex\mp\model.py", line 1885, in binary_var_dict
        return self._var_dict(keys, self.binary_vartype, lb=lb, ub=ub, name=name, key_format=key_format)
      File "C:\Users\PycharmProjects\Excel_Read\venv\lib\site-packages\docplex\mp\model.py", line 1553, in _var_dict
        return self._lfactory.new_var_dict(keys, vartype, lb, ub, name, key_format, ordered=self._keep_ordering)
      File "C:\Users\PycharmProjects\Excel_Read\venv\lib\site-packages\docplex\mp\mfactory.py", line 387, in new_var_dict
        var_list = self.new_var_list(ctn, key_seq, vartype, lb, ub, actual_name, 1, key_format)
      File "C:\Users\PycharmProjects\Excel_Read\venv\lib\site-packages\docplex\mp\mfactory.py", line 359, in new_var_list
        xlbs = self._expand_bounds(key_seq, lb, default_lb, number_of_vars, true_if_lb=True)
      File "C:\Users\PycharmProjects\Excel_Read\venv\lib\site-packages\docplex\mp\mfactory.py", line 313, in _expand_bounds
        self._bad_bounds_fatal(var_bound)
      File "C:\Users\PycharmProjects\Excel_Read\venv\lib\site-packages\docplex\mp\mfactory.py", line 319, in _bad_bounds_fatal
        self.fatal("unexpected variable bound: {0!s}, expecting: None|number|function|iterable", bad_bound)
      File "C:\Users\PycharmProjects\Excel_Read\venv\lib\site-packages\docplex\mp\mfactory.py", line 165, in fatal
        self._model.fatal(msg, *args)
      File "C:\Users\PycharmProjects\Excel_Read\venv\lib\site-packages\docplex\mp\model.py", line 775, in fatal
        self._error_handler.fatal(msg, args)
      File "C:\Users\PycharmProjects\Excel_Read\venv\lib\site-packages\docplex\mp\error_handler.py", line 210, in fatal
        raise DOcplexException(resolved_message)
    docplex.mp.utils.DOcplexException: unexpected variable bound: X, expecting: None|number|function|iterable

    Process finished with exit code 1


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Python API error

    Posted 01/04/19 12:19 AM

    Originally posted by: EdKlotz


    I just cut and pasted the example to which you refer and got the same error.   It appears to be out of date, either regarding the DoCplex version or the Python version, I'm not sure which.    I was able to get past this error by changing the relevant line to

     

    x = model.binary_var_dict(keys=idx, name="X")

     

    The program then solved the model, but hit an error at the end:

     

     python sudoku.py
    Traceback (most recent call last):
      File "sudoku.py", line 35, in <module>
        solution.print_information()

    AttributeError: 'SolveSolution' object has no attribute 'print_information'


    Indeed, looking at the DoCplex 12.8 docs, the Solution and SolveSolution classes do not have such a method.

     

    I'll look into this more tomorrow, but overall I think your installation is set up properly.


    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: Python API error

    Posted 01/04/19 02:30 AM

    Can you try again with the stuff that is on the official website https://pypi.org/project/docplex/

    If you go to the documentation section then you can find the same document here: https://github.com/IBMDecisionOptimization/docplex-doc/blob/master/docs/mp/creating_model.html


    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: Python API error

    Posted 01/04/19 12:31 PM

    Originally posted by: open_ball


    Hi,

    Thanks for the answer. I have two questions if you don't mind. Is DoCplex the correct tool for the modelling in Python? So basically I am asking if I am on the right track. The second one is, where can I find examples showing how to create simple LPs such as facility location, max flow, TSP etc. to practice? I am able to see those examples in CPLEX folder for Java, but could not find an example for Python showing how to write the constraints (e.g., conditional summation) and creating linear models.


    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: Python API error

    Posted 01/04/19 01:25 PM

    Yes docplex is the correct tool. There are two "products" that you may be confusing here:

    1. There is CPLEX optimization studio. This is the core engine (the actual solver algorithm) together with APIs for C, C++, Java, C#, Python, matlab. This has lots of examples that can be found in the installation directory. I guess the Java examples you found are for these?
    2. There is docplex which is built on the Python API from 1. docplex has its own set of examples that can be found for example from the  docplex website on pypi.

    In order to model/solve optimization problems you can use any of the APIs for 1 (including Python) or docplex. Depending on what you are aiming at you can also use OPL, the optimization programming language, which is the most descriptive way to code models.


    #CPLEXOptimizers
    #DecisionOptimization


  • 6.  Re: Python API error

    Posted 01/04/19 01:31 PM

    NB:

    You may call OPL from Python through DOOPL https://pypi.org/project/doopl/

    Many docplex examples in CPLEX_Studio128\python\examples

    regards


    #CPLEXOptimizers
    #DecisionOptimization


  • 7.  Re: Python API error

    Posted 01/05/19 01:37 PM

    Originally posted by: open_ball


    @DanielJunglas, you are right! I guess I was confusing them. What I want to do is similar to Sudoku example that I shared in the question. I am not able to accomplish what I want in OPL. That's why I'd like to build up everything in Python.

    @AlexFleischer This (CPLEX_Studio128\python\examples) actually solves my problem. I was looking for these examples. 

    Thanks everyone!


    #CPLEXOptimizers
    #DecisionOptimization