资源简介
遗传算法完整代码,针对多元函数求最优解,C++
代码片段和文件信息
#include
#include
#include
#include
//变量定义
#define POPSIZE 500
#define maximization 1
#define minimization 2
#define cmax 100
#define cmin 0
#define length1 10
#define length2 10
#define chromlength length1+length2 //染色体长度
int functionmode=maximization;
int popsize; //种群大小
int maxgeneration; //最大世代数
double pc; //交叉率
double pm; //变异率
struct individual {
char chrom[chromlength+1];
double value;
double fitness; //适应度
};
int generation; //世代数
int best_index;
int worst_index;
struct individual bestindividual; //最佳个体
struct individual worstindividual; //最差个体
struct individual currentbest;
struct individual population[POPSIZE];
//函数声明
void generateinitialpopulation();
void generatenextpopulation();
void evaluatepopulation();
long decodechromosome(char *intint);
void calculateobjectvalue();
void calculatefitnessvalue();
void findbestandworstindividual();
void performevolution();
void selectoperator();
void crossoveroperator();
void mutationoperator();
void input();
void outputtextreport();
void generateinitialpopulation( ) //种群初始化
{
int ij;
for (i=0;i for(j=0;j population[i].chrom[j]=(rand()%10<5)?‘0‘:‘1‘;
}
population[i].chrom[chromlength]=‘\0‘;
}
}
void generatenextpopulation() //生成下一代
{
selectoperator();
crossoveroperator();
mutationoperator();
}
void evaluatepopulation() //评价个体,求最佳个体
{
calculateobjectvalue();
calculatefitnessvalue();
findbestandworstindividual();
}
long decodechromosome(char *string int pointint length) //给染色体解码
{
int i;
long decimal=0;
char*pointer;
for(i=0pointer=string+point;i {
if(*pointer-‘0‘)
decimal +=(long)pow(2i);
}
return (decimal);
}
void calculateobjectvalue() //计算函数值
{
int i;
long temp1temp2;
double x1x2;
for (i=0; i temp1=decodechromosome(population[i].chrom0length1);
temp2=decodechromosome(population[i].chromlength1length2);
x1=4.096*temp1/1023.0-2.048;
x2=4.096*temp2/1023.0-2.048;
population[i].value=100*(x1*x1-x2)* (x1*x1-x2)+(1-x1)*(1-x1);
}
}
void calculatefitnessvalue()//计算适应度
{
int i;
double temp;
for(i=0;i {
if(functionmode==maximization){
if((population[i].value+cmin)>0.0) {
temp=cmin+population[i].value;
}
else {
temp=0.0;
}
}
else if (functionmode==minimization){
if(population[i].value temp=cmax-population[i].value;}
else{ temp=0.0;}
}
population[i].fitness=temp;
}
}
void findbestandworstindividual( ) //求最佳个体和最差个体
{
int i;
double sum=0.0;
bestindividual=population[0];
worstindividual=population[0];
for (i=1;i if (population[i].fitness>bestindividual.fitness){
bestindividual=population[i]
相关资源
- C++中头文件与源文件的作用详解
- C++多线程网络编程Socket
- VC++ 多线程文件读写操作
- 利用C++哈希表的方法实现电话号码查
- 移木块游戏,可以自编自玩,vc6.0编写
- C++纯文字DOS超小RPG游戏
- VC++MFC小游戏实例教程(实例)+MFC类库
- 连铸温度场计算程序(C++)
- 6自由度机器人运动学正反解C++程序
- Em算法(使用C++编写)
- libstdc++-4.4.7-4.el6.i686.rpm
- VC++实现CMD命令执行与获得返回信息
- 白话C++(全)
- C++标准库第1、2
- 大数类c++大数类
- C++语言编写串口调试助手
- c++素数筛选法
- C++ mqtt 用法
- 商品库存管理系统 C++ MFC
- c++ 多功能计算器
- C++17 In Detail
- 嵌入式QtC++编程课件
- 颜色识别形状识别STM103嵌入式代码
- c++ 邮件多附件群发
- c++ 透明代理(hookproxy)
- mfc 调用redis
- FTP客户端源码(c++)
- c++ 画图(14Qt-XPS)
- c++多边形交并差运算
- VC++基于OpenGL模拟的一个3维空间模型
评论
共有 条评论