Search code examples
pythongekko

How to solve @error: Equation Definition in GEKKO


I want to solve a optimization problem in python.
here is the codes. Suppose :

W = [0.010858969983152403,0.15750120163876366,0.14594534721059332,0.08588827233293823,0.14391967610026943,0.17068447485608854,0.17510026127394213,0.11010179660425223]

C = np.array([[0.99365367, 0.97892888, 1.01870907, 1.00434405, 0.99742434,
        0.98994678, 1.00610998, 1.0014477 ],
       [0.99065144, 1.00254236, 0.97842508, 0.93742212, 0.99908661,
        0.99232329, 0.99406251, 0.99902616],
       [0.99355243, 0.9896095 , 1.00603939, 1.01114646, 1.00859356,
        1.00421901, 0.9994433 , 0.96580307],
       [0.99310202, 1.00188421, 1.01455517, 0.99027971, 0.99445973,
        0.99638549, 0.98567891, 1.00278336],
       [0.98696926, 0.99425696, 1.01039431, 1.0066784 , 0.99775556,
        0.99873331, 0.99854812, 1.00948166]])

Now Optimization by GEKKO:

import numpy as np
from gekko import GEKKO

nd = 5
qw = GEKKO()
x = qw.Array(qw.Var,nd,value=1/nd,lb=0,ub=1) # x.shape --> (5, )

qw.Equation(sum(x) == 1)

ww = np.array(W) # ww.shape --> (8, )

def Log_Caculator(Array):
    '''
    final goal of This function is to Calculate Logarithm of every element of the 'Array'
    and return the new Array
    '''
    for j in range(len(Array)):
        Array[j] = qw.log10(Array[j])
    
    return Array

qw.Maximize(ww * Log_Caculator(np.dot(x.T ,  C)))
qw.solve(disp=True)
for i,xi in enumerate(x):
    print(i+1,xi.value)

Output:

Exception:  @error: Equation Definition
 Equation without an equality (=) or inequality (>,<)
 ((0.15750120163876366)*(log10(((((((v1)*(0.97892888))+((v2)*(1.00254236)))+((v3
 )*(0.9896095)))+((v4)*(1.00188421)))+((v5)*(0.99425696))))))
 STOPPING...

by debugging feature of Visual Studio Code I got these:
before executing qw.solve(disp=True) :

enter image description here

after executing qw.solve(disp=True) :

enter image description here

if you try to compare them, you figure out that x is changed! it means the optimal solution has been find! I think optimization as been done by algorithm.
But still it shows me The bug that I mentioned in Output section.
How should I fix that problem?


Solution

  • The problem can be fixed with a change to the objective function:

    qw.Maximize(np.dot(ww,Log_Caculator(np.dot(x.T, C))))
    

    The objective function defined with m.Maximize() or m.Minimize() must be a scalar (single) value. The additional np.dot() function is one way to make ww * Log_Caculator(np.dot(x.T , C)) a scalar.

    Here is the complete script:

    import numpy as np
    from gekko import GEKKO
    
    W = [0.010858969983152403,0.15750120163876366,0.14594534721059332,\
         0.08588827233293823,0.14391967610026943,0.17068447485608854,\
         0.17510026127394213,0.11010179660425223]
    ww = np.array(W)
    
    C = np.array([[0.99365367, 0.97892888, 1.01870907, 1.00434405, \
                   0.99742434, 0.98994678, 1.00610998, 1.0014477 ],
                  [0.99065144, 1.00254236, 0.97842508, 0.93742212, \
                   0.99908661, 0.99232329, 0.99406251, 0.99902616],
                  [0.99355243, 0.9896095 , 1.00603939, 1.01114646, \
                   1.00859356, 1.00421901, 0.9994433 , 0.96580307],
                  [0.99310202, 1.00188421, 1.01455517, 0.99027971, \
                   0.99445973, 0.99638549, 0.98567891, 1.00278336],
                  [0.98696926, 0.99425696, 1.01039431, 1.0066784 , \
                   0.99775556, 0.99873331, 0.99854812, 1.00948166]])
    
    nd = 5
    qw = GEKKO()
    x = qw.Array(qw.Var,nd,value=1/nd,lb=0,ub=1)
    qw.Equation(sum(x) == 1)
    
    def Log_Caculator(Array):
        for j in range(len(Array)):
            Array[j] = qw.log10(Array[j])    
        return Array
    
    qw.Maximize(np.dot(ww,Log_Caculator(np.dot(x.T, C))))
    qw.solve(disp=True)
    for i,xi in enumerate(x):
        print(i+1,xi.value)
    

    Here is the solution:

    EXIT: Optimal Solution Found.
     
     The solution was found.
     
     The final value of the objective function is  -5.540853005129416E-004
     
     ---------------------------------------------------
     Solver         :  IPOPT (v3.12)
     Solution time  :   9.600000004866160E-003 sec
     Objective      :  -5.540853005129416E-004
     Successful solution
     ---------------------------------------------------
     
    1 [3.7304341888e-05]
    2 [4.8543858174e-06]
    3 [3.9756289011e-05]
    4 [2.5331754827e-05]
    5 [0.99989275323]