差分进化算法球一个函数的最小值解c语言,差分进化算法(DE)求函数最小值

本文介绍使用差分进化算法求解特定函数Z=3*cos(X.*Y)+X+Y的最小值过程。该算法包括初始化种群、变异、交叉、约束边界及选择等步骤,并通过Matlab实现。最终找到函数的近似最小值及其对应的X和Y值。

差分进化算法求函数 Z = 3 * cos(X .* Y) + X + Y , -4 <= X <= 4, -4 <= Y <= 4。

0ed2a6b13af6

函数图片

计算目标函数值

计算目标函数值的函数:

function z = calobj (pop)

% 计算目标函数值

% pop input 种群

% z output 目标函数值

z = 3 * cos(pop(:,1) .* pop(:,2)) + pop(:,1) + pop(:,2);

end

初始化种群

目标函数有两个参数,生成每个个体有两个基因的种群:

function pop = initpop(popsize, chromlength, xl, xu)

% 生成初始种群

% popsize input 种群规模

% chromlengt input 染色体长度

% xl input x下限

% xu input x上限

% pop output 种群

pop = rand(popsize, chromlength) * (xu - xl) + xl;

end

变异

变异函数如下:

function mutationpop = mutation (pop, F)

% 变异操作

% pop input 种群

% F input 缩放因子

% mutationpop output 变异后种群

[popsize, chromlength] = size(pop);

mutationpop = zeros(popsize, chromlength);

for i = 1:popsize

% 取3个互异的索引 r0 r1 r2

r = randperm(popsize);

index = find (r ~= i);

rn = r(index(1:3));

r0 = rn(1); r1 = rn(2); r2 = rn(3);

% fprintf('i = %d, r0 = %d, r1 = %d, r2 = %d\n', i, r0, r1, r2);

mutationpop(i,:) = pop(r0,:) + F .* (pop(r1,:) - pop(r2,:));

end

end

交叉

交叉函数如下:

function crossoverpop = crossover(pop, mpop, cr)

% 交叉

% pop input 种群

% mpop input 变异后的种群

% cr input 交叉概率

% crossoverpop output 交叉后的种群

[popsize, chromlength] = size(pop);

crossoverpop = mpop;

r = rand(popsize, chromlength);

index = find (r > cr);

crossoverpop(index) = pop(index);

jrand = randi(chromlength, 1, popsize);

crossoverpop(sub2ind(size(crossoverpop), [1:popsize], jrand)) ...

= mpop(sub2ind(size(mpop), [1:popsize], jrand));

end

在交叉操作之后,应约束边界:

function newpop = constrictboundary(pop, xl, xu)

% 约束边界(边界吸收)

% pop input 种群

% xl input 自变量最小值(包含)

% xu input 自变量最大值(包含)

% newpop output 约束边界后的种群

newpop = pop;

newpop(newpop < xl) = xl;

newpop(newpop > xu) = xu;

end

选择

function newpop = selection(pop, npop)

% 选择(小值优化)

% pop input 种群1(原始种群)

% pop input 种群2(变异-交叉种群)

% newpop output 选择后的种群

newpop = pop;

index = find(calobj(npop) <= calobj(pop));

newpop(index, :) = npop(index, :);

end

主程序

主程序如下:

clc;

clear;

NP = 20; % 种群规模

D = 2; % 参数个数

G = 30; % 最大进化代数

F = 0.5; % 缩放因子

Cr = 0.8; % 交叉因子

xl = -4; % x下限(也是y下限)

xu = 4; % x上限(也是y上限)

bestvalue = zeros(3, G);

% 优化

gen = 0;

pop = initpop(NP, D, xl, xu);

objvalue = calobj(pop);

while gen < G

mpop = mutation(pop, F); % 变异

cpop = crossover(pop, mpop, Cr); % 交叉

cpop = constrictboundary(cpop, xl, xu); % 约束边界

pop = selection(pop, cpop); % 选择

objvalue = calobj(pop);

gen = gen + 1;

% 记录最优

[~, index] = min(objvalue);

bestvalue(1:2, gen) = pop(index,:)';

bestvalue(3,gen) = objvalue(index);

end

fprintf('bestX = %f, bestY = %f, bestZ = %f\n', ...

bestvalue(1,end), bestvalue(2,end), bestvalue(3,end));

