Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
Expand all | Collapse all

Misleading examples for Python Cplex??

  • 1.  Misleading examples for Python Cplex??

    Posted 12/12/15 04:15 AM

    Originally posted by: gioker


    The documentation for the python interface to Cplex states:

    
    >>> 
    import cplex
    
    >>> c = cplex.Cplex()
    
    >>> c.variables.add(names = [
    "x1", 
    "x2", 
    "x3"])
    
    >>> c.linear_constraints.add(lin_expr = [cplex.SparsePair(ind = [
    "x1", 
    "x3"], val = [1.0, -1.0]),
     
                                        cplex.SparsePair(ind = ["x1", "x2"], val = [1.0, 1.0]),
     
                                        cplex.SparsePair(ind = ["x1", "x2", "x3"], val = [-1.0] * 3),
     
                                        cplex.SparsePair(ind = ["x2", "x3"], val = [10.0, -2.0])],
     
                                senses = ["E", "L", "G", "R"],
     
                                rhs = [0.0, 1.0, -1.0, 2.0],
     
                                range_values = [0.0, 0.0, 0.0, -10.0],
     
                                names = ["c0", "c1", "c2", "c3"],)
    
    >>> c.linear_constraints.get_rhs()
    
    [0.0, 1.0, -1.0, 2.0]
    

    but it returns the following error:

     

      File "findsol.py", line 98, in <module>
        findsol(n_sol)
      File "findsol.py", line 50, in findsol
        names = ["c0", "c1", "c2", "c3"],)
      File "/Library/Python/2.7/site-packages/cplex/_internal/_subinterfaces.py", line 1187, in add
        rmat = _C_HBMatrix(lin_expr, self._cplex._env_lp_ptr, 0, self._env.parameters.read.apiencoding.get())
      File "/Library/Python/2.7/site-packages/cplex/_internal/_matrices.py", line 74, in __init__
        raise TypeError(" invalid matrix input type -- ", self._mat[0])
    TypeError: (' invalid matrix input type -- ', u'x1')

     

    I have a Mac with the latest releases of both Python and Cplex.

    Thanks.

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Misleading examples for Python Cplex??

    Posted 12/12/15 06:23 AM

    Originally posted by: gioker


    Found the problem. 

    At the beginning of the code I forgot I added:

     

    from __future__ import unicode_literals

     

    Once removed the problem was gone. Of course, an other solution would be  *.encode('UTF-8').


    #CPLEXOptimizers
    #DecisionOptimization