细粒度模式下的GPU加速遗传算法

函数主体部分

#define CL_TARGET_OPENCL_VERSION 120

#include <malloc.h>
#include <time.h>
#include <CL/cl.h>
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <fstream>
#include <stdlib.h>
#include <iostream>
using namespace std;
//read a kernel file to a string
int convertToString(const char *filename, std::string& s);


const char* programSource=NULL;
void show_matrix(int* m,int dim1,int dim2,char* matrix_name);
void show_matrix_float(float* m,int dim1,int dim2,char* matrix_name);
void bubblesort(float *arr, int size)  
{  
    int i, j, tmp;  
    for (i = 0; i < size - 1; i++) {  
        for (j = 0; j < size - i - 1; j++) {  
            if (arr[j] < arr[j+1]) {  
                tmp = arr[j];  
                arr[j] = arr[j+1];  
                arr[j+1] = tmp;  
            }  
        }  
    }  
}
float max(float *arr,int size)
{
    bubblesort(arr,size);
    return arr[0];
}
void mut_vec(int *arr,int size,float pm)
{
    for(int i=0;i<size;i++)
    {
        float mut_p;
        mut_p=rand()%100*1.0/100;
        if(mut_p<pm)
        {
            arr[i]=rand()%3-1;
        }
    }
}
void get_cross_rates(float *arr,int size)
{
    // 随机初始化等位基因交叉概率组
    for (int i = 0; i < size; i++) 
		arr[i] = rand()%10*1.0/10;
}
int* get_best_individual(int* population,int pop_num,int code_len,float* fitness,float best_fitness)
{
    int* best_indi;
    best_indi=(int*)malloc(sizeof(int)*code_len);
    for(int i=0;i<pop_num;i++)
    {
        if (fitness[i]==best_fitness)
        {
            for(int j=0;j<code_len;j++)
            {
                best_indi[j]=population[i*code_len+j];
            }
            return best_indi;
        }
    }
}
int main() {
	// This code executes on the OpenCL host
	// Host data
	int *population = NULL;  // Input array
    int *population_original=NULL;
	int *p1s = NULL;  // 种群中挑选的父代
	int *p2s = NULL;  // Output array
    int *children1=NULL;
    int *children2=NULL;
    int *population_extend=NULL;//作为中间种群状态存储父代与子代共同构成的种群
    int *mutation_vector=NULL;
    float *fitness=NULL;
    float* fitness_sorted;
    const int pop_num=5000;//种群大小
    const int p_num=pop_num/2;//单类父代个体数
    const int pop_ex_num=2*pop_num;
    const int code_len=20;//个体编码长度
    const float pc=0.6;//等位基因交叉概率
    const float pm=0.1;//等位基因变异概率
    const int evol_num=100;//进化代数
    float *cross_rates=NULL;
    int *best_p=NULL;
    //size of populations
    const int size_pop = pop_num*code_len;
    const int size_pop_ex=2*size_pop;
    const int size_p=pop_num/2*code_len;

    // 为种群分配内存
	// Allocate space for input/output data
    population = (int*)malloc(sizeof(int)*size_pop);  // 
    population_original = (int*)malloc(sizeof(int)*size_pop);  // 
    population_extend = (int*)malloc(sizeof(int)*size_pop_ex);  // 
	p1s = (int*)malloc(sizeof(int)*size_p);  // 
	p2s = (int*)malloc(sizeof(int)*size_p);  // 
    children1=(int*)malloc(sizeof(int)*size_p);
    children2=(int*)malloc(sizeof(int)*size_p);
    best_p=(int*)malloc(sizeof(int)*code_len);
    mutation_vector=(int*)malloc(sizeof(int)*code_len);
    cross_rates=(float*)malloc(sizeof(float)*code_len);
    fitness=(float*)malloc(sizeof(float)*(pop_num+p_num*2));
    fitness_sorted=(float*)malloc(sizeof(float)*pop_num*2);
    // 随机初始化种群
    srand((unsigned)time(NULL));//随机数播种
	int i;
	for (i = 0; i < size_pop; i++) 
    {
		population[i] = rand()%10;//编码值于0-9之间
        population_original[i]=population[i];
    }
    // 初始化两父代群
	p1s=population;// 将两个父代的指针指向种群的起始和中点。
	p2s=population+size_pop/2;
    // 随机初始化一个最优值
    for (i = 0; i < code_len; i++) 
		best_p[i] = rand()%10;
    printf("finish initiate!!!\n");
    /*********************data show***********************/
    //show_matrix(population,pop_num,code_len,"population");
    //show_matrix(population_extend,pop_num+p_num+p_num,code_len,"population_extend");
    //show_matrix_float(cross_rates,code_len,1,"cross_rates");
/************************八股文部分*******************/
	// Use this to check the output of each API call
    // 保存各函数返回值的状态
	cl_int status;
	// 获取平台信息
	cl_uint numPlatforms = 0;
    // 查询可用的平台个数
	status = clGetPlatformIDs(0, NULL, &numPlatforms);
	// Allocate enough space for each platform
	cl_platform_id *platforms = NULL;
    // 分配内存
	platforms = (cl_platform_id*)malloc(numPlatforms * sizeof(cl_platform_id));
	// Fill in the platforms
    // 得到可用的OpenCL列表
	status = clGetPlatformIDs(numPlatforms, platforms, NULL);
	// 获取设备信息
	cl_uint numDevices = 0;
    // 查询设备数量 OpenCL设备
	status = clGetDeviceIDs(platforms[0], CL_DEVICE_TYPE_ALL, 0, NULL, &numDevices);
	// Allocate enough space for each device
	cl_device_id *devices;
    // 设备分配内存
	devices = (cl_device_id*)malloc(numDevices * sizeof(cl_device_id));
	// Fill in the devices 
    // 得到与platform关联的可用的OpenCL设备
	status = clGetDeviceIDs(platforms[0], CL_DEVICE_TYPE_ALL, numDevices, devices, NULL);
	// 创建上下文信息
	cl_context context;
	context = clCreateContext(NULL, numDevices, devices, NULL, NULL, &status);
	// 创建命令队列
	cl_command_queue cmdQueue;
    // 指定上下文与设备创建
	cmdQueue = clCreateCommandQueue(context, devices[0], 0, &status);
/****************八股文暂时结束****************/
    
    
	// 创建数据内存  内存对象  buffer
	// Create a buffer object that will contain the data from the host array A B C
    cl_mem buf_population;  // 种群
    cl_mem buf_p1s;
    cl_mem buf_p2s;    
    cl_mem buf_population_extend;  // 种群过渡态
    cl_mem buf_children1;
    cl_mem buf_children2;
    cl_mem buf_cross_rates;
    cl_mem buf_best_p;
    cl_mem buf_fitness;
    cl_mem buf_fitness_sorted;
    cl_mem buf_mutation_vector;
	buf_population = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(int)*size_pop,NULL, &status);// 可读写
	buf_p1s = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(int)*size_pop/2,NULL, &status);// 可读写
	buf_p2s = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(int)*size_pop/2,NULL, &status);// 可读写
	buf_population_extend = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(int)*size_pop_ex,NULL, &status);// 可读写
    buf_children1 = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(int)*size_pop/2,NULL, &status);// 可读写
    buf_children2 = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(int)*size_pop/2,NULL, &status);// 可读写
    buf_fitness = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(float)*pop_num*2,NULL, &status);// 可读写
    buf_fitness_sorted = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(float)*pop_num*2,NULL, &status);// 可读写
    buf_mutation_vector = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(float)*code_len,NULL, &status);// 可读写
    buf_best_p = clCreateBuffer(context, CL_MEM_READ_ONLY, sizeof(int)*code_len,NULL, &status);// 只读
    buf_cross_rates = clCreateBuffer(context, CL_MEM_READ_ONLY, sizeof(float)*code_len,NULL, &status);// 只读
    
   // 主机端内存数据传入到OpenCL buffer中
    //将种群放到OpenCL的buffer中。
	status = clEnqueueWriteBuffer(cmdQueue, buf_population, CL_FALSE,0, sizeof(int)*size_pop, population, 0, NULL, NULL);
	status = clEnqueueWriteBuffer(cmdQueue, buf_p1s, CL_FALSE,0, sizeof(int)*size_pop/2, p1s, 0, NULL, NULL);
	status = clEnqueueWriteBuffer(cmdQueue, buf_p2s, CL_FALSE,0, sizeof(int)*size_pop/2, p2s, 0, NULL, NULL);
	status = clEnqueueWriteBuffer(cmdQueue, buf_cross_rates, CL_FALSE,0, sizeof(float)*code_len, cross_rates, 0, NULL, NULL);
	status = clEnqueueWriteBuffer(cmdQueue, buf_best_p, CL_FALSE,0, sizeof(int)*code_len, best_p, 0, NULL, NULL);
    
	// 创建CL程序
    // read the kernel funtion
    const char* crossover_fun_str=NULL;//可改写指针但是不能改写指针指向的内容
    const char* pop_merge_fun_str=NULL;//可改写指针但是不能改写指针指向的内容
    const char* fitness_fun_str=NULL;//可改写指针但是不能改写指针指向的内容
    const char* pop_produce_fun_str=NULL;//可改写指针但是不能改写指针指向的内容
    const char* mutation_fun_str=NULL;//可改写指针但是不能改写指针指向的内容
    std::string sourcestr;
    std::string sourcestr_pop;
    std::string sourcestr_fit;
    std::string sourcestr_pop_produce;
    std::string sourcestr_mutation;
    convertToString("crossover.cl", sourcestr);
    crossover_fun_str = sourcestr.c_str();
    convertToString("pop_merge.cl", sourcestr_pop);
    pop_merge_fun_str = sourcestr_pop.c_str();
    convertToString("fitness.cl", sourcestr_fit);
    fitness_fun_str = sourcestr_fit.c_str();
    convertToString("pop_produce.cl", sourcestr_pop_produce);
    pop_produce_fun_str = sourcestr_pop_produce.c_str();
    convertToString("mutation.cl", sourcestr_mutation);
    mutation_fun_str = sourcestr_mutation.c_str();
   // 利用核函数(crossover,pop_merge)代码串,创建一个OpenCL程序对象
	cl_program program_crossover = clCreateProgramWithSource(context, 1,(const char**)&crossover_fun_str, NULL, &status);
	cl_program program_pop_merge = clCreateProgramWithSource(context, 1,(const char**)&pop_merge_fun_str, NULL, &status);
	cl_program program_fitness = clCreateProgramWithSource(context, 1,(const char**)&fitness_fun_str, NULL, &status);
	cl_program program_pop_produce = clCreateProgramWithSource(context, 1,(const char**)&pop_produce_fun_str, NULL, &status);
	cl_program program_mutation = clCreateProgramWithSource(context, 1,(const char**)&mutation_fun_str, NULL, &status);
	// 编译CL程序(交叉操作核函数)
	// Build (compile) the program for the device
	status = clBuildProgram(program_crossover, numDevices, devices,NULL, NULL, NULL);
	status = clBuildProgram(program_pop_merge, numDevices, devices,NULL, NULL, NULL);
	status = clBuildProgram(program_fitness, numDevices, devices,NULL, NULL, NULL);
	status = clBuildProgram(program_pop_produce, numDevices, devices,NULL, NULL, NULL);
	status = clBuildProgram(program_mutation, numDevices, devices,NULL, NULL, NULL);
	// 创建CL内核
	// Create the vector addition kernel
	cl_kernel kernel_crossover;
	kernel_crossover = clCreateKernel(program_crossover, "crossover", &status);
    cl_kernel kernel_pop_merge;
	kernel_pop_merge = clCreateKernel(program_pop_merge, "pop_merge", &status);
    cl_kernel kernel_fitness;
	kernel_fitness = clCreateKernel(program_fitness, "fitness", &status);
    cl_kernel kernel_pop_produce;
	kernel_pop_produce = clCreateKernel(program_pop_produce, "pop_produce", &status);
    cl_kernel kernel_mutation;
	kernel_mutation = clCreateKernel(program_mutation, "mutation", &status);
    
    
/******************八股文开始*************************/
    // Define an index space (global work size) of work 
	// items for execution. A workgroup size (local work size) 
	// is not required, but can be used.
	size_t globalWorkSize[1];
    // work items in each dimension
	globalWorkSize[0] = pop_num*2;
	// 计算,执行kernel
    size_t global[2];
/******************八股文结束*************************/
    
    
/****************函数参数初始化***************************/
	// crossover传参
    status = clSetKernelArg(kernel_crossover, 0, sizeof(int), &p_num);
    status = clSetKernelArg(kernel_crossover, 1, sizeof(int), &code_len);
    status = clSetKernelArg(kernel_crossover, 2, sizeof(float), &pc);
    status = clSetKernelArg(kernel_crossover, 3, sizeof(cl_mem), &buf_cross_rates); 
    status = clSetKernelArg(kernel_crossover, 4, sizeof(cl_mem), &buf_p1s);
    status = clSetKernelArg(kernel_crossover, 5, sizeof(cl_mem), &buf_p2s);
    status = clSetKernelArg(kernel_crossover, 6, sizeof(cl_mem), &buf_children1);
    status = clSetKernelArg(kernel_crossover, 7, sizeof(cl_mem), &buf_children2);
	// mutation 传参
    status = clSetKernelArg(kernel_mutation, 0, sizeof(int), &pop_num);
    status = clSetKernelArg(kernel_mutation, 1, sizeof(int), &code_len);
    status = clSetKernelArg(kernel_mutation, 2, sizeof(cl_mem), &buf_mutation_vector);
    status = clSetKernelArg(kernel_mutation, 3, sizeof(cl_mem), &buf_population_extend);
    //pop_merge传参
    status = clSetKernelArg(kernel_pop_merge, 0, sizeof(int), &p_num);
    status = clSetKernelArg(kernel_pop_merge, 1, sizeof(int), &pop_num);
    status = clSetKernelArg(kernel_pop_merge, 2, sizeof(int), &code_len);
    status = clSetKernelArg(kernel_pop_merge, 3, sizeof(cl_mem), &buf_population_extend); 
    status = clSetKernelArg(kernel_pop_merge, 4, sizeof(cl_mem), &buf_population);
    status = clSetKernelArg(kernel_pop_merge, 5, sizeof(cl_mem), &buf_children1);
    status = clSetKernelArg(kernel_pop_merge, 6, sizeof(cl_mem), &buf_children2);
    //fitness 传参
    status = clSetKernelArg(kernel_fitness, 0, sizeof(int), &pop_num);
    status = clSetKernelArg(kernel_fitness, 1, sizeof(int), &code_len);
    status = clSetKernelArg(kernel_fitness, 2, sizeof(cl_mem), &buf_best_p);
    status = clSetKernelArg(kernel_fitness, 3, sizeof(cl_mem), &buf_population_extend); 
    status = clSetKernelArg(kernel_fitness, 4, sizeof(cl_mem), &buf_fitness);
    //pop_produce 传参
    status = clSetKernelArg(kernel_pop_produce, 0, sizeof(int), &pop_num);
    status = clSetKernelArg(kernel_pop_produce, 1, sizeof(int), &code_len);
    status = clSetKernelArg(kernel_pop_produce, 2, sizeof(cl_mem), &buf_fitness_sorted);
    status = clSetKernelArg(kernel_pop_produce, 3, sizeof(cl_mem), &buf_population_extend); 
    status = clSetKernelArg(kernel_pop_produce, 4, sizeof(cl_mem), &buf_population);
    status = clSetKernelArg(kernel_pop_produce, 5, sizeof(cl_mem), &buf_fitness);
    // 执行crossover核函数                                 workdim
	status = clEnqueueNDRangeKernel(cmdQueue, kernel_crossover, 1, NULL,globalWorkSize, NULL, 0, NULL, NULL);
    mut_vec(mutation_vector,code_len,pm); //生成变异向量
	status = clEnqueueWriteBuffer(cmdQueue, buf_mutation_vector, CL_FALSE,0, sizeof(int)*code_len, mutation_vector, 0, NULL, NULL);    //mutation vector 放入gpu
    // 执行crossover核函数                                 workdim
	status = clEnqueueNDRangeKernel(cmdQueue, kernel_mutation, 1, NULL,globalWorkSize, NULL, 0, NULL, NULL);
    // 执行pop_merge 核函数                                workdim
	status = clEnqueueNDRangeKernel(cmdQueue, kernel_pop_merge, 1, NULL,globalWorkSize, NULL, 0, NULL, NULL);
    // 执行fitness 核函数                                workdim
	status = clEnqueueNDRangeKernel(cmdQueue, kernel_fitness, 1, NULL,globalWorkSize, NULL, 0, NULL, NULL);
	clEnqueueReadBuffer(cmdQueue, buf_fitness, CL_TRUE, 0,sizeof(int)*pop_num*2, fitness, 0, NULL, NULL);    //fitness读出
    for(int i=0;i<pop_num*2;i++)
        fitness_sorted[i]=fitness[i];
    //适应度值排序
    bubblesort(fitness_sorted,pop_num*2);
    //将fitness sorted传入gpu
	status = clEnqueueWriteBuffer(cmdQueue, buf_fitness_sorted, CL_FALSE,0, sizeof(int)*pop_num*2, fitness_sorted, 0, NULL, NULL);
    // 执行 pop_produce 核函数                                workdim
	status = clEnqueueNDRangeKernel(cmdQueue, kernel_pop_produce, 1, NULL,globalWorkSize, NULL, 0, NULL, NULL);
    //读取新产生的pop
	clEnqueueReadBuffer(cmdQueue, buf_population, CL_TRUE, 0,sizeof(int)*size_pop, population, 0, NULL, NULL);
//     show_matrix(population,pop_num,code_len,"population new");
    
/************************************遗传优化********************************************/
    time_t start,end;
    start=time(NULL);
    float best_fit;
    float origin_best_fit;
    for(int g=0;g<evol_num;g++)
    {
        /***************crossover*****************/
        get_cross_rates(cross_rates,code_len);//随机初始化交叉向量
        //show_matrix_float(cross_rates,1,code_len,"cross Rates");
        status = clEnqueueWriteBuffer(cmdQueue, buf_cross_rates, CL_FALSE,0, sizeof(int)*code_len, cross_rates, 0, NULL, NULL); //将crossrates放入gpu
        status = clEnqueueNDRangeKernel(cmdQueue, kernel_crossover, 1, NULL,globalWorkSize, NULL, 0, NULL, NULL);
        mut_vec(mutation_vector,code_len,pm); //生成变异向量
        status = clEnqueueWriteBuffer(cmdQueue, buf_mutation_vector, CL_FALSE,0, sizeof(int)*code_len, mutation_vector, 0, NULL, NULL);    //mutation vector 放入gpu
        /***************mutation*****************/                                       
        status = clEnqueueNDRangeKernel(cmdQueue, kernel_mutation, 1, NULL,globalWorkSize, NULL, 0, NULL, NULL);
        /***************population merge*****************/                                     
        status = clEnqueueNDRangeKernel(cmdQueue, kernel_pop_merge, 1, NULL,globalWorkSize, NULL, 0, NULL, NULL);
        /**********************fitness*****************/                                 
        status = clEnqueueNDRangeKernel(cmdQueue, kernel_fitness, 1, NULL,globalWorkSize, NULL, 0, NULL, NULL);
        clEnqueueReadBuffer(cmdQueue, buf_fitness, CL_TRUE, 0,sizeof(int)*pop_num*2, fitness, 0, NULL, NULL);    //fitness读出
        if(g==0)
            origin_best_fit=max(fitness,pop_num*2);
        for(int i=0;i<pop_num*2;i++)
            fitness_sorted[i]=fitness[i];
        //适应度值排序
        bubblesort(fitness_sorted,pop_num*2);
        //将fitness sorted传入gpu
        status = clEnqueueWriteBuffer(cmdQueue, buf_fitness_sorted, CL_FALSE,0, sizeof(int)*pop_num*2, fitness_sorted, 0, NULL, NULL);
        /***************population produce*****************/        
        status = clEnqueueNDRangeKernel(cmdQueue, kernel_pop_produce, 1, NULL,globalWorkSize, NULL, 0, NULL, NULL);
        //读取新产生的pop
        clEnqueueReadBuffer(cmdQueue, buf_population, CL_TRUE, 0,sizeof(int)*size_pop, population, 0, NULL, NULL);
        /*******************优化过程监视**************/
        clEnqueueReadBuffer(cmdQueue, buf_fitness, CL_TRUE, 0,sizeof(int)*pop_num*2, fitness, 0, NULL, NULL); 
        bubblesort(fitness,pop_num*2);
         //show_matrix_float(fitness,pop_num*2,1,"fitness");
        best_fit=max(fitness,pop_num*2);
        printf("%d generation:best fitness:%f\n",g,best_fit);
        if(best_fit==4)
            break;
    }
    end=time(NULL);
    printf("time cost:%ld  s\n",end-start);
    // 读取结果,从OpenCL buffer读入主机端内存中
	// Read the device output buffer to the host output array
	clEnqueueReadBuffer(cmdQueue, buf_children1, CL_TRUE, 0,
		sizeof(int)*size_pop/2, children1, 0, NULL, NULL);
	clEnqueueReadBuffer(cmdQueue, buf_children2, CL_TRUE, 0,
		sizeof(int)*size_pop/2, children2, 0, NULL, NULL);
	clEnqueueReadBuffer(cmdQueue, buf_population_extend, CL_TRUE, 0,
		sizeof(int)*size_pop*2, population_extend, 0, NULL, NULL);

	int result = 1;
    int* best_one=get_best_individual(population,pop_num,code_len,fitness,best_fit);
//     show_matrix(best_one,1,code_len,"best one searched");
//     show_matrix(best_p,1,code_len,"global best");
    printf("The best fitness of original population:%f\n",origin_best_fit);
    printf("The best fitness of final population:%f\n",best_fit);
    

	// Free OpenCL resources
	clReleaseKernel(kernel_crossover);
	clReleaseProgram(program_crossover);
	clReleaseCommandQueue(cmdQueue);
	clReleaseMemObject(buf_population);
	clReleaseMemObject(buf_population_extend);
	clReleaseMemObject(buf_children1);
	clReleaseMemObject(buf_children2);
    clReleaseMemObject(buf_best_p);
	clReleaseContext(context);
                   
	// Free host resources
	free(population);
	free(population_extend);
	free(children1);
	free(children2);
	free(cross_rates);
	free(best_p);
	free(platforms);
	free(devices);

	return 0;
}

int convertToString(const char *filename, std::string& s)
{// s is the string to store the kernel function
    size_t size;
    char* str;
    std::fstream f(filename, (std::fstream::in | std::fstream::binary));
    if (f.is_open())
    {
        size_t fileSize;
        f.seekg(0, std::fstream::end);
        size = fileSize = (size_t)f.tellg();
        f.seekg(0, std::fstream::beg);
        str = new char[size + 1];
        if (!str)
        {
            f.close();
            return 0;
        }
        f.read(str, fileSize);
        f.close();
        str[size] = '\0';
        s = str;
        delete[] str;
        return 0;
    }
    printf("Error: Failed to open file %s\n", filename);
    return 1;
}
void show_matrix(int* m,int dim1,int dim2,char* matrix_name)
{
    printf("matrix name is :%s \n",matrix_name);
    for (int i = 0; i < dim1; i++) {
        for (int j=0;j< dim2;j++)
            printf("%d ",m[i*dim2+j]);
        printf("\n");
	}
}
void show_matrix_float(float* m,int dim1,int dim2,char* matrix_name)
{
    printf("matrix name is :%s \n",matrix_name);
    for (int i = 0; i < dim1; i++) {
        for (int j=0;j< dim2;j++)
            printf("%.3f ",m[i*dim2+j]);
        printf("\n");
	}
}

交叉操作核函数

__kernel void crossover(
    const int p_num,
    const int individual_len,
    const float pc,
    __global float* cross_rates,
    __global int* p1s,
    __global int* p2s, 
    __global int* children1,
    __global int* children2)
{
    int i = get_global_id(0);
    int k;
    
    if (i < p_num) {
        for(int j=0;j<individual_len;j++)
        {
            if(cross_rates[j]>pc)//概率大于pc则获取父代1的基因
            {
                children1[i*individual_len+j]=p1s[i*individual_len+j];
                children2[i*individual_len+j]=p2s[i*individual_len+j];
            }
            else
            {
                children1[i*individual_len+j]=p2s[i*individual_len+j];
                children2[i*individual_len+j]=p1s[i*individual_len+j];
            }
        }
    }
}

变异操作核函数

__kernel void mutation(
            const int pop_num,    
            const int code_len,
            __global int* mutation_vector,
            __global int* population_extend)
{
    int i = get_global_id(0);
    if(i<pop_num*2)
    {
        for(int j=0;j<code_len;j++)
        {
            population_extend[i*code_len+j]+=mutation_vector[j];
        }
        
    }
}

选择操作核函数

__kernel void pop_produce(
    const int pop_num,
    const int code_len,
    __global float* fitness_sorted,
    __global int* population_extend,
    __global int* population,
    __global float* fitness)
{
    int i = get_global_id(0);
    if (i < pop_num) 
    {
       for(int j=0;j<pop_num*2;j++)
        {
            if(fitness_sorted[i]==fitness[j])
            {
                for(int k=0;k<code_len;k++)
                {
                    population[i*code_len+k]=population_extend[j*code_len+k];
                }
            }
        }
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值