微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用Gekko释放变量

如何解决如何使用Gekko释放变量

我正在尝试使用GEKKO进行简单的代数运算。

给定等式6 = X1 * X2,我首先使用Gekko @ExtendWith({ MockitoExtension.class,InjectExtension.class }) class MyControllerTest { @InjectMocks private MyController myController; @Mock private Logger log; @Mock private Authenticator auther; @InjectionSource private Boolean securityEnabled; @Test void testDoSomething_secEnabled() throws Exception { securityEnabled = Boolean.TRUE; myController.doSomething(); // wahoo no NPE! Test the "if then" half of the branch } @Test void testDoSomething_secdisabled() throws Exception { securityEnabled = Boolean.FALSE; myController.doSomething(); // wahoo no NPE! Test the "if else" half of branch } } 函数将X1设置为2。在求解并打印了该方程之后,我尝试使用Gekko fix()函数释放X1,并再次使用free()函数将X2固定为2。 fix()函数似乎没有正确释放X2变量。

free

解决方法

restart file遵守规范。对于IMODE=3,它是运行目录rto.t0中的m.path。您可以使用以下方法删除该重启文件:

import os
os.remove(m.path+'\\rto.t0')

有一个选项m.options.SPECS=0也应该起作用,以忽略重新启动文件中的固定/空闲规范,但这未与求解引擎进行通信。我创建了new GitHub issue to address this bug

import os
from gekko import GEKKO

m = GEKKO(remote=False)

# Variables
x1 = m.Var()
x2 = m.Var()

# Equation
FindX1 = 6 == x1*x2
m.Equation(FindX1)

# Fix x1 to 2
m.fix(x1,val=2)

# Solve X2
m.solve(disp=False)
print("X1: %s and X2: %s" % (x1.VALUE,x2.VALUE))

# Fix x2 to 2
m.fix(x2,val=2)

# Free x1
m.free(x1)

os.remove(m.path+'\\rto.t0')
#m.options.SPECS = 0

# Solve X1
m.solve(disp=False)
print("X1: %s and X2: %s" % (x1.VALUE,x2.VALUE))

m.open_folder()

这会产生正确的响应:

X1: [2.0] and X2: [3.0]
X1: [3.0] and X2: [2.0]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。