1、pac kage c om . a nt ;/ *作者:z m c*功能:求出最大路径和最大绩效值*日期:2011-05-16*/i m port j a va . i o. F i l e ;i m port s t a t i c j a va . l a ng.Ma t h. pow;i m port s t a t i c j a va . l a ng.Ma t h. s qr t ;i m port s t a t i c j a va . l a ng.Ma t h. r a ndom;i m port j a va . ut i l . Ha s hMa p;i m port
2、j a va . i o. F i l e R e a der ;i m port j a va . i o. B uf f e r e dRe a der ;publi c c l a s s A C O f or Va l ue s t a t i c i nt P E R S O N N O = 10;/ /总的员工人数s t a t i c i nt S T AR T N O = 1; / /起始的员工号/ /绩效矩阵pr i vat e double pjVa l ue;/ /绩效矩阵的倒数数组pr i vat e double pjVa l ueB a c kwa r ds;/ /
3、启发信息表,初始化为1pr i vat e double pher om one;/ /信息素权重,路径权重pr i vat e i nt a l pha, bet a ;/ /迭代的次数pr i vat e i nt i t e r a t i onTi m e s ;/ /蚂蚁的数量pr i vat e i nt number s O f Ant ;/ /信息素蒸发率pr i vat e double r a t e ;A C O f or Va l ue ( S t r i ng f i l e , i nt i t e r a t i onTi m e s , i nt number
4、s O f A nt , i nt a l pha, i nt bet a , double r a t e ) / /加载文件t his . i ni t i a l i z e D a t a ( f i l e ) ;/ /初始化参数t his . i t e r a t i onTi m e s = i t e r a t i onTi m e s ;/ /设置蚂蚁数量t his . number s O f Ant = number s O f Ant ;/ /设置权重t his . a l pha = a l pha;t his . bet a = bet a ;/ /设置蒸发率t
5、 his . r a t e = r a t e ;pr i vat e void dis pla y( )S ys t e m . out. pri nt l n( “绩效矩阵:“ ) ;f or ( i nt i = 0; i m a p = new Ha s hMa p ( ) ;/ /读取文件B uf f e r e dRe a der r e a der = new B uf f e r e dRe a der ( new F i l e R e a der ( new F i l e ( f i l e na m e ) ) ) ;/ /分配绩效矩阵存储空间pjV a l ue =
6、 new double t his . P E R S O N N O + 1 t his . P E R S O N N O + 1 ;/ /分配绩效倒数矩阵存储空间pjVa l ueB a c kwa r ds = new double t hi s . P E R S O N N O + 1 t his . P E R S O N N O + 1 ;/ /分配信息素矩阵存储空间pher om one = new double t hi s . P E R S O N N O + 1 t his . P E R S O N N O + 1 ;i nt t e m p1,t e m p2;d
7、ouble t e m p3;f or ( S t r i ng s t r = r e a der . r e a dLi ne( ) ; s t r ! = null ; s t r = r e a der . r e a dLi ne( ) ) / /将读到的信息保存入H a s hMa p,利用正则表达式来匹配是否符合P J Va l ue类的一个格式i f ( s t r . m a t c hes ( “ ( 0- 9 + ) ( , ) ( 0- 9 + ) ( , ) ( 0-9 + ) ( . ? ) ( 0-9 *) “ ) ) S t r i ng dat a = s t
8、 r . s pli t ( “ , “ ) ;t e m p1=I nt e ger . par s e I nt ( dat a 0 ) ;t e m p2=I nt e ger . par s e I nt ( dat a 1 ) ;t e m p3=D ouble . par s e D ouble ( dat a 2 ) ;P J Va l ue c i t y = new P J Va l ue ( t e m p1,t e m p2,t e m p3);m a p. put( c i t y.per s onNo, c i t y) ;pjVa l ue t e m p1 t e
9、 m p2= t e m p3;f or ( i nt i = 1; i bes t T ot a l ) bes t T ot a l = a nt i . t ot a l ;/ / bes t T o ur表示从第2个位置开始存入读出S ys t e m . a r r a ycopy(a nt i . t our, 1, bes t T our, 1, bes t T our. l e ngth- 1) ;/ /释放信息素(在最优的路径上才释放信息素)a nt i . r e l e a s e P her om one( ) ;/ /蒸发信息素e vapora t e P her o
10、m one( ) ;/ /报告本次迭代的信息/ / S ys t e m . out. f or m a t ( “本次为倒数第% d次迭代,当前最优路径长度为% 10.2f n“, i t e r a t i onTi m e s , bes t T o t a l ) ;/ /迭代总数减去1,进行下次迭代i t e r a t i onTi m e s - - ;/ /输出最好的路径长度S ys t e m . out. f or m a t ( “得到的最优的路径长度为: % 10.2f n“, bes t T o t a l ) ;/ /输出最好的路径S ys t e m . out.
11、 pri nt l n( “最优路径如下:“ ) ;f or ( i nt i = 1; i bes t T our. l e ngth; i + + ) S ys t e m . out. pri nt ( “ “ + bes t T our i ) ;S ys t e m . out. pri nt l n( ) ;double t e m p=0;f or ( i nt i = 1; i bes t T our. l e ngth- 1; i + + )t e m p+= pjVa l ue bes t T our i bes t T our i + 1 ;S yst e m . out
12、. pr i nt ( pjVa l ue bes t T our i bes t T o ur i + 1 + “ ; “ ) ;t e m p+= pjVa l ue bes t T our bes t T o ur. l e ngth- 1 bes t T o ur 1 ;S ys t e m . out. pri nt l n( pj Va l ue bes t T our bes t T our. l e ngth- 1 bes t T our 1 + “ t “ + t e m p) ;/ /蒸发信息素pr i vat e void e vapora t e P her om on
13、e( ) f or ( i nt i = 1; i pher om one. l e ngth; i + + )f or ( i nt j = 1; j pher om one. l e ngth; j + + ) pher om one i j *= 1- r a t e ;publi c s t a t i c void m a i n( S t r i ng a r gs ) / / T O D O A ut o- gener a t e d m e t hod s t ub/ / AC O f or T S P t s p = new A C P f or T S P ( “ t s p数据文件名“ ,迭代次数,蚂蚁数量,信息素权重,路径权重,信息素蒸发率);AC O f or Va l ue t s p=new A C O f or Va l ue( “ d: w or ks pac e s pjVa l ue. t xt “ , 200,15,2,2, 0.015);t s p.go() ;t s p.dis pla y( ) ;