Index names of decision variables implicitly changes when added via gurobi pandas method
回答済みI have decision variables with index names like AB-101, AN-102, AV-104 etc. These indices become AB_101 when added via gurobipy_pandas.add_vars method and remain as-is when added via model.addVars method. This creates confusion while trying to fetch these values post-processing. Is there a way to mitigate this?
-
Hi Madhushini,
Can you share how you are creating variables? This may influence what can be done.
For example in the following snippet:
import gurobipy as gp
import gurobipy_pandas as gppd
import pandas as pd
m = gp.Model()
vars = gppd.add_vars(
model=m,
pandas_obj=pd.Series(range(10)),
name=pd.Series([f"AB-{100+i}" for i in range(10)]),
)the variable names are AB-100, AB-101, ... , AB-109 and there is no conversion to underscores (at least not with pandas 2).
- Riley
0 -
Hi Riley,
I am reading these values from an excel sheet. Please see the code snippet below:
lstNames = ['AB-101','AC-102','BG-203']weekLst = list(range(0, 21))tmp_data = pandas.DataFrame(list(product(lstNames, weekLst)), columns=['Name', 'Age'])opt_data=tmp_data.set_index(["Name","Age"])m = gp.Model()CV_StartVars = gppd.add_vars(m, opt_data, name="dens", lb=0, ub=GRB.INFINITY, vtype=GRB.CONTINUOUS)CV_EndVars = m.addVars(lstNames, lb=0, ub=GRB.INFINITY, vtype=GRB.CONTINUOUS, name="Yale" )m.optimize()var_vals = m.getVars()print(var_vals)0 -
Hi Madhushini,
I can confirm that gurobipy-pandas is deliberately replacing "-" with "_" in the names. As an aside, it is also replacing "+", "-", "*", "^" with "_".
If you wish to avoid this default behavior you can add the index_formatter argument to add_vars:
CV_StartVars = gppd.add_vars(
m,
opt_data,
name="dens",
lb=0,
ub=GRB.INFINITY,
vtype=GRB.CONTINUOUS,
index_formatter="disable",
)- Riley
0 -
Thanks Riley. Unfortunately it gives me the following error when I try adding the index_formatter to add_vars:
File "/opt/anaconda3/envs/shrimpfeed/lib/python3.9/site-packages/gurobipy_pandas/api.py", line 158, in add_vars
return add_vars_from_dataframe(
File "/opt/anaconda3/envs/shrimpfeed/lib/python3.9/site-packages/gurobipy_pandas/variables.py", line 170, in add_vars_from_dataframe
namearg = create_names(name, data.index, index_formatter)
File "/opt/anaconda3/envs/shrimpfeed/lib/python3.9/site-packages/gurobipy_pandas/util.py", line 39, in create_names
mapper = create_mapper(index_formatter)
File "/opt/anaconda3/envs/shrimpfeed/lib/python3.9/site-packages/gurobipy_pandas/index_mappers.py", line 21, in create_mapper
if arg == "disable":
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Warning: environment still referenced so free is deferred
Warning: remote job 1655ab5b-ea63-42b3-8f35-2441dae9dcfe on server http://gurobi-dev.cargill.com:7183 killed because environment was freed0 -
Hi Madhushini,
Apologies, I did not pick up this error. I've since realized the simplest solution is to use
index_formatter="disable"
I have updated my answer above to reflect this.
- Riley
0 -
It works now, thanks a lot for your help and support Riley.
0
サインインしてコメントを残してください。
コメント
6件のコメント