首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TypeError:必须是实数,而不是GK_Operators

TypeError:必须是实数,而不是GK_Operators
EN

Stack Overflow用户
提问于 2021-01-02 02:48:33
回答 1查看 533关注 0票数 3

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

TypeError:必须是实数,而不是GK_Operators

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

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

发布于 2021-01-02 04:05:54

以下是一些小窍门:

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

下面是修改后的脚本版本:

代码语言:javascript
复制
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)

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

代码语言:javascript
复制
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网站上找到的Hock & Schitkowski基准87是:

代码语言:javascript
复制
! 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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65535623

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档