3

The function intpolatn receives the integer variable u_hub_next from a GEKKO maximized objective function. intpolatn is just meant to check if the integer falls between windvel[0] and windvel[1] and succeeding elif statements, and then return the appropriate list, depending on which elif condition satisfies the value of u_hub_next. However, GEKKO keeps throwing the error TypeError: object of type 'int' has no len() at the if conditional, and hence the code cannot proceed to the remainder of the python code. A snippet of the code is given below without succeeding elif statements and without the gekko section: I'd appreciate any guidance.

def intpolatn(u_hub_next):
    windvel = np.array([0, 3, 4, 6, 7, 14])
    thrust_coeff = np.array([0, 0.9, 0.9, 0.7, 0.83, 0.39])
    # print(u_hub_next)
    if windvel[0] <= u_hub_next < windvel[1]:
        m = 0
        c_t = 0
        axial = 0
        p_u = check_p_u(u_hub_next) 
        u_hub_next = 0
        return [u_hub_next, c_t, axial, p_u]
Fonnie
  • 133
  • 9

1 Answers1

1

Extract the value of u_hub_next with:

uhn = u_hub_next.value[0]

The value is a float type so use int(uhn) to convert it to an integer type or one of the Numpy integer types for even more control.

John Hedengren
  • 12,068
  • 1
  • 21
  • 25
  • Unfortunately, that still doesn't solve the problem. In fact, if I print u_hub_next immediately before the if statement, where it is being commented out, it outputs an integer value. Too bad I cannot reproduce the error here, since it is a collection of scripts that call each other. It used to run without the error but suddenly started misbehaving after I changed the value of axi_max in this question -> https://stackoverflow.com/questions/67196304/the-code-below-continues-to-give-the-error-error-equation-definition-equatio/67200354#67200354 – Fonnie May 05 '21 at 20:37
  • If that function is part of the optimization problem (not just an output after the solution is finished) then you should use the `m.if3()` or `m.if2()` function. If you just need the output of `u_hub_next` then `int(u_hub_next.value[0])` should work. Maybe try it with a simple problem such as with one of these https://apmonitor.com/wiki/index.php/Main/IntegerBinaryVariables to verify that you can convert a Gekko variable into a Python `int` type. – John Hedengren May 06 '21 at 15:38
  • 1
    The function works with the output variables from GEKKO, and unfortunately, `int(u_hub_next.value[0])` still doesn't work. Now, I have the output variables from my question in -> https://stackoverflow.com/questions/67196304/why-does-the-code-terminate-with-a-solution-not-found-error-and-exit-converg but cannot use the output to proceed with the remainder of the system model. – Fonnie May 07 '21 at 19:10
  • Could you let us know what `int(u_hub_next.value[0])` produces? What isn't working? If the solver did not find a successful solution, it may not have a value. – John Hedengren May 11 '21 at 18:09
  • 1
    The solver found a solution and I needed to use the values of the optimized variables outside the gekko environment to test an if-else condition. Python could not recognize the gekko output as integers and so the if-else test could not go through. It has been resolve though. I had to convert the gekko returned list to an array. I suppose the trick is to do some form of conversion; either to a numpy array, a dictionary, a pandas dataframe or whatever works for the scenario, whenever you want to do such conditional tests outside gekko using gekko generated output. Thank you! @John Hedengren – Fonnie May 12 '21 at 15:28