📄 individual.cpp
字号:
// Individual.cpp: implementation of the CIndividual class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Individual.h"
#include "math.h"
#include "stdlib.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
long mpower(int m,int n)
{
int i=0;
long result=1;
for(i=0;i<n;i++)
{
result*=m;
}
return result;
}
CIndividual::CIndividual()
{
this->BehaverListNum=0;
this->BitListNum=0;
}
CIndividual::~CIndividual()
{
delete this->pBehaverList;
delete this->pBitList;
}
//////////////////////////////////////////////////////////////////////////////////
// 填充两个链表,分别存储了表现型和基因型 //
//////////////////////////////////////////////////////////////////////////////////
void CIndividual::AddBehaver(int _BitNumbers, double _MinValue, double _MaxValue)
{
int i;
BIT * pBitBefore;
Behaver * pBehaverBefore;
/////初始化一个新加入的基因型位段////////////////////////////
if(this->BitListNum==0) ///初使化位段链表
{
this->pBitList=new BIT;
pBitBefore=this->pBitList;
}
else ///追加位段
{
pBitBefore=this->pBitList;
while((pBitBefore->pmBit)!=NULL)
{
pBitBefore=pBitBefore->pmBit;
}
pBitBefore->pmBit=new BIT;
pBitBefore=pBitBefore->pmBit;
}
/////加入一个表现型///////////////////////////////////////////
if(this->BehaverListNum==0) ///初使化表现型链表
{
this->pBehaverList=new Behaver;
pBehaverBefore=this->pBehaverList;
}
else ///追加表现型
{
pBehaverBefore=this->pBehaverList;
while((pBehaverBefore->pmBehaver)!=NULL)
{
pBehaverBefore=pBehaverBefore->pmBehaver;
}
pBehaverBefore->pmBehaver=new Behaver;
pBehaverBefore=pBehaverBefore->pmBehaver;
}
pBehaverBefore->pmBehaver=NULL;
pBehaverBefore->pmBit=pBitBefore;
pBehaverBefore->BitNumbers=_BitNumbers;
pBehaverBefore->MinValue=_MinValue;
pBehaverBefore->MaxValue=_MaxValue;
this->BehaverListNum++;
//////填充新位段//////////////////////////////////////////////
for(i=1;i<_BitNumbers;i++)
{
pBitBefore->pmBit=new BIT;
pBitBefore=pBitBefore->pmBit;
}
pBitBefore->pmBit=NULL;
this->BitListNum+=_BitNumbers;
}
double CIndividual::GetBehaverValue(int index)
{
Behaver * pBehaver;
BIT * pBit;
int i;
long sum=0;
pBehaver=this->GetBehaver(index);
pBit=pBehaver->pmBit;
for(i=pBehaver->BitNumbers-1;i>=0;i--)
{
sum+=mpower(2,i)*(pBit->bit);
pBit=pBit->pmBit;
}
return(pBehaver->MinValue+((double)sum/mpower(2,pBehaver->BitNumbers))*(pBehaver->MaxValue-pBehaver->MinValue));
}
BIT * CIndividual::GetBit(int index)
{
BIT * pBit;
int i;
pBit=this->pBitList;
for(i=1;i<index;i++)
{
pBit=pBit->pmBit;
}
return pBit;
}
void CIndividual::SetBit(int index, int value)
{
if((value==0)|(value==1))
{
GetBit(index)->bit=value;
}
else
{
//错误信息//
}
}
void CIndividual::Display(CListCtrl * pLstCtrl,int index)
{
int i;
BIT * pBit;
CString str,str2;
if(this->BitListNum==0)return;
pBit=this->pBitList;
str2.Format("%d",pBit->bit);
str+=str2;
while(pBit->pmBit!=NULL)
{
pBit=pBit->pmBit;
str2.Format("%d",pBit->bit);
str+=str2;
}
pLstCtrl->SetItemText(index,1,str);
for(i=1;i<=this->BehaverListNum;i++)
{
str.Format("%f",this->GetBehaverValue(i));
pLstCtrl->SetItemText(index,i+1,str);
}
str.Format("%f",this->Fitness);
pLstCtrl->SetItemText(index,i+1,str);
str.Format("%f",this->Possible_Select);
pLstCtrl->SetItemText(index,i+2,str);
str.Format("%f",this->Possible_Sel_Sum);
pLstCtrl->SetItemText(index,i+3,str);
}
void CIndividual::SetBehaverValue(int index,double value)
{
long temp;
int i;
unsigned char tchar[32];
Behaver * pBehaver;
BIT * pBit;
pBehaver=this->GetBehaver(index);
temp=(long)((value-pBehaver->MinValue)/(pBehaver->MaxValue-pBehaver->MinValue)*mpower(2,pBehaver->BitNumbers));
for(i=31;i>=0;i--)
{
tchar[i]=temp%2;
temp/=2;
}
pBit=pBehaver->pmBit;
for(i=32-pBehaver->BitNumbers;i<32;i++)
{
pBit->bit=tchar[i];
pBit=pBit->pmBit;
}
}
Behaver * CIndividual::GetBehaver(int index)
{
Behaver * pBehaver;
int i;
pBehaver=this->pBehaverList;
for(i=1;i<index;i++)
{
pBehaver=pBehaver->pmBehaver;
}
return pBehaver;
}
void CIndividual::InitialValue()
{
int i;
for(i=1;i<=this->BehaverListNum;i++)
{
this->SetBehaverValue(i,this->GetBehaver(i)->MinValue+((double)((rand())%99)/100)*(this->GetBehaver(i)->MaxValue-this->GetBehaver(i)->MinValue));
}
}
void CIndividual::NotBit(int index)
{
if(this->GetBit(index)->bit==0)
this->SetBit(index,1);
else
this->SetBit(index,0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -