如何将 GEKKO 返回的整数转换回 Python 可识别的整数?

问题描述

函数intpolatnGEKKO 最大化目标函数接收整数变量u_hub_nextintpolatn 仅用于 check,如果整数介于 windvel[0] and windvel[1] 和后继 elif statements 之间,然后返回适当的列表,具体取决于 elif condition满足u_hub_next的值。但是,GEKKO 不断抛出错误 TypeError: object of type 'int' has no len()if 条件下,因此代码无法继续执行其余部分蟒蛇代码。下面给出了一段代码,没有成功的 elif 语句,也没有 gekko 部分:希望得到任何指导。

def intpolatn(u_hub_next):
    windvel = np.array([0,3,4,6,7,14])
    thrust_coeff = np.array([0,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]

解决方法

提取 u_hub_next 的值:

uhn = u_hub_next.value[0]

该值是 float 类型,因此使用 int(uhn) 将其转换为整数类型或 Numpy integer types 之一以获得更多控制。