TypeError: must be real number, not GK_Operators

Viewed 494

I don't know much about python, but I need help to understand why my optimization equation pops up

TypeError: must be real number, not GK_Operators

What did I do wrong? This problem could be found on 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)
1 Answers

A few tips:

  • Use the Python power operator ** instead of ^
  • Every equation must have an equality == or inequality >, <, >=, <=.
  • Define multiple variables with one line using m.Array() such as for slk and add that have dimension 3.
  • See the solver result with disp=True, especially if the problem is not solvable or has an error.

Here is a modified version of your script:

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)

The solver correctly produces the result that the problem is infeasible.

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

The Hock & Schitkowski benchmark 87 that you likely found on the APMonitor website is:

! 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

You can solve this through the online interface. It looks like your equations aren't quite correct, but they are close. You can use the online interface to compare answers.

Related