Issue with Adding Constraints in Gurobi Using Data from Excel
回答済みHello,
I'm currently working on an optimization model using Gurobi and I have encountered an issue when trying to add constraints. My model involves reading data from an Excel file and using it to define constraints. Here's a brief overview of my setup:
I read the data from an Excel file:
“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“
file_path = 'D:/Pycharm/yanghao_lizi_test/THT_DATA/Load.xlsx' load_df = pd.read_excel(file_path) # Selecting data for a specific day day = 6000 h_load = load_df.iloc[day + 1].values # Ensuring the data is in float format h_load = h_load.astype(float)
”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“
P3eb = model.addVars(T, name='P3eb', lb=0, ub=400)
P3buy_chp = model.addVars(T, name='P3chg', lb=0, ub=300)
for t in range(T):
model.addConstr(0.2 * h_load[t] - 0.95 * P3eb[t] - 0.5 * P3buy_chp[t] == 0, name=f"constraint_{t}")
“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”
However, when I run the code, I encounter the following error:
Traceback (most recent call last):
File "D:\Pycharm\yanghao_lizi_test\毕设模型\gurobi.py", line 294, in <module>
model.addConstr(0.2 * h_load[t] == 0.95 * P3eb[t] + 0.5 * P3buy_chp[t])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "src\\gurobipy\\tempconstr.pxi", line 57, in gurobipy.TempConstr.__bool__
gurobipy.GurobiError: Constraint has no bool value (are you trying "lb <= expr <= ub"?)
This issue arises specifically when I use data read from the Excel file. If I manually define h_load
as a list of values, the code works perfectly fine. For example:
h_load = [98.66655339, 96.37407933, 135.3339449, 94.53863385, 106.3833308, 138.7100619, 139.834439, 151.3381099, 304.5551849, 349.1563104, 356.0433984, 373.6430718, 369.8418957, 386.8469317, 337.4491338, 338.3883315, 350.9313867, 355.8703691, 334.9339505, 184.4436763, 144.5003903, 111.3945643, 87.33653369, 81.97636845]
I've ensured that the data is properly formatted as floats and there are no missing values. I'm puzzled as to why the constraint works with manually defined data but fails with data read from the Excel file.
Could anyone provide insights or suggestions on how to resolve this issue?
Thank you in advance for your help!
-
Hi 汉涛 田,
I suspect this comes down to the data being numpy floats when read from the excel file. See Constraint has no bool value (are you trying "lb <= expr <= ub"?)- Riley
0
サインインしてコメントを残してください。
コメント
1件のコメント