蚁群算法实验报告.docx

上传人:b****1 文档编号:14913361 上传时间:2023-06-28 格式:DOCX 页数:55 大小:778.26KB
下载 相关 举报
蚁群算法实验报告.docx_第1页
第1页 / 共55页
蚁群算法实验报告.docx_第2页
第2页 / 共55页
蚁群算法实验报告.docx_第3页
第3页 / 共55页
蚁群算法实验报告.docx_第4页
第4页 / 共55页
蚁群算法实验报告.docx_第5页
第5页 / 共55页
蚁群算法实验报告.docx_第6页
第6页 / 共55页
蚁群算法实验报告.docx_第7页
第7页 / 共55页
蚁群算法实验报告.docx_第8页
第8页 / 共55页
蚁群算法实验报告.docx_第9页
第9页 / 共55页
蚁群算法实验报告.docx_第10页
第10页 / 共55页
蚁群算法实验报告.docx_第11页
第11页 / 共55页
蚁群算法实验报告.docx_第12页
第12页 / 共55页
蚁群算法实验报告.docx_第13页
第13页 / 共55页
蚁群算法实验报告.docx_第14页
第14页 / 共55页
蚁群算法实验报告.docx_第15页
第15页 / 共55页
蚁群算法实验报告.docx_第16页
第16页 / 共55页
蚁群算法实验报告.docx_第17页
第17页 / 共55页
蚁群算法实验报告.docx_第18页
第18页 / 共55页
蚁群算法实验报告.docx_第19页
第19页 / 共55页
蚁群算法实验报告.docx_第20页
第20页 / 共55页
亲,该文档总共55页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

蚁群算法实验报告.docx

《蚁群算法实验报告.docx》由会员分享,可在线阅读,更多相关《蚁群算法实验报告.docx(55页珍藏版)》请在冰点文库上搜索。

蚁群算法实验报告.docx

蚁群算法实验报告

蚁群算法

1.实验目的

各个蚂蚁在没有事先告诉他们食物在什么地方的前提下开始寻找食物。

当一只找到食物以后,它会向环境释放一种信息素,吸引其他的蚂蚁过来,这样越来越多的蚂蚁会找到食物。

有些蚂蚁并没有象其它蚂蚁一样总重复同样的路,他们会另辟蹊径,如果令开辟的道路比原来的其他道路更短,那么,渐渐,更多的蚂蚁被吸引到这条较短的路上来。

最后,经过一段时间运行,可能会出现一条最短的路径被大多数蚂蚁重复着。

2.编程环境

