没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论












C++ 遗传算法源程序
// CMVSOGA.h : main header le for the CMVSOGA.cpp
////////////////////////////////////////////////////////////////////
///// /////
///// 沈阳航空工业学院 动力工程系 /////
///// 作者:李立新 /////
///// 完成日期:2006.08.02 /////
/////修改日期:2007.04.10 /////
////////////////////////////////////////////////////////////////////
#if !
dened(AFX_CMVSOGA_H__45BECA_61EB_4A0E_9746_9A94D1CCF767__INCL
UDED_)
#dene
AFX_CMVSOGA_H__45BECA_61EB_4A0E_9746_9A94D1CCF767__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "Afxtempl.h"
#dene variablenum 14
class CMVSOGA
{
public:
CMVSOGA();
~CMVSOGA();
void selectionoperator();
void crossoveroperator();
void mutationoperator();
void initialpopulation(int, int ,double ,double,double *,double *);//种群
初始化
void generatenextpopulation();//生成下一代种群
void evaluatepopulation();//评价个体,求最佳个体
void calculateobjectvalue();//计算目标函数值
void calculatetnessvalue();//计算适应度函数值
void ndbestandworstindividual();//寻找最佳个体和最差个体
void performevolution();
void GetResult(double *);
void GetPopData(CList <double,double>&);
void SetFitnessData(CList <double,double>&,CList <double,double>&,CList
<double,double>&);
private:

struct individual
{
double chromosome[variablenum];//染色体编码长度应该为变量的个数
double value;
double tness;//适应度
};
double variabletop[variablenum];//变量值
double variablebottom[variablenum];//变量值
int popsize;//种群大小
//int generation;//世代数
int best_index;
int worst_index;
double crossoverrate;//交叉率
double mutationrate;//变异率
int maxgeneration;//最大世代数
struct individual bestindividual;//最佳个体
struct individual worstindividual;//最差个体
struct individual current; //当前个体
struct individual current1; //当前个体
struct individual currentbest;//当前最佳个体
CList <struct individual,struct individual &> population;//种群
CList <struct individual,struct individual &> newpopulation;//新种群
CList <double,double> ctness;//存储适应度值
//怎样使链表的数据是一个结构体????主要是想把种群作成链表。节省空间。
};
#endif
执行文件:
// CMVSOGA.cpp : implementation le
//
#include "stdafx.h"
//#include "vld.h"
#include "CMVSOGA.h"
#include "math.h"
#include "stdlib.h"
#ifdef _DEBUG
#dene new DEBUG_NEW
#undef THIS_FILE

static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMVSOGA.cpp
CMVSOGA::CMVSOGA()
{
best_index=0;
worst_index=0;
crossoverrate=0;//交叉率
mutationrate=0;//变异率
maxgeneration=0;
}
CMVSOGA::~CMVSOGA()
{
best_index=0;
worst_index=0;
crossoverrate=0;//交叉率
mutationrate=0;//变异率
maxgeneration=0;
population.RemoveAll();//种群
newpopulation.RemoveAll();//新种群
ctness.RemoveAll();
}
void CMVSOGA::initialpopulation(int ps, int gen ,double cr ,double mr,double
*xtop,double *xbottom)//第一步,初始化。
{
//应该采用一定的策略来保证遗传算法的初始化合理,采用产生正态分布随机数初始化?
选定中心点为多少?
int i,j;
popsize=ps;
maxgeneration=gen;
crossoverrate=cr;
mutationrate =mr;
for (i=0;i<variablenum;i++)
{
variabletop[i] =xtop[i];
variablebottom[i] =xbottom[i];
}
//srand( (unsigned)time( NULL ) );//寻找一个真正的随机数生成函数。
for(i=0;i<popsize;i++)
{
for (j=0;j<variablenum ;j++)
{
current.chromosome[j]=double(rand()%10000)/10000*(variabletop[j]-

variablebottom[j])+variablebottom[j];
}
current.tness=0;
current.value=0;
population.InsertAfter(population.FindIndex(i),current);//除了初始化使用
insertafter 外,其他的用 setat 命令。
}
}
void CMVSOGA::generatenextpopulation()//第三步,生成下一代。
{
//srand( (unsigned)time( NULL ) );
selectionoperator();
crossoveroperator();
mutationoperator();
}
//void CMVSOGA::evaluatepopulation() //第二步,评价个体,求最佳个体
//{
//calculateobjectvalue();
//calculatetnessvalue();//在此步中因该按适应度值进行排序.链表的排序.
//ndbestandworstindividual();
//}
void CMVSOGA:: calculateobjectvalue() //计算函数值,应该由外部函数实现。主要
因为目标函数很复杂。
{
int i,j;
double x[variablenum];
for (i=0; i<popsize; i++)
{
current=population.GetAt(population.FindIndex(i));
current.value=0;
//使用外部函数进行,在此只做结果的传递。
for (j=0;j<variablenum;j++)
{
x[j]=current.chromosome[j];
current.value=current.value+(j+1)*pow(x[j],4);
}
////使用外部函数进行,在此只做结果的传递。
population.SetAt(population.FindIndex(i),current);
}
}
void CMVSOGA::mutationoperator()//对于浮点数编码,变异算子的选择具有决定意
义。
//需要 guass 正态分布函数,生成方差为 sigma,均值为浮点数编码值 c。
剩余18页未读,继续阅读

坪上先生
- 粉丝: 0
- 资源: 4
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


安全验证
文档复制为VIP权益,开通VIP直接复制

- 1
- 2
前往页