Minimizing the divergence of a product mix and a demand vector according to a given divdictionary
AnsweredHello Everybody,
I´m trying to find a production schedule vector that is as close as possible to a given demand vector and at the same time satisfies constraints.
I build a small example to illustrate this:
I have four products {1,2,3,4}. I have a demand vector with ten 'orders':
demand = {
1: 3,
2: 2,
3: 2,
4: 1,
5: 1,
6: 1,
7: 4,
8: 3,
9: 2,
10: 1
}
I have a budget (ressource limit) for each product
capacity = {1: 3, 2: 3, 3: 3, 4: 2}
And I have a 'divergence matrix' which holds information about the 'distance' within each pair of products.
div = {
(1,1): 0.0, (1,2): 0.2, (1,3): 0.3, (1,4): 0.7,
(2,1): 0.2, (2,2): 0.0, (2,3): 0.1, (2,4): 0.6,
(3,1): 0.3, (3,2): 0.1, (3,3): 0.0, (3,4): 0.6,
(4,1): 0.7, (4,2): 0.6, (4,3): 0.6, (4,4): 0.0,
}
Now I want to build a vector that mimizes the distance between demand and prod vector according to my div dict and follwoing the capacity constraints.
I tried to build a first version without contraints (expecting both vectors to be the same) but I think I didn´t formulate the problem right.
model = gp.Model('test')
prod_vars = model.addVars(demand, lb = 1, ub = 4, vtype=GRB.INTEGER, name='Choice')
delta = model.addVar(vtype=GRB.CONTINUOUS, name='Delta Prod <> Demand')
model.setObjective(delta, GRB.MINIMIZE)
model.addConstr(delta == gp.quicksum(div[prod_vars[i],demand[i]] for i in demand.keys()), name='Delta calc')
model.update()
model.optimize()
Do you have any suggestions on how to model this and how to add the capacity constraints?
I'm receiving the following error:
KeyError: (<gurobi.Var prod_vars[1]>, 3)
Kind regards,
Maik

Hi Maik,Copying your snippet in Python gives me a different error message:
gurobipy.GurobiError: Variable has not yet been added to the model
The reason is that the sum is over \(\texttt{div[prod_vars[i],demand[i]]}\). In Gurobi, you cannot use decision variables as indices. Please take a look at different types of the optimization problems supported by the Gurobi Optimizer.
I am still not clear what the decision variables are in your problem. You have 10 orders for 4 different products.
 Each product has a capacity. As an example, there are orders 4, 5, 7, and 10 for product 1, but only three of them can be delivered using product 1 because the capacity for product 1 is three.
Can an order be delivered using a different product? For example, is it possible to deliver order 10 using product 2 which is the most similar one to product 1 (it has the lowest deviation value in the div matrix)? Do you want to find which product should be used to deliver each order to minimize the deviation between the products used to deliver orders from the original required products?Best regards,Maliheh2 
Hi Maliheh,
thx for your quick response.
Concerning your general questions about the optimization problem: Yes :) I think you got it all right! The aim is to find a production plan that sticks to the ressources and then minimizes the devitation from the originial demand plan.
I wanted to minimize the total deviation in a way that the optimizer sets a certain product for each order. So there should be a decision variable for each order which contains an integer referring to the corresponding product. Then there was a demand vector and a production plan vector, whose pairwise value devationen could be extraced from the given div matrix and summarized.
I understand, that this does not seem possible in gurobi the way I tried to build it. Is there another way you can think of realizing this kind of task?
Best regards,
Maik
0 
Hi Maik,
You can define the decision variables as binary variables \(x_{ij} \) being equal to 1 if order \(i\) is delivered by product \(j\) and to 0 otherwise.
Let us further define \(r_i\) to represent the required product for order \(i\) (it is the demand dictionary in your example) and \(d_{kj}\) to represent the penalty of serving an order using product \(j\) instead of product \(k\) (it is the div dictionary in your example) . As an example, \(r_1 = 3, r_2 = 2, ...\) and \(d_{11} = 0, d_{12}=0.2, ...\).
You can then define your objective function as \( \sum_{i, j} d_{r_{i}j} x_{ij} \). For example, if order 1 is delivered by product 4 instead of 3, we will have \(x_{14} = 1\) and the penalty will be \(d_{34} \times x_{14} = 0.6\). You, of course, will need to add constraints to make sure that each order is delivered by one product and that the capacity constraint is satisfied.
Best regards,Maliheh
1
Please sign in to leave a comment.
Comments
3 comments