Gekko 长期性能

2023-12-28

在下面的代码中(全年 PV 斜率优化 - 每小时时间步长,CSV 数据下载link https://drive.google.com/file/d/172HaD87t9V_PDElMER4MwDaWyuCZ3KA5/view?usp=sharing),有什么办法可以加快优化的性能吗?我建模效率低下吗?如果我在代码中将“days_to_consider”变量设置为较低的天数(例如14天),则优化可以相对较快地完成,但是“days_to_consider”变量增加到180+天,我的计算机没有找不到解决办法。

快速获得解决方案对我来说很重要,因为我最终要做的是模拟微电网(光伏、建筑、电动汽车、电池等)中的最优控制。

我的代码如下所示。

from gekko import GEKKO
import numpy as np
import matplotlib.pyplot as plt
import csv

m = GEKKO(remote=False)

days_to_consider = 14 # number of days to optimize slope (14 days are ok, 365 days can't be solved)
m.time = np.linspace(0, 24*days_to_consider-1, 24*days_to_consider) # Hourly time step

# Read the weather data from CSV
with open("PV_Input.csv", encoding='utf-8-sig') as csv_file:
    csv_reader  = csv.reader(csv_file, delimiter=',')
    inputs = [row for row in csv.reader(csv_file)]

#initialize variables
Eb_raw = []
beta_raw = [] # unit: radians
phi_raw = []
Ed_raw = []
Et_r_raw = []

for i in range(2,24*days_to_consider+2):
    Eb_raw.append(float(inputs[i][1])) # solar beam radiation 
    beta_raw.append(float(inputs[i][6])) # solar altitude
    phi_raw.append(float(inputs[i][7])) # solar azimuth
    Ed_raw.append(float(inputs[i][4])) # solar diffuse radiation
    Et_r_raw.append(float(inputs[i][5])) # solar ground reflected

# Assign the time-dependent coefficients
Eb = m.Param(value=Eb_raw) 
beta = m.Param(value=beta_raw)
phi = m.Param(value=phi_raw)
Ed = m.Param(value=Ed_raw)
Et_r = m.Param(value=Et_r_raw)

azimuth = 0.0 # assumed azimuth is fixed, unit: rad
rho_g = 0.14 # reflectance 

area = 100 # PV area 
P_pk = 250 # peak power
p_factor = 0.8 # performance factor

misc = m.Param(value=area * P_pk * p_factor/1000) # area * peak power * performance factor / 1000

# Initialize variables
slope = m.MV(value=0.9225608396276507861, lb=0.0, ub=1.5708) # unit: radian
slope.STATUS = 1
slope.DCOST = 1 # penalty for unnecessary changes
slope.DMAX = 5 # for smooth slope changes

PV_elec = m.SV()

# build PV Equation
cos_theta = m.Intermediate(m.cos(beta)*(m.cos(phi)*m.cos(azimuth)+m.sin(phi)*m.sin(azimuth))*m.sin(slope)+m.sin(beta)*m.cos(slope))           
gamma = m.Intermediate(m.max3(0.45, 0.55+0.437*cos_theta+0.313*(cos_theta)**2))

m.Equation(PV_elec == misc*(Eb*(m.cos(beta)*m.cos(phi)*m.cos(azimuth)*m.sin(slope) \
+ m.cos(beta)*m.sin(phi)*m.sin(azimuth)*m.sin(slope)\
+ m.sin(beta)*m.cos(slope))\
+ Ed*(gamma*m.sin(slope) + m.cos(slope))\
+ 0.5*rho_g*(1-m.cos(slope))*(Eb*m.sin(beta)+Ed)))    

m.Maximize(PV_elec)
m.options.IMODE = 6 # Optimal control
m.options.SOLVER = 1
m.solve(disp=True)

# Unit conversion to degree
conversion_rad_to_deg = 180/3.14159265359

slope_in_degree = [i*conversion_rad_to_deg for i in slope]

# Plot the results
plt.subplot(2,1,1)
plt.plot(m.time, PV_elec, 'k')
plt.ylabel('PV Power [kW]')

plt.subplot(2,1,2)
plt.step(m.time, slope_in_degree,'r')
plt.ylabel('slope [deg]')
plt.xlabel('Time [hr]')
plt.show()

该问题具有挑战性的部分原因是它是一个混合整数非线性规划问题,其中max3. The max3函数用于剪辑0.55+0.437*cos_theta+0.313*(cos_theta)**2下限为 0.45。

你的问题的动态是DMAX限制角度变化速度的约束。你的问题中没有微分方程。如果没有DMAX约束,那么你可以分别解决每个时间段。对于 14 天的时间段,您的解决方案是:

它使用 APOPT 求解器在大约 17 秒内求解。

 Number of state variables:    3350
 Number of total equations: -  2680
 Number of slack variables: -  670
 ---------------------------------------
 Degrees of freedom       :    0
 
 ----------------------------------------------
 Dynamic Control with APOPT Solver
 ----------------------------------------------
Iter:     1 I:  0 Tm:     16.77 NLPi:   76 Dpth:    0 Lvs:    0 Obj: -1.24E+06 Gap:  0.00E+00
 Successful solution
 
 ---------------------------------------------------
 Solver         :  APOPT (v1.0)
 Solution time  :  16.814799999999998 sec
 Objective      :  -1237075.78834978
 Successful solution
 ---------------------------------------------------

如果您使用松弛变量和互补约束重新表述问题,那么它的求解速度会快得多(2.9 秒),并且给出几乎相同的解决方案。

gamma = m.Var(0.5,lb=0.45)
slk = m.Var(lb=0); m.Minimize(1e-3*slk)
m.Equation(slk*(gamma-0.45)<1e-3)
m.Equation(gamma==0.55+0.437*cos_theta+0.313*(cos_theta)**2+slk)

现在,它可以解决全年(365 天)的问题,需要 133 秒才能解决包含 78,831 个变量和 61,313 个方程的问题。

 Number of state variables:    78831
 Number of total equations: -  61313
 Number of slack variables: -  8759
 ---------------------------------------
 Degrees of freedom       :    8759

EXIT: Optimal Solution Found.

 The solution was found.

 The final value of the objective function is  -4.464484997593126E+7
 
 ---------------------------------------------------
 Solver         :  IPOPT (v3.12)
 Solution time  :  132.942 sec
 Objective      :  -4.464484990790293E+7
 Successful solution
 ---------------------------------------------------

这是完整的脚本。

from gekko import GEKKO
import numpy as np
import matplotlib.pyplot as plt
import csv

m = GEKKO(remote=False)

#(14 days are ok, 365 days can't be solved)
days_to_consider = 365 # number of days to optimize slope
# Hourly time step
m.time = np.linspace(0, 24*days_to_consider-1, \
                        24*days_to_consider) 

# Read the weather data from CSV
with open("PV_Input.csv", encoding='utf-8-sig') as csv_file:
    csv_reader  = csv.reader(csv_file, delimiter=',')
    inputs = [row for row in csv.reader(csv_file)]

#initialize variables
Eb_raw = []
beta_raw = [] # unit: radians
phi_raw = []
Ed_raw = []
Et_r_raw = []

for i in range(2,24*days_to_consider+2):
    Eb_raw.append(float(inputs[i][1])) # solar beam radiation 
    beta_raw.append(float(inputs[i][6])) # solar altitude
    phi_raw.append(float(inputs[i][7])) # solar azimuth
    Ed_raw.append(float(inputs[i][4])) # solar diffuse radiation
    Et_r_raw.append(float(inputs[i][5])) # solar ground reflected

# Assign the time-dependent coefficients
Eb = m.Param(value=Eb_raw) 
beta = m.Param(value=beta_raw)
phi = m.Param(value=phi_raw)
Ed = m.Param(value=Ed_raw)
Et_r = m.Param(value=Et_r_raw)

azimuth = 0.0 # assumed azimuth is fixed, unit: rad
rho_g = 0.14 # reflectance 

area = 100 # PV area 
P_pk = 250 # peak power
p_factor = 0.8 # performance factor

# area * peak power * performance factor / 1000
misc = m.Param(value=area * P_pk * p_factor/1000)

# Initialize variables
# unit: radian
slope = m.MV(value=0.9225608396276507861, lb=0.0, ub=1.5708)
slope.STATUS = 1
slope.DCOST = 1 # penalty for unnecessary changes
slope.DMAX = 5 # for smooth slope changes

PV_elec = m.SV()

# build PV Equation
cos_theta = m.Intermediate(m.cos(beta)*(m.cos(phi)\
            *m.cos(azimuth)+m.sin(phi)*m.sin(azimuth))\
            *m.sin(slope)+m.sin(beta)*m.cos(slope))           
gamma = m.Var(0.5,lb=0.45)
slk = m.Var(lb=0); m.Minimize(1e-3*slk)
m.Equation(slk*(gamma-0.45)<1e-3)
m.Equation(gamma==0.55+0.437*cos_theta+0.313*(cos_theta)**2+slk)

m.Equation(PV_elec == misc*(Eb*(m.cos(beta)\
            *m.cos(phi)*m.cos(azimuth)*m.sin(slope) \
            + m.cos(beta)*m.sin(phi)*m.sin(azimuth)*m.sin(slope)\
            + m.sin(beta)*m.cos(slope))\
            + Ed*(gamma*m.sin(slope) + m.cos(slope))\
            + 0.5*rho_g*(1-m.cos(slope))*(Eb*m.sin(beta)+Ed)))    

m.Maximize(PV_elec)
m.options.IMODE = 6 # Optimal control
m.options.SOLVER = 3

#m.options.COLDSTART = 1
#m.solve(disp=True)

m.options.COLDSTART  = 0
#m.options.TIME_SHIFT = 0
m.solve(disp=True)

# Unit conversion to degree
conversion_rad_to_deg = 180/3.14159265359

slope_in_degree = [i*conversion_rad_to_deg for i in slope]

# Plot the results
plt.subplot(3,1,1)
plt.plot(m.time, PV_elec, 'k')
plt.ylabel('PV Power [kW]')

plt.subplot(3,1,2)
plt.step(m.time, gamma,'b')
plt.plot([0,max(m.time)],[0.45,0.45],'k:')
plt.ylabel('gamma')

plt.subplot(3,1,3)
plt.step(m.time, slope_in_degree,'r')
plt.ylabel('slope [deg]')
plt.xlabel('Time [hr]')
plt.show()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Gekko 长期性能 的相关文章

  • 能源系统的 Python GEKKO MINLP 优化:如何构建 2D 数组的中间体

    我目前正在 Python GEKKO 中实现 MINLP 优化问题 以确定三联产能源系统的最佳运行策略 当我将不同代表日的所有时段的能源需求作为输入数据时 基本上我所有的决策变量 中间变量等都是二维数组 我怀疑 2D 中间体的声明是我的问题
  • 控制范围和预测范围

    我已经回顾了模型预测控制的参考书目和 Gekko 编程结构 尽管我了解它的编程方式及其目的 例如 我想了解 Gekko 如何根据 Seborg 中的相关内容来管理控制范围和预测范围之间的差异 我看不出代码有什么区别 下面是一个用于说明的 M
  • 如何使用gekko估计FOPDT方程中的theta值?

    我正在尝试使用 GEKKO 来拟合某个数据集 使用 FOPDT 优化方法来估计 k tau 和 theta 我看到了使用 odeint 的示例https apmonitor com pdc index php Main FirstOrder
  • 使用 gekko 进行 Python 优化

    我第一次使用 gekko 来对 python 进行优化 我对 python 没有太多经验 但我知道基础知识 运行优化时出现错误代码 13 import Gekko optimization package from gekko import
  • Gekko 长期性能

    在下面的代码中 全年 PV 斜率优化 每小时时间步长 CSV 数据下载link https drive google com file d 172HaD87t9V PDElMER4MwDaWyuCZ3KA5 view usp sharing
  • SCIPY - 构建约束而不单独列出每个变量

    我正在使用 SCIPY 来优化使用远期价格的存储设施 交易期限为 1 年 根据每月价差 例如 3 月 21 日与 5 月 20 日价差 是否足够高以覆盖可变运营成本 可以从该设施注入和提取天然气 附图代表了问题 这里的值是任意的 与代码中的
  • Gekko 返回错误的成功解决方案

    以下代码返回Successful Solution Objective 0 但这不是最佳解决方案 最优解是 6 通过阅读其他问题 我认为这是在目标函数中使用非 Gekko 函数的问题 但我使用的唯一非 Gekko 函数是np matmul
  • 为什么代码终止时出现“未找到解决方案”错误和“退出:收敛到局部不可行点。问题可能不可行”?

    我似乎不明白为什么IPOPT无法找到解决方案 最初 我认为这个问题完全不可行 但是当我减少总列数至以下任意号码161000 or 注释掉最后一个约束方程其中包含总列数 它解决并退出Optimal Solution Found and a f
  • 访问目标函数 Gekko 外部函数中的决策变量值

    我需要访问目标函数之外的决策变量 我有以下内容 tc var for index in index f a tc var index m Var value 25 name tc var format index lb 15 ub 45 i
  • GEKKO - 使用自定义目标函数进行参数估计 - 错误代码 -13

    我已经使用 Gekko 教程 线性和非线性回归 中介绍的相同技术成功地执行了稳态参数估计 下面是代码 coding utf 8 Spyder Editor This is a temporary script file from io im
  • 优化求和函数 - GEKKO

    我刚刚开始学习优化 在寻找以下问题的最佳值时遇到一些问题 注意 这只是我想到的一个随机问题 没有实际应用 Problem where x可以是列表中的任何值 2 4 6 并且y介于 1 和 3 之间 我的尝试 from gekko impo
  • 使用 Python Gekko 的全局最小值与局部最小值解决方案

    一个简单的优化示例有 2 个局部最小值 0 0 8 有目标的936 0 and 7 0 0 有目标的951 0 在 Python Gekko 中使用本地优化器的技术有哪些 APOPT BPOPT IPOPT 寻找全局解决方案 from ge
  • “int '对象不可下标”

    我开始学习GEKKO 现在 我正在解决一个 knapsak 问题来学习 但是这次我收到错误 int object is not subscriptable 你能看一下这段代码吗 问题的根源是什么 我应该如何定义 1 10 矩阵 from g
  • Python gekko 方程定义中的换行符

    我目前正在手动实现有限元的伽辽金法 并使用 python gekko 来求解所得的非线性代数方程组 这对于小型系统来说不会产生任何问题并且工作正常 一旦系统变得更加复杂 涉及长方程和指数项m exp 对于求解器来说 方程可能不再具有正确的格
  • Python:有限制的非线性优化(Gekko?)

    我希望能够用Python解决以下问题 给定观测数据 x 1 x n 和已知的固定目标 B 和公差 E 求解参数 a0 a1 和 a2 从而最小化 总和 w i 2 其中 w i exp a0 a1x i a2x i 2 具有以下两个限制 s
  • Gekko优化包和numpy反函数

    我使用 Gekko 为一组反应动力学选择 A 最优实验 目标函数是最小化迹 inv Z Z 其中 Z 是通过将其参数周围的 ODE 线性化而计算出的尺度灵敏度矩阵 正如您所看到的 目标函数涉及 Z Z 的倒数 我使用了 numpy 甚至 s
  • 使用 z = f(x, y) 形式的 B 样条方法来拟合 z = f(x)

    作为一个潜在的解决方案这个问题 https stackoverflow com questions 76476327 how to avoid creating many binary switching variables in gekk
  • 整数约束优化,目标函数表示执行另一个程序调用

    我正在研究整数优化问题 想尝试 GEKKO 问题描述 x1 x2 x3 x9 x10 x11 1 16 范围内的整数 是目标函数的 11 个整数参数 我想找到一组 x 值来最小化目标函数的输出 然而 由于问题无法用数学来表达 因此需要通过运
  • 如何解决 Gekko 中由于 m.connection 导致的警告消息?

    我正在使用 m connection 来估计变量初始条件 但收到 12 条警告消息 例如 此外 APM文件显示 我不确定如何解决这些消息 我按照这个解释 如果 pos1 或 pos2 不是 None 则关联的 var 必须是 GEKKO 变
  • 如何使用 Python Gekko 求解绝对值 abs() 目标?

    使用 Python Gekko 中的 IPOPT 成功解决了具有平方目标的优化问题 from gekko import GEKKO import numpy as np m GEKKO x m Var y m Param 3 2 m Obj

随机推荐