题目:利用遗传算法求解区间[0,31]上的二次函数y=x*x的最大值。
//题目: 利用遗传算法求解区间[0, 31]上的二次函数y = x*x的最大值
#include
#include
#include
#include
using namespace std;
int N = 4;//种群规模
int T = 100;//最大换代数
double Pc = 0.4;//交叉率
double Pm = 0.01;//变异率
int option = 0;
int t;//代数计数器
bool flag = true;//标记是否是第一次计算结果
struct solution{
int num; //十进制数
char b[5]; //5位二进制数编码染色体
int f; //适应度
double p; //选择概率
double q; //积累概率
}*s, *next;
void showCondition(){
cout << "种群规模: " << N << endl;
cout << "最大换代数: " << T << endl;
cout << "交叉率: " << Pc << endl;
cout << "变异率: " << Pm << endl << endl;
if(!flag)
system("pause");
}
//交换字符
void swap(char *a, char *b){
char c = *a;
*a = *b;
*b = c;
}
//十进制转换为二进制
void DtoB(int n, char str[]){
int tmp, i = 0;
memset(str, '0', 5);
while(n){
tmp = n % 2;
str[i] = (char)tmp;
n /= 2;
i++;
}
//翻转
swap(&str[0], &str[4]);
swap(&str[1], &str[3]);
}
//生成初始种群
void init(){
t = 1;
srand((unsigned)time(0));
if(flag){
s = new struct solution[N];
next = new struct solution[N];
flag = false;
}
&

该博客探讨了如何运用遗传算法解决区间[0,31]上二次函数y=x*x的最大值寻找问题,展示了遗传算法在优化搜索中的应用。"
127121716,2078949,PPP-RTK技术详解:高精度电离层建模与应用,"['高精度定位', 'PPP', 'RTK', '电离层延迟', 'GNSS']
最低0.47元/天 解锁文章
3834

被折叠的 条评论
为什么被折叠?



