• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C语言 > C++实现遗传算法

C++实现遗传算法

作者:pass86 字体:[增加 减小] 来源:互联网 时间:2017-05-28

pass86 通过本文主要向大家介绍了遗传算法c++程序,遗传算法c++代码,rsa加密算法c++实现,rsa算法c++实现源码,apriori算法c++实现等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例讲述了C++实现简单遗传算法。分享给大家供大家参考。具体实现方法如下:

// CMVSOGA.h : main header file for the CMVSOGA.cpp
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
 
#if !defined(AFX_CMVSOGA_H__45BECA_61EB_4A0E_9746_9A94D1CCF767__INCLUDED_)
#define AFX_CMVSOGA_H__45BECA_61EB_4A0E_9746_9A94D1CCF767__INCLUDED_
 
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "Afxtempl.h"
#define 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 calculatefitnessvalue();     //计算适应度函数值
 void findbestandworstindividual();     //寻找最佳个体和最差个体
 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 fitness;       //适应度
 };
 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> cfitness;     //存储适应度值
 //怎样使链表的数据是一个结构体????主要是想把种群作成链表。节省空间。
};
#endif
 
 
 
执行文件:
 
// CMVSOGA.cpp : implementation file
//
 
#include "stdafx.h"
//#include "vld.h"
#include "CMVSOGA.h"
#include "math.h"
#include "stdlib.h"
 
 
#ifdef _DEBUG
#define 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(); //新种群
 cfitness.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.fitness=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();
// calculatefitnessvalue();  //在此步中因该按适应度值进行排序.链表的排序.
// findbestandworstindividual();
//}
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。
{
// srand((unsigned int) time (NULL));
 int i,j;
 double r1,r2,p,sigma;//sigma高斯变异参数
 
 for (i=0;i<popsize;i++)
 {
 current=population.GetAt(population.FindIndex(i));
 
 //生成均值为current.chromosome,方差为sigma的高斯分布数
 for(j=0; j<variablenum; j++)
 {  
  r1 = double(rand()%10001)/10000;
  r2 = double(rand()%10001)/10000;
  p = double(rand()%10000)/10000;
  if(p<mutationrate)
  {
  double sign;
  sign=rand()%2;
  sigma=0.01*(variabletop[j]-variablebottom [j]);
  //高斯变异
  if(sign)
  {
   current.chromosome[j] = (current.chromosome[j] 
   + sigma*sqrt(-2*log(r1)/0.4323)*sin(2*3.1415926*r2));
  }
  else
  {
   current.chromosome[j] = (current.chromosome[j] 
   - sigma*sqrt(-2*log(r1)/0.4323)*sin(2*3.1415926*r2));
  }
  if (current.chromosome[j]>variabletop[j])
  {
   current.chromosome[j]=double(rand()%10000)/10000*(variabletop[j]-variablebottom[j])+variablebottom[j];
  }
  if (current.chromosome[j]<variablebottom [j])
  {
   current.chromosome[j]=double(rand()%10000)/10000*(variabletop[j]-variablebottom[j])+variablebottom[j];
  }
  }
 }
 population.SetAt(population.FindIndex(i),current);
 }
}
void CMVSOGA::selectionoperator()  //从当前个体中按概率选择新种群,应该加一个复制选择,提高种群的平均适应度
{
 int i,j,pindex=0;
 double p,pc,sum;
 i=0;
 j=0;
 pindex=0;
 p=0;
 pc=0;
 sum=0.001;
 newpopulation.RemoveAll();
 cfitness.RemoveAll();
 //链表排序
// population.SetAt (population.FindIndex(0),current); //多余代码
 for (i=1;i<popsize;i++)
 { 
 current=population.GetAt(population.FindIndex(i));
 for(j=0;j<i;j++)  //从小到大用before排列。
 {
  current1=population.GetAt(population.FindIndex(j));//临时借用变量
  if(current.fitness<=current1.fitness) 
  {
  population.InsertBefore(population.FindIndex(j),current);
  population.RemoveAt(population.FindIndex(i+1));
  break;
  }
 }
// m=population.GetCount();
 }
 //链表排序
 for(i=0;i<popsize;i++)//求适应度总值,以便归一化,是已经排序好的链。
 {
 current=population.GetAt(population.FindIndex(i)); //取出来的值出现问题.
 sum+=current.fitness;
 }
 for(i=0;i<popsize; i++)//归一化
 {
 current=population.GetAt(population.FindIndex(i)); //population 有值,为什么取出来的不正确呢??
 current.fitness=current.fitness/sum;
 cfitness.InsertAfter (cfitness .FindIndex(i),current.fitness);
 }
 
 for(i=1;i<popsize; i++)//概率值从小到大;
 {
 current.fitness=cfitness.GetAt (cfitness.FindIndex(i-1))
  +cfitness.GetAt(cfitness.FindIndex(i));  //归一化
 cfitness.SetAt (cfitness .FindIndex(i),current.fitness);
 population.SetAt(population.FindIndex(i),current);
 }
 for (i=0;i<popsize;)//轮盘赌概率选择。本段还有问题。
 {
 p=double(rand()%999)/1000+0.0001; //随机生成概率
 pindex=0; //遍历索引
 pc=cfitness.GetAt(cfitness.FindIndex(1)); //为什么取不到数值???20060910
 while(p>=pc&&pindex<popsize) //问题所在。
 {
  pc=cfitness.GetAt(cfitness .FindIndex(pindex));



 
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • C++实现遗传算法
  • C++实现简单遗传算法
  • C++遗传算法类文件实例分析

相关文章

  • 2017-05-28C语言小程序 数组操作示例代码
  • 2017-05-28C 语言指针概念的详解
  • 2017-05-28C++中new与delete、malloc与free应用分析
  • 2022-04-30什么是IDE(集成开发环境)?
  • 2017-05-28C++入门之基础语法学习教程
  • 2017-05-28c++关键字mutable深入解析
  • 2017-05-28数组和指针的区别深入剖析
  • 2017-05-28C++求斐波那契数的实例代码
  • 2017-05-28C++ 迷宫游戏实现代码
  • 2017-05-28C语言逻辑运算符知识整理

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • 深入理解strcpy与memcpy的区别
    • C++设计模式之观察者模式
    • C++ 字符串去重排序实例代码
    • c++中vector&lt;int&gt;和vector&lt;int*&gt;的用法区别
    • c++ STL set_difference set_intersection set_union 操作
    • C++ 字符串的反转五种方法实例
    • MFC命名规则汇总
    • c++加法高精度算法的简单实现
    • C++短路求值(逻辑与、逻辑或)实例
    • C语言采用文本方式和二进制方式打开文件的区别分析

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有