How to Define a Constraint for Calculating the sum of one Multi-Dimensional Variables in Gurobi
AnsweredThe quicksum for the opan veriable should be <= 7, . However, I need to add the constraint below:
My decission variable is:
# define the 1st decision variable
open_warehouse = (warehouse_data.gppd.add_vars(model, vtype=GRB.BINARY, name="open"))
origin | fixed_cost | var_cost | cap | open |
chennai | $ 50,000 | $ 2.23 | 400,000 | <gurobi.Var *Awaiting Model Update*> |
bhopal | $ 56,000 | $ 2.32 | 240,000 | <gurobi.Var *Awaiting Model Update*> |
jaipur | $ 65,000 | $ 2.25 | 300,000 | <gurobi.Var *Awaiting Model Update*> |
kolkata | $ 65,000 | $ 2.45 | 200,000 | <gurobi.Var *Awaiting Model Update*> |
nasik | $ 55,000 | $ 2.21 | 300,000 | <gurobi.Var *Awaiting Model Update*> |
pune | $ 89,000 | $ 2.56 | 300,000 | <gurobi.Var *Awaiting Model Update*> |
sonipat | $ 95,000 | $ 2.78 | 240,000 | <gurobi.Var *Awaiting Model Update*> |
∑(open) <= 7 |
While adding the constrained so i am getting an error,
constrained code is
for origin in open_warehouse:
cons4 = model.addConstr(quicksum(open_warehouse[origin, open] for open in open_warehouse) <= 7, name="num_of_ware" % origin)
-
Hi Mohd,
Could you please share what kind of error you get? And could you please share a minimal reproducible example of your code?
Thanks,
Matthias0 -
In case it helps:
0 -
Hi Team,
hope you are doing well.
please review a code one time i am getting an eroor to add the constraind.
https://colab.research.google.com/drive/1g0r2nftA7qu0V-jhQU2jxv70RQVS-1M0
0 -
Hi Team,
Hope everything is good.
This is my code in which i am getting an error i am trying to define in 2 way
first way is:-
for origin in open_warehouse_ver:cons4 = model.addConstr(quicksum(open_warehouse_ver[origin, open_warehouse] for open_warehouse in open_warehouse_ver) <= max_warehouses, name="cons4 %s" % origin)error:-KeyError Traceback (most recent call last)
/usr/local/lib/python3.10/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance) 3801 try: -> 3802 return self._engine.get_loc(casted_key) 3803 except KeyError as err:
5 frames
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: ('fixed_cost', 'fixed_cost')
The above exception was the direct cause of the following exception:
KeyError Traceback (most recent call last)
src/gurobipy/gurobi.pxi in gurobipy.quicksum()
/usr/local/lib/python3.10/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance) 3802 return self._engine.get_loc(casted_key) 3803 except KeyError as err: -> 3804 raise KeyError(key) from err 3805 except TypeError: 3806 # If we have a listlike key, _check_indexing_error will raise
KeyError: ('fixed_cost', 'fixed_cost')
second way is : -no_open_we_cons = gppd.add_constrs(model,open_warehouse_ver['open_warehouse'].sum(),GRB.LESS_EQUAL, max_warehouses,name = "no_open_we_cons")no_open_we_conseroor:-ValueError Traceback (most recent call last)
<ipython-input-34-1a9bc2ae7f50> in <cell line: 1>() ----> 1 no_open_we_cons = gppd.add_constrs( 2 model, 3 open_warehouse['open_warehouse'].sum(), 4 GRB.LESS_EQUAL, max_warehouses, 5 name = "no_open_we_cons"
5 frames
/usr/local/lib/python3.10/dist-packages/pandas/core/internals/construction.py in _extract_index(data) 654 655 if not indexes and not raw_lengths: --> 656 raise ValueError("If using all scalar values, you must pass an index") 657 658 elif have_series:
ValueError: If using all scalar values, you must pass an index
0 -
Thanks! Could you also share an example definition for open_warehouse_ver?
0 -
could you please review the code and i have shared the link of my google colab https://colab.research.google.com/drive/1g0r2nftA7qu0V-jhQU2jxv70RQVS-1M0
0 -
We are having 7 location in origin table where we can open the ware house on the location and the number of ware house should be LESS_EQUAL, max_warehouse:
0 -
We are having 7 location in origin table where we can open the ware house on the location and the number of ware house should be LESS_EQUAL, max_warehouse:
0 -
Hi Mohd,
Note that when accessing your Colab notebook we cannot see any of the data that you are reading in from /content/drive... and therefore cannot execute your code.
This makes it very difficult to troubleshoot. Are you able to provide this data?
- Riley
0 -
Hi Team,
this is the link of my whole file and you will get the access to all data.https://drive.google.com/drive/folders/1p4IbxQCvMSEPjqI3ezCyy9aVxOTM2Ea3
0 -
Thanks Mohd,
So open_warehouse_ver is a pandas dataframe:
fixed_cost var_cost cap open_warehouse
origin
chennai 50000 2.23 400000 <gurobi.Var open_warehouse[chennai]>
bhopal 56000 2.32 240000 <gurobi.Var open_warehouse[bhopal]>
jaipur 65000 2.25 300000 <gurobi.Var open_warehouse[jaipur]>
kolkata 65000 2.45 200000 <gurobi.Var open_warehouse[kolkata]>
nasik 55000 2.21 300000 <gurobi.Var open_warehouse[nasik]>
pune 89000 2.56 300000 <gurobi.Var open_warehouse[pune]>
sonipat 95000 2.78 240000 <gurobi.Var open_warehouse[sonipat]>If you were to run
for origin in open_warehouse_ver:
print(origin)you will see that it is printing the column names of this dataframe. So in your code:
for origin in open_warehouse_ver:
cons4 = model.addConstr(quicksum(open_warehouse_ver[origin, open_warehouse] ...the origin variable is looping through column names which doesn't work because you're indexing the pandas dataframe incorrectly. This is not an issue specific to Gurobi. Perhaps it would be worthwhile reviewing the pandas guide on indexing and selecting data.
It is not clear what the constraint you are trying to model is, but if you are simply wanting to sum up all the variables in the open_warehouse column and constrain the sum to be less than max_warehouses then you can use
model.addConstr(open_warehouse_ver["open_warehouse"].sum() <= max_warehouses, name="cons4")
If this is not what you are trying to achieve then please help us to understand.
- Riley
0 -
Hi Team,
Hope you are doing well.
This is the defined constrained.
no_open_we_cons = model.addConstr(open_warehouse_ver["open_warehouse"].sum() <= max_warehouses, name="no_open_we_cons")no_open_we_cons<gurobi.Constr no_open_we_cons>i am getting the open warehouse variable ZERO value.originchennai <gurobi.Var open_warehouse[chennai] (value 0.0)> bhopal <gurobi.Var open_warehouse[bhopal] (value 0.0)> jaipur <gurobi.Var open_warehouse[jaipur] (value 0.0)> kolkata <gurobi.Var open_warehouse[kolkata] (value 0.0)> nasik <gurobi.Var open_warehouse[nasik] (value 0.0)> pune <gurobi.Var open_warehouse[pune] (value 0.0)> sonipat <gurobi.Var open_warehouse[sonipat] (value 0.0)> Name: open_warehouse, dtype: objectit should be taken the optiimze value please look into the code please.https://drive.google.com/drive/folders/1p4IbxQCvMSEPjqI3ezCyy9aVxOTM2Ea30 -
Hi Mohd,
I am guessing you are missing a constraint, perhaps something relating these variables to the flow variables. As you look through your constraints and objectives ask yourself why these variables should not be zero. Your model is minimizing cost, and these variables represent a portion of the cost so the solver will want to make their values small. If there is nothing stopping them from being zero then they will be zero.
- Riley
0 -
Hi Team,
Hope you are doing well.
could you please arrange half an hour metting.
0 -
Hi Mohd,
We cannot provide this kind of 1-on-1 support here on the community forum, but as a commercial user, you can always create a support request at https://support.gurobi.com, where you will get 1-on-1 support.
- Silke
0 -
Hi Team,
Hope you are doing.
how do i set the constraind in which i get optimize value in open_warehouse_ver. because i am still getting 0 value and it should be around 4.
i am using this code to define the constrained.
no_open_we_cons = model.addConstr(open_warehouse_ver["open_warehouse"].sum() <= max_warehouses, name="no_open_we_cons")no_open_we_consopen_warehouse_ver["open_warehouse"]origin chennai <gurobi.Var open_warehouse[chennai] (value 0.0)> bhopal <gurobi.Var open_warehouse[bhopal] (value 0.0)> jaipur <gurobi.Var open_warehouse[jaipur] (value 0.0)> kolkata <gurobi.Var open_warehouse[kolkata] (value 0.0)> nasik <gurobi.Var open_warehouse[nasik] (value 0.0)> pune <gurobi.Var open_warehouse[pune] (value 0.0)> sonipat <gurobi.Var open_warehouse[sonipat] (value 0.0)> Name: open_warehouse, dtype: objectThere should be a some value0 -
Hi Mohd,
Your question looks largely unchanged, and consequently so does my answer
I am guessing you are missing a constraint, perhaps something relating these variables to the flow variables. As you look through your constraints and objectives ask yourself why these variables should not be zero. Your model is minimizing cost, and these variables represent a portion of the cost so the solver will want to make their values small. If there is nothing stopping them from being zero then they will be zero.
I am guessing a warehouse needs to be open in order to accept flow in or out of it? Currently you do not have any constraints which enforce this.
I think you will find inspiration in our facility.py example, in particular the "production constraints".
- Riley
0
Please sign in to leave a comment.
Comments
17 comments