⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ant.cpp

📁 刚根据蚁群算法文档编写的蚁群算法的程序
💻 CPP
字号:
// Ant.cpp: implementation of the Ant class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Project.h"
#include "Ant.h"
#include "math.h"
#include "Global.h"
#include "City.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

Ant::Ant()
{
	current_position = 0;
	for(int m = 0; m <= NumOfCity - 1; m++)
	{
		tabu[m] = NumOfCity + 1;
	}

	bool isAllowed = false;

	for(int i = 0; i <= NumOfAnt - 1; i++)
	{
		for(int j = 0; j <= NumOfAnt - 1; j++)
		{
			prob[i][j] = 0;
		}
	}
//	length = 0;

	for(int o = 0; o <= NumOfCity - 1; o ++)
	{
		for(int o1 = 0; o1 <= NumOfCity - 1; o1 ++)
		{
			derta_t[o][o1] = 0;
		}
	}

	length = 0;
}

Ant::~Ant()
{

}

//DEL double Ant::probability(int i, int j)
//DEL {
//DEL 	double result = 0.2;
//DEL 	return result;
//DEL }


//DEL bool Ant::IsAllowed(int i)
//DEL {
//DEL 	for(int j = 0; j <= NumOfCity - 1; j++)
//DEL 	{
//DEL 		if(tabu[i] == i + 1)
//DEL 		{
//DEL 			return true;
//DEL 			exit(0);
//DEL 		}
//DEL 	}
//DEL }


int Ant::compute_next_position(int i)
{
	double temp = 0;
	int position;
	for(int j = 0; j <= NumOfCity - 1; j++)
	{
		if(prob[i][j] >= temp && !isInTabu(i))
		{
			position = j;
		}
	}
	return position;
}

void Ant::initialize()
{
	for(int i = 0; i <= NumOfAnt - 1; i++)
	{
		for(int j = 0; j <= NumOfAnt - 1; j++)
		{
			prob[i][j] = 0;
		}
	}
}

bool Ant::isInTabu(int i)
{
	bool result;
	result = false;
	for(int j = 0; j <= NumOfCity - 1; j++)
	{
		if(i == tabu[j])
		{
			result = true;
			break;
		}
	}
	return result;
}

double Ant::getProbability(int i, int j)
{
	CCity city[NumOfCity], c[NumOfCity];
	double num, den, temp, result;
	temp = 0;
//	int r;
	for(int r = 0; r <= NumOfCity - 1; r++)
	{
		for(int q = 0; q <= NumOfCity - 1; q++)
		{
			c[r].yita[r][q] = c[r].getYita(r, q);
		}
	}

	if(!isInTabu(j))
		num = pow(city[i].tao[i][j], alpha) * pow(c[i].yita[i][j], beta);
	else
		num = 0;
 	 

	for(int m = 0; m <= NumOfCity - 1; m++)
	{
		if(!isInTabu(m))
		{
			temp = temp + pow(city[i].tao[i][m], alpha) * pow(c[i].yita[i][m], beta);
		}
		else
		{
//			num = 0;
//			break;
		}
	}
	den = temp;
	if(num == 0)
		result = 0;
	else
		result = num / den;
	return result;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -