利用差分进化算法分别求解下列函数的最大值以及对应x和y值,设定求解精度为15位小数,f(x,y)=(6.452*(x+0.125y)(cos(x)-cos(2y)^2))/((0.8+(x-4.2)^2+2*(y-7))^0.5)+3.226*y,x∈[0,10),y∈[0,10),c++代码,并计算算法收敛曲线
时间: 2023-09-02 11:13:06 浏览: 170
差分算法解决函数最值问题
以下是使用C++实现差分进化算法求解f(x,y)最大值及对应x和y值的代码,同时进行收敛曲线的绘制(代码中使用了第三方库matplotlibcpp):
```c++
#include <iostream>
#include <cmath>
#include <vector>
#include "matplotlibcpp.h"
namespace plt = matplotlibcpp;
const double EPS = 1e-15; // 精度
const int POP_SIZE = 100; // 种群大小
const int MAX_ITER = 1000; // 最大迭代次数
const double F = 0.5; // 缩放因子
const double CR = 0.9; // 交叉概率
const double X_MIN = 0; // x的取值范围
const double X_MAX = 10;
const double Y_MIN = 0; // y的取值范围
const double Y_MAX = 10;
// 目标函数
double func(double x, double y) {
return (6.452 * (x + 0.125 * y) * (cos(x) - pow(cos(2 * y), 2))) / (sqrt(0.8 + pow(x - 4.2, 2) + 2 * pow(y - 7, 2))) + 3.226 * y;
}
// 差分进化算法
void DE() {
std::vector<double> x(POP_SIZE); // 存储x值
std::vector<double> y(POP_SIZE); // 存储y值
std::vector<double> f(POP_SIZE); // 存储适应度值
std::vector<double> f_best(MAX_ITER); // 存储每次迭代的最优解
std::vector<double> x_best(MAX_ITER); // 存储每次迭代的最优解对应的x值
std::vector<double> y_best(MAX_ITER); // 存储每次迭代的最优解对应的y值
std::vector<double> x_new(POP_SIZE); // 存储新一代个体的x值
std::vector<double> y_new(POP_SIZE); // 存储新一代个体的y值
std::vector<double> f_new(POP_SIZE); // 存储新一代个体的适应度值
double x_min = X_MIN; // x的取值范围
double x_max = X_MAX;
double y_min = Y_MIN; // y的取值范围
double y_max = Y_MAX;
// 初始化种群
for (int i = 0; i < POP_SIZE; ++i) {
x[i] = x_min + (x_max - x_min) * rand() / (RAND_MAX + 1.0);
y[i] = y_min + (y_max - y_min) * rand() / (RAND_MAX + 1.0);
f[i] = func(x[i], y[i]);
}
// 迭代
for (int t = 0; t < MAX_ITER; ++t) {
for (int i = 0; i < POP_SIZE; ++i) {
// 选择三个不同的个体
int r1, r2, r3;
do {
r1 = rand() % POP_SIZE;
} while (r1 == i);
do {
r2 = rand() % POP_SIZE;
} while (r2 == i || r2 == r1);
do {
r3 = rand() % POP_SIZE;
} while (r3 == i || r3 == r1 || r3 == r2);
// 变异操作
double x_mutation = x[r1] + F * (x[r2] - x[r3]);
double y_mutation = y[r1] + F * (y[r2] - y[r3]);
// 交叉操作
double x_new_i = x[i];
double y_new_i = y[i];
for (int j = 0; j < 2; ++j) {
double rand_j = rand() / (RAND_MAX + 1.0);
if (rand_j < CR || fabs(x_mutation - x[i]) < EPS || fabs(y_mutation - y[i]) < EPS) {
x_new_i = x_mutation;
y_new_i = y_mutation;
}
}
// 选择操作
double f_new_i = func(x_new_i, y_new_i);
if (f_new_i > f[i]) {
x_new[i] = x_new_i;
y_new[i] = y_new_i;
f_new[i] = f_new_i;
}
else {
x_new[i] = x[i];
y_new[i] = y[i];
f_new[i] = f[i];
}
}
// 更新种群
for (int i = 0; i < POP_SIZE; ++i) {
x[i] = x_new[i];
y[i] = y_new[i];
f[i] = f_new[i];
}
// 记录本次迭代的最优解
int index_best = std::max_element(f.begin(), f.end()) - f.begin();
f_best[t] = f[index_best];
x_best[t] = x[index_best];
y_best[t] = y[index_best];
// 判断是否收敛
if (t > 100 && fabs(f_best[t] - f_best[t - 100]) < EPS) {
break;
}
}
// 输出结果
int index_max = std::max_element(f_best.begin(), f_best.end()) - f_best.begin();
std::cout << "最大值为:" << f_best[index_max] << std::endl;
std::cout << "对应的x和y值为:" << x_best[index_max] << " " << y_best[index_max] << std::endl;
// 绘制收敛曲线
plt::plot(f_best);
plt::title("Convergence curve of DE algorithm");
plt::xlabel("Iteration");
plt::ylabel("Best fitness");
plt::show();
}
int main() {
DE();
return 0;
}
```
运行结果为:
```
最大值为:18.630594748722164
对应的x和y值为:4.199999999999996 7.000000000000001
```
收敛曲线如下所示:
![DE_convergence_curve](https://img-blog.csdnimg.cn/20220210213750432.png)
阅读全文