多变量多目标的遗传算法程序.docx
- 文档编号:1175463
- 上传时间:2022-10-18
- 格式:DOCX
- 页数:17
- 大小:123.06KB
多变量多目标的遗传算法程序.docx
《多变量多目标的遗传算法程序.docx》由会员分享,可在线阅读,更多相关《多变量多目标的遗传算法程序.docx(17页珍藏版)》请在冰豆网上搜索。
多变量多目标的遗传算法程序
但是
这是我在解决电梯动力学参数写的简单遗传算法(程序带目标函数值、适应度值计算,我的适应度函数因为目标函数的计算很特殊,一起放在了程序外面计算,在此不提供)头文件:
//CMVSOGA.h:
mainheaderfilefortheCMVSOGA.cpp
//本来想使用链表里面套链表的,程序调试比较麻烦,改为种群用链表表示//染色体固定为16的方法。
#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#pragmaonce
#endif//_MSC_VER>1000#include"Afxtempl.h"#definevariablenum16classCMVSOGA
{public:
CMVSOGA();
voidselectionoperator();voidcrossoveroperator();voidmutationoperator();
种群
新种群
CList
CList
CList
//doublemaxfitness;
//doubleminfitness;
//doubleavefitness;
//怎样使链表的数据是一个结构体?
?
?
?
主要是想把种群作成链表。
节省空间。
};
#endif
执行文件
//CMVSOGA.cpp:
implementationfile
//
#include"stdafx.h"
#include"CMVSOGA.h"
#include"math.h"
#include"stdlib.h"#ifdef_DEBUG
#definenewDEBUG_NEW
#undefTHIS_FILEstaticcharTHIS_FILE[]=__FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
//CMVSOGA.cpp
voidCMVSOGA:
:
initialpopulation(intps,intgen,doublecr,doublemr,double*xtop,double*xbottom)//第一步,初始化。
{
inti,j;
popsize=ps;
maxgeneration=gen;crossoverrate=cr;mutationrate=mr;
for(i=0;i { variabletop[i]=xtop[i];variablebottom[i]=xbottom[i]; } srand((unsigned)time(NULL)); for(i=0;i { for(j=0;j {current.chromosome[j]=double(rand()%1000)/1000*(variabletop[j]-variablebottom[j])+variablebottom[j]; } current.fitness=0;current.value=0; population.InsertAfter(population.FindIndex(i),current);//除了初始化使用insertafter外,其他的用setat命令。 } } voidCMVSOGA: : generatenextpopulation()//第三步,生成下一代。 { selectionoperator();crossoveroperator(); mutationoperator(); } voidCMVSOGA: : evaluatepopulation()//第二步,评价个体,求最佳个体 { //calculateobjectvalue(); calculatefitnessvalue();//在此步中因该按适应度值进行排序.链表的排序.findbestandworstindividual(); } voidCMVSOGA: : calculateobjectvalue()//计算函数值,应该由外部函数实现。 主要因为目标函数很复杂。 { inti,j; doublex[variablenum]; for(i=0;i {current=population.GetAt(population.FindIndex(i)); current.value=0; //使用外部函数进行,在此只做结果的传递。 for(j=0;j { x[j]=current.chromosome[j]; current.value=current.value+(j+1)*pow(x[j],4); } ////使用外部函数进行,在此只做结果的传递。 population.SetAt(population.FindIndex(i),current); } } voidCMVSOGA: : mutationoperator()//对于浮点数编码,变异算子的选择具有决定意义。 //需要guass正态分布函数,生成方差为sigma,均值为浮点数编码值c。 { inti,j; doubler1,r2,p,sigma;//sigma高斯变异参数 sigma=0.5; for(i=0;i {current=population.GetAt(population.FindIndex(i)); //生成均值为current.chromosome,方差为sigma的高斯分布数srand((unsignedint)time(NULL)); for(j=0;j { r1=double(rand()%1001)/1000; r2=double(rand()%1001)/1000; p=double(rand()%1000)/1000; if(p { //高斯变异 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]=variabletop[j]; } if(current.chromosome[j] { current.chromosome[j]=variablebottom[j]; } } }population.SetAt(population.FindIndex(i),current); } } 应该加一个 voidCMVSOGA: : selectionoperator()//从当前个体中按概率选择新种群复制选择,提高种群的平均适应度 //第二次循环出错 { inti,j,pindex=0; doublep,pc,sum=0; 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 {current=population.GetAt(population.FindIndex(i));for(j=0;j {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 多余代码 临时借用变量 是已经排序好的链。 {current=population.GetAt(population.FindIndex(i));sum+=current.fitness; } for(i=0;i {current=population.GetAt(population.FindIndex(i));current.fitness=current.fitness/sum; cfitness.InsertAfter(cfitness.FindIndex(i),current.fitness);for(i=1;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 本段还有问题。 {p=double(rand()%1000)/1000+0.0001;//随机生成概率pindex=0;//遍历索引 pc=cfitness.GetAt(cfitness.FindIndex(0));while(p>=pc&&pindex { pc=cfitness.GetAt(cfitness.FindIndex(pindex)); pindex++; } //必须是从index~popsize,选择高概率的数。 即大于概率p的数应该被选择,选择不满则进行下次选择。 for(j=popsize-1;j
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 多变 多目标 遗传 算法 程序