% 绘图

figure(1);

x = [-4:0.1:4]; y = [-4:0.1:4];

[X, Y] = meshgrid(x, y);

Z = 3 * cos(X .* Y) + X + Y;

surf(X, Y, Z);

hold on;

scatter3(bestvalue(1,:), bestvalue(2,:), bestvalue(3,:), ...

'MarkerEdgeColor','k', 'MarkerFaceColor',[0 .75 .75]);

xlabel('x'); ylabel('y'); zlabel('z'); title('函数图');

hold off;

figure(2);

plot(bestvalue(3,:));

xlabel('进化代数'); ylabel('最优目标函数值'); title('目标函数值变化图');

执行结果

bestX = -3.947841, bestY = -4.000000, bestZ = -10.937414

0ed2a6b13af6

最优点图

0ed2a6b13af6

目标值变化图

以下是使用C语言实现差分进化算法DE)求Rastrigin函数最小值的代码: ```c #include <stdio.h> #include <stdlib.h> #include <math.h> #include <time.h> #define POP_SIZE 50 #define DIM 2 #define MAX_GENERATIONS 1000 #define F 0.5 #define CR 0.9 // 定义Rastrigin函数 double rastrigin(double *x) { double A = 10.0; double sum = A * DIM; for (int i = 0; i < DIM; i++) { sum += x[i] * x[i] - A * cos(2 * M_PI * x[i]); } return sum; } // 初始化种群 void initialize_population(double population[POP_SIZE][DIM], double bounds[DIM][2]) { for (int i = 0; i < POP_SIZE; i++) { for (int j = 0; j < DIM; j++) { population[i][j] = bounds[j][0] + (double)rand() / RAND_MAX * (bounds[j][1] - bounds[j][0]); } } } // 变异操作 void mutation(double population[POP_SIZE][DIM], double mutant[DIM], int current_index, double bounds[DIM][2]) { int r1, r2, r3; do { r1 = rand() % POP_SIZE; } while (r1 == current_index); do { r2 = rand() % POP_SIZE; } while (r2 == current_index || r2 == r1); do { r3 = rand() % POP_SIZE; } while (r3 == current_index || r3 == r1 || r3 == r2); for (int i = 0; i < DIM; i++) { mutant[i] = population[r1][i] + F * (population[r2][i] - population[r3][i]); // 边界处理 if (mutant[i] < bounds[i][0]) { mutant[i] = bounds[i][0]; } else if (mutant[i] > bounds[i][1]) { mutant[i] = bounds[i][1]; } } } // 交叉操作 void crossover(double population[POP_SIZE][DIM], double mutant[DIM], double trial[DIM], int current_index) { int jrand = rand() % DIM; for (int j = 0; j < DIM; j++) { if ((double)rand() / RAND_MAX < CR || j == jrand) { trial[j] = mutant[j]; } else { trial[j] = population[current_index][j]; } } } // 选择操作 void selection(double population[POP_SIZE][DIM], double trial[DIM], int current_index) { double f_current = rastrigin(population[current_index]); double f_trial = rastrigin(trial); if (f_trial < f_current) { for (int i = 0; i < DIM; i++) { population[current_index][i] = trial[i]; } } } int main() { srand(time(NULL)); double population[POP_SIZE][DIM]; double bounds[DIM][2] = {{-5.12, 5.12}, {-5.12, 5.12}}; double mutant[DIM]; double trial[DIM]; // 初始化种群 initialize_population(population, bounds); // 迭代 for (int gen = 0; gen < MAX_GENERATIONS; gen++) { for (int i = 0; i < POP_SIZE; i++) { // 变异 mutation(population, mutant, i, bounds); // 交叉 crossover(population, mutant, trial, i); // 选择 selection(population, trial, i); } } // 找到最优 double best_fitness = rastrigin(population[0]); int best_index = 0; for (int i = 1; i < POP_SIZE; i++) { double fitness = rastrigin(population[i]); if (fitness < best_fitness) { best_fitness = fitness; best_index = i; } } // 输出结果 printf("最优: "); for (int i = 0; i < DIM; i++) { printf("%f ", population[best_index][i]); } printf("\n最优值: %f\n", best_fitness); return 0; } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值