简体   繁体   English

python 中的 GEKKO 类型错误

[英]GEKKO TypeError in python

suppose:认为:

# ww is a numpy array
ww.shape
>>>(10, 1)

# C is a numpy array
C.shape
>>>(5, 10)

i want to solve a optimization problem in python with specific objective function.我想用特定的目标 function 解决 python 中的优化问题。
Here is the code that i wrote for that purpose:这是我为此目的编写的代码:

from gekko import GEKKO

m = GEKKO()
x1 = m.Var(value=0.2, lb=0, ub=1, integer=False) #float variable. Lower bound = 0, Upper Bound = 1, inirial Value = 0.2
x2 = m.Var(value=0.2, lb=0, ub=1, integer=False) #float variable. Lower bound = 0, Upper Bound = 1, inirial Value = 0.2
x3 = m.Var(value=0.2, lb=0, ub=1, integer=False) #float variable. Lower bound = 0, Upper Bound = 1, inirial Value = 0.2
x4 = m.Var(value=0.2, lb=0, ub=1, integer=False) #float variable. Lower bound = 0, Upper Bound = 1, inirial Value = 0.2
x5 = m.Var(value=0.2, lb=0, ub=1, integer=False) #float variable. Lower bound = 0, Upper Bound = 1, inirial Value = 0.2

x = [x1, x2, x3, x4, x5]

# My subjective function
m.Equation(x1 + x2 + x3 + x4 + x5 == 1)

# My specific Objective Function
## Remember that I specified about ww and C arrays right upside of these Codes
def Objective(x):
    i = 0
    j = 0
    C_b = np.zeros((1,C.shape[1])) # so C_b.shape would be (1, 10)
    
    for i in range(C.shape[1]):
        for j in range(5):
            C_b[0][i] += math.log10(x[j] * C[j,i])
    
    
    return -sum((C_b * ww)[0])


m.Obj(Objective(x))
m.solve(disp=False)

print(x1.value, x2.value, x3.value, x4.value, x5.value)

Output: Output:

TypeError: must be real number, not GK_Operators

Picture of Error:错误图片:

在此处输入图像描述

i guess this error is cause of specific objective function!我猜这个错误是特定目标函数的原因! because with simple objective functions like:因为具有简单的目标函数,例如:

m.Obj(x1 + x2)

I don't get error.我没有收到错误。 so I guess the error comes from specific objective function.所以我猜错误来自特定的目标函数。

How can I fix this error?我该如何解决这个错误? where is the problem?问题出在哪里?

This should work for you.这应该适合你。

from gekko import GEKKO
import numpy as np

nd = 5; md = 10
ww = np.random.rand(md)
C  = np.random.rand(nd,md)

m = GEKKO()
x = m.Array(m.Var,nd,value=1/nd,lb=0,ub=1)
m.Equation(sum(x)==1)
for i in range(C.shape[1]):
    for j in range(C.shape[0]):
        m.Maximize(ww[i]*(m.log10(x[j]*C[j,i])))
m.solve(disp=True)
for i,xi in enumerate(x):
    print(i+1,xi.value)

The solution is always 1/nd that is also the same as the initial guess.解始终是1/nd ,这也与初始猜测相同。 You can check that the solver converges to this optimal solution (not just stops at the initial guess) by setting the initial guess to something like 1 .您可以通过将初始猜测设置为类似1来检查求解器是否收敛到此最优解(不仅仅是在初始猜测处停止)。

The Error Fixed by changing the shape of ww .通过更改ww的形状修复了错误。
before fixing problem:在解决问题之前:

ww.shape
>>>(10, 1)

fixed The problem with:修复了以下问题:

ww.shape
>>>(10, )

Now proposed algorithm worked without any kind of error or problem.现在提出的算法没有任何错误或问题。 That mean it was cause of shape of ww , it fixed after I changed the shape of ww to (10, ) instead (10. 1) .这意味着它是ww形状的原因,在我将 ww 的形状更改为 (10, ) 而不是 (10. 1) 后它已修复。

now Suppose:现在假设:

# ww is a numpy array
ww.shape
>>>(10, )

# C is a numpy array
C.shape
>>>(5, 10)

Corrected & Proposed Algorithm:更正和建议的算法:

from gekko import GEKKO
import numpy as np

nd = 5

m = GEKKO()
x = m.Array(m.Var,nd,value=1/nd,lb=0,ub=1)
m.Equation(sum(x)==1)

i = 0
j = 0
for i in range(C.shape[1]):
    for j in range(C.shape[0]):
        m.Maximize(ww[i]*(m.log10(x[j] *C[j,i])))
        
m.solve(disp=True)
for i,xi in enumerate(x):
    print(i+1,xi.value)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM