BYU-PRISM / GEKKO

GEKKO Python for Machine Learning and Dynamic Optimization
https://machinelearning.byu.edu
Other
573 stars 102 forks source link

GEKKO TypeError in python #105

Closed shayandavoodii closed 3 years ago

shayandavoodii commented 3 years ago

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. 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:

TypeError: must be real number, not GK_Operators

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?

APMonitor commented 3 years ago

Thanks for posting the question to Stack Overflow. I'll close it here with the link to the thread. https://stackoverflow.com/questions/66154495/gekko-typeerror-in-python

shayandavoodii commented 3 years ago

Thanks for posting the question to Stack Overflow. I'll close it here with the link to the thread. https://stackoverflow.com/questions/66154495/gekko-typeerror-in-python

Thank you! I'll be appreciated for your help. Can you help me for that? Really I'm asking this question every where and no one helped me till now.