简体   繁体   English

Pyomo - 将目标 function 放在索引上?

[英]Pyomo - lopping an objective function over an index?

I want to loop an objective function on an index.我想在索引上循环一个目标 function。 Let's say that I have the following dataframe:假设我有以下 dataframe:

index_i index_i index_j index_j value价值
10400 10400 Mexico墨西哥 23 23
10400 10400 Guatemala危地马拉 44 44
10600 10600 Mexico墨西哥 25 25
10600 10600 Guatemala危地马拉 30 30

I want to do something like this:我想做这样的事情:

for i in model.i:
         model.obj = Objective(sum(x[i,j] for j in model.j), maximize)
         ...
         results = opt.solve(model)
 

I want to do the loop directly in the objective function or solve, because I don't want to build i-times the model variables and parameters (ie, looping the opt model i-times).我想直接在目标 function 中进行循环或求解,因为我不想构建 i-times model 变量和参数(即循环 opt Z20F35E630DAF44DFA4C3F68FZCZ i-times)。

Thanks!!!谢谢!!!

You have it correct in the first instance.你一开始就说对了。 It isn't clear what you are trying to avoid... ?目前尚不清楚您要避免什么...? Realize all this is doing is constructing an expression to hand to the model.意识到这一切正在构建一个表达式以交给 model。 It isn't "looping" as it solves, if that is your concern.如果这是您所关心的,那么它解决的不是“循环”。

Your second effort, looping over i, will create a new obj for each value of i and then solve with that specific value only .您的第二次尝试,循环 i,将为 i 的每个值创建一个新的 obj,然后使用该特定值求解。 Is that what you intend?那是你的意图吗?

For example:例如:

from pyomo.environ import *

m = ConcreteModel()

# SETS
m.I = Set(initialize=[1,2,3])
m.J = Set(initialize=list('ABC'))

# VARIABLES
m.x = Var(m.I, m.J, domain=Binary)

# GOOD OBJ
m.obj1 = Objective(expr=sum(m.x[i,j] for i in m.I for j in m.J))

# BAD OBJ!!  DON'T DO THIS
for i in m.I:
    m.obj2 = Objective(expr=sum(m.x[i,j] for j in m.J))


m.pprint()

Yields:产量:

3 Set Declarations
    I : Size=1, Index=None, Ordered=Insertion
        Key  : Dimen : Domain : Size : Members
        None :     1 :    Any :    3 : {1, 2, 3}
    J : Size=1, Index=None, Ordered=Insertion
        Key  : Dimen : Domain : Size : Members
        None :     1 :    Any :    3 : {'A', 'B', 'C'}
    x_index : Size=1, Index=None, Ordered=True
        Key  : Dimen : Domain : Size : Members
        None :     2 :    I*J :    9 : {(1, 'A'), (1, 'B'), (1, 'C'), (2, 'A'), (2, 'B'), (2, 'C'), (3, 'A'), (3, 'B'), (3, 'C')}

1 Var Declarations
    x : Size=9, Index=x_index
        Key      : Lower : Value : Upper : Fixed : Stale : Domain
        (1, 'A') :     0 :  None :     1 : False :  True : Binary
        (1, 'B') :     0 :  None :     1 : False :  True : Binary
        (1, 'C') :     0 :  None :     1 : False :  True : Binary
        (2, 'A') :     0 :  None :     1 : False :  True : Binary
        (2, 'B') :     0 :  None :     1 : False :  True : Binary
        (2, 'C') :     0 :  None :     1 : False :  True : Binary
        (3, 'A') :     0 :  None :     1 : False :  True : Binary
        (3, 'B') :     0 :  None :     1 : False :  True : Binary
        (3, 'C') :     0 :  None :     1 : False :  True : Binary

2 Objective Declarations
    obj1 : Size=1, Index=None, Active=True
        Key  : Active : Sense    : Expression
        None :   True : minimize : x[1,A] + x[1,B] + x[1,C] + x[2,A] + x[2,B] + x[2,C] + x[3,A] + x[3,B] + x[3,C]
    obj2 : Size=1, Index=None, Active=True
        Key  : Active : Sense    : Expression
        None :   True : minimize : x[3,A] + x[3,B] + x[3,C]

6 Declarations: I J x_index x obj1 obj2

Note in the 2nd obj, it is overwriting itself in the loop and only captures the last value of i (3).请注意,在第二个 obj 中,它在循环中覆盖自身,并且仅捕获i (3) 的最后一个值。

If this isn't what you were asking about, edit your question for clarity.如果这不是您要问的问题,请编辑您的问题以清楚起见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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