python多进程multiprocessing.Pool的坑你想象不到(多目标遗传算法)

用geatpy包求解多目标问题,明明看起来没啥问题,但是就是跑不出来,后来发现必须用cmd跑才行,打开anaconda prompt,然后cd Desktop,再python [文件绝对路径],运行即可。然后中间弹框图像,要关闭后程序才能接着运行下去。。。好大一个连环坑啊啊啊啊啊

# -*- coding: utf-8 -*-
import geatpy as ea # import geatpy
from MyProblem import MyProblem # 导入自定义问题接口
#import numpy as np
import multiprocessing as mp
import pandas as pd

"""==================================实例化问题对象================================"""
problem = MyProblem()     # 生成问题对象
"""==================================种群设置================================"""
Encoding = 'BG'           # 编码方式
NIND1 = 50 
NIND2 = 50                # 种群规模
NIND30 = 50


Field = ea.crtfld(Encoding, problem.varTypes, problem.ranges, problem.borders) # 创建区域描述器

population1 = ea.Population(Encoding, Field, NIND1) # 实例化种群对象(此时种群还没被初始化,仅仅是完成种群对象的实例化)
arg1=(problem,population1)

population2 = ea.Population(Encoding, Field, NIND2)
arg2=(problem,population2)

population30 = ea.Population(Encoding, Field, NIND30)
arg30=(problem,population30)

populations=(arg1,arg2,arg30)

"""==================================算法参数设置================================"""
#def test(n):
#    return n

if __name__=='__main__':

    print('pool1 is running')
    pool=mp.Pool(processes=mp.cpu_count())
    results=[]
    for i in populations:
        results.append(pool.apply_async(ea.moea_NSGA30_templet, (*i,)))
    #for i in range(50):
        #results.append(pool.apply_async(test, (i, )))
        print('pool2 is running')
    pool.close()
    print('before join')
    pool.join()
    print('after join')
    for res in results:
        #print(res.get())
        myAlgorithm=res.get()
    myAlgorithm.MAXGEN = 30
    NDSet = myAlgorithm.run()
    NDSet.save() 
    """========打印种群规模*迭代次数==============="""
    print("three_obj",50,"*",30)
    """========存储表现型,即决策变量的值============"""
    three_Phen_50_30=NDSet.Phen
    print(three_Phen_50_30)
    three_Phen_50_30 = pd.DataFrame(three_Phen_50_30)
    three_Phen_50_30.to_csv("three_Phen_20_300.csv",index=False,sep=',')
    """========存储帕累托前沿,即目标函数的值========"""
    three_obj_50_30=NDSet.ObjV
    print('用时:%s 秒'%(myAlgorithm.passTime))
    print('非支配个体数:%s 个'%(NDSet.sizes))
    print('单位时间找到帕累托前沿点个数:%s 个'%(int(NDSet.sizes // myAlgorithm.passTime)))


你可能感兴趣的:(Pyhon,多进程)