- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我喜欢在 y 模型中约束变量值 u < 1。将 ub=1 添加到变量定义 u = m.Var(name='u', value=0, lb=-2, ub=1) 但它导致“找不到解决方案”(退出:收敛到一个点local infeasibility. 问题可能是不可行的。)我想我必须重新制定问题以避免这种情况,但我无法找到应该如何完成的示例。在约束变量值时,如何编写合适的模型来避免不可行的解决方案?
我已经通过添加像 m.Equation(u < 1) 这样的方程来重新表述问题,但没有成功。
import numpy as np
from gekko import GEKKO
import matplotlib.pyplot as pyplt
m = GEKKO(remote=False)
t = np.linspace(0, 1000, 101) # time
d = np.ones(t.shape)
d[0:10] = 0
y_delay=0
# Add data to model
m.time = t
K = m.Const(0.01, name='K')
r = m.Const(name='r', value=0) # Reference
d = m.Param(name='d', value=d) # Disturbance
y = m.Var(name='y', value=0, lb=-2, ub=2) # State variable
u = m.Var(name='u', value=0, lb=-2, ub=1) # Output
e = m.Var(name='e', value=0)
Tc = m.FV(name='Tc', value=1200, lb=60, ub=1200) # time constant
# Update variable status
Tc.STATUS = 1 # Optimizer can adjust value
Kp = m.Intermediate(1 / K * 1 / Tc, name='Kp')
Ti = m.Intermediate(4 * Tc, name='Ti')
# Model equations
m.Equations([y.dt() == K * (u-d),
e == r-y,
u.dt() == Kp*e.dt()+Kp/Ti*e])
# Model constraints
m.Equation(y < 0.5)
m.Equation(y > -0.5)
# Model objective
m.Obj(-Tc)
# options
m.options.IMODE = 6 # Problem type: 6 = Dynamic optimization
# solve
m.solve(disp=True, debug=True)
print('Tc: %6.2f [s]' % (Tc.value[-1], ))
fig1, (ax1, ax2, ax3) = pyplt.subplots(3, sharex='all')
ax1.plot(t, y.value)
ax1.set_ylabel("y", fontsize=8), ax1.grid(True, which='both')
ax2.plot(t, e.value)
ax2.set_ylabel("e", fontsize=8), ax2.grid(True, which='both')
ax3.plot(t, u.value)
ax3.plot(t, d.value)
ax3.set_ylabel("u and d", fontsize=8), ax3.grid(True, which='both')
pyplt.show()
退出:收敛到局部不可行点。问题可能是不可行的。
发生错误。 错误码为2
如果我将 u 的上限更改为 2,优化问题将按预期解决。
最佳答案
如您所见,对变量的严格约束可能导致解决方案不可行。我建议您通过将变量 y
指定为受控变量并使用 SPHI
和 SPLO
设置上限和下限设置点范围来使用软约束.
y = m.CV(name='y', value=0) # Controlled variable
y.STATUS = 1
y.TR_INIT = 0
y.SPHI = 0.5
y.SPLO = -0.5
我还从 y
和 u
中删除了 lb
和 ub
导致不可行。您还有一个目标是使用 m.Obj(-Tc)
最大化 Tc
的值。它达到最大限制:1200
当求解器能够调整该值时。从图中可以看出,y
的值超出了设定点范围。 Controller 可能无法将其保持在该范围内。软约束(基于目标)的约束方法会惩罚偏差,但不会导致不可行的解决方案。如果您需要增加对违反SPHI
或SPLO
的处罚,参数WSPHI
和WSPLO
可以是调整。
看来您有一个一阶动态模型,并且您正在尝试优化 PID 参数。如果您需要模拟 Controller 输出(执行器)的饱和度,则 if3
、max3
、min3
或相应的 if2
、max2
、min2
函数可能会有用。 CV
objectives 有更多信息和 tuning在动态优化类(class)中。
以下是解决您的问题的可行方案:
import numpy as np
from gekko import GEKKO
import matplotlib.pyplot as pyplt
m = GEKKO() # remote=False
t = np.linspace(0, 1000, 101) # time
d = np.ones(t.shape)
d[0:10] = 0
y_delay=0
# Add data to model
m.time = t
K = m.Const(0.01, name='K')
r = m.Const(name='r', value=0) # Reference
d = m.Param(name='d', value=d) # Disturbance
e = m.Var(name='e', value=0)
u = m.Var(name='u', value=0) # Output
Tc = m.FV(name='Tc', value=1200, lb=60, ub=1200) # time constant
y = m.CV(name='y', value=0) # Controlled variable
y.STATUS = 1
y.TR_INIT = 0
y.SPHI = 0.5
y.SPLO = -0.5
# Update variable status
Tc.STATUS = 1 # Optimizer can adjust value
Kp = m.Intermediate((1 / K) * (1 / Tc), name='Kp')
Ti = m.Intermediate(4 * Tc, name='Ti')
# Model equations
m.Equations([y.dt() == K * (u-d),
e == r-y,
u.dt() == Kp*e.dt()+(Kp/Ti)*e])
# Model constraints
#m.Equation(y < 0.5)
#m.Equation(y > -0.5)
# Model objective
m.Obj(-Tc)
# options
m.options.IMODE = 6 # Problem type: 6 = Dynamic optimization
m.options.SOLVER = 3
m.options.MAX_ITER = 1000
# solve
m.solve(disp=True, debug=True)
print('Tc: %6.2f [s]' % (Tc.value[-1], ))
fig1, (ax1, ax2, ax3) = pyplt.subplots(3, sharex='all')
ax1.plot(t, y.value)
ax1.plot([min(t),max(t)],[0.5,0.5],'k--')
ax1.plot([min(t),max(t)],[-0.5,-0.5],'k--')
ax1.set_ylabel("y", fontsize=8), ax1.grid(True, which='both')
ax2.plot(t, e.value)
ax2.set_ylabel("e", fontsize=8), ax2.grid(True, which='both')
ax3.plot(t, u.value)
ax3.plot(t, d.value)
ax3.set_ylabel("u and d", fontsize=8), ax3.grid(True, which='both')
pyplt.show()
关于gekko - 如何在 GEKKO 中约束模型变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57924230/
我很好奇 IPOPT 求解器的每个求解器输出列的建议。有什么 Material 可以解释这个吗? 以下是IPOPT求解器的求解器输出。我想知道 inf_pr , inf_du , lg(mu) , |
我是 Python 中的 Gekko 库的新手,想知道我是否可以在 Gekko 中按照 LP 公式建模。 LP 公式意味着我想找到最佳的电器调度类次,以使总电力成本最小化。但是,如约束所示,每天的总耗
当使用 max2 在 Python GEKKO 中按顺序求解模型(即 IMODE >= 4)失败时和 max3 GEKKO 附带的功能。 这是针对用例,其中 np.maximum或标准max函数将 G
我正在学习如何根据实验室批量 react 器数据使用 GEKKO 进行动力学参数估计,该数据主要由 A、C 和 P 三种物质的浓度分布组成。为了我的问题,我使用的是我以前的模型精选于 question
我正在尝试使用 GEKKO 优化电力系统.具体使用 MPC 到 IEEE 14 bus测试用例。 该系统包含 14 条总线,模型由状态变量 theta 和 omega(分别为发电机的功率角和旋转频率)
EDIT2:好吧,我是个白痴。我关于横向误差的数学有问题。我已经将该等式重写为更简单的东西并且它现在可以工作了。我会离开这里以防它对任何人有用。但如果可能的话,我仍然希望有人能确保我没有做任何过分的事
我终于回到了我的项目上并找到了我的下一个障碍。 我有一个封闭的歧管: 这也是游戏即时建模中的示例轨迹 我可以让我的系统像普通汽车一样运行。我很好奇在 gekko 中合并这种类型的约束的最佳方法是什么。
Gekko 中是否有一个类轮来检索拉格朗日乘数(例如 GAMS 中的边际),或者如果不是其他方式的单行? 谢谢您的帮助。 最佳答案 这是检索拉格朗日乘数的一行。 lam = np.loadtxt(m.
我喜欢在 y 模型中约束变量值 u -0.5) # Model objective m.Obj(-Tc) # options m.options.IMODE = 6 # Problem type:
我正在尝试解决 gekko 中的一个简单混合操作。 blender mx采用两个入口流 Feed1和 Feed2 .预期结果是导出流的质量流量mx.outlet应该是入口流的质量流量的总和。 这是我尝
我想将 LINGO 代码转换为 Python GEKKO 代码。这是 Lingo 代码、lingo 结果和 gekko 代码。我不能写第二个和第三个约束。它返回索引错误,但我不明白为什么?有人可以帮忙
我正在研究一个相当大的 MINLP,模型大小约为 270,000 个变量和方程 - 5,000 个二进制文件。将 Gekko 与 APOT 求解器结合使用,我可以在大约 868 秒(不到 15 分钟)
我有一个 RTO 问题,我想用一些时间相关的参数解决多个模拟时间步长。但是,我在运行时遇到了困难,并注意到总系统时间与实际求解时间相比相对较大。因此,我试图减少总的解析时间,因为所有方程式都保持不变—
intpolatn 函数从 GEKKO 最大化目标函数接收整数变量 u_hub_next。 intpolatn 只是为了检查整数是否落在 windvel[0] 和 windvel[1] 之间以及后续的
我正在优化的问题是在传输网络中 build 发电厂。为此,我在每辆公交车上放置了发电厂,并让优化告诉我应该 build 哪些发电厂以最大限度地降低运行成本。 为了模拟植物的放置,我尝试使用一组二进制变
我在 super 计算中心本地使用 Gekko。我有一个解决 MINLP 的脚本,它可以轻松扩展以添加更多混合整数变量。我想了解随着变量数量的增加以及我需要请求的内存量,计算的成本会有多高。 当我只请
我目前正在使用 MPC 让 TCLab 加热器达到某个设定点温度。我试图让 MHE 每 50 秒更新一次某些参数值。我有一个以前的 MPC 模型,效果很好,我尝试在我的主循环中添加一个部分,让它切换以
使用 Gekko 拟合数据的数值 ODE 解。 嗨,大家好! 我想知道是否可以使用 GEKKO 拟合 ODE 的系数。 我尝试复制 example given here 失败. 这是我想出的(但有缺陷
祝大家节日快乐!我终于有一些时间来处理我的项目了,当然我和往常一样被困住了,哈哈。 我正在寻找可以让我能够模拟以下内容的指导/示例: 我有一个二进制(0 或 1)输入(我们称之为“跳跃”),我希望它只
我无法理解从 GEKKO 模型收到的错误消息。 就上下文而言,该模型应该优化气 Spring 辅助门的气 Spring 力和尺寸参数,以最大限度地减少运算符(operator)关闭门所需的力。我的目的
我是一名优秀的程序员,十分优秀!