鱼C论坛

 找回密码
 立即注册
查看: 612|回复: 1

多进程计算最优化问题,win7系统下pycharm显示ran out of input,请问怎么解决?

[复制链接]
发表于 2020-2-29 09:49:06 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
先谢过各位大神。
多进程计算最优化问题,win7系统下pycharm显示ran out of input,请问怎么解决,找了一天都没找到,哭
补充一下,我加入了import dill as pickle。不加的话会报错不能pickle lambda。加入后会报错ran out of input.
但是我看这个input不是我自己程序中的input,好像是多线程自身启动时的input,崩溃中。。。

  1. import multiprocessing
  2. import numpy as np
  3. import pandas as pd
  4. import copy
  5. import time
  6. from scipy.optimize import minimize
  7. import dill as pickle


  8. # 目标函数:
  9. def sq(b):
  10.     global x, y
  11.     return (np.dot((np.dot(x, b) - (y - rf)), (np.dot(x, b) - (y - rf)).T))

  12. def func():
  13.     return sq

  14. # 约束条件,包括等式约束和不等式约束
  15. def con():
  16.     cons = ({'type': 'ineq', 'fun': lambda b: b[0]},
  17.             {'type': 'ineq', 'fun': lambda b: b[1]},
  18.             {'type': 'ineq', 'fun': lambda b: b[2]},
  19.             {'type': 'ineq', 'fun': lambda b: b[3]},
  20.             {'type': 'ineq', 'fun': lambda b: b[4]},
  21.             {'type': 'eq', 'fun': lambda b: b[0] + b[1] + b[2] - 1},
  22.             {'type': 'eq', 'fun': lambda b: b[3] + b[4] - 1},)
  23.     return cons


  24. def get_res(func, b0, cons):
  25.     res = minimize(func(), b0, method='SLSQP', constraints=cons)
  26.     return (res)

  27. def get_result2(res):
  28.     if max(res.x[:3]) == res.x[0]:
  29.         a = 'x1'
  30.     elif max(res.x[:3]) == res.x[1]:
  31.         a = 'x2'
  32.     else:
  33.         a = 'x3'
  34.     if res.x[3] - res.x[4] > 0.2:
  35.         b = 'y1'
  36.     elif res.x[4] - res.x[3] > 0.2:
  37.         b = 'y2'
  38.     else:
  39.         b = 'y3'
  40.     return (a + b)

  41. def get_result3(y0):
  42.     global y
  43.     global b0
  44.     global cons
  45.     y = copy.deepcopy(y0)
  46.     res = minimize(func(), b0, method='SLSQP', constraints=cons)
  47.     return (get_result2(res))


  48. # 以下为多进程内容

  49. def func_m(i, data_all, y, cons, b0, out_all):
  50.     print(3)
  51.     data = data_all.loc[i - 59:i, :]
  52.     data.replace(to_replace='-', value=np.nan, inplace=True)
  53.     data.dropna(axis=1, inplace=True)
  54.     rf = data.loc[:, 'GC007']
  55.     del data['GC007']
  56.     y_all = data[data.columns[1:-6]]
  57.     x = data[data.columns[-6:]]
  58.     x['c'] = 1
  59.     x['000001.SH'] = x['000001.SH'] - rf
  60.     out = pd.DataFrame()
  61.     out['code'] = y_all.columns
  62.     print('xxxxxxxxxxx')
  63.     out['result'] = [get_result3(y0) for y0 in y_all.T.values]
  64.     out_num = pd.DataFrame()
  65.     out_num['mix_type'] = list(set(out.result))
  66.     out_num['num'] = [out[out.result == t].shape[0] for t in out_num['mix_type']]
  67.     out_num.sort_values('mix_type', inplace=True)
  68.     out_num.reset_index(drop=True, inplace=True)
  69.     out_num.to_excel('C:/Users/T/Desktop/结果/'+str(i)+'.xlsx')
  70.     #out_all[str(i)] = out_num


  71. data_all = pd.read_excel('C:/Users/T/Desktop/分类.xlsx', sheet_name=3)

  72. if __name__ == "__main__":
  73.     y = []
  74.     x = pd.DataFrame()
  75.     cons = con()
  76.     b0 = [1 / 3, 1 / 3, 1 / 3, 0.5, 0.5, 0.5, 0.5]
  77.     run_list = list(range(data_all.shape[0] - 1, 59, -1))
  78.     run_zip_list = zip(run_list[0:47], run_list[47:47 * 2], run_list[47 * 2:47 * 3],
  79.                        run_list[47 * 3:47 * 4], run_list[47 * 4:47 * 5], run_list[47 * 5:47 * 6],
  80.                        run_list[47 * 6:47 * 7], run_list[47 * 7:47 * 8], run_list[47 * 8:47 * 9],
  81.                        run_list[47 * 9:47 * 10], run_list[47 * 10:47 * 11], run_list[47 * 11:47 * 12],
  82.                        run_list[47 * 12:47 * 13], run_list[47 * 13:47 * 14], run_list[47 * 14:47 * 15],
  83.                        )
  84.     # 这样缺了i=59的情况,需要额外补充
  85.     print('xx')
  86.     t1 = time.time()
  87.     out_all = multiprocessing.Manager().dict()  # 主进程与子进程共享这个字典

  88.     for i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12, i13, i14, i15 in run_zip_list:
  89.         p1 = multiprocessing.Process(target=func_m, args=(i1, data_all, y, cons, b0, out_all,))
  90.         # p2=multiprocess.Process(target=func_m,args=(i2,data_all,y,cons,b0,out_all,x,))
  91.         # p3=multiprocess.Process(target=func_m,args=(i3,data_all,y,cons,b0,out_all,x,))
  92.         '''
  93.         p4=multiprocess.Process(target=func_m,args=(i4,data_all,y,cons,b0,out_all))
  94.         p5=multiprocess.Process(target=func_m,args=(i5,data_all,y,cons,b0,out_all))
  95.         p6=multiprocess.Process(target=func_m,args=(i6,data_all,y,cons,b0,out_all))
  96.         p7=multiprocess.Process(target=func_m,args=(i7,data_all,y,cons,b0,out_all))
  97.         p8=multiprocess.Process(target=func_m,args=(i8,data_all,y,cons,b0,out_all))
  98.         p9=multiprocess.Process(target=func_m,args=(i9,data_all,y,cons,b0,out_all))
  99.         p10=multiprocess.Process(target=func_m,args=(i10,data_all,y,cons,b0,out_all))
  100.         p11=multiprocess.Process(target=func_m,args=(i11,data_all,y,cons,b0,out_all))
  101.         p12=multiprocess.Process(target=func_m,args=(i12,data_all,y,cons,b0,out_all))
  102.         p13=multiprocess.Process(target=func_m,args=(i13,data_all,y,cons,b0,out_all))
  103.         p14=multiprocess.Process(target=func_m,args=(i14,data_all,y,cons,b0,out_all))
  104.         p15=multiprocess.Process(target=func_m,args=(i15,data_all,y,cons,b0,out_all))
  105.         '''
  106.         t1 = time.time()
  107.         p1.start()
  108.         p1.join()
  109.         '''
  110.         for p in (p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,p13,p14,p15):
  111.             p.start()
  112.         for p in (p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,p13,p14,p15):
  113.             p.join()
  114.         '''
  115.         break
  116.     t2 = time.time()
  117.     print((t2 - t1) / 60)














复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-2-29 16:48:28 | 显示全部楼层
顶一下。。。求解答
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 13:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表