问题描述
我正在尝试使用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]