山东科技大学并行程序设计——MPI+OpenMP实现粒子群算法

本文详细介绍了串行和并行实现的粒子群优化(PSO)算法,包括种群初始化、速度更新以及寻找全局最优解的过程,并通过MPI进行并行性能对比分析。
摘要由CSDN通过智能技术生成

2.3.1 串行程序 

#include <iostream>
#include<cstdio>
#include<cmath>
#include<ctime>
#define c1 1.49445 //加速度因子一般是根据大量实验所得
#define c2 1.49445
#define w 0.9
#define sizepop 8000 // 种群规模
#define popmax 10 // 个体最大取值
#define popmin -10 // 个体最小取值
#define Vmax 0.5 // 速度最大值
#define Vmin -0.5 //速度最小值
#define dim 10 // 粒子的维数

double pop[sizepop][dim]; // 定义种群数组
double V[sizepop][dim]; // 定义种群速度数组
double fitness[sizepop]; // 定义种群的适应度数组
double fitnessgbest; // 群体极值适应度值
double pbest[sizepop][dim]; // 个体极值的位置
double gbest[dim]; // 总体极值的位置
double g_pbest[dim];
double fitnesspbest[sizepop]; //个体极值适应度的值

using namespace std;

double func(double * arr, int n) {
    double fitness1 = 0;
    for (int i = 0; i < n; i++) {
        fitness1 += arr[i] * arr[i];
    }
    return fitness1;
}

void pop_init() {
    for (int i = 0; i < sizepop; i++) {
        for (int j = 0; j < dim; j++) {
            pop[i][j] = (((double)rand()) / RAND_MAX - 0.5) * 4; //-2到2之间的随机数
            V[i][j] = ((double)rand()) / RAND_MAX - 0.5; //-0.5到0.5之间
            pbest[i][j] = pop[i][j];
        }
        fitness[i] = func(pop[i], dim);
    }
    fitnessgbest = fitness[0];
    int idx = 0;
    for (int i = 1; i < sizepop; i++)
        if (fitnessgbest < fitness[i]) {
            fitnessgbest = fitness[i];	//寻找最优值 
            idx = i;	//寻找最优解 
        }
    for (int i = 0; i < dim; i++)
        gbest[i] = pop[idx][i];
}

void update_V_p_g(int pop_size, int n) {
    for (int i = 0; i < pop_size; i++) {
        for (int j = 0; j < n; j++) {
            double rand1 = (((double)rand()) / RAND_MAX);
            double rand2 = (((double)rand()) / RAND_MAX);
            V[i][j] = w * V[i][j] + c1 * rand1 * (pbest[i][j] - pop[i][j]) + c2 * rand2 * (gbest[j] - pop[i][j]);
            // 不越界
            V[i][j] = min(max(V[i][j], Vmin), Vmax);
            pop[i][j] = pop[i][j] + V[i][j];
        }
    }
    // pbest
    for (int i = 0; i < pop_size; i++) {
        if (func(pop[i], dim) < func(pbest[i], dim))
            for (int j = 0; j < dim; j++) pbest[i][j] = pop[i][j];
    }
    // gbest
    double res = func(pbest[0], dim);
    int index = 0;
    for (int i = 1; i < pop_size; i++) {
        if (func(pbest[i], dim) < res) {
            res = func(pbest[i], dim);
            index = i;
        }
    }
    if (res < fitnessgbest) {
        fitnessgbest = res;
        for (int i = 0; i < dim; i++) gbest[i] = pbest[index][i];
    }
}

void PSO() {
    for (int t = 0; t < 50; t++) {
        update_V_p_g(sizepop, dim);
    }
}

int main(int argc, char ** argv) {
    pop_init();
    clock_t start_t=clock();  //开始计时
    PSO();
    clock_t  end_t=clock();  //结束计时
    std::cout << "result is " << fitnessgbest << std::endl;
    for (int i = 0; i < dim; i++)
        cout << gbest[i] << " ";
    cout << endl;
    printf("the time of cpu is %lf s\n", (double)(end_t - start_t) / CLOCKS_PER_SEC);

    return 0;
}

2.3.2 MPI程序

#include <iostream>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include <iostream>
#include "mpi.h"

#define c1 1.49445 //加速度因子一般是根据大量实验所得
#define c2 1.49445
#define w 0.9
#define maxgen 2000 // 迭代次数
#define sizepop 1000 // 种群规模
#define Vmax 0.5 // 速度最大值
#define Vmin -0.5 //速度最小值
#define dim 10 // 粒子的维数

double pop[sizepop][dim]; // 定义种群数组
double V[sizepop][dim]; // 定义种群速度数组
double fitness[sizepop]; // 定义种群的适应度数组
double fitnessgbest; // 群体极值适应度值
double pbest[sizepop][dim]; // 个体极值的位置
double gbest[dim]; // 总体极值的位置
double g_pbest[dim];

using namespace std;

double func(double * arr, int n) {
    double fitness1 = 0;
    for (int i = 0; i < n; i++) {
        fitness1 += arr[i] * arr[i];
    }
    return fitness1;
}

void pop_init() {
    for (int i = 0; i < sizepop; i++) {
        for (int j = 0; j < dim; j++) {
            pop[i][j] = (((double)rand()) / RAND_MAX - 0.5) * 4; //-2到2之间的随机数
            V[i][j] = ((double)rand()) / RAND_MAX - 0.5; //-0.5到0.5之间
            pbest[i][j] = pop[i][j];
        }
        fitness[i] = func(pop[i], dim);
    }
    fitnessgbest = fitness[0];
    int idx = 0;
    for (int i = 1; i < sizepop; i++)
        if (fitnessgbest < fitness[i]) {
            fitnessgbest = fitness[i];
            idx = i;
        }
    for (int i = 0; i < dim; i++)
        gbest[i] = pop[idx][i];
}

void update_V_p_g(int pop_size, int n) {
    for (int i = 0; i < pop_size; i++) {
        for (int j = 0; j < n; j++) {
            double rand1 = (((double)rand()) / RAND_MAX);
            double rand2 = (((double)rand()) / RAND_MAX);
            V[i][j] = w * V[i][j] + c1 * rand1 * (pbest[i][j] - pop[i][j]) + c2 * rand2 * (gbest[j] - pop[i][j]);
            // 不越界
            V[i][j] = min(max(V[i][j], Vmin), Vmax);
            pop[i][j] = pop[i][j] + V[i][j];
        }
    }
    // pbest
    for (int i = 0; i < pop_size; i++) {
        if (func(pop[i], dim) < func(pbest[i], dim))
            for (int j = 0; j < dim; j++) pbest[i][j] = pop[i][j];
    }
    // gbest
    double res = func(pbest[0], dim);
    int index = 0;
    for (int i = 1; i < pop_size; i++) {
        if (func(pbest[i], dim) < res) {
            res = func(pbest[i], dim);
            index = i;
        }
    }
    if (res < fitnessgbest) {
        fitnessgbest = res;
        for (int i = 0; i < dim; i++) gbest[i] = pbest[index][i];
    }
}

int main(int argc, char ** argv) {
    pop_init();
	 
    double start = 0.0, stop = 0.0;  //记录时间的变量
    double start_g = 0.0, stop_g = 0.0;
    double start_b = 0.0, stop_b = 0.0;
    double t_temp = 0.0, t_last = 0.0;

    int my_rank, comm_sz;
    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
    MPI_Comm_size(MPI_COMM_WORLD, &comm_sz);

    double* buf3 = NULL;
    double* buf5 = NULL;
    int* buf4 = NULL;
    if (my_rank == 0) {
        buf3 = (double*)malloc(comm_sz * sizeof(double));
        buf5 = (double*) malloc(comm_sz * dim * sizeof(double));
        buf4 = (int*)malloc(comm_sz * sizeof(int));
    }

    double result = fitnessgbest;
    MPI_Barrier(MPI_COMM_WORLD);

    start = MPI_Wtime();   //开始计算时间
    for (int t = 0; t < 50; t++) {
        update_V_p_g(sizepop, dim);

        start_g = MPI_Wtime();
        //最优值buf3,最优解buf5
        MPI_Gather(&fitnessgbest, 1, MPI_DOUBLE, buf3, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);
        MPI_Gather(gbest, dim, MPI_DOUBLE, buf5, dim, MPI_DOUBLE, 0, MPI_COMM_WORLD);
        stop_g = MPI_Wtime();
        t_temp = stop_g - start_g;
        t_last += t_temp;

        if (my_rank == 0) {
            double temp = buf3[0];
            int idx = 0;
            for (int i = 0; i < comm_sz; i++)
                if (buf3[i] < temp) {
                    temp = buf3[i];
                    idx = i;
                }
            fitnessgbest = temp;
            for (int i = 0; i < dim; i++) {
                gbest[i] = buf5[idx * dim + i];
            }
			// 通信时间 
            start_b = MPI_Wtime();
            MPI_Bcast(&fitnessgbest, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);
            MPI_Bcast(gbest, dim, MPI_DOUBLE, 0, MPI_COMM_WORLD);
            stop_b = MPI_Wtime();
            t_temp = stop_b - start_b;
            t_last += t_temp;
        }

        if (fitnessgbest < result) {
            result = fitnessgbest;
            for (int i = 0; i < dim; i++) g_pbest[i] = gbest[i];
        }
    }
    stop = MPI_Wtime();  //结束计算时间
    MPI_Finalize();
    if (my_rank == 0) {
        cout << "result is " << result << endl;
        for (int i = 0; i < dim; i++)
            cout << g_pbest[i] << " ";
        cout << endl;
        printf("the time of mpi is %f s\n", stop - start);
        printf("the time of message passing is %f s\n", t_last);
    }
    return 0;
}

2.3.3性能对比与分析

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值