Search code examples
pythonpython-3.xoptimizationgekko

Solve "TypeError: must be real number, not GK_Operators"


I got the error as "TypeError: must be real number, not GK_Operators" at the time of solving a system of nonlinear inequalities. In which place I should edit to get the answer?

from gekko import GEKKO
import math

m = GEKKO(remote=False)
x1,x2,lambda1_L,lambda2_L,lambda1_U,lambda2_U,mu1,mu2,mu3,mu4,mu5,mu6,mu7,mu8,W1=[m.Var(1) for i in range(15)] 
sigma_p = math.sqrt(0.028573*x1*x1+0.03129*x2*x2+0.020231*x1*x2)
A= (0.028573*x1+0.010115*x2)/sigma_p
B=(0.03129*x2+0.010115*x1)/sigma_p
m.Equations([292.7182*lambda1_L+(2.25*A-0.025926)*lambda2_L + 446.444*lambda1_U+(2.25*A-0.040535)*lambda2_U-446.444*mu1-0.405858*mu2+(2*A-0.057146*x1-0.020231*x2)*mu3+mu4-mu5+mu6==0,\
            272.9655*lambda1_L+(2.25*B-0.03633)*lambda2_L+513.4587*lambda1_U+(2.25*B-0.051024)*lambda2_U-513.4587*mu1-0.466781*mu2+(2*B-0.06258*x2-0.020231*x1)*mu3+mu4-mu7+mu8==0,\
            mu1*(W1-446.444*x1-513.4587*x2-33)==0,\
            mu2*(0.13-0.405858*x1-0.466781*x2)<=0,\
            mu3*(x1+x2-1)==0,\
            mu5*(0.03-x1)<=0,\
            mu6*(x1-0.38)<=0,\
            mu7*(0.05-x2)<=0,\
            mu8*(x2-0.42)<=0])
m.solve(disp=False)
print(x1.value,x2.value,lambda1_L.value,lambda2_L.value,lambda1_U.value,lambda2_U.value,mu1.value,mu2.value,mu3.value,mu4.value,mu5.value,mu6.value,mu7.value,mu8.value,W1.value)
TypeError                                 Traceback (most recent call last)
<ipython-input-12-1e6244268d90> in <module>
      1 m = GEKKO(remote=False)
      2 x1,x2,lambda1_L,lambda2_L,lambda1_U,lambda2_U,mu1,mu2,mu3,mu4,mu5,mu6,mu7,mu8,W1=[m.Var(1) for i in range(15)]
----> 3 sigma_p = math.sqrt(0.028573*x1*x1+0.03129*x2*x2+0.020231*x1*x2)
      4 A= (0.028573*x1+0.010115*x2)/sigma_p
      5 B=(0.03129*x2+0.010115*x1)/sigma_p

TypeError                                 Traceback (most recent call last)
<ipython-input-12-1e6244268d90> in <module>
      1 m = GEKKO(remote=False)
      2 x1,x2,lambda1_L,lambda2_L,lambda1_U,lambda2_U,mu1,mu2,mu3,mu4,mu5,mu6,mu7,mu8,W1=[m.Var(1) for i in range(15)]
----> 3 sigma_p = math.sqrt(0.028573*x1*x1+0.03129*x2*x2+0.020231*x1*x2)
      4 A= (0.028573*x1+0.010115*x2)/sigma_p
      5 B=(0.03129*x2+0.010115*x1)/sigma_p

TypeError: must be real number, not GK_Operators

Solution

  • The problem is fixed by using the gekko version of square root instead of the math version of square root.

    sigma_p = m.sqrt(0.028573*x1*x1+0.03129*x2*x2+0.020231*x1*x2)
    

    Gekko performs automatic differentiation of the equations for the gradient based optimizers while the math package only returns a number. The error is from the math package that doesn't know how to process a gekko variable.

    from gekko import GEKKO
    import math
    
    m = GEKKO(remote=False)
    x = [m.Var(1) for i in range(15)]
    x1,x2,lambda1_L,lambda2_L,lambda1_U,lambda2_U, \
      mu1,mu2,mu3,mu4,mu5,mu6,mu7,mu8,W1 = x
    sigma_p = m.sqrt(0.028573*x1*x1+0.03129*x2*x2+0.020231*x1*x2)
    A= (0.028573*x1+0.010115*x2)/sigma_p
    B=(0.03129*x2+0.010115*x1)/sigma_p
    m.Equations([292.7182*lambda1_L+(2.25*A-0.025926)*lambda2_L \
                   +446.444*lambda1_U+(2.25*A-0.040535)*lambda2_U-446.444*mu1\
                   -0.405858*mu2+(2*A-0.057146*x1-0.020231*x2)*mu3+mu4-mu5+mu6==0,\
                 272.9655*lambda1_L+(2.25*B-0.03633)*lambda2_L+513.4587*lambda1_U \
                   +(2.25*B-0.051024)*lambda2_U-513.4587*mu1-0.466781*mu2 \
                   +(2*B-0.06258*x2-0.020231*x1)*mu3+mu4-mu7+mu8==0,\
                 mu1*(W1-446.444*x1-513.4587*x2-33)==0,\
                 mu2*(0.13-0.405858*x1-0.466781*x2)<=0,\
                 mu3*(x1+x2-1)==0,\
                 mu5*(0.03-x1)<=0,\
                 mu6*(x1-0.38)<=0,\
                 mu7*(0.05-x2)<=0,\
                 mu8*(x2-0.42)<=0])
    m.solve(disp=True)
    print(x)
    

    Even though this produces a solution, there may be a problem with the degrees of freedom. There are 6 degrees of freedom but no objective function to guide the selection of these values. I recommend including some type of m.Minimize() or m.Maximize() expression to guide the selection of the degrees of freedom.

     Number of state variables:             20
     Number of total equations: -            9
     Number of slack variables: -            5
     ---------------------------------------
     Degrees of freedom       :              6
    

    Because there is no objective function, the solver returns one feasible solution (not necessarily the optimal). There are likely an infinite number of potential solutions because of the degrees of freedom.

     ----------------------------------------------
     Steady State Optimization with APOPT Solver
     ----------------------------------------------
     
     Iter    Objective  Convergence
        0  1.78380E+00  1.00000E+00
        1  2.20431E-01  4.77059E-01
        2  3.35310E-03  2.18830E-02
        3  4.73856E-04  2.05371E-03
        4  1.03342E-02  3.99540E-04
        5  2.02200E-03  4.93012E-03
        6  3.78386E-04  9.64651E-04
        7  7.16183E-22  1.67425E-04
        8  2.37863E-25  2.59737E-12
        9  2.37863E-25  2.59737E-12
     Successful solution
     
     ---------------------------------------------------
     Solver         :  APOPT
     Solution time  :   7.500000006984919E-003 sec
     Objective      :   0.000000000000000E+000
     Successful solution
     ---------------------------------------------------
     
    
    [[0.39019890594], [0.41982360358], [0.059780756266], [0.99937364895], 
     [-0.03984160433], [0.99941169604], [-1.8636381477e-32], [0.88946533859], 
     [2.0930945006e-27], [0.99733039744], [0.81887700372], [-0.066019044556], 
     [0.81623113522], [3.7911033817], [1.0320500392]]