wondery 发表于 2013-2-7 21:14:17

js版蚁群算法

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><HEAD><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>蚁群算法js版</title><style>.ant{position:absolute;background-color:#000000;overflow:hidden;width:2px;height:2px;}.food{position:absolute;background-color:#0000ff;overflow:hidden;width:4px;height:4px;}.nest{position:absolute;background-color:#ff0000;overflow:hidden;width:2px;height:2px;}</style><script type="text/JavaScript">//============================//系统参数初始化//----------------------------//生命体数量与轨迹长度Unit=40;Path=5;//生命体速度上下限v0=2;vM=4;//生命体加速度变化范围Kr=0.1;Kv=0.1*(vM-v0);//生命体运动范围x0=0;xM=document.documentElement.clientWidth;y0=0;yM=document.documentElement.clientHeight;//生命体出生地(巢穴)xi0=x0+(xM-x0)*Math.random();yi0=y0+(yM-y0)*Math.random();str0='<div class="ant" style="left:'+xi0+';top:'+yi0+';"></div>';//食物所在地xf=x0+(xM-x0)*Math.random();yf=y0+(yM-y0)*Math.random();//气味感知范围R_2=5*5;//============================var r=new Array();var v=new Array();var dr=new Array();var dv=new Array();var x=new Array();var y=new Array();var life=new Array();//单击暂停var xi0,yi0,xf,yf;var Time0,str0;window.status='pause';function document.onclick(){if(window.status=='pause'){window.status=0;nest.style.left=xi0;nest.style.top=yi0;food.style.left=xf;food.style.top=yf;//测试初始化时间用Time0=(new Date()).getTime();init(0);}else{window.status='pause';}}//窗口大小调整后刷新页面以调整系统参数function window.onresize(){//window.location.href=document.location;}//初始化函数function init(i){if(window.status!='pause'&&i<Unit){if(!life){document.body.appendChild(life=document.createElement(str0));x=xi0;y=yi0;r=Math.random();v=1/Math.random();dr=Kr*Math.random();dv=Kv*Math.random();}Move(i);window.status=i+1;setTimeout('init('+(i+1)+')',i);//}else{//alert('生成耗时:'+((new Date()).getTime()-Time0)+'ms');}}//运动函数Total=Unit*Path;P2=2*Math.PI;function Move(i){if(window.status!='pause'){k=i%Unit;X=x;Y=y;R=r;V=v;if(!life){str='<div class="ant" style="left:'+X+';top:'+Y+';"></div>';document.body.appendChild(life=document.createElement(str));}obj=life;R+=dr*(2*Math.random()-1);V+=dv*(2*Math.random()-1);X+=Math.sin(P2*R)*V;Y+=Math.cos(P2*R)*V;//遇到食物原路返回并减小角度变化distance=(X-xf)*(X-xf)+(Y-yf)*(Y-yf);if(distance<R_2){R+=0.5;r/=2;v*=2;}distance=(X-xi0)*(X-xi0)+(Y-yi0)*(Y-yi0);if(distance<R_2){R+=0.5;r/=2;v*=2;}/*----------------------------------/*================================*///碰撞边界反弹R=(X<x0||X>xM)?-R:R;R=(Y<y0||Y>yM)?0.5-R:R;X=x+Math.sin(P2*R)*V;Y=y+Math.cos(P2*R)*V;/*================================*///溢出边界重生(类似流星效果)if(X<x0||X>xM||Y<y0||Y>yM){X=xi0;Y=yi0;}/*----------------------------------/*================================*///边界限制x=X=(X<x0)?x0:(X>xM)?xM-2:X;y=Y=(Y<y0)?y0:(Y>yM)?yM-2:Y;r=R>1?R-1:R<0?R+1:R;v=V=(V<v0)?v0:((V<vM)?V:vM);/*================================*/ obj.style.left=x=X;obj.style.top=y=Y;setTimeout('Move('+(i+Unit)%Total+')',Unit);}}//根据浏览器自动加载动画switch(navigator.appName.toLowerCase()){case "netscape":window.addEventListener("load",document.onclick,false);break;case "microsoft internet explorer":default:window.attachEvent("onload",document.onclick);break;}</script></head><body scroll="no"><div id="food" class="food"></div><div id="nest" class="nest"></div></body></html>
页: [1]
查看完整版本: js版蚁群算法