?? allocationant.cpp
字號:
// AllocationAnt.cpp: implementation of the CAllocationAnt class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "genetest.h"
#include "AllocationAnt.h"
#include "GenetestView.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CAllocationAnt::CAllocationAnt()
{
//m_bSearchFinished = FALSE;
//SetAllocArray();
m_fFitness = 0.0;
}
CAllocationAnt::~CAllocationAnt()
{
}
void CAllocationAnt::SetAllocArray()
{
int i,j;
for(i=0;i<maxTarget;i++)
{
m_nTargetAllocated[i] = 0;
m_nSortArray[i] = i;
for(j=0;j<maxWeapon;j++)
m_nArray[i][j]=0;
}
}
void CAllocationAnt::SearchPath()
{
//SetAllocArray();
//得到武器數
CGenetestView *pView;
pView = (CGenetestView *)((CFrameWnd*)(AfxGetApp()->m_pMainWnd))->GetActiveView();
int nWeapon = pView->m_nweapon;
int nTarget = pView->m_ntarget;
for(int n=0;n<nWeapon;n++)
{
//初始時讓螞蟻隨機的位于一個武器點上
//然后根據跡來選擇一個與之匹配的目標點。
//首先判斷該點是否已被分配,即判斷m_nArray所代表矩陣的第randpos列是否有值為1,
//若已經為1,則重新找武器點
BOOL flag = TRUE;
int i;
int randpos;
while(flag == TRUE)//已被分配
{
int r = rand();
randpos = r%nWeapon;
for(i=0;i<nTarget;i++)
{
int c = m_nArray[i][randpos];
if(c == 1)
{
flag = TRUE;
break;
}
}
if(i==nTarget)//找到的武器點還沒有被分配,因為若是分配了,for循環會終止,i不能增長到nTarget
flag = FALSE;
}
//--------計算轉移概率-------------------------
float TransferProp;
float maxPheromone,Sum;
//定義與randpos點相連邊的跡最大的目標點的索引
int TransferTarget;
int nMaxTarget;
//尋找最大的跡,并計算跡的和
Sum = 0;
SortTargetIndexByPheromone(randpos);
int j = 0;
while(!AnalysisTarget(m_nSortArray[j])&&j<nTarget)
{
j++;
}
if(j==nTarget)
pView->WriteLog("出現錯誤");
nMaxTarget = m_nSortArray[j];
maxPheromone = pView->m_PheromoneArray[nMaxTarget][randpos];
for(i=0;i<nTarget;i++)
{
Sum += pView->m_PheromoneArray[i][randpos];
}
//轉移概率的定義本來還考慮了“路徑可見度”的影響,此處沒有涉及,只是考慮了跡
TransferProp = maxPheromone/Sum;//用跡最大的邊定義轉移概率
//判斷轉移概率是否小于設定P0值,若小于,則將TransferTarger點分配給randpos點
if(TransferProp <= TransferP0)
{
TransferTarget = nMaxTarget;
m_nArray[TransferTarget][randpos] = 1;
m_nTargetAllocated[TransferTarget]++;
}
else//若大于p0,則選擇跡小的邊
{
TransferTarget = rand()%nTarget;
while(TransferTarget==nMaxTarget)
{
TransferTarget = rand()%nTarget;
}
m_nArray[TransferTarget][randpos] = 1;
m_nTargetAllocated[TransferTarget]++;
}
}
//-------------------計算轉移概率及找分配點的代碼,在此函數中可能無用,移至別處--------
}
//計算路徑長度,在MMAS算法中,是只有路徑最短的螞蟻才進行跡的更新,跡更新函數在View類中定義,
//此處,計算路徑長度實際上是計算適應度函數,應該是適應度函數最大的螞蟻才進行跡的更新
//在Run函數中調用ComputePathLength()還是在View類中調用合適呢????
float CAllocationAnt::ComputePathLength()
{
//得到武器數和目標數
CGenetestView *pView;
pView = (CGenetestView *)((CFrameWnd*)(AfxGetApp()->m_pMainWnd))->GetActiveView();
int nWeapon = pView->m_nweapon;
int nTarget = pView->m_ntarget;
float Sum = 0.0;
float mul;
for(int i = 0;i < nTarget;i++)
{
mul = 1.0;
for(int j = 0;j < nWeapon;j++)
{
if(m_nArray[i][j]==1)//Weapon J被分配給了Target I
{
mul = mul*(1 - pView->m_SuccessArray[i][j]);//計算失敗的概率
}
}
mul = 1 - mul;//得到成功的概率
Sum = Sum + pView->m_Worthiness[i]*mul; //得到對目標打擊成功的概率和
}
return Sum;
}
void CAllocationAnt::ReadTargetLimit()
{
CGenetestView *pView;
pView = (CGenetestView *)((CFrameWnd*)(AfxGetApp()->m_pMainWnd))->GetActiveView();
int nTarget = pView->m_ntarget;
POSITION pos = pView->m_WeaponList.GetHeadPosition();
int i = 0;
while(pos != NULL)
{
m_nTargetLimit[i] = pView->m_WeaponList.GetAt(pos)->m_nMaxWeapon;
i++;
pView->m_WeaponList.GetNext(pos);
}
}
//根據傳進來的目標索引,分析該目標分配的武器是否已經達到允許分配最多武器數的限制,若沒達到
//返回TRUE,否則返回FALSE
BOOL CAllocationAnt::AnalysisTarget(int TargetIndex)
{
CGenetestView *pView;
pView = (CGenetestView *)((CFrameWnd*)(AfxGetApp()->m_pMainWnd))->GetActiveView();
int nTarget = pView->m_ntarget;
BOOL flag = FALSE;
if(m_nTargetLimit[TargetIndex] > m_nTargetAllocated[TargetIndex])
flag = TRUE;
return flag;
}
//針對某一武器,將各目標按照該武器與目標之間的跡的大小,從大到小排序,將排序結果放在m_nSortArray中
void CAllocationAnt::SortTargetIndexByPheromone(int nWeapon)
{
CGenetestView *pView;
pView = (CGenetestView *)((CFrameWnd*)(AfxGetApp()->m_pMainWnd))->GetActiveView();
int nTarget = pView->m_ntarget;
float maxPheromone = 0.0;
int nMaxTarget = -1;
CString str,str1;
str.Format("武器%d",nWeapon+1);
pView->WriteLog(str);
//str = "";
for(int i=0;i<nTarget;i++)
{
for(int j = 0;j<nTarget;j++)
{
//去除已經排過序的值
if(i!=0)
{
BOOL flag = FALSE;
for(int k=0;k<i;k++)
{
if(j==m_nSortArray[k]){
flag = TRUE;
break;
}
}
if(flag) continue;
}
if(pView->m_PheromoneArray[j][nWeapon]>maxPheromone)
{
nMaxTarget = j;//與點randpos相連邊的跡最大的點
maxPheromone = pView->m_PheromoneArray[j][nWeapon];
}
}
m_nSortArray[i] = nMaxTarget;
maxPheromone = 0.0;
//str1.Format("%d ",nMaxTarget+1);
//str += str1;
}
//pView->WriteLog(str);
}
//將分配結果輸出到文件
void CAllocationAnt::WriteResult()
{
CGenetestView *pView;
pView = (CGenetestView *)((CFrameWnd*)(AfxGetApp()->m_pMainWnd))->GetActiveView();
int nTarget = pView->m_ntarget;
int nWeapon = pView->m_nweapon;
CString str,str1;
for(int i = 0; i < nTarget;i++)
{
str = "";
str1 = "";
for(int j = 0;j < nWeapon;j++)
{
if(m_nArray[i][j]==1)
{
str1.Format("第%d個、",j+1);
str += str1;
}
}
if(str1!="")
{
str1.Format("武器被分配到%d號目標",i+1);
str = str.Left(str.GetLength()-2);
str += str1;
}
else
str.Format("沒有武器分配到第%d號目標",i+1);
pView->WriteLog(str);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -