Files
envi-code/SourceCode/Code2026/tg_shs/shs_subgeLib/TGAPopulation.cpp
T
gjm 164968b62e chore
把非utf8-bom编码的cpp/h文件改为 utf8 bom 编码, msvc识别utf8编码时,如果不是bom格式的,会使用当前cp_oem来解码.
2026-10-04 00:04:20 +08:00

93 lines
1.8 KiB
C++

//-------------------------------------------------------------------------------------------------------+
// Copyright (C), 1998-2007, Beijing Tangent Software Co. Ltd.
// = FileName : TGAPopulation 类
// = Version : ver2.0
// = Author : wlw
// = CreateDate : 2002-09-09
// = Description: TGAPopulation 定义
// = Maintainers:
//
//-------------------------------------------------------------------------------------------------------+
#include "StdAfx.h"
#include "TGAPopulation.h"
#include "TArray.h"
TGAPopulation::TGAPopulation()
: m_iMaxNum(100),m_iMutationRatio(10),m_iEvolution(0)
{
}
TGAPopulation::~TGAPopulation()
{
}
bool TGAPopulation::IsContinue()
{
return true;
}
void TGAPopulation::Step()
{
}
void TGAPopulation::SetMaxNum(long num)
{
m_iMaxNum = num;
}
long TGAPopulation::GetMaxNum() const
{
return m_iMaxNum;
}
void TGAPopulation::SetMutationRatio(long r)
{
m_iMutationRatio = r;
}
long TGAPopulation::GetMutationRatio()
{
return m_iMutationRatio;
}
void TGAPopulation::Append(TGAIndividual* pNew)
{
if(pNew != NULL)
{
BFindDataTo(*pNew);
InsertCur(pNew);
}
}
void TGAPopulation::Evolve()
{
TArray<TGAIndividual*> parent(*this);
long last = GetLastIndex();
long s = GetLength()*GetLength();
for(long i = 0; i < parent.GetLength() && IsContinue(); i++, Step())
{
long r = rand();
long j = last - ((long)sqrt((double)(r % s)));
(*parent[j]).Multiply(*parent[i],this);
j = r % parent.GetLength();
(*parent[j]).Multiply(*parent[i],this);
}
parent.Clear();
while(GetLength() > GetMaxNum())
{
DelTail();
}
long num = GetMaxNum() * m_iMutationRatio / 100;
for(i = 0; i < num && IsContinue(); i++, Step())
{
MoveTo(rand() % GetLength());
Append(GetCurData()->Mutation());
}
m_iEvolution++;
}