
#include <stdio.h>
#include <cmath>
#include <iostream>
#include <fstream>
#include <time.h>
using namespace std
const int iAntCount=34//蚂蚁数量
const int iCityCount=51//城市数量
const int iItCount=2000//最大跌代次数
const double Q=100
const double alpha=1
const double beta=5
const double rou=0.5
int besttour[iCityCount]//最有路径列表
double rnd(int low,double uper)//获得随机数
{
double p=(rand()/(double)RAND_MAX)*((uper)-(low))+(low)
return (p)
}
int rnd(int uper)
{
return (rand()%uper)
}
class GInfo//tsp地图信息,包含了信息素,城市距离,和信息素变化矩阵
{
public:
double m_dDeltTrial[iCityCount][iCityCount]
double m_dTrial[iCityCount][iCityCount]
double distance[iCityCount][iCityCount]
}
GInfo Map
class ant
{
private:
int ChooseNextCity()//选择城市
double prob[iCityCount]
int m_iCityCount
int AllowedCity[iCityCount]//没有走过的城市
public:
void addcity(int city)
int tabu[iCityCount]
void Clear()
void UpdateResult()
double m_dLength
double m_dShortest
void move()
ant()
void move2last()
}
void ant::move2last()
{
int i
for(i=0i<iCityCounti++)
if (AllowedCity[i]==1)
{
addcity(i)
break
}
}
void ant::Clear()
{
m_dLength=0
int i
for(i=0i<iCityCounti++)
{
prob[i]=0
AllowedCity[i]=1
i=tabu[iCityCount-1]
m_iCityCount=0
addcity(i)
}
}
ant::ant()
{
m_dLength=m_dShortest=0
m_iCityCount=0
int i
for(i=0i<iCityCounti++) {
AllowedCity[i]=1
prob[i]=0
}
}
void ant::addcity(int city)
{
//add city to tabu
tabu[m_iCityCount]=city
m_iCityCount++
AllowedCity[city]=0
}
int ant::ChooseNextCity()
{
//Update the probability of path selection
//select a path from tabu[m_iCityCount-1] to next
int i
int j=10000
double temp=0
int curCity=tabu[m_iCityCount-1]
for (i=0i<iCityCounti++) {
if((AllowedCity[i]==1))
{
temp+=pow((1.0/Map.distance[curCity][i]),beta)*pow((Map.m_dTrial[curCity][i]),alpha)
}
}
double sel=0
for (i=0i<iCityCounti++) {
if((AllowedCity[i]==1))
{
prob[i]=pow((1.0/Map.distance[curCity][i]),(int)beta)*pow((Map.m_dTrial[curCity][i]),(int)alpha)/temp
sel+=prob[i]
}
else
prob[i]=0
}
double mRate=rnd(0,sel)
double mSelect=0
for ( i=0i<iCityCounti++) {
if((AllowedCity[i]==1))
mSelect+=prob[i]
if (mSelect>=mRate) {j=ibreak}
}
if (j==10000)
{
temp=-1
for (i=0i<iCityCounti++) {
if((AllowedCity[i]==1))
if (temp) {
temp=pow((1.0/Map.distance[curCity][i]),beta)*pow((double)(Map.m_dTrial[curCity][i]),alpha)
j=i
}
}
}
return j
}
void ant::UpdateResult()
{
// Update the length of tour
int i
for(i=0i<iCityCount-1i++)
m_dLength+=Map.distance[tabu[i]][tabu[i+1]]
m_dLength+=Map.distance[tabu[iCityCount-1]][tabu[0]]
}
void ant::move()
{
//the ant move to next town and add town ID to tabu.
int j
j=ChooseNextCity()
addcity(j)
}
class project
{
public:
void UpdateTrial()
double m_dLength
void initmap()
ant ants[iAntCount]
void GetAnt()
void StartSearch()
project()
}
void project::UpdateTrial()
{
//calculate the changes of trial information
int i
int j
for(i=0i<iAntCounti++) {
for (j=0j<iCityCount-1j++)
{
Map.m_dDeltTrial[ants[i].tabu[j]][ants[i].tabu[j+1]]+=Q/ants[i].m_dLength
Map.m_dDeltTrial[ants[i].tabu[j+1]][ants[i].tabu[j]]+=Q/ants[i].m_dLength
}
Map.m_dDeltTrial[ants[i].tabu[iCityCount-1]][ants[i].tabu[0]]+=Q/ants[i].m_dLength
Map.m_dDeltTrial[ants[i].tabu[0]][ants[i].tabu[iCityCount-1]]+=Q/ants[i].m_dLength
}
for (i=0i<iCityCounti++) {
for (j=0j<iCityCountj++)
{
Map.m_dTrial[i][j]=(rou*Map.m_dTrial[i][j]+Map.m_dDeltTrial[i][j] )
Map.m_dDeltTrial[i][j]=0
}
}
}
void project::initmap()
{
int i
int j
for(i=0i<iCityCounti++)
for (j=0j<iCityCountj++)
{
Map.m_dTrial[i][j]=1
Map.m_dDeltTrial[i][j]=0
}
}
project::project()
{
//initial map,read map infomation from file . et.
initmap()
m_dLength=10e9
ifstream in("eil51.tsp")
struct city
{
int num
int x
int y
}cc[iCityCount]
for (int i=0i<iCityCounti++)
{
in>>cc[i].num>>cc[i].x>>cc[i].y
besttour[i]=0
}
int j
for(int i=0i<iCityCounti++)
for (j=0j<iCityCountj++)
{
{
Map.distance[i][j]=sqrt(pow((double)(cc[i].x-cc[j].x),2)+pow((double)(cc[i].y-cc[j].y),2))
}
}
}
void project::GetAnt()
{
//randomly put ant into map
int i=0
int city
srand( (unsigned)time( NULL ) +rand())
for (i=0i<iAntCounti++)
{
city=rnd(iCityCount)
ants[i].addcity(city)
}
}
void project::StartSearch()
{
//begin to find best solution
int max=0//every ant tours times
int i
int j
double temp
int temptour[iCityCount]
while (max<iItCount)
{
for(j=0j<iAntCountj++)
{
for (i=0i<iCityCount-1i++)
ants[j].move()
}
for(j=0j<iAntCountj++)
{ ants[j].move2last()
ants[j].UpdateResult ()
}
//find out the best solution of the step and put it into temp
int t
temp=ants[0].m_dLength
for (t=0t<iCityCountt++)
temptour[t]=ants[0].tabu[t]
for(j=0j<iAntCountj++)
{
if (temp>ants[j].m_dLength) {
temp=ants[j].m_dLength
for ( t=0t<iCityCountt++)
temptour[t]=ants[j].tabu[t]
}
}
if(temp<m_dLength){
m_dLength=temp
for ( t=0t<iCityCountt++)
besttour[t]=temptour[t]
}
printf("%d : %f\n",max,m_dLength)
UpdateTrial()
for(j=0j<iAntCountj++)
ants[j].Clear()
max++
}
printf("The shortest toure is : %f\n",m_dLength)
for ( int t=0t<iCityCountt++)
printf(" %d ",besttour[t])
}
int main()
{
project TSP
TSP.GetAnt()
TSP.StartSearch()
return 0
}
蚁群算法又称蚂蚁算法,是一种用来在图中寻找优化路径的机率型算法。它由Marco Dorigo于1992年在他的博士论文中提出,其灵感来源于蚂蚁在寻找食物过程中发现路径的行为。蚁群算法是一种模拟进化算法,初步的研究表明该算法具有许多优良的性质.针对PID控制器参数优化设计问题,将蚁群算法设计的结果与遗传算法设计的结果进行了比较,数值仿真结果表明,蚁群算法具有一种新的模拟进化优化方法的有效性和应用价值。思维学普遍认为,人类大脑的思维分为抽象(逻辑)思维、形象(直观)思维和灵感(顿悟)思维三种基本方式。
逻辑性的思维是指根据逻辑规则进行推理的过程;它先将信息化成概念,并用符号表示,然后,根据符号运算按串行模式进行逻辑推理;这一过程可以写成串行的指令,让计算机执行。然而,直观性的思维是将分布式存储的信息综合起来,结果是忽然间产生想法或解决问题的办法。这种思维方式的根本之点在于以下两点:1.信息是通过神经元上的兴奋模式分布储在网络上2.信息处理是通过神经元之间同时相互作用的动态过程来完成的。
人工神经网络就是模拟人思维的第二种方式。这是一个非线性动力学系统,其特色在于信息的分布式存储和并行协同处理。虽然单个神经元的结构极其简单,功能有限,但大量神经元构成的网络系统所能实现的行为却是极其丰富多彩的。
神经网络的研究内容相当广泛,反映了多学科交叉技术领域的特点。目前,主要的研究工作集中在以下几个方面:
(1)生物原型研究。从生理学、心理学、解剖学、脑科学、病理学等生物科学方面研究神经细胞、神经网络、神经系统的生物原型结构及其功能机理。
(2)建立理论模型。根据生物原型的研究,建立神经元、神经网络的理论模型。其中包括概念模型、知识模型、物理化学模型、数学模型等。
(3)网络模型与算法研究。在理论模型研究的基础上构作具体的神经网络模型,以实现计算机馍拟或准备制作硬件,包括网络学习算法的研究。这方面的工作也称为技术模型研究。
(4)人工神经网络应用系统。在网络模型与算法研究的基础上,利用人工神经网络组成实际的应用系统,例如,完成某种信号处理或模式识别的功能、构作专家系统、制成机器人等等。
纵观当代新兴科学技术的发展历史,人类在征服宇宙空间、基本粒子,生命起源等科学技术领域的进程中历经了崎岖不平的道路。我们也会看到,探索人脑功能和神经网络的研究将伴随着重重困难的克服而日新月异。
遗传算法,是模拟达尔文生物进化论的自然选择和遗传学机理的生物进化过程的计算模型,是一种通过模拟自然进化过程搜索最优解的方法,它最初由美国Michigan大学J.Holland教授于1975年首先提出来的,并出版了颇有影响的专著《Adaptation in Natural and Artificial Systems》,GA这个名称才逐渐为人所知,J.Holland教授所提出的GA通常为简单遗传算法(SGA)。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)