简体   繁体   中英

How to multiply decision variable with a matrix in Gurobi Python

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM