python - TypeError : must be real number, 不是 GK_Operators

标签 python optimization gekko

我不太了解python,但我需要帮助来理解为什么我的优化方程会弹出

TypeError: must be real number, not GK_Operators

我做错了什么?这个问题可以在 apmonitor.com, hs87 上找到。

from gekko import GEKKO

import math
m = GEKKO ()
#Parameters
a = 131.078
b = 1.48477  #Hock & Schittkowski say 1.48577
c = 0.90798
d0 = 1.47588
e0 = 1.47588
d = math.cos(d0)
e = math.sin(e0)
lim1 = 300
lim2 = 100
lim3 = 200     
rate1 = 30
rate2 = 31
rate3 = 28
rate4 = 29
rate5 = 30
  
#var x1 >= 0, <= 400;
#var x2 >= 0, <= 1000;
#var x3 >= 340, <= 420;
#var x4 >= 340, <= 420;
#var x5 >= -1000, <= 1000;  # Hock & Schittkowski say <= 10000
#var x6 >= 0, <= 0.5236;

add[1:3] > 0
slk[1:3]
x1 = m.Var(value = 390)
x2 = m.Var(value = 1000)
x3 = m.Var(value = 419.5)
x4 = m.Var(value = 340.5)
x5 = m.Var(value = 198.175)
x6 = m.Var(value = 0.5)

add1 = x1 - lim1 + slk1
add2 = x2 - lim2 + slk2
add3 = x2 - lim3 + slk3
m.Equations(300 - x3*x4*math.cos(b - x6)/a + c*x3^2*d/a)
m.Equations(-x3*x4*math.cos(b + x6)/a + c*x4^2*d/a)
m.Equations(-x3*x4*math.sin(b + x6)/a + c*x4^2*e/a)
m.Equations(200 - x3*x4*math.sin(b - x6)/a + c*x3^2*e/a == 0)
m.obj(rate1*x1 + (rate2-rate1)*add1)
m.obj(rate3*x2 + (rate4-rate3)*add2 + (rate5-rate4)*add3)
m.solve(disp=False)

print(x1.value)
print(x2.value)
print(x3.value)
print(x4.value)
print(x5.value)
print(x6.value)

最佳答案

一些提示:

  • 使用 Python 幂运算符 **而不是 ^
  • 每个等式必须有一个等式 ==或不平等> , < , >= , <= .
  • 使用m.Array() 在一行中定义多个变量例如 slkadd具有维度 3。
  • 使用 disp=True 查看求解器结果,尤其是当问题无法解决或有错误时。

这是您的脚本的修改版本:

from gekko import GEKKO

import math
m = GEKKO ()
#Parameters
a = 131.078
b = 1.48477 
c = 0.90798
d0 = 1.47588
e0 = 1.47588
d = math.cos(d0)
e = math.sin(e0)
lim1 = 300
lim2 = 100
lim3 = 200     
rate1 = 30
rate2 = 31
rate3 = 28
rate4 = 29
rate5 = 30

add = m.Array(m.Var,3,lb=0)
slk = m.Array(m.Var,3,lb=0)
x1 = m.Var(value = 390,lb=0,ub=400)
x2 = m.Var(value = 1000,lb=0,ub=1000)
x3 = m.Var(value = 419.5,lb=340,ub=420)
x4 = m.Var(value = 340.5,lb=340,ub=420)
x5 = m.Var(value = 198.175,lb=-1000,ub=1000)
x6 = m.Var(value = 0.5,lb=0,ub=0.5236)

m.Equation(add[0] == x1 - lim1 + slk[0])
m.Equation(add[1] == x2 - lim2 + slk[1])
m.Equation(add[2] == x2 - lim3 + slk[2])
m.Equation(300 - x3*x4*m.cos(b - x6)/a + c*x3**2*d/a == 0)
m.Equation(-x3*x4*m.cos(b + x6)/a + c*x4**2*d/a == 0)
m.Equation(-x3*x4*m.sin(b + x6)/a + c*x4**2*e/a == 0)
m.Equation(200 - x3*x4*m.sin(b - x6)/a + c*x3**2*e/a == 0)
m.Minimize(rate1*x1 + (rate2-rate1)*add[0])
m.Minimize(rate3*x2 + (rate4-rate3)*add[1] + (rate5-rate4)*add[2])
m.solve(disp=True)

print(x1.value)
print(x2.value)
print(x3.value)
print(x4.value)
print(x5.value)
print(x6.value)

求解器正确地给出了问题不可行的结果。

EXIT: Converged to a point of local infeasibility. Problem may be infeasible.
 
 An error occured.
 The error code is            2
 
 
 ---------------------------------------------------
 Solver         :  IPOPT (v3.12)
 Solution time  :   4.060000000026776E-002 sec
 Objective      :    5159.30673181783     
 Unsuccessful with error code            0
 ---------------------------------------------------
 
 Creating file: infeasibilities.txt
 Use command apm_get(server,app,'infeasibilities.txt') to retrieve file
 @error: Solution Not Found

您可能在 APMonitor website 上找到的 Hock & Schitkowski 基准测试 87是:

! Nonlinear electrical network
! The problem is corrected to conform to the problem as stated in
!   D. M. Himmelblau, Applied Nonlinear Programming,
!   McGraw-Hill, 1972, pp. 413-414
! except that jump-discontinuities in the objective function are
! omitted by stating it as the sum of two piecewise-linear terms
Model hs87 
  Parameters
    a = 131.078
    b = 1.48477  ! Hock & Schittkowski say 1.48577
    c = 0.90798
    d0 = 1.47588
    e0 = 1.47588
    d = cos(d0)
    e = sin(e0)

    lim[1] = 300
    lim[2] = 100
    lim[3] = 200

    rate[1] = 30
    rate[2] = 31
    rate[3] = 28
    rate[4] = 29
    rate[5] = 30
  End Parameters

  Variables
    add[1:3] > 0
    slk[1:3]

    x[1] = 390,     >= 0,     <= 400
    x[2] = 1000,    >= 0,     <= 1000
    x[3] = 419.5,   >= 340,   <= 420
    x[4] = 340.5,   >= 340,   <= 420
    x[5] = 198.175, >= -1000, <= 1000  ! Hock & Schittkowski say <= 10000
    x[6] = 0.5,     >= 0,     <= 0.5236  
    obj[1:2]
  End Variables

  Equations
    ! piecewise linear
    add[1] = x[1] - lim[1] + slk[1]
    add[2] = x[2] - lim[2] + slk[2]
    add[3] = x[2] - lim[3] + slk[3]

    x[1] = 300 - x[3]*x[4]*cos(b - x[6])/a + c*x[3]^2*d/a
    x[2] = -x[3]*x[4]*cos(b + x[6])/a + c*x[4]^2*d/a
    x[5] = -x[3]*x[4]*sin(b + x[6])/a + c*x[4]^2*e/a
    200 - x[3]*x[4]*sin(b - x[6])/a + c*x[3]^2*e/a = 0  

    ! best known objective = 8827.5977
    obj[1] = rate[1]*x[1] + (rate[2]-rate[1])*add[1]
    obj[2] = rate[3]*x[2] + (rate[4]-rate[3])*add[2] + (rate[5]-rate[4])*add[3]
  End Equations
End Model

您可以通过 online interface 解决这个问题.看起来你的方程式不太正确,但它们很接近。您可以使用在线界面比较答案。

关于python - TypeError : must be real number, 不是 GK_Operators,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65535623/

相关文章:

Python Nosetest 多处理在类/包级别启用和禁用

python - HTTP403 错误 urllib2.urlopen(URL)

php - 将 5 个 mysql 查询变成 1 个

python-3.x - 尝试使用 GEKKO OPTIMIZER 时出现 "No loop matching the specified signature and casting was found for ufunc solve"

python - 多处理 |捕获一个退出的口译员

python - 递归列表函数

c++ - 为什么优化器会删除我的代码?

algorithm - 启发式地解决有额外约束的旅行推销员的想法

python - 两相区域 coolprop 数据的 GEKKO bspline 问题

python - 壁虎。 X值不超过一定点