Java版GA_TSP (2) Java版GA_TSP(我的第一个Java程序)

  嗯哼,上一篇博客中用Java实现了遗传算法求解TSP(Java版GA_TSP(我的第一个Java程序)),但明显求解效果不太好,都没太好意思贴出具体的结果,今天捣腾了下,对算法做了一些小改进,求解效果也稍微好了点。总算是能见人了,具体改进如下:

  (1)    选择操作由原先的单一轮盘赌策略(Roulette Strategy)改为轮盘赌策略 + 精英策略(Elite Strategy), 在保证解的多样性的基础上,强化集中搜索。

  (2)    在构建新种群方式上,将原先的无差别接受扰动改为不接受退化的个体。

  []选择操作是形成进行繁衍(扰动产生新解)操作的前提,注意区分上述(1)和(2)。

  下面贴干货:

 1 int[][] newPop = new int[PopSize][N];
 2 //5%的精英策略
 3 for (int p = 0; p < 0.05*PopSize; p++) {
 4     for (int q = 0;q < N; q++) {
 5         newPop[p][q] = BS[q];
 6     }
 7 }
 8 //95%的轮盘赌策略
 9 for (int p = (int)0.05*PopSize; p < PopSize; p++) {
10     double rand = Math.random();
11     for (int r = 0; r < PopSize; r++) {
12         if (rand > cusFit[r] && rand <= cusFit[r+1]) {
13             for (int q = 0;q < N; q++) {
14                 newPop[p][q] = Pop[r][q];
15             }
16         }
17     }
18 }
19 
20 //扰动操作
21 for (int p = 1; p < PopSize; p++) {
22     double R = Math.random();
23     
24     int[] S = new int[N];
25     for (int i = 0; i < N; i++) {
26         S[i] = newPop[p][i];
27     }
28     
29     int[] S0 = new int[N];
30     int[] S1 = new int[N];
31     if (R < 0.33) {
32         S0 = Sharking.Swap(S);
33         if (GA.Pop.fit(S0) < GA.Pop.fit(S)) {
34             System.arraycopy(S, 0, S1, 0, N);
35         }else {
36             System.arraycopy(S0, 0, S1, 0, N);                        
37         }
38     }else if (R > 0.67) {
39         S0 = Sharking.Insert(S);
40         if (GA.Pop.fit(S0) < GA.Pop.fit(S)) {
41             System.arraycopy(S, 0, S1, 0, N);
42         }else {
43             System.arraycopy(S0, 0, S1, 0, N);                        
44         }
45     }else {
46         S0 = Sharking.Flip(S);
47         if (GA.Pop.fit(S0) < GA.Pop.fit(S)) {
48             System.arraycopy(S, 0, S1, 0, N);
49         }else {
50             System.arraycopy(S0, 0, S1, 0, N);                        
51         }
52     }

  求解结果如下,总迭代次数为200次,有效进化次数7次,误差19.13%。没有多次运行,感觉结果一般,但明显比第一版的遗传算法求解效果好很多。再接再厉~~~~

猜你喜欢

转载自www.cnblogs.com/Alex-Xu-OR/p/9471322.html