'I created variables with for loop and it resulted to KeyError

I created a model with gurobi. I created 3 variables, x,y,w. But when I'm adding constraints, the first line always has problem. It seems that even x_vars(0,0,0) doesn't even exist. I think I've got something wrong when creating the variables. I'm been "debugging" for the entire afternoon and it still doesn't work.

    # Constants
  Land = 500
  rWheat = 200
  rCorn = 240
  mBeet = 6000
  prob = 1/3
  wheatYield = [3.0,2.5,2.0]
  cornYield = [3.6,3.0,2.4]

 # Model
  mFarm = grb.Model('Farm')
  x_vars
  y_vars
  w_vars
  # Scenarios - A for wheat; B for corn
  SA=3
  SB=3
  for a in range (SA):
    for b in range (SB):
      x_vars = mFarm.addVars([(i,a,b) for i in range(3)],vtype=grb.GRB.CONTINUOUS,name="x")
      y_vars = mFarm.addVars([(i,a,b) for i in range(2)],vtype=grb.GRB.CONTINUOUS,name="y")
      w_vars = mFarm.addVars([(i,a,b) for i in range(4)],vtype=grb.GRB.CONTINUOUS,name="w")
    
  # Constraits 
  for a in range (SA):
    for b in range (SB):
      mFarm.addConstr(grb.quicksum(x_vars[i,a,b] for i in range(2))<=Land)
      mFarm.addConstr(w_vars[2,a,b]+w_vars[3,a,b]<=x_vars[2,a,b])
      mFarm.addConstr(w_vars[2,a,b]<=mBeet)
      mFarm.addConstr(x_vars[0,a,b]*wheatYield[a]+y_vars[0,a,b]-w_vars[0,a,b]>=rWheat)
      mFarm.addConstr(x_vars[1,a,b]*cornYield[b]+y_vars[1,a,b]-w_vars[1,a,b]>=rCorn)

my Google colab screenshot

I got the message of

KeyError                                  Traceback (most recent call last)
<ipython-input-95-cb120c86edd9> in <module>()
     25 for a in range (SA):
     26   for b in range (SB):
---> 27     mFarm.addConstr(grb.quicksum(x_vars[i,a,b] for i in range(2))<=Land)
     28     mFarm.addConstr(w_vars[2,a,b]+w_vars[3,a,b]<=x_vars[2,a,b])
     29     mFarm.addConstr(w_vars[2,a,b]<=mBeet)

src/gurobipy/gurobi.pxi in gurobipy.quicksum()

<ipython-input-95-cb120c86edd9> in <genexpr>(.0)
     25 for a in range (SA):
     26   for b in range (SB):
---> 27     mFarm.addConstr(grb.quicksum(x_vars[i,a,b] for i in range(2))<=Land)
     28     mFarm.addConstr(w_vars[2,a,b]+w_vars[3,a,b]<=x_vars[2,a,b])
     29     mFarm.addConstr(w_vars[2,a,b]<=mBeet)

KeyError: (0, 0, 0)


Solution 1:[1]

You should create the variables like this:

x_vars = mFarm.addVars(3, SA, SB, vtype=grb.GRB.CONTINUOUS,name="x")
y_vars = mFarm.addVars(2, SA, SB, vtype=grb.GRB.CONTINUOUS,name="y")
w_vars = mFarm.addVars(4, SA, SB, vtype=grb.GRB.CONTINUOUS,name="w")

In your code, you are overwriting the Python variables x_vars, y_vars, and w_vars in every iteration of the loops, so eventually, you end up with just the final set of variables [i, 2, 2].

Please refer to the documentation of Model.addVars() for further information.

Then, you can construct the constraints like in your code, referencing them via the respective indices, although you might want to check out Model.addConstrs() for a more compact version similar to addVars.

Side note: In Python, it is not necessary to declare the variables before you assign them.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1