有没有办法让Gekko与Pandas合作?
我正在尝试使 gekko最大化功能“利润”,但我无法使其起作用。我总是遇到以下错误。
我需要 gekko solver 才能为(stroge1,weight2,stoge3 ... stroge8)选择最佳值。强>
每个重量可以是0(0%)和15(15%)之间的任何值,所有权重的总和必须为100(100%)。
需要所有数据范围来计算EACK PICS(PICK1,PICK2,PICK3 ... PICC8)和基准测试的最终利润。
最后,“结果”是每个选秀权利的利润的总和减去基准的利润。
所以: 权重是变量; 他们的界限从0到15; 约束是他们的总和必须为100。
到目前为止,我只能找到Gekko最大化定义方程式的示例。可以做这样的事情吗?
这是我的代码:
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)
这就是我最终得到的:
--------- 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>
?
请某个人能帮助我吗? 谢谢您的宝贵时间!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Gekko首次通过模型构建了模型的符号形式,然后将变量和方程式编译为具有自动分化基于梯度的求解器的字节代码。这意味着仅在构建模型时而不是在求解模型时使用任何功能调用。
有多种方法可以通过二进制变量进行选择来重新制定模型。这是二进制变量的示例。
其他逻辑条件还包括 语句,例如
m.if3(x,0,1)
和sos1
( type-1 type-1 的特殊有序集)。 SOS是二进制变量的组合,仅允许一个二进制变量。从一组可能的数值值中选择一个是相互排斥的选项。Gekko中允许使用numpy,pandas或其他创建符号表示的函数。
Gekko builds a symbolic form of a model the first time through and then compiles the variables and equations into byte-code with automatic differentiation for the gradient-based solvers. This means that any function call is only used when building the model, not when solving the model.
There are ways to reformulate the model with binary variables for selections. Here is an example of a binary variable.
Other logical conditions include
if
statements such asm.if3(x,0,1)
andsos1
(special ordered sets of type-1). The SOS is a combination of binary variables with only one that is allowed to be non-zero. Chose one from a set of possible numeric values that are mutually exclusive options.Numpy, Pandas or other functions that create a symbolic representation of the equation are allowed in Gekko.