I am learning to build optimization models through Gurobi python and I am having some issues finding the pythonic way of defining decision variables and constraints:
Assuming I have these sets:
time={morning, afternoon, evening};
interval={early,late};
food={burger, banana, apple, orange};
and my decision variable is binary eat[time,interval,food]. However I only have a defined set of possible options as below and cannot enumerate all elements of my sets:
time interval food number value
morning early banana 2 500
morning early apple 3 600
afternoon early burger 1 800
evening late orane 2 400
so my eat variables can only be the following:
eat[morning,early,banana]
eat[morning,early,apple]
eat[afternoon,early,burger]
eat[evening,late,orange]
and I cannot do:
eat = m.addVars(time, interval, food, name = "Eat", vtype=GRB.BINARY)
I can do something like:
eat = {}
for row in input.to_dict('records'):
key = (row['time'], row['interval'],row['food'])
eat[key] = m.addVar(name = "Eat", vtype=GRB.BINARY)
But I still have trouble defining my objective which is multiplying number and value and eat and I am looking for a more consistent, elegant way:
obj = quicksum(number[i,j,k]*value[i, j, k] * eat[i, j, k] for i in time
for j in interval for k in food)
The above will enumerate all which is wrong and I tried something like this:
obj = quicksum(number[key]*value[key] * eat[key] \
for key in eat)
which limits it to only defined combinations in the dictionary but then I am struggling with constraints when I have to separate elements of dictionary like below:
m.addConstrs(quicksum(eat[i,j,k] for k in food)==1 for i in time for j in interval)
or something like
m.addConstrs(quicksum(eat[morning,j,banana] ==1) for j in interval)
Sorry for the long questions. Any help from optimization/python experts would be great.