问题描述
我有一个简单的投资优化问题,我正在尝试使用 Pymoo 解决。我想找到最佳的投资解决方案,其目标是在每个投资解决方案都在固定预算之内的情况下,最大程度地减少违约并最大化回报。
这是我定义问题的方式:
budget = 100 # set maximum budget for investing
num_loans = 10 # set the number of loans to invest in
# define predicted returns and predicted defaults. For the sake of demonstration,I'm giving
# all the same values here,but in a real scenario they each have a different value
predicted_returns = np.array([0.02 for i in range(num_loans)])
predicted_defaults = np.array([0.01 for i in range(num_loans)])
class MyProblem(Problem):
def __init__(self):
super().__init__(n_var=num_loans,n_obj=2,n_constr=2,xl=np.array([ 0.0 for i in range(num_loans)]),xu=np.array([ budget for i in range(num_loans)])
)
def _evaluate(self,x,out,*args,**kwargs):
# I want to minimise the probability of default of my portfolio
f1 = np.array([ np.sum(portfolio * predicted_defaults) for portfolio in x ] )
# I want to maximise the return of my portfolio
f2 = - np.array( [ np.sum(portfolio * predicted_returns) for portfolio in x ] )
# I cannot invest more than my Budget.
g1 = np.array([ np.sum(portfolio) - budget for portfolio in x ] )
out["F"] = np.column_stack([f1,f2 ])
out["G"] = np.column_stack([g1 ])
问题定义的其余部分遵循http://pymoo.org/getting_started.html
上的pymoo示例当我尝试打印len(res.F)
时,消息为TypeError: object of type 'nonetype' has no len()
。
将约束定义替换为g1 = np.array([ -1 for portfolio in x ] )
,
len(res.F)
返回人口总数。
本质上,约束条件应表明每个解决方案都不应超出预算。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)