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
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?
