2

我正在使用 GEKKO 解决 Python 中的 MINLP 问题,该问题涉及与电力系统仿真软件 PowerFactory 的联合仿真。MINLP 问题目标函数需要从内部调用 PowerFactory 并在将值返回给 MILP 问题定义之前执行特定任务。在等式约束定义中,我还需要使用两个 Pandas 数据框(具有 10000x2 单元格的表)来获取与约束表达式中决策变量的值相对应的特定值。但是,由于这些函数调用中 GEKKO 变量的性质存在一些问题,我在目标/约束函数中编写任何涉及循环或 Pandas 'loc' 或 'iloc' 函数的代码时遇到错误。在这方面的任何帮助将不胜感激。代码结构如下:

    import powerfactory as pf
    from gekko import GEKKO
    import pandas as pd
    
    # Execute the PF setup commands for Python
    
    # Pandas dataframe in question
    df1 = pd.read_csv("table1.csv")
    df2 = pd.read_csv("table2.csv")
    
    def constraint1(X):
          P = 0
          for i in range(length(X)):
            V = X[i] * 1000
            D = round(V, 1)
            P += df1.loc[D, "ColumnName"]
          return P
    def objective(X):
            for i in range(length(X)):
                 V = X[i] * 1000
                 D = round(V, 1)
                 I = df2.loc[D, "ColumnName"]
        # A list with the different values of 'I' is generated for passing to PF    
        # Some PowerFactory based commands below, involving inputs, and extracting results from PF, using a list generated from the values of 'I'. PF returns some result values to the code
         return results
    # MINLP problem definition:
    m = GEKKO(remote=False)
    x = m.Array(m.Var, nInv, value=1.0, lb=0.533, ub=1.0)
    m.Equation(constraint1(x) == 30)
    m.Minimize(objective(x))
    m.options.SOLVER = 3
    m.solve(disp=False)
    # Command for exporting the results to a .txt file        
           

在另一个问题公式中,我试图在目标和约束函数中运行 MINLP 优化问题作为嵌套优化问题。但是,我也遇到了错误。代码结构如下:

    import powerfactory as pf
    from gekko import GEKKO
    
    # Execute the PF setup commands for Python
    
    def constraint1(X):
          P = 0
          for i in range(length(X)):
            V = X[i] * 1000
            # 2nd MINLP problem: Finds 'I' from value of 'V', a single element
            # Calculate 'Pcal' from 'I'
            P += Pcal
          return P
    def objective(X):
            Iset = []
            for i in range(length(X)):
                 V = X[i] * 1000
                 # 3rd MINLP problem: Finds 'I' from value of 'V'
                 # Appends 'I' to a 'Iset'
            # 'Iset' list passed on to PF    
        # Some PowerFactory based commands below, involving inputs, and extracting results from PF, using the 'Iset' list. PF returns some result values to the code
         return results
    # Main MINLP problem definition:
    m = GEKKO(remote=False)
    x = m.Array(m.Var, nInv, value=1.0, lb=0.533, ub=1.0)
    m.Equation(constraint1(x) == 30)
    m.Minimize(objective(x))
    m.options.SOLVER = 3
    m.solve(disp=False)
    # Command for exporting the results to a .txt file  
4

1 回答 1

0

Gekko 没有对外部函数的回调。这是因为求解器是基于梯度的,并且前导步骤是自动微分以获得稀疏形式的精确一阶和二阶导数。与Gekko 和 CoolProp类似,一种选择是构建优化器可以使用的外部(在本例中为 PowerFactory)模型的近似值。两个选项是三次样条( cspline) 或基本样条( bspline)。

基础样条

如果您不能使用这些近似值,那么我建议您切换到不同的平台来解决优化问题。也许您可以尝试scipy.optimize.minimize通过有限差分获得梯度并添加分支和绑定来解决混合整数部分。

于 2020-08-01T11:51:01.140 回答