Re: Attribute errors occurs in objective function: AttributeError: 'generator' object has no attribute 'sense':

820 views
Skip to first unread message

Luciano Rigolin de Almeida

unread,
Apr 26, 2021, 3:28:31 AM4/26/21
to Shax Studio, Python-MIP
Kick in the objective function some variable of the solution. You only have constants.

Luciano

Em sáb., 24 de abr. de 2021 às 20:58, Shax Studio <shakhaw...@gmail.com> escreveu:
Can someone help  me out here! As I'm pretty new here in this topics, I would love to get any suggestion from you guys.  

Thanks

from mip import Model, xsum, minimize, BINARY

c_var_i = [10,13]
y_t_i = [[1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6]]
d_t = [[1, 2, 3, 4, 6, 7], [1, 2, 5, 4, 5, 4]]
k_i = [[1, 2, 3, 4, 6, 7], [1, 2, 5, 4, 5, 4]]

del_t = .25

I = range(len(y_t_i))
print(I)
J = range(len(c_var_i))
print(J)
# c_sum = [c * sum(y_t_i[i]) for i,c in enumerate(c_var_i)]
# c_sum

m = Model("Electricity Demand")

y = [[m.add_var() for i in I]]
print(y)

m.objective = minimize(c * xsum(y_t_i[i]) for i,c in enumerate(c_var_i))

*********
getting some error here:


--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-32-96edd6ed2f2b> in <module> 4 print(y) 5 ----> 6 m.objective = minimize(c * xsum(y_t_i[i]) for i,c in enumerate(c_var_i)) 7 ~/Library/Python/3.8/lib/python/site-packages/mip/model.py in minimize(objective) 1529 if isinstance(objective, mip.Var): 1530 objective = mip.LinExpr([objective], [1.0]) -> 1531 objective.sense = mip.MINIMIZE 1532 return objective 1533 AttributeError: 'generator' object has no attribute 'sense'


*********

m += xsum(y_t_i[i] * del_t for i in I) == [[(i * .25) for i in c] for c in d_t]
m += [[i for i in c] for c in y_t_i] <= [[i for i in c] for c in k_i]

m.optimize()

selected = [i for i in I if y[i].x >= 0.99]
print("selected items: {}".format(selected))

--
You received this message because you are subscribed to the Google Groups "Python-MIP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-mip+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python-mip/52a7c386-e241-4af7-b08d-c4802cc8885cn%40googlegroups.com.

Shax Studio

unread,
Apr 26, 2021, 3:23:33 PM4/26/21
to Python-MIP
Okay. 

I'm doing now like this->

from mip import Model, xsum, minimize, BINARY

c_var_i = [10,13] # variable, production-dependent costs for power plant 𝑖 [€/𝑀𝑊h]
#y_t_i = [[1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6]]
d_t = [[1, 2, 3, 4, 6, 7], [1, 2, 5, 4, 5, 4]] # demand for electricity at time t [MW]
k_i = [[1, 2, 3, 4, 6, 7], [1, 2, 5, 4, 5, 4]] # Capacity (maximum generation) of power plants

del_t = .25 # duration of time step [h]

I = set(range(len(k_i)))
print(I)

m = Model("Electricity Demand")

y_t_i = [m.add_var() for i in I] # Continous variable 
print(y_t_i)

#m.objective = minimize(c * xsum(y_t_i[i]) for i,c in zip(c_var_i, k_i))

m.objective = minimize(c * xsum(y) for c, y in zip(c_var_i, y_t_i))


[<mip.entities.Var object at 0x121341b50>, <mip.entities.Var object at 0x121341be0>]
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-11-0bca49b4fe9c> in <module> 6 #m.objective = minimize(c * xsum(y_t_i[i]) for i,c in zip(c_var_i, k_i)) 7 ----> 8 m.objective = minimize(c * xsum(y) for c, y in zip(c_var_i, y_t_i)) 9 10 #m.objective = minimize([xsum(y_t_i[i]) for i in c] for c in k_i) ~/Library/Python/3.8/lib/python/site-packages/mip/model.py in minimize(objective) 1529 if isinstance(objective, mip.Var): 1530 objective = mip.LinExpr([objective], [1.0]) -> 1531 objective.sense = mip.MINIMIZE 1532 return objective 1533 AttributeError: 'generator' object has no attribute 'sense'

Luciano Rigolin de Almeida

unread,
Apr 27, 2021, 3:19:05 AM4/27/21
to Shax Studio, Python-MIP
Hello.

Analyzing your code better, I think you need to use m.objective as follows:

m.objective = minimize(xsum(c * y for c in c_var_i for y in y_t_i))

It is even better if you use it like this:

m.objective = minimize(xsum(c * y_t_i[i] for c in c_var_i for i in I))

The problem is in your 'c' outside of xsum.

Att,

Luciano

Shax Studio

unread,
Apr 27, 2021, 10:04:10 AM4/27/21
to Python-MIP
Thank you so much. But 
my approach is like getting something like 

10*(1+2+3+4+5) and 13*(1+2+3+4+5) -> this means c*sum(y[i])

Luciano Rigolin de Almeida

unread,
Apr 27, 2021, 10:12:24 AM4/27/21
to Shax Studio, Python-MIP
OK, but do you want to minimize which variable? Can you send your objective function?

Att,

Luciano

Shax Studio

unread,
May 1, 2021, 8:46:41 AM5/1/21
to Python-MIP
Here is my objective function
Screenshot 2021-05-01 at 14.45.31.png

Shax Studio

unread,
May 1, 2021, 8:47:03 AM5/1/21
to Python-MIP
I want to minimise Y t,i

Chaymae Makri

unread,
Sep 26, 2022, 4:19:02 AM9/26/22
to Python-MIP
I want to ask you if you find the response to your problem because I get the same error-Python. Thanks
Reply all
Reply to author
Forward
0 new messages