MicrosoftVisualStudio2008(C#)

3.实验要求

程序开始运行,蚂蚁们开始从窝里出动了,寻找食物;他们会顺着屏幕爬满整个画面,直到找到食物再返回窝。

4.源代码

Form1窗体代码:

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Drawing;

usingSystem.Drawing.Drawing2D;

usingSystem.Threading;

usingSystem.Windows.Forms;

namespaceANTS

{

publicpartialclassForm1:

Form

{

inttemp1=0,temp2=0;

publicForm1()

{

InitializeComponent();

}

privatevoidForm1_Paint(objectsender,PaintEventArgse)

{

Bitmapbmp=newBitmap(PlayGround.MaxX+10,PlayGround.MaxY+10);//新?

建¡§画-布?

Graphicsg=Graphics.FromImage(bmp);//获?

取¨?

这a块¨¦内¨²存ä?

画-布?

的Ì?

Graphics引°y用®?

Penpb=newPen(_borderColor,2);//画-边À?

框¨°

Penph=newPen(_homeColor,3);//画-窝?

Penpf=newPen(_foodColor,3);//画-食º3物?

g.DrawEllipse(pf,PlayGround.FoodLocation.X,PlayGround.FoodLocation.Y,10,10);

g.DrawEllipse(ph,PlayGround.HomeLocation.X,PlayGround.HomeLocation.Y,10,10);

g.DrawRectangle(pb,1,1,PlayGround.MaxX+2,PlayGround.MaxY+2);

if(StoneInitialFlag)

{

StoneInitialFlag=false;

PlayGround.InitialStone();

}

DrawStone(bmp);//画-障?

碍ã-物?

this.CreateGraphics().DrawImage(bmp,0,0);//显?

示º?

新?

建¡§的Ì?

画-布?

bmp.Dispose();//释º¨ª放¤?

内¨²存ä?

}

privatevoidtabPage2_Click(objectsender,EventArgse)

{

groundSetground=newgroundSet();

ground.Show();

}

privatevoidtabPage1_Click(objectsender,EventArgse)

{

antsSetant=newantsSet();

ant.Show();

}

privatevoidjieshaoToolStripMenuItem_Click(objectsender,EventArgse)

{

Introduceintroduce=newIntroduce();

introduce.Show();

}

privatevoidaboutToolStripMenuItem_Click(objectsender,EventArgse)

{

Formabout=newForm();

about.Show();

}

privatevoidForm1_Load(objectsender,EventArgse)

{

this.timer3.Enabled=true;

this.Opacity=0;

radioButton1.Checked=true;

System.Media.SoundPlayerplayer=newSystem.Media.SoundPlayer();

player.SoundLocation="Rihanna-WhereHaveYouBeen.mp3";

player.LoadAsync();

Thread.Sleep(5000);

player.Stop();

}

publicvoidtimer1_Tick(objectsender,EventArgse)

{

{

try

{

Graphicsg=CreateGraphics();

Penpc=newPen(this.BackColor,1);//清?

除y蚂¨¬蚁°?

Penpa=newPen(_antColor,1);//画-蚂¨¬蚁°?

Penpaf=newPen(_antfColor,1);//画-带ä?

着Á?

食º3物?

的Ì?

蚂¨¬蚁°?

g.SmoothingMode=SmoothingMode.HighQuality;

ColorC=Color.Green;

PenP=newPen(Brushes.White,0);

P.StartCap=LineCap.Round;

P.EndCap=LineCap.Round;

for(inti=0;i

{

{

g.FillEllipse(newSolidBrush(this.BackColor),AntS[i].Bounds);

 

g.DrawArc(P,AntS[i].GetX(),AntS[i].GetY(),Antc.AntSize,Antc.AntSize,-100,-70);

}

AntS[i].AntOneStep();

{

g.FillEllipse(newSolidBrush(C),AntS[i].Bounds);

g.DrawArc(P,AntS[i].GetX(),AntS[i].GetY(),Antc.AntSize,Antc.AntSize,-100,-70);

}

}

Penph=newPen(_homeColor,3);//画-窝?

Penpf=newPen(_foodColor,3);//画-食º3物?

g.DrawEllipse(pf,PlayGround.FoodLocation.X,PlayGround.FoodLocation.Y,10,10);

g.DrawEllipse(ph,PlayGround.HomeLocation.X,PlayGround.HomeLocation.Y,10,10);

temp1+=3;

if(temp1>=100)

{

temp2=temp2+temp1/100;

temp1=temp1%60;

}

label3.Text=temp2.ToString()+"秒?

"+temp1.ToString();

}

catch(Exceptionee)

{

//label2.Text=ee.Message;

}

}

}

ThreadTimer2Trd;

publicvoidtimer2_Tick(objectsender,EventArgse)

{

Timer2Trd=newThread(newThreadStart(this.DoTimer2));

Timer2Trd.Start();

}

privatevoidDoTimer2()

{

try

{

//显?

示º?

信?

息¡é

ShowMessageCounter++;

if(ShowMessageCounter>=2)

{

ShowMessageCounter=0;

if(!

string.IsNullOrEmpty(PlayGround.ShowMessage))

{

PlayGround.ShowMessage="";

}

elseif(!

string.IsNullOrEmpty(PlayGround.ShowMessage2))

{

label2.Text=PlayGround.ShowMessage2;

}

}

Bitmapbmp=newBitmap(PlayGround.MaxX+10,PlayGround.MaxY+10);

DealSmellCounter++;

if(DealSmellCounter>=5)

{

DealSmellCounter=0;

if(checkBox1.Checked||checkBox2.Checked)

{

ClearSmell(bmp);

}

elseif(ClearSmellCounter<2)

{

ClearSmellCounter++;

ClearSmell(bmp);

}

PlayGround.DealGround();

DrawSmell(bmp);

DrawSmellCounter=-1;

}

if(checkBox1.Checked||checkBox2.Checked)

{

DrawSmellCounter++;

if(DrawSmellCounter>=1)

{

DrawSmellCounter=0;

DrawSmell(bmp);

}

}

this.CreateGraphics().DrawImage(bmp,0,0);

bmp.Dispose();

}

catch(Exceptionee)

{

//label2.Text=ee.Message.ToString();

}

}

privatevoidDrawSmell(Graphicsg)

{

inthsm,fsm;

for(intxxx=3;xxx

for(intyyy=3;yyy

{

hsm=PlayGround.HomeSmell[xxx,yyy];

fsm=PlayGround.FoodSmell[xxx,yyy];

if(hsm<=0&&fsm<=0)continue;

if(checkBox1.Checked&&hsm>0)

{

intco=hsm>200?

0:

200-hsm;

g.DrawRectangle(newPen(Color.FromArgb(co,255,co)),xxx,yyy,1,1);

}

if(checkBox2.Checked&&fsm>0)

{

intco=fsm>200?

0:

200-fsm;

g.DrawRectangle(newPen(Color.FromArgb(co,co,255)),xxx,yyy,1,1);

}

}

}

privatevoidDrawSmell(Bitmapbmp)

{

inthsm,fsm;

for(intxxx=3;xxx

for(intyyy=3;yyy

{

hsm=PlayGround.HomeSmell[xxx,yyy];

fsm=PlayGround.FoodSmell[xxx,yyy];

if(hsm<=0&&fsm<=0)continue;

if(checkBox1.Checked&&hsm>0)

{

intco=hsm>200?

0:

200-hsm;

bmp.SetPixel(xxx,yyy,Color.FromArgb(co,255,co));

}

if(checkBox2.Checked&&fsm>0)

{

intco=fsm>200?

0:

200-fsm;

bmp.SetPixel(xxx,yyy,Color.FromArgb(co,co,255));

}

}

}

publicvoidDrawStone(Graphicsg)

{

Penps=newPen(_stoneColor,1);//画-障?

碍ã-物?

for(intxxx=3;xxx

for(intyyy=3;yyy

{

if(PlayGround.IsStone(xxx,yyy))g.DrawRectangle(ps,xxx,yyy,1,1);

}

}

publicvoidDrawStone(Bitmapbmp)

{

for(intxxx=3;xxx

for(intyyy=3;yyy

{

if(PlayGround.IsStone(xxx,yyy))bmp.SetPixel(xxx,yyy,_stoneColor);

}

}

privatevoidClearSmell(Graphicsg)

{

Penpcsm=newPen(this.BackColor,1);

for(intxxx=3;xxx

for(intyyy=3;yyy

{

if(PlayGround.HomeSmell[xxx,yyy]<=0&&PlayGround.FoodSmell[xxx,yyy]<=0)continue;

g.DrawRectangle(pcsm,xxx,yyy,1,1);

}

}

privatevoidClearSmell(Bitmapbmp)

{

for(intxxx=3;xxx

for(intyyy=3;yyy

{

if(PlayGround.HomeSmell[xxx,yyy]<=0&&PlayGround.FoodSmell[xxx,yyy]<=0)continue;

bmp.SetPixel(xxx,yyy,this.BackColor);

}

}

privatevoidstart_Click(objectsender,EventArgse)

{

if(!

timer1.Enabled)

{

timer1.Enabled=true;

timer2.Enabled=true;

DoRestart();

}

}

publicstaticvoidInitialAnt()

{

AntS=newAntc[MaxAnt];

Randomran=newRandom();

List_ranList=newList();

intrannum;

for(inti=0;i

{

do

{

rannum=ran.Next(10000);

}

while(_ranList.Contains(rannum));

_ranList.Add(rannum);

AntS[i]=newAntc(rannum);

}

}

privatevoidrestart_Click(objectsender,EventArgse)

{

DoRestart();

}

publicvoidDoRestart()

{

boolt1State,t2State;

t1State=timer1.Enabled;

t2State=timer2.Enabled;

timer1.Enabled=false;

timer2.Enabled=false;

PlayGround.ShowMessage="";

Bitmapbmp=newBitmap(PlayGround.MaxX+10,PlayGround.MaxY+10);

Graphicsg=Graphics.FromImage(bmp);

g.Clear(this.BackColor);

PlayGround.InitialPlayGround();

if(checkBox4.Checked)PlayGround.InitialStone();

InitialAnt();

Penpb=newPen(_borderColor,2);/

Penph=newPen(_homeColor,3);//画-窝?

Penpf=newPen(_foodColor,3);

g.DrawEllipse(pf,PlayGround.FoodLocation.X,PlayGround.FoodLocation.Y,10,10);

g.DrawEllipse(ph,PlayGround.HomeLocation.X,PlayGround.HomeLocation.Y,10,10);

g.DrawRectangle(pb,1,1,PlayGround.MaxX+3,PlayGround.MaxY+3);

DrawStone(bmp);

this.CreateGraphics().DrawImage(bmp,0,0);

bmp.Dispose();

timer1.Enabled=t1State;

timer2.Enabled=t2State;

}

privatevoidend_Click(objectsender,EventArgse)

{

timer1.Enabled=false;

timer2.Enabled=false;

}

privatevoidxianshishezhiToolStripMenuItem_Click(objectsender,EventArgse)

{

}

#regionMemberFields

publicboolStoneInitialFlag=true;

publicstaticintMaxAnt=_maxAnt;

publicstaticAntc[]AntS=newAntc[_maxAnt];

publicColor_antColor=Color.Black;

publicColor_antfColor=Color.Purple;

publicstaticColor_homeColor=Color.Green;

publicstaticColor_foodColor=Color.Blue;

publicstaticColor_borderColor=Color.Black;

publicstaticColor_stoneColor=Color.Red;

publicColor_hSmellColor=Color.Orange;

publicColor_fSmellColor=Color.LightBlue;

publicintShowMessageCounter=0;

publicintDrawSmellCounter=0;

publicintDealSmellCounter=0;

publicintClearAntCounter=0;

publicintClearSmellCounter=0;

publicconstint_maxAnt=25;

#endregion

privatevoidquit_Click(objectsender,EventArgse)

{

Application.Exit();

}

privatevoidradioButton1_CheckedChanged(objectsender,EventArgse)

{

this.BackgroundImage=Image.FromFile("背À3景¡ã/a.jpg");

}

privatevoidradioButton2_CheckedChanged(objectsender,EventArgse)

{

this.BackgroundImage=Image.FromFile("背À3景¡ã/2.jpg");

}

privatevoidradioButton3_CheckedChanged(objectsender,EventArgse)

{

this.BackgroundImage=Image.FromFile("背

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 幼儿教育

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2