我的优化任务定义为:
minimize varaince(result) subject to expectation(result) >=k
我定义了两个函数:第一个是返回结果方差的目标函数,第二个是约束:
R=[]
def objective(plein):
for i in range(0, len(dataframe)):
R.append((1- max(0, (dataframe.iloc[i,1]- plein) /dataframe.iloc[i,1]) )*(dataframe.iloc[i,2]-dataframe.iloc[i,1]))
#the first column of dataframe contains costs and the second one premiums
R_annee=[R[x - y: x] for x, y in zip(accumulate(length_to_split), length_to_split)]
return np.var([sum(r) for r in R_annee])
I=[]
def cont(plein):
for i in range(0, len(dataframe)):
if dataframe.iloc[i,1]>plein:
I.append((1- (dataframe.iloc[i,1]- plein) /dataframe.iloc[i,1]) *(dataframe.iloc[i,2]-dataframe.iloc[i,1]))
else :
I.append((dataframe.iloc[i,2]-dataframe.iloc[i,1]))
I_annee=[I[f - g: f] for f, g in zip(accumulate(length_to_split), length_to_split)]
return np.mean([sum(i) for i in I_annee]) - 1000000 #we will minimize the function objective (which returns variance of net result) with respect to expected net result is equal or larger than 1000000
当我使用optimize.minimize时:
my_constrain={'type': 'ineq', 'fun': cont}
minimize(funct, 700000, constraints=my_constrain)
它指示优化已成功终止,但是仅进行一次迭代并返回初始猜测。 在这种情况下我该怎么办?你能帮我解决这个任务吗?