Gurobi / gurobipy-pandas

Convenience wrapper for building optimization models from pandas data
https://gurobipy-pandas.readthedocs.io/en/stable/
Apache License 2.0
83 stars 15 forks source link

set_attr accessor cannot set vtype #68

Closed silkehorn closed 4 months ago

silkehorn commented 5 months ago

According to the parameter documentation, the value parameter in set_attr can be int, float, str or Series. However, the code only seems to allow Series and anything that can be a float. It fails when trying to set vtype (which is a str).

> vars["z"].gppd.set_attr("vtype", gp.GRB.CONTINUOUS)

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[12], line 5
      1 # change type of z from semi-integer to continuous
      2 #for v in vars["z"]:
      3 #    v.vtype=gp.GRB.CONTINUOUS
----> 5 vars["z"].gppd.set_attr("vtype", gp.GRB.CONTINUOUS)
      6 vars["z"].gppd.set_attr("lb", 0)
      8 m.params.OutputFlag = 0

File ~/venvs/gurobi-finance/lib/python3.11/site-packages/gurobipy_pandas/accessors.py:330, in GRBSeriesAccessor.set_attr(self, attr, value)
    328         entry.x.setAttr(attr, entry.v)
    329 else:
--> 330     value = float(value)
    331     for v in self._obj:
    332         v.setAttr(attr, value)

ValueError: could not convert string to float: 'C'
simonbowly commented 5 months ago

Thanks @silkehorn for catching the oversight, I'll add this case

simonbowly commented 4 months ago

This is fixed in version 1.1.1 (just released). Note you can also just do:

x.gppd.vtype = GRB.CONTINUOUS