使用约束列表创建Pyomo的约束列表,而不是单独添加约束?

2024-05-20 20:59:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在将Pyomo变量存储在Pandas数据帧中,并一直使用它来生成约束数组

是否有一种方法可以将它们添加到我的模型中(例如,通过使用它初始化约束列表),而不必遍历它们并单独添加它们?我认为我不能使用规则来创建约束,因为我是基于我的数据框架进行索引的

这就是我存储变量的方式——我使用Pandas是因为我发现通过数据框中的值进行索引非常容易:

model.duid_bids = pe.Var(bid_df['DUID_BAND_DATETIME'], domain=pe.PositiveReals)

bid_df['PE_BIDS'] = bid_df['DUID_BAND_DATETIME'].apply(lambda x: model.duid_bids[x])

我想做一些这样的事情,但这是行不通的:

model.bid_volume = pe.Constraint(expr=bid_df['PE_BIDS'] <= bid_df['VOLUME'])

如果我这样单独添加它们,它会起作用:

pe.Constraint(expr=bid_df['PE_BIDS'].iloc[0] <= bid_df['VOLUME'].iloc[0])

非常感谢


Tags: 数据pandasdfbanddatetimemodelpebids
1条回答
网友
1楼 · 发布于 2024-05-20 20:59:55

我认为你太努力了,想把pyomo推入一个pandas框中。但这只是一种观点。我不认为这样做有什么好处,因为这里没有“矢量化”任何操作,因为pyomo约束构造没有矢量化

我建议(其他人可能会有所不同)只在pyomo中进行优化,而不将任何组件放入df中,而是根据约束的需要提取常量。关于我是否将您使用的索引集放入pyomoSet中,我认为这使T/S更容易,并且pyomo将在内部生成一个虚拟集,您可以在下面的第一个示例中看到,但这是一个附带的故事

这里有两个在你的结构上的切口,我认为是可行的,并且从pandas中分离出一点。这假设df中的datetime是唯一的,为了简单起见,可以将其制作成索引。(实际上,它必须是唯一的,因为您已经将其用作索引集…回答了我自己的问题)

import pyomo.environ as pe
import pandas as pd

data = {'DATETIME': [1,2,3],
        'VOLUME': [1000, 2000, 3000]}

df = pd.DataFrame(data)
df.set_index('DATETIME', inplace=True)
print(df.head())  # quick check...

model = pe.ConcreteModel()

model.duid_bids = pe.Var(df.index, domain=pe.PositiveReals)

# volume constraint
def vol_constraint(m, date):
    return model.duid_bids[date] <= df['VOLUME'].loc[date]
model.vol_constraint = pe.Constraint(df.index, rule=vol_constraint)

model.pprint()

############ alternate approach:  full pyomo... ?

model2 = pe.ConcreteModel()

# sets
model2.D = pe.Set(initialize=df.index)

# vars
model2.bid = pe.Var(model2.D, domain=pe.PositiveReals)

# constraint
def vol_constraint2(m, date):
    return model2.bid[date] <= df['VOLUME'][date]
model2.vol_constraint = pe.Constraint(model2.D, rule=vol_constraint2)

model2.pprint()

model2.obj = pe.Objective(expr=sum(model2.bid[date] for date in model2.D), sense=pe.maximize)

solver = pe.SolverFactory('glpk')
status = solver.solve(model2)
print(status)

# stuff the result into the dataframe
df['bid'] = pd.Series(model2.bid.get_values())
print(df)

生成:

          VOLUME
DATETIME        
1           1000
2           2000
3           3000
2 Set Declarations
    duid_bids_index : Size=1, Index=None, Ordered=False
        Key  : Dimen : Domain : Size : Members
        None :     1 :    Any :    3 : {1, 2, 3}
    vol_constraint_index : Size=1, Index=None, Ordered=False
        Key  : Dimen : Domain : Size : Members
        None :     1 :    Any :    3 : {1, 2, 3}

1 Var Declarations
    duid_bids : Size=3, Index=duid_bids_index
        Key : Lower : Value : Upper : Fixed : Stale : Domain
          1 :     0 :  None :  None : False :  True : PositiveReals
          2 :     0 :  None :  None : False :  True : PositiveReals
          3 :     0 :  None :  None : False :  True : PositiveReals

1 Constraint Declarations
    vol_constraint : Size=3, Index=vol_constraint_index, Active=True
        Key : Lower : Body         : Upper  : Active
          1 :  -Inf : duid_bids[1] : 1000.0 :   True
          2 :  -Inf : duid_bids[2] : 2000.0 :   True
          3 :  -Inf : duid_bids[3] : 3000.0 :   True

4 Declarations: duid_bids_index duid_bids vol_constraint_index vol_constraint

模型2为清晰起见分开…

1 Set Declarations
    D : Size=1, Index=None, Ordered=Insertion
        Key  : Dimen : Domain : Size : Members
        None :     1 :    Any :    3 : {1, 2, 3}

1 Var Declarations
    bid : Size=3, Index=D
        Key : Lower : Value : Upper : Fixed : Stale : Domain
          1 :     0 :  None :  None : False :  True : PositiveReals
          2 :     0 :  None :  None : False :  True : PositiveReals
          3 :     0 :  None :  None : False :  True : PositiveReals

1 Constraint Declarations
    vol_constraint : Size=3, Index=D, Active=True
        Key : Lower : Body   : Upper  : Active
          1 :  -Inf : bid[1] : 1000.0 :   True
          2 :  -Inf : bid[2] : 2000.0 :   True
          3 :  -Inf : bid[3] : 3000.0 :   True

3 Declarations: D bid vol_constraint

Problem: 
- Name: unknown
  Lower bound: 6000.0
  Upper bound: 6000.0
  Number of objectives: 1
  Number of constraints: 4
  Number of variables: 4
  Number of nonzeros: 4
  Sense: maximize
Solver: 
- Status: ok
  Termination condition: optimal
  Statistics: 
    Branch and bound: 
      Number of bounded subproblems: 0
      Number of created subproblems: 0
  Error rc: 0
  Time: 0.010575056076049805
Solution: 
- number of solutions: 0
  number of solutions displayed: 0

          VOLUME     bid
DATETIME                
1           1000  1000.0
2           2000  2000.0
3           3000  3000.0

相关问题 更多 >