Decision Optimization

 View Only
Expand all | Collapse all

Python DOcplex variable declaration issue

  • 1.  Python DOcplex variable declaration issue

    Posted Fri February 12, 2021 02:15 PM
    Dears,
    I'm facing a crazy issue in building my optimization problem. 
    I'm initializing a set of variable named "y" that are indexed by sets "I" and "L" where I=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15] and L=[0,1,2,3,4,5,6,7]
    I'm doing it with the following expression:

    y=model_first.binary_var_dict({(i,l) for i in newFacilities for l in capacityLevels}, name="y")

    Today, I updated the version of CPLEX APi and now this code returns me the following errors:
    TypeError: 'set' object is not subscriptable
    However, if I try to execute the same code on an older CPLEX version, everything is fine.

    I tried to understand the problem and it seems that if I reduce the size of my set lower than 12, then everything is fine (e.g. with I=[0,1,2,3,4,5,6,7,8,9,10,11])
    I'm using the Academic version of CPLEX, and it shouldn't have any limitations.
    Does anyone find the same issue?

    ------------------------------
    Riccardo Aldrighetti
    ------------------------------

    #DecisionOptimization


  • 2.  RE: Python DOcplex variable declaration issue

    Posted Sun February 14, 2021 12:28 PM
    Edited by System Fri January 20, 2023 04:42 PM
    Dear Riccardo

    This is a reported bug of DOcplex 2.20, which has nothing to do with the Academic version (still note the academic version is limited to 1000 variables, but this will show up only on solve())
    There is a simple workaround: pass a list instead of a set to `integer_var_dict` and it will work as expected.
    This bug will be fixed in the next version.


    In addition, there is a more concise way of declaring a mtraix of variables with two keys: `Model.integer_var_matrix()`, which takes two key sets; it also accepts integers, interpreted as ranges. Therefore your original code could be written as


      ys = mdl.integer_var_matrix(keys1=12, keys2=13, name='y')

    and this construct is not impacted by the above bug.



                 Philippe.

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