improve time to build model tupledict.sum()
回答済みI've been using this method for processing, but it's slow when dealing with large volumes of data. I'm looking for a better approach
ex)
in_out = {
('route1','item1',1):gurobi.LinExpr,('route1','item1',2):gurobi.LinExpr,('route1','item1',3):gurobi.LinExpr,
('route2','item1',1):gurobi.LinExpr,('route2','item1',2):gurobi.LinExpr,('route2','item1',3):gurobi.LinExpr,
('route3','item3',1):gurobi.LinExpr,('route3','item3',2):gurobi.LinExpr,('route3','item3',3):gurobi.LinExpr
}
target_item = ['item1']
time_index = [1,2,3]
for time in time_index:
for item in target_item:
model.addConstr( VALUE > in_out('*',item,time) )
# gp.quicksum(in_out[key] for key in in_out.keys() if item == key[1] and int(time) == int(key[2]) )
I tested using quicksum for summing all the parts that meet the conditions in the constraint, but it took even longer. I want to perform faster than both methods.
-
Hi Minsu Kim,
There are a few parts that are not clear to me:
- in_out is a normal Python dict, not a Gurobi tupledict.
- Each item in the dict is a linear expression. Should these be variables instead, or are you really referring to expressions including multiple terms?
- In the addConstr(), do you actually want to sum over all in_out entries for a specific item and time? There is currently no call to the sum() method.
Best regards,
Mario0 -
Hi Mario,
Thank you for your response. I created 'in_out' as follows.
in_out_before = {
('route1','item1',1):some_value,('route1','item1',2):some_value,('route1','item1',3):some_value,
('route2','item1',1):some_value,('route2','item1',2):some_value,('route2','item1',3):some_value,
('route3','item3',1):some_value,('route3','item3',2):some_value,('route3','item3',3):some_value
}in_out = model.addVars(in_out_before, name="in_out")
The contents of the dictionary are real values obtained from linear planning.
The part contains a typo. The corrected content is as follows.
model.addConstr( VALUE > in_out.sum('*',item,time) )
0 -
Hi,
You could try to use model.addConstrs() to create all constraints in one step, but this might not be too much different to your loop variant. With respect to term-based modelling, you seem to have already an efficient variant.
You can only get faster by switching to matrix-based modelling where you essentially define the constraint matrix first, and add all constraints in one step. Here are a few resources in this direction:
0 -
Hi!
I wrote down the example in a little more detail.
Is it possible to convert it to a matrix model even if it is a model like this?
I wrote down the example in a little more detail.Is it possible to convert it to a matrix model even if it is a model like this?
##############################################################################################################################
in_out_before = {
('route1','item1',1):some_value,('route1','item1',2):some_value,('route1','item1',3):some_value,
('route2','item1',1):some_value,('route2','item1',2):some_value,('route2','item1',3):some_value,
('route3','item3',1):some_value,('route3','item3',2):some_value,('route3','item3',3):some_value
}
in_out = model.addVars(in_out_before, name="in_out")
const_value = {
('item1',1):10,('item1',2):10,('item1',3):10,
('item3',1):10,('item3',2):10,('item3',3):10
}
in_out = {
('route1','item1',1):gurobi.LinExpr,('route1','item1',2):gurobi.LinExpr,('route1','item1',3):gurobi.LinExpr,
('route2','item1',1):gurobi.LinExpr,('route2','item1',2):gurobi.LinExpr,('route2','item1',3):gurobi.LinExpr,
('route3','item3',1):gurobi.LinExpr,('route3','item3',2):gurobi.LinExpr,('route3','item3',3):gurobi.LinExpr
}
target_item = ['item1','item3']
time_index = [1,2,3]
for time in time_index:
for item in target_item:
model.addConstr( const_value[item,time] > in_out.sum('*',item,time) )0 -
Yes, every model can be built in matrix form. You must define the coefficient matrix first and then multiply with matrix variables.
But the code will probably not be easy to read and debug anymore. So you need to decide if reducing model building time at the cost of readability pays off.
0
サインインしてコメントを残してください。
コメント
5件のコメント