先謝過各位大神。
多行程計算最優化問題,win7系統下pycharm顯示ran out of input,請問怎么解決,找了一天都沒找到,哭
補充一下,我加入了import dill as pickle。不加的話會報錯不能pickle lambda。加入后會報錯ran out of input.
但是我看這個input不是我自己程式中的input,好像是多執行緒自身啟動時的input,崩潰中。。。
import multiprocessing
import numpy as np
import pandas as pd
import copy
import time
from scipy.optimize import minimize
import dill as pickle
# 目標函式:
def sq(b):
global x, y
return (np.dot((np.dot(x, b) - (y - rf)), (np.dot(x, b) - (y - rf)).T))
def func():
return sq
# 約束條件,包括等式約束和不等式約束
def con():
cons = ({'type': 'ineq', 'fun': lambda b: b[0]},
{'type': 'ineq', 'fun': lambda b: b[1]},
{'type': 'ineq', 'fun': lambda b: b[2]},
{'type': 'ineq', 'fun': lambda b: b[3]},
{'type': 'ineq', 'fun': lambda b: b[4]},
{'type': 'eq', 'fun': lambda b: b[0] + b[1] + b[2] - 1},
{'type': 'eq', 'fun': lambda b: b[3] + b[4] - 1},)
return cons
def get_res(func, b0, cons):
res = minimize(func(), b0, method='SLSQP', constraints=cons)
return (res)
def get_result2(res):
if max(res.x[:3]) == res.x[0]:
a = 'x1'
elif max(res.x[:3]) == res.x[1]:
a = 'x2'
else:
a = 'x3'
if res.x[3] - res.x[4] > 0.2:
b = 'y1'
elif res.x[4] - res.x[3] > 0.2:
b = 'y2'
else:
b = 'y3'
return (a + b)
def get_result3(y0):
global y
global b0
global cons
y = copy.deepcopy(y0)
res = minimize(func(), b0, method='SLSQP', constraints=cons)
return (get_result2(res))
# 以下為多行程內容
def func_m(i, data_all, y, cons, b0, out_all):
print(3)
data = data_all.loc[i - 59:i, :]
data.replace(to_replace='-', value=https://bbs.csdn.net/topics/np.nan, inplace=True)
data.dropna(axis=1, inplace=True)
rf = data.loc[:, 'GC007']
del data['GC007']
y_all = data[data.columns[1:-6]]
x = data[data.columns[-6:]]
x['c'] = 1
x['000001.SH'] = x['000001.SH'] - rf
out = pd.DataFrame()
out['code'] = y_all.columns
print('xxxxxxxxxxx')
out['result'] = [get_result3(y0) for y0 in y_all.T.values]
out_num = pd.DataFrame()
out_num['mix_type'] = list(set(out.result))
out_num['num'] = [out[out.result == t].shape[0] for t in out_num['mix_type']]
out_num.sort_values('mix_type', inplace=True)
out_num.reset_index(drop=True, inplace=True)
out_num.to_excel('C:/Users/T/Desktop/結果/'+str(i)+'.xlsx')
#out_all[str(i)] = out_num
data_all = pd.read_excel('C:/Users/T/Desktop/分類.xlsx', sheet_name=3)
if __name__ == "__main__":
y = []
x = pd.DataFrame()
cons = con()
b0 = [1 / 3, 1 / 3, 1 / 3, 0.5, 0.5, 0.5, 0.5]
run_list = list(range(data_all.shape[0] - 1, 59, -1))
run_zip_list = zip(run_list[0:47], run_list[47:47 * 2], run_list[47 * 2:47 * 3],
run_list[47 * 3:47 * 4], run_list[47 * 4:47 * 5], run_list[47 * 5:47 * 6],
run_list[47 * 6:47 * 7], run_list[47 * 7:47 * 8], run_list[47 * 8:47 * 9],
run_list[47 * 9:47 * 10], run_list[47 * 10:47 * 11], run_list[47 * 11:47 * 12],
run_list[47 * 12:47 * 13], run_list[47 * 13:47 * 14], run_list[47 * 14:47 * 15],
)
# 這樣缺了i=59的情況,需要額外補充
print('xx')
t1 = time.time()
out_all = multiprocessing.Manager().dict() # 主行程與子行程共享這個字典
for i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12, i13, i14, i15 in run_zip_list:
p1 = multiprocessing.Process(target=func_m, args=(i1, data_all, y, cons, b0, out_all,))
# p2=multiprocess.Process(target=func_m,args=(i2,data_all,y,cons,b0,out_all,x,))
# p3=multiprocess.Process(target=func_m,args=(i3,data_all,y,cons,b0,out_all,x,))
'''
p4=multiprocess.Process(target=func_m,args=(i4,data_all,y,cons,b0,out_all))
p5=multiprocess.Process(target=func_m,args=(i5,data_all,y,cons,b0,out_all))
p6=multiprocess.Process(target=func_m,args=(i6,data_all,y,cons,b0,out_all))
p7=multiprocess.Process(target=func_m,args=(i7,data_all,y,cons,b0,out_all))
p8=multiprocess.Process(target=func_m,args=(i8,data_all,y,cons,b0,out_all))
p9=multiprocess.Process(target=func_m,args=(i9,data_all,y,cons,b0,out_all))
p10=multiprocess.Process(target=func_m,args=(i10,data_all,y,cons,b0,out_all))
p11=multiprocess.Process(target=func_m,args=(i11,data_all,y,cons,b0,out_all))
p12=multiprocess.Process(target=func_m,args=(i12,data_all,y,cons,b0,out_all))
p13=multiprocess.Process(target=func_m,args=(i13,data_all,y,cons,b0,out_all))
p14=multiprocess.Process(target=func_m,args=(i14,data_all,y,cons,b0,out_all))
p15=multiprocess.Process(target=func_m,args=(i15,data_all,y,cons,b0,out_all))
'''
t1 = time.time()
p1.start()
p1.join()
'''
for p in (p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,p13,p14,p15):
p.start()
for p in (p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,p13,p14,p15):
p.join()
'''
break
t2 = time.time()
print((t2 - t1) / 60)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/85406.html
上一篇:奔向月薪10萬
下一篇:初學者的python疑問
