Unsupported operand type(s) for *: 'float' and 'generator'
AnsweredHi,
How can I debug the "unsupported operand type(s) for *: 'float' and 'generator'" error in the following code associated with the objective function below?
obj = quicksum(people[i]*(1quicksum(math.exp(rate*q)*gamma_1[i,q] for q in Q_1)) for i in regions if i != depot)
+ quicksum(w[t]*(quicksum(people[i]*(quicksum((rate*q)*gamma_2[i,t,q] for q in Q_2[t])
 quicksum((rate*q)*gamma_3[i,t,q] for q in Q_3[t]))) for i in regions if i != depot) for t in periods if t != periods[0])

Hi,
Could you please clarify what type your variables have? For example, which ones are Gurobi model variables, and which are constants? Or, better yet, provide a selfcontained code example so we can see what is what.
Silke
0 
Hi,
Please find the definition of the parameters and variables below:
# Parameters
num_region = 5 #number of regions, including depot
num_period = 2 #number of planning periods
regions = []
for i in range(num_region):
regions.append(i)
periods = []
for i in range(num_period):
periods.append(i+1) # periods = [1, 2, ...]
depot = regions[0]
rate = 1 # detection rate of UAVs
divisor = 10 # used to trun S to S_init; S = divisor*S_init
minn = 0 # minimum search time/divisor
maxx = int(dur/divisor) # maximum search time/divisor
people = {depot: 0} #number of people who need help in each region
for i in [reg for reg in regions if reg != depot]:
people[i] = random.randint(50, 100)
weights = list(np.linspace(0,1, num_period+1))
weights.sort(reverse=True) # victims prefer to be visited during the earlier periods
w = {} #periods' discount weights
for i in periods:
w[i] = weights[i]
# additional linearization parameters
Q_1 = []
for i in range(minn, maxx+1):
Q_1.append(divisor*i)
Q_2 = {}
for t in [per for per in periods if per != periods[0]]:
Q_2[t] = []
for i in range(minn, maxx*(t1)+1):
Q_2[t].append(divisor*i)
Q_3 = {}
for t in [per for per in periods if per != periods[0]]:
Q_3[t] = []
for i in range(minn, maxx*(t)+1):
Q_3[t].append(divisor*i)
# Create model
model = Model("linear")
# Create the variables
# additional linearization variables
gamma_1 = {}
for i in [reg for reg in regions if reg != depot]:
for q in Q_1:
gamma_1[i, q] = model.addVar(vtype=GRB.BINARY, name="gamma_1_%s,%s" %(i,q))
gamma_2 = {}
for i in [reg for reg in regions if reg != depot]:
for t in [per for per in periods if per != periods[0]]:
for q in Q_2[t]:
gamma_2[i, t, q] = model.addVar(vtype=GRB.BINARY, name="gamma_2_%s,%s,%s" %(i, t, q))
gamma_3 = {}
for i in [reg for reg in regions if reg != depot]:
for t in [per for per in periods if per != periods[0]]:
for q in Q_3[t]:
gamma_3[i, t, q] = model.addVar(vtype=GRB.BINARY, name="gamma_3_%s,%s,%s" %(i, t, q))0 
Hi,
Thanks for posting your code. This looks as though some of the parentheses may be misplaced. Your objective expression is pretty long and convoluted. Maybe different formatting can help break this up a bit and make it easier to parse (and maintain). E.g.:
obj = quicksum(
people[i]*(1
quicksum(math.exp(rate*q)*gamma_1[i,q] for q in Q_1)
)
for i in regions if i != depot
) + quicksum(
w[t]*(
quicksum(
people[i]*(
quicksum((rate*q)*gamma_2[i,t,q] for q in Q_2[t])
 quicksum((rate*q)*gamma_3[i,t,q] for q in Q_3[t])
)
for i in regions if i != depot
)
)
for t in periods if t != periods[0]
)In this code, I moved one of the closing parenthesis before "for i in regions if i != depot" behind this expression. Now I don't get the error.
Silke
0 
Hi Silke,
Thank you! My problem is resolved!
0
Please sign in to leave a comment.
Comments
4 comments