|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
先谢过各位大神。
多进程计算最优化问题,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=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)
复制代码 |
|