粒子群算法的多线程并行计算(python)

前言:我又从仓库里面翻出来了2020年写的一篇博文,作为我无知青葱岁月的留影。

粒子群算法求解多配送中心的车辆调度优化问题(python3)

紧接上一篇文章《遗传+粒子群 求解多配送中心车辆调度问题(python)》,考虑采用多线程的方式封装粒子群算法,加快算法的整体求解速度。

1.代码实现

def get_lower_solution(location_index):
    location_customer_list = allocation_customer(location_index)
    ind_routes = []
    ind_value = 0
    thread_list = []
    for i in range(len(location_index)):
        supply_index = location_index[i]
        demand_nodes = location_customer_list[i]
        demand_nodes = [i + 1 for i in demand_nodes]  # 需求节点的编号从1开始
        demand_node_num = len(demand_nodes)
        # 采用粒子群算法针对需求节点的索引进行计算下层模型的成本
        pso_parameter_dict['dim'] = pso_parameter_dict['solution_layer'] * demand_node_num  # 一个粒子的维度
        pso_parameter_dict['x_bound'] = [[1, data_dict['vehicle_num']], [1, demand_node_num]]  # 自变量的上下限限制
        distance_graph, pathTime_graph = get_pathLen_time(supply_index)
        # 初始化粒子群类
        pso = pso_operation(pso_parameter_dict, data_dict,
                            demand_nodes, distance_graph, pathTime_graph, )
        thread_list.append(pso)
    for ind_thread in thread_list:
        ind_thread.start()
    for ind_thread in thread_list:
        ind_thread.join()
        best_routes, best_value = ind_thread.get_result()
        ind_routes += best_routes
        ind_value += best_value
    return ind_routes, ind_value

2.效能比较

感觉并没有快,为了防止粒子群算法封装的类函数的属性被修改,所以采用了锁的机制。

3.小结

参考文章:
1.python队列基本使用https://www.cnblogs.com/yhleng/p/9493457.html
2.Python建立多线程任务并获取每个线程返回值https://www.cnblogs.com/bethansy/p/7965820.html
3.Python—线程的锁https://blog.csdn.net/weixin_41678001/article/details/84487536
4.Python threading(多线程)https://www.cnblogs.com/leozhanggg/p/10317494.html
5.Python中的多线程
https://baijiahao.baidu.com/s?id=1637308955850713833&wfr=spider&for=pc
6.Python多线程与队列https://www.cnblogs.com/yonguo123/p/11901050.html
7.Python多线程——队列(Queue)https://www.jianshu.com/p/e30d302ebdeb
8.python 实现多线程并返回函数返回值的三种方法https://blog.csdn.net/qq_43178297/article/details/102805223
9.Python3用多线程替代for循环提升程序运行速度
https://www.cnblogs.com/LanTianYou/p/11498525.html
10.[Python技巧]如何加快循环操作和Numpy数组运算速度
https://blog.csdn.net/lc013/article/details/100033413
11.多线程https://www.liaoxuefeng.com/wiki/897692888725344/923056337842176

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Logintern09

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值