I'm trying to make GEKKO maximize the function "Profit" but I can't make it work. I always end up with the error below.
I need GEKKO solver to choose the best values for (Weight1, Weight2, Weight3... Weight8) so the "Result" returned by the function "Profit" is the highest possible.
Each Weight can be any value between 0 (0%) and 15 (15%) and the sum of all Weights must be 100 (100%).
All DataFrames are required to calculate the final profit of eack Pick (Pick1, Pick2, Pick3... Pick8) and the Benchmark.
In the end the "Result" is the sum of each Pick's profit minus the Benchmark's profit.
So: Weights are the Variables; Their bounds are from 0 to 15; The constraint is their sum must be 100.
So far I could only find examples of GEKKO Maximizing defined equations. Can it do something like this?
Here is my code:
from gekko import GEKKO
import pandas as pd
m = GEKKO()
Weight1 = m.Var(value=15,lb=0, ub=15)
Weight2 = m.Var(value=15,lb=0, ub=15)
Weight3 = m.Var(value=15,lb=0, ub=15)
Weight4 = m.Var(value=15,lb=0, ub=15)
Weight5 = m.Var(value=15,lb=0, ub=15)
Weight6 = m.Var(value=15,lb=0, ub=15)
Weight7 = m.Var(value=15,lb=0, ub=15)
Weight8 = m.Var(value=15,lb=0, ub=15)
m.Param(Weight1 + Weight2 + Weight3 + Weight4 + Weight5 + Weight6 + Weight7 + Weight8 == 100) #Sum of Weights must be 100%
def Profit(Weight1, Weight2, Weight3, Weight4, Weight5, Weight6, Weight7, Weight8):
RETURNS = OPTIMIZATION_TABLE
PICKS = RETURNS[["picks"]].drop_duplicates()
PICKS["Weights"] = ""
PICKS.loc[(PICKS['picks']== "pick2") ,'Weights'] = Weight1/100
PICKS.loc[(PICKS['picks']== "pick1") ,'Weights'] = Weight2/100
PICKS.loc[(PICKS['picks']== "pick3") ,'Weights'] = Weight3/100
PICKS.loc[(PICKS['picks']== "pick4") ,'Weights'] = Weight4/100
PICKS.loc[(PICKS['picks']== "pick5") ,'Weights'] = Weight5/100
PICKS.loc[(PICKS['picks']== "pick6") ,'Weights'] = Weight6/100
PICKS.loc[(PICKS['picks']== "pick7") ,'Weights'] = Weight7/100
PICKS.loc[(PICKS['picks']== "pick8") ,'Weights'] = Weight8/100
COMPLETE = pd.merge(RETURNS, PICKS, left_on="picks", right_on="picks", how="left")
COMPLETE["final_return"] = COMPLETE.Weights * COMPLETE.daily_return
DATE_GROUP = pd.DataFrame(COMPLETE.groupby(['date'],as_index=False)['final_return'].sum())
DATE_GROUP = DATE_GROUP.sort_values("final_return", ascending=True)
DATE_GROUP = DATE_GROUP.reset_index(drop=True)
BENCHMARK_GROUP = BENCHMARK[["date", "daily_return"]]
BENCHMARK_GROUP = BENCHMARK_GROUP.sort_values('daily_return', ascending=True)
BENCHMARK_GROUP = BENCHMARK_GROUP.reset_index(drop=True)
FINAL_TABLE = pd.merge(BENCHMARK_GROUP, DATE_GROUP, left_index=True, right_index=True)
FINAL_TABLE["dif"] = FINAL_TABLE.final_return - FINAL_TABLE.daily_return
Result = sum(FINAL_TABLE.dif)
return Result
m.Maximize(Profit)
m.solve(disp=True)
And this is what I get in the end:
--------- APM Model Size ------------
Each time step contains
Objects : 0
Constants : 0
Variables : 9
Intermediates: 0
Connections : 0
Equations : 1
Residuals : 1
@error: Model Expression
*** Error in syntax of function string: Invalid element: <functionsolverat0x000
001c63000dfc0>
Position: 1
<functionsolverat0x000001c63000dfc0>
?
Can someome help me, please? Thank you for your time!