Reputation: 11
Hello good day to you all.
I have a small problem in which I assign locations at which demands to return goods to a lets say distribution centre occur. In order to handle this demand at these locations, we have to install certain technologies (A and B). We can only install as much of B as we have of A, or 0 of B. So, if B = 2, then A must be 2 as well. However, B can never be lets say B = 1 if A = 2. It can be B = 0 and A = 2.
Now the problem occurs in which I would like some help. If coded this in Python and it works for individual locations. But if I want to extend it to cover all locations, I run into errors... Below the code of the small problem:
# Create a new model
m = gp.Model("mip1")
BigM = 100
q = locations[i]
# Create parameters
cost_A = 20000
cost_B = 13500
cost_transport = 0.007
cap_A = 400000
cap_B = 500000
# Create variables
x = m.addVar(lb=0, ub=3, vtype=GRB.INTEGER, name="A")
y = m.addVar(lb=0, ub=3, vtype=GRB.INTEGER, name="B")
z = m.addVar(lb=0, vtype=GRB.INTEGER, name="flow_1")
a = m.addVar(vtype=GRB.BINARY, name="BigM")
# Set objective to minimize cost
m.setObjective(cost_A * x + cost_B * y + cost_transport * z, GRB.MINIMIZE)
# Add constraint: amount of UBC
m.addConstr(z == q, "accept returned demand")
# Add constraint: capacity
m.addConstr(z <= cap_A * x + cap_B * y, "capacity of locations")
# Add constraint: only B if location exists/has A
m.addConstr(y <= x, "B constraint_1")
# Add constraint: only B if location exists/has A
m.addConstr(x - y <= BigM * a, "B constraint_2")
# Add constraint: only B if location exists/has A
m.addConstr(y <= BigM * (1 - a), "B constraint_3")
# Optimize model
m.optimize()
This model above creates output on how many A and B to install at a certain location, given the demand at that location. This ofcourse is very simple. However, I now want to extend this problem considering multiple locations and that is where I run into errors... KeyError: (0, 1)
, KeyError: (1, 1)
, KeyError: (1)
and KeyError: (0)
all have been there already..
I thought it would be "simply" making addVars and addConstraints, but then you need to assigns sets etc.. I am actually quite lost in the process. Is there anybody who could please help me out?
Thank you all very much in advance for your time and consideration.
P.S. it does not have to be solved by solvers like Gurobi, if you know anything else I am happy to hear!
Upvotes: 1
Views: 137
Reputation: 11938
Yes, you are going to have to tear down most of that and introduce sets... :). My Gurobi syntax is weak, so here are a few things to get you started in pseudocode.
You will need a couple of sets
T = {A, B}
L = {LA, Chicago, NY} # for example
You need a variable to assign (this is an assignment problem) a quantity of tech T
at location L
...
X[tech, loc] # domain non-neg integers, right?
Then you can re-construct your constraints & such above using summations over the indices as appropriate. I'm sure there are a bunch of Gurobi examples to help with that. Start VERY small so you can troubleshoot.
You will need a constraint for your conditions on tech B. You didn't say if that constraint was global or enforced by location. Let's assume it is by location (the harder case). You then need another indicator variable to indicate whether they are equal or not.
Eq[loc] # indicate whether B=A at particular location, binary var
Then you can use that with Big M to control the equality by location by making 3 constraints per location
X[B, loc] <= X[A, loc]
X[A, loc] - X[B, loc] <= (1 - Eq[loc]) * M
X[B, loc] <= Eq[loc] * M
Upvotes: 1