//-------------------------------------------------------------------------------------------------------+ // 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 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++; }