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)))


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值