📄 unit1.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include <stdio.h>
#ifdef __BORLANDC__
#include<alloc.h>
#else
#include<malloc.h>
#endif
#include <stdlib.h>
#include <math.h>
//------------------------------------------------------------------------------
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
/* 随机数发生器使用的静态变量 */
static double oldrand[55];
static int jrand;
static double rndx2;
static int rndcalcflag;
/* 输出文件指针 */
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
maxruns = 10;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
GAmain();
}
//---------------------------------------------------------------------------
/********************************************************************/
/* 基于基本遗传算法的函数最优化 SGA.C */
/* A Function Optimizer using Simple Genetic Algorithm */
/********************************************************************/
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TForm1::GAmain()
{
struct individual *temp;
title();
for(run=1; run<=maxruns; run++)
{
initialize();
for(gen=0; gen<maxgen; gen++)
{
Form1->ListBox1->Items->Add("第"+IntToStr(run)+"/"+IntToStr(maxruns)
+"次运行:当前代为"+IntToStr(gen)+",共"+IntToStr(maxgen)+"代");
// 产生新一代
generation();
//计算新一代种群的适应度统计数据
statistics(newpop);
//输出新一代统计数据
report();
temp = oldpop;
oldpop = newpop;
newpop = temp;
}
freeall();
}
}
//----------------------------------------------------------------------------
void __fastcall TForm1::initialize() /* 遗传算法初始化 */
{
/* 键盘输入遗传算法参数 */
initdata();
/* 确定染色体的字节长度 */
chromsize = (lchrom/(8*sizeof(unsigned)));
if(lchrom%(8*sizeof(unsigned))) chromsize++;
/*分配给全局数据结构空间 */
initmalloc();
/* 初始化随机数发生器 */
randomize();
/* 初始化全局计数变量和一些数值*/
nmutation = 0;
ncross = 0;
bestfit.fitness = 0.0;
bestfit.generation = 0;
/* 初始化种群,并统计计算结果 */
initpop();
statistics(oldpop);
initreport();
}
//------------------------------------------------------------------------
void __fastcall TForm1::initdata() /* 遗传算法参数输入 */
{
popsize=30;
if((popsize%2) != 0)
{
ShowMessage("种群大小已设置为偶数");
popsize++;
};
lchrom=22; /* 染色体长度*/
printstrings=1; /* 输出染色体编码的判断,0 -- 不输出, 1 -- 输出 */
maxgen=15; /* 最大世代数*/
pcross=0.8; /* 交叉概率 */
pmutation=0.005; /* 变异概率 */
}
//-----------------------------------------------------------------------------
void __fastcall TForm1::initpop() /* 随机初始化种群 */
{
int j, j1, k, stop;
unsigned mask = 1;
for(j = 0; j < popsize; j++)
{
for(k = 0; k < chromsize; k++)
{
oldpop[j].chrom[k] = 0;
if(k == (chromsize-1))
stop = lchrom - (k*(8*sizeof(unsigned)));
else
stop =8*sizeof(unsigned);
for(j1 = 1; j1 <= stop; j1++)
{
oldpop[j].chrom[k] = oldpop[j].chrom[k]<<1;
if(flip(0.5))
oldpop[j].chrom[k] = oldpop[j].chrom[k]|mask;
}
}
oldpop[j].parent[0] = 0; /* 初始父个体信息 */
oldpop[j].parent[1] = 0;
oldpop[j].xsite = 0;
objfunc(&(oldpop[j])); /* 计算初始适应度*/
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::initreport() /* 初始参数输出 */
{
Form1->ListBox1->Items->Add(" 基本遗传算法参数");
Form1->ListBox1->Items->Add("-------------------------------------------------");
Form1->ListBox1->Items->Add(" 种群大小(popsize) = "+IntToStr(popsize));
Form1->ListBox1->Items->Add(" 染色体长度(lchrom) = "+IntToStr(lchrom));
Form1->ListBox1->Items->Add(" 最大进化代数(maxgen) = "+IntToStr(maxgen));
Form1->ListBox1->Items->Add(" 交叉概率(pcross) = "+FloatToStr(pcross));
Form1->ListBox1->Items->Add(" 变异概率(pmutation) = "+FloatToStr(pmutation));
Form1->ListBox1->Items->Add("------------------------------------------------- ");
}
//------------------------------------------------------------------------
void __fastcall TForm1::generation()
{
int mate1, mate2, jcross, j = 0;
/* 每代运算前进行预选 */
preselect();
/* 选择, 交叉, 变异 */
do{
/* 挑选交叉配对 */
mate1 = select();
mate2 = select();
/* 交叉和变异 */
jcross = crossover(oldpop[mate1].chrom, oldpop[mate2].chrom, newpop[j].chrom, newpop[j+1].chrom);
mutation(newpop[j].chrom);
mutation(newpop[j+1].chrom);
/* 解码, 计算适应度 */
objfunc(&(newpop[j]));
/*记录亲子关系和交叉位置 */
newpop[j].parent[0] = mate1+1;
newpop[j].xsite = jcross;
newpop[j].parent[1] = mate2+1;
objfunc(&(newpop[j+1]));
newpop[j+1].parent[0] = mate1+1;
newpop[j+1].xsite = jcross;
newpop[j+1].parent[1] = mate2+1;
j = j + 2;
} while(j < (popsize-1));
}
//------------------------------------------------------------------------------
void __fastcall TForm1::initmalloc() /*为全局数据变量分配空间 */
{
unsigned nbytes;
int j;
/* 分配给当前代和新一代种群内存空间 */
nbytes = popsize*sizeof(struct individual);
if((oldpop = (struct individual *) malloc(nbytes)) == NULL)
nomemory("oldpop");
if((newpop = (struct individual *) malloc(nbytes)) == NULL)
nomemory("newpop");
/* 分配给染色体内存空间 */
nbytes = chromsize*sizeof(unsigned);
for(j = 0; j < popsize; j++)
{
if((oldpop[j].chrom = (unsigned *) malloc(nbytes)) == NULL)
nomemory("oldpop chromosomes");
if((newpop[j].chrom = (unsigned *) malloc(nbytes)) == NULL)
nomemory("newpop chromosomes");
}
if((bestfit.chrom = (unsigned *) malloc(nbytes)) == NULL)
nomemory("bestfit chromosome");
}
//----------------------------------------------------------------------------
void __fastcall TForm1::freeall() /* 释放内存空间 */
{
for(int i = 0; i < popsize; i++)
{
free(oldpop[i].chrom);
free(newpop[i].chrom);
}
free(oldpop);
free(newpop);
free(bestfit.chrom);
}
//-----------------------------------------------------------------------------
void __fastcall TForm1::nomemory(char *string) /* 内存不足,退出*/
{
ShowMessage("malloc:out of memory making");
exit(-1);
}
//------------------------------------------------------------------------------
void __fastcall TForm1::report() /* 输出种群统计结果 */
{
AnsiString str =" ";
str = "个体 染色体编码 适应度 父个体I & 父个体II 交叉位置 染色体编码 适应度";
if(printstrings == 1)
{
Form1->ListBox1->Items->Add(" 模拟计算统计报告 ");
Form1->ListBox1->Items->Add(" ");
Form1->ListBox1->Items->Add("世代数"+IntToStr(gen));
Form1->ListBox1->Items->Add(str);
Form1->ListBox1->Items->Add("-------------------------------------------------------------------------------------");
writepop();
Form1->ListBox1->Items->Add("-------------------------------------------------------------------------------------");
}
Form1->ListBox1->Items->Add("第"+IntToStr(gen)+"代统计:");
Form1->ListBox1->Items->Add("总交叉操作次数 = "+IntToStr(ncross)+"; 总变异操作数 = "+IntToStr(nmutation));
Form1->ListBox1->Items->Add("最小适应度:"+FloatToStr(min)+"最大适应度:"+FloatToStr(max)+"平均适应度: "+FloatToStr(avg));
Form1->ListBox1->Items->Add("迄今发现最佳个体 => 所在代数:"+IntToStr(bestfit.generation));
Form1->ListBox1->Items->Add("适应度:"+FloatToStr(bestfit.fitness)+"染色体:");
writechrom((&bestfit)->chrom);
Form1->ListBox1->Items->Add( "对应的变量值: "+FloatToStr(bestfit.varible));
Form1->ListBox1->Items->Add(" ");
Form1->ListBox1->Items->Add("---------------------------------------------------------");
Form1->ListBox1->Items->Add(" ");
}
//-----------------------------------------------------------------------------
void __fastcall TForm1::writepop()
{
struct individual *pind;
int j;
for(j=0; j<popsize; j++)
{
Form1->ListBox1->Items->Add(IntToStr(j+1));
/* 当前代个体 */
pind = &(oldpop[j]);
writechrom(pind->chrom);
Form1->ListBox1->Items->Add(FloatToStr(pind->fitness));
/* 新一代个体 */
pind = &(newpop[j]);
Form1->ListBox1->Items->Add(FloatToStr(pind->parent[0])+" , "+FloatToStr(pind->parent[1]));
Form1->ListBox1->Items->Add(FloatToStr(pind->xsite));
writechrom(pind->chrom);
Form1->ListBox1->Items->Add(FloatToStr(pind->fitness));
}
}
//-------------------------------------------------------------------------------
void __fastcall TForm1::writechrom(unsigned *chrom) /* 输出染色体编码 */
{
int j, k, stop;
unsigned mask = 1, tmp;
AnsiString str1=" ";
for(k = 0; k < chromsize; k++)
{
tmp = chrom[k];
if(k == (chromsize-1))
stop = lchrom - (k*(8*sizeof(unsigned)));
else
stop =8*sizeof(unsigned);
for(j = 0; j < stop; j++)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -