メインコンテンツへスキップ

`tuplelist` in Gurobipy 11

回答済み

コメント

8件のコメント

  • Matthias Miltenberger
    Gurobi Staff Gurobi Staff

    Hi!

    There have been indeed some updates to the tupledict and tuplelist implementations in version 11 to make them more Pythonic. Here's the list of updates: Release Notes for Gurobi 11.0

    Our documentation on tuplelist, both version 10 and 11, explains that a list needs to be passed when creating a tuplelist. So, your code may have only worked "by accident". Can you please try updating it as follows, by adding the square brackets:

    grb.tuplelist([(custom_obj1, custom_obj2, custom_obj3)])

    Cheers,
    Matthias

    0
  • Riley Clement
    Gurobi Staff Gurobi Staff

    There might be something else going on here too.  The following runs without problem on v11.0.3 (with Python 3.11)

    import gurobipy as gp

    class Foo:
        ...

    gp.tuplelist((Foo(),))

    - Riley

    0
  • Premkumar Subramanian
    First Comment
    First Question

    Hi Matthias, The typing issue still continues even with the list as the list is expected to have `int | float | str`

     error: List item 0 has incompatible type "<Custom Python class>"; expected "int | float | str"  [list-item]
    0
  • Premkumar Subramanian
    First Comment
    First Question

    Hi Riley, The code may even be able to run without issues, but I am blocked by this typing issue. MyPy, the type checker, doesn't see the custom Python class objects as valid ones in the list

    0
  • Riley Clement
    Gurobi Staff Gurobi Staff

    Ah ok, it's a MyPy issue.  Can't you just tell MyPy to ignore the error?
    https://mypy.readthedocs.io/en/stable/error_codes.html

     

    0
  • Premkumar Subramanian
    First Comment
    First Question

    That can be a last resort. But I still wonder why this error is thrown in the first place. I don't want to have unexpected results from Gurobi as I suppress this error. 

    0
  • Riley Clement
    Gurobi Staff Gurobi Staff

    The error is thrown because arbitrary objects are not intended to be used with tuplelists.  The typing introduced in v11 and MyPy are doing their job as intended here.

    From the tupledict docs linked by Matthias:

    A tuplelist is designed to store tuples containing scalar values (int, float, string, …). It may produce unpredictable results with other Python objects

    You can also see this in the docstring when running help(grb.tupledict) within Python.  This goes back as least as far as v8 (2018).

    If you want to use custom objects in a tuplelist then you can suppress the error, and you are taking a risk - it seems this is a risk that you have been unknowingly taking all along with v10 (and earlier).

    If there is some functionality you use from tuplelist that you want to use with your custom objects then feel free to open up a support request (assuming you are a commercial customer) and I'll help you engineer a safe solution.

    - Riley

    0
  • Premkumar Subramanian
    First Comment
    First Question

    Thanks Riley! 
    Indeed it seems that we have been already unknowingly taking the risk by passing the custom objects to a tuplelist.
    As we didn't include `gurobipy-stubs` for gurobipy 10, we never noticed the typing issue. As type hints are included in gurobipy 11, we get to see the issue now.
    Will explore further on how to deal with this issue. If required, I will then raise a support request.

    0

サインインしてコメントを残してください。