'None type' object not subscriptable

91 views
Skip to first unread message

Adwaitvedant Mathkar

unread,
May 12, 2012, 10:41:30 PM5/12/12
to gur...@googlegroups.com
Hey,
I'm running the following program:

from gurobipy import *
from string import *
def optimize_linear_function(gradient,dimension,upper_bound):
    m=Model("MNL_func_optimization")
    weighted_sum = {}
    s={}
    for index in range(dimension):
        weighted_sum[index] = m.addVar(obj = gradient[index])
        s[index] = m.addVar(ub=upper_bound,lb=0,name = str(index))            #I HAVE DECLARED VARIABLE s HERE

    m.modelSense = GRB.MAXIMIZE
    m.update()

    for index in range(dimension):   
        m.addConstr( weighted_sum[index] <= s[index])
        m.addConstr( weighted_sum[index] <= -s[index])

   
   
    m.addConstr(quicksum(s) <= upper_bound)                  #THIS IS WHERE I GET THE ERROR
    m.optimize()

    for index in range(dimension):
        print weighted_sum[index].x

-----------------------------------------------------------------

On running the program I get:

TypeError: 'NoneType' object is not subscriptable

Probably saying that dictionary s is non-type. However I have clearly added s to variable list

       s[index] = m.addVar(ub=upper_bound,lb=0,name = str(index))

Question being, why am I still getting such an error?

Any help would be greatly appreciated!

Thanks!

Best,
Adwait
--
Adwaitvedant Mathkar
Junior Undergraduate 
Department of Electrical Engineering
IIT Bombay

Greg Glockner

unread,
May 14, 2012, 6:34:09 PM5/14/12
to gur...@googlegroups.com
quicksum is intended to be used for a list, not a dictionary. It looks like you simply need a list of variables, so change "s" to a list.

Reply all
Reply to author
Forward
0 new messages