3

I'm very new to Gurobi and I'm trying to implement an assignment problem as follows:

      # Create decision variables for the allocation
        x = {}
        for s in arr1:
           for t in arr2:
              x[s,t] = m.addVar(vtype=GRB.BINARY, name="allocation")

              x[s,t] == x[s,t] * mymat[s]
        m.modelSense = GRB.MAXIMIZE
        m.update()

I want to know that writing a linear expression as above is possible so that I can maximize the allocation of people to the same task who have similar preferences.

However running the model written as above generated an error when retrieving results using x[s,t].x . The error was

GurobiError: Unable to retrieve attribute 'X'

Data coming to the model from a separate file and the code is as follows:

    data=c.execute('select id,pref from data')
    result = c.fetchall()
    pref_data=dict(result)
   
    mymat=defaultdict(int)
    a=1
    for i in range(1,10):
       row = [] 
       for x in range(0,i):
          row.append(0)
      for j in range(i+1, 10):
          if pref_data[i]==pref_data[j]:
             row.append(1)
          else:
             row.append(0)
      mymat[a]=row
      a+=1
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ann
  • 403
  • 2
  • 5
  • 17
  • The X attribute (`Var.X` or `Var.get('X')`) represents the value of an optimal solution, so you cannot access the X attribute until after you have called `Model.optimize()`. – Greg Glockner Oct 27 '16 at 15:19
  • Yes I understand. I actually have called x[s,t] .x at the end of the program. Sorry I copy pasted only a part of the code. However I get this error when I add the term x[s,t] == x[s,t] * mymat[s] . So my major problem is to know how to fix this error? – Ann Oct 27 '16 at 15:46
  • See this [related question](http://stackoverflow.com/a/19277051/14167) on how to use "irreducible infeasible set" to diagnose infeasible problems. – David Nehme Oct 27 '16 at 18:31

1 Answers1

0

Your model is infeasible unless mymat[a] = 1, for all a. If that is not the case, Gurobi will tell you the instance is infeasible (which you can check using the status = Model.optimize()) and there is no value to be retrieved from your variable.

pbc1303
  • 142
  • 9