Search code examples
pythoncontrolsgekkooptimal

How to use Complex conjugate in GEKKO


I have an optimal control problem with GEKKO. And I need take the complex conjugate of a variable. I know that GEKKO have method about .exp .log and so on, but I don't find the method about .conj

So how can I use conj. And in GEKKO, can variable be complex?


Solution

  • Gekko allows complex numbers, but the solvers only work with real numbers so special structure needs to be added to deal with the real and imaginary parts. Here is an example that calculates the square root of a decision variable with an objective to maximize the imaginary part of the number. The complex conjugate is calculated.

    from gekko import GEKKO
    m = GEKKO()
    x = m.Var(2, lb=-9, ub=16)  # -9<=x<=16
    b = m.if3(x, 0, 1)          # binary switch
    s = m.Intermediate(m.sqrt(m.abs3(x)))  # sqrt(x)
    r = m.Intermediate(b*s)     # real
    i = m.Intermediate((1-b)*s) # imaginary
    
    # Complex conjugate
    r_conj = r  # real part is the same
    i_conj = m.Intermediate(-i) # imaginary part
    
    # Maximize imaginary part
    m.Maximize(i_conj**2)
    m.solve(disp=False)
    print("Original:", r.value[0], '+', i.value[0], 'j')
    print("Conjugate:", r_conj.value[0], '+', i_conj.value[0], 'j')
    

    The result is:

    Original: 0.0 + 3.0 j
    Conjugate: 0.0 + -3.0 j