0


java实现遗传算法求解最短路径问题

  1. 遗传算法(Genetic AlgorithmGA)最早是由美国的John holland20世纪70年代提出,该算法是根据大自然中生物体进化规律而设计提出的。是模拟达尔文生物进化论的自然选择和遗传学机理的生物进化过程的计算模型,是一种通过模拟自然进化过程搜索最优解的方法。

1、问题描述:

  1. 1所示为一个最短路径问题,每条边代表一条可以通行的弧,边上的数值表示这条弧的长度,多条弧相互连接形成路径,目标是寻找一条从节点0出发到节点5的最短路径。

a0cb89d8537b4c1ba31d60e63be2bcf8.jpeg

2、编码

  1. 从表现型到基因型的映射称为编码。图1中每条路径的每个节点对应一个基因,通过对节点的有序组合可以将每条路径映射为一个向量。每个向量长度为6,起始和结束位置的数值分别为05,代表从节点0出发,到节点5终止,图1中节点间边的长度代表节点间的距离,若两节点间无边相连,则这两个节点间的距离为一个极大的数M。由于向量长度固定为6,而解中可能并不包含所有的节点,个体中可能会存在多个相邻且重复出现的节点,因此设置节点到其本身的距离为0

3、个体类

  1. 每个个体包含路径Path和适应度length(即路径长度)两个属性,每个个体路径属性中的起点为0,结束点为5,其余位置数值随机生成(0-5范围内的整数),向量长度固定为6。个体类中定义的compareTo方法是为了用于在选择算子中采用迭代器进行个体的删除。
  1. public class Individual implements Comparable<Individual>{
  2. int[] Path = new int[6]; //存储路径
  3. int length; //表示适应度
  4. public int[] getPath() {
  5. return Path;
  6. }
  7. public void setPath(int[] path) {
  8. Path = path;
  9. }
  10. public int getLength() {
  11. return length;
  12. }
  13. public void setLength(int length) {
  14. this.length = length;
  15. }
  16. public int compareTo(Individual o) {
  17. if(this.getLength() > o.getLength())
  18. {
  19. return -1;
  20. }
  21. else if(this.getLength()<o.getLength())
  22. {
  23. return 1;
  24. }
  25. else
  26. {
  27. return 0;
  28. }
  29. }
  30. }

4、遗传算法解决最短路径问题主方法

  1. 主方法包括(1)数据的初始化(2)定义初始种群(3)循环依次调用选择、交叉和变异算子(4)输出迭代后的结果。以邻接矩阵的形式表达图1中各节点间的距离, 建立一个集合表示种群,随机产生10个个体并添加到初始种群完成种群的初始化,设置迭代次数并依次调用三个算子更新种群,最终输出结果。详细代码如下:
  1. static int[][] matrix = new int[6][6];
  2. final static int M = 10000;
  3. static Random rand = new Random();
  4. public static void main(String[] args) {
  5. //邻接矩阵
  6. matrix[0] = new int[]{0, 6, 3, M, M, M};/*1*/
  7. matrix[1] = new int[]{6, 0, 2, 5, M, M};/*2*/
  8. matrix[2] = new int[]{3, 2, 0, 3, 4, M};/*3*/
  9. matrix[3] = new int[]{M, 5, 3, 0, 2, 3};/*4*/
  10. matrix[4] = new int[]{M, M, 4, 2, 0, 5};/*5*/
  11. matrix[5] = new int[]{M, M, M, 3, 5, 0};/*6*/
  12. //定义初始种群
  13. Math.random();
  14. List<Individual> Population = new ArrayList<>();
  15. for (int i = 0; i < 10; i++) {
  16. //随机生成十个个体添加到初始种群列表
  17. Individual Popu = new Individual();
  18. Popu.Path[0] = 0;
  19. Popu.Path[1] = rand.nextInt(5);
  20. Popu.Path[2] = rand.nextInt(5);
  21. Popu.Path[3] = rand.nextInt(5);
  22. Popu.Path[4] = rand.nextInt(5);
  23. Popu.Path[5] = 5;
  24. Popu.length = M;
  25. Population.add(Popu);
  26. }
  27. for(int i = 0; i<2000; i++){
  28. System.out.println("第"+(i+1)+"次迭代开始!");
  29. //初始种群中选择出5个较优的个体
  30. List<Individual> NewSelPopu = Selection(Population);
  31. //交叉
  32. List<Individual> NewCroPopu = Crossover(NewSelPopu);
  33. //变异
  34. Population = Variation(NewCroPopu);
  35. System.out.println("第"+ (i+1) + "次迭代完成!");
  36. }
  37. //输出迭代后的种群
  38. System.out.print("2000次迭代后集合中个体的长度:");
  39. for(Individual a : Population){
  40. System.out.print(a.length +" ");
  41. }
  42. //对集合中的个体排序
  43. Collections.sort(Population);
  44. //输出排序后的集合中个体的长度
  45. System.out.print("\n" +"2000次迭代后所有个体的最短路径长度为:" + Population.get(9).length);
  46. System.out.println("\n"+"最短路径为:" + Arrays.toString(Population.get(9).Path));
  47. }

5、适应度

  1. 该问题中适应度即为每个个体所代表的路径长度,适应度函数如下:
  1. static void Fitness(Individual in){
  2. //计算路径长度
  3. in.length = matrix[in.Path[0]][in.Path[1]] + matrix[in.Path[1]][in.Path[2]] +
  4. matrix[in.Path[2]][in.Path[3]] + matrix[in.Path[3]][in.Path[4]] + matrix[in.Path[4]][in.Path[5]];
  5. }

6、选择算子

  1. 输入:包含10个个体的种群。
  2. 输出:包含5个个体的种群。
  3. 计算所输入的种群的所有个体的适应度,并按照适应度将这些个体进行升序排列,删除掉其中适应度较大的五个个体,并返回剩余的种群。代码如下:
  1. static List<Individual> Selection(List<Individual> Population){
  2. System.out.print("排序前集合中个体的长度:");
  3. for(Individual a : Population){
  4. System.out.print(a.length +" ");
  5. }
  6. //对集合中的个体排序
  7. Collections.sort(Population);
  8. //输出排序后的集合中个体的长度
  9. System.out.print("\n" +"排序后集合中个体的长度:");
  10. for(Individual a : Population){
  11. System.out.print(a.length +" ");
  12. }
  13. //使用迭代器删除个体
  14. Iterator<Individual> iterator = Population.iterator();
  15. while(iterator.hasNext() && Population.size()>5){
  16. Individual next = iterator.next();
  17. if(next != null)
  18. iterator.remove();
  19. }
  20. //输出删除后的个体的长度
  21. System.out.print("\n" + "选择后的个体的长度:");
  22. for(Individual a : Population){
  23. System.out.print(a.length +" ");
  24. }
  25. System.out.println("\n" + "选择完成!");
  26. return Population;
  27. }

7、交叉算子

  1. 输入:包含5个个体的种群。
  2. 输出:包含7个个体的种群。
  3. 在经过选择算子后生成的包含5个个体的种群中随机选择两个不同的个体,选择一个不是首也不是尾的基因,将所选择的两个个体对应的基因进行交叉,并将新产生的个体添加到种群中去,返回新的种群。代码如下:
  1. static List<Individual> Crossover(List<Individual> NewSelPopu){
  2. //复制集合
  3. List<Individual> CroPopu = new ArrayList<>();
  4. for (int i = 0; i < 5; i++) {
  5. Individual ind = new Individual();
  6. System.arraycopy(NewSelPopu.get(i).Path, 0, ind.Path, 0, 6);
  7. ind.length = NewSelPopu.get(i).length;
  8. CroPopu.add(ind);
  9. }
  10. //随机选择两个不同的个体
  11. int i = rand.nextInt(5);
  12. int j = rand.nextInt(5);
  13. while(i == j){
  14. j = rand.nextInt(5);
  15. }
  16. //随机选择一个不是首尾的基因进行交叉
  17. int k = rand.nextInt(4) + 1;
  18. int l = CroPopu.get(i).Path[k];
  19. CroPopu.get(i).Path[k] = CroPopu.get(j).Path[k];
  20. CroPopu.get(j).Path[k] = l;
  21. //更新length并添加到集合中
  22. Fitness(CroPopu.get(i));
  23. Fitness(CroPopu.get(j));
  24. NewSelPopu.add(CroPopu.get(i));
  25. NewSelPopu.add(CroPopu.get(j));
  26. //输出交叉产生的个体
  27. System.out.println("交叉产生的个体为:" + Arrays.toString(CroPopu.get(i).Path) + "和" + Arrays.toString(CroPopu.get(j).Path));
  28. //输出交叉后的个体适应度
  29. System.out.print("交叉后的个体的长度:");
  30. for(Individual a : NewSelPopu){
  31. System.out.print(a.length +" ");
  32. }
  33. System.out.println("\n"+"交叉完成!");
  34. return NewSelPopu;
  35. }

8、变异算子

  1. 输入:包含7个个体的种群。
  2. 输出:包含10个个体的种群。
  3. 随机选择一个个体,将这个个体的随机一个不为首或尾的基因进行变异,随机产生一个[0,5]中的数值代替该基因处的数值,将变异后产生的新的个体添加到种群中。重复以上步骤三次,共计产生三个新的个体。这里需要注意的是,由于每次选择要变异的个体都是随机的,可能存在两次甚至三次选择同一个个体进行变异的情况,这也符合自然界中生物遗传的思想。代码如下:
  1. static List<Individual> Variation(List<Individual> NewCroPopu){
  2. //复制集合
  3. List<Individual> VarPopu = new ArrayList<>();
  4. for (int i = 0; i < 7; i++) {
  5. Individual ind = new Individual();
  6. System.arraycopy(NewCroPopu.get(i).Path, 0, ind.Path, 0, 6);
  7. ind.length = NewCroPopu.get(i).length;
  8. VarPopu.add(ind);
  9. }
  10. //变异三次
  11. for (int i = 0; i < 3; i++) {
  12. //随机选择一个个体的一个基因进行变异
  13. int j = rand.nextInt(7);
  14. VarPopu.get(j).Path[(rand.nextInt(4) + 1)] = rand.nextInt(5);
  15. //更新length并添加到集合中
  16. Fitness(VarPopu.get(j));
  17. NewCroPopu.add(VarPopu.get(j));
  18. //输出交叉产生的个体
  19. System.out.println("第"+ (i+1) +"次变异产生的个体为:" + Arrays.toString(VarPopu.get(i).Path));
  20. }
  21. //输出变异后的个体适应度
  22. System.out.print("变异后的个体的长度:");
  23. for(Individual a : NewCroPopu){
  24. System.out.print(a.length +" ");
  25. }
  26. System.out.println("\n"+"变异完成!");
  27. return NewCroPopu;
  28. }

9、总结

  1. 本文解决的问题复杂度较低,适合代码或者遗传算法的初学者尝试解决。另外在解决该问题时,本文所采用的编码方式较为简单,虽可以很好的解决此类简单问题,但在求解更复杂的问题时可能会存在计算结果为不可行解的情况,因此在采用遗传算法解决更复杂的问题时,非常有必要对编码方式进行进一步的加工,使其更适合问题特性且计算结果更优。完整的代码如下:
  1. import java.util.*;
  2. public class GeneticAlgorithm {
  3. static int[][] matrix = new int[6][6];
  4. final static int M = 10000;
  5. static Random rand = new Random();
  6. public static void main(String[] args) {
  7. //邻接矩阵
  8. matrix[0] = new int[]{0, 6, 3, M, M, M};/*1*/
  9. matrix[1] = new int[]{6, 0, 2, 5, M, M};/*2*/
  10. matrix[2] = new int[]{3, 2, 0, 3, 4, M};/*3*/
  11. matrix[3] = new int[]{M, 5, 3, 0, 2, 3};/*4*/
  12. matrix[4] = new int[]{M, M, 4, 2, 0, 5};/*5*/
  13. matrix[5] = new int[]{M, M, M, 3, 5, 0};/*6*/
  14. //定义初始种群
  15. Math.random();
  16. List<Individual> Population = new ArrayList<>();
  17. for (int i = 0; i < 10; i++) {
  18. //随机生成十个个体添加到初始种群列表
  19. Individual Popu = new Individual();
  20. Popu.Path[0] = 0;
  21. Popu.Path[1] = rand.nextInt(5);
  22. Popu.Path[2] = rand.nextInt(5);
  23. Popu.Path[3] = rand.nextInt(5);
  24. Popu.Path[4] = rand.nextInt(5);
  25. Popu.Path[5] = 5;
  26. Popu.length = M;
  27. Population.add(Popu);
  28. }
  29. //输出初始种群
  30. for (int i = 0; i < 10; i++) {
  31. System.out.println("初始种群中第" + (i+1) + "个个体为:");
  32. for (int j = 0; j < 6; j++) {
  33. System.out.print(Population.get(i).Path[j]);
  34. }
  35. //更新length
  36. for (int j = 0; j < 10; j++) {
  37. Fitness(Population.get(j));
  38. }
  39. System.out.println("\n" +"适应度为:" +Population.get(i).length);
  40. System.out.println();
  41. }
  42. for(int i = 0; i<2000; i++){
  43. System.out.println("第"+(i+1)+"次迭代开始!");
  44. //初始种群中选择出5个较优的个体
  45. List<Individual> NewSelPopu = Selection(Population);
  46. //交叉
  47. List<Individual> NewCroPopu = Crossover(NewSelPopu);
  48. //变异
  49. Population = Variation(NewCroPopu);
  50. System.out.println("第"+ (i+1) + "次迭代完成!");
  51. }
  52. //输出迭代后的种群
  53. System.out.print("2000次迭代后集合中个体的长度:");
  54. for(Individual a : Population){
  55. System.out.print(a.length +" ");
  56. }
  57. //对集合中的个体排序
  58. Collections.sort(Population);
  59. //输出排序后的集合中个体的长度
  60. System.out.print("\n" +"2000次迭代后所有个体的最短路径长度为:" + Population.get(9).length);
  61. System.out.println("\n"+"最短路径为:" + Arrays.toString(Population.get(9).Path));
  62. }
  63. //选择函数,删除种群中较大的5个个体,返回两个所选的适应度最好的个体
  64. //输入:10个个体的种群
  65. //输出:5个个体的种群
  66. static List<Individual> Selection(List<Individual> Population){
  67. System.out.print("排序前集合中个体的长度:");
  68. for(Individual a : Population){
  69. System.out.print(a.length +" ");
  70. }
  71. //对集合中的个体排序
  72. Collections.sort(Population);
  73. //输出排序后的集合中个体的长度
  74. System.out.print("\n" +"排序后集合中个体的长度:");
  75. for(Individual a : Population){
  76. System.out.print(a.length +" ");
  77. }
  78. //使用迭代器删除个体
  79. Iterator<Individual> iterator = Population.iterator();
  80. while(iterator.hasNext() && Population.size()>5){
  81. Individual next = iterator.next();
  82. if(next != null)
  83. iterator.remove();
  84. }
  85. //输出删除后的个体的长度
  86. System.out.print("\n" + "选择后的个体的长度:");
  87. for(Individual a : Population){
  88. System.out.print(a.length +" ");
  89. }
  90. System.out.println("\n" + "选择完成!");
  91. return Population;
  92. }
  93. //交叉产生两个新的个体
  94. //输入:5个个体的种群
  95. //输出:7个个体的种群
  96. static List<Individual> Crossover(List<Individual> NewSelPopu){
  97. //复制集合
  98. List<Individual> CroPopu = new ArrayList<>();
  99. for (int i = 0; i < 5; i++) {
  100. Individual ind = new Individual();
  101. System.arraycopy(NewSelPopu.get(i).Path, 0, ind.Path, 0, 6);
  102. ind.length = NewSelPopu.get(i).length;
  103. CroPopu.add(ind);
  104. }
  105. //随机选择两个不同的个体
  106. int i = rand.nextInt(5);
  107. int j = rand.nextInt(5);
  108. while(i == j){
  109. j = rand.nextInt(5);
  110. }
  111. //随机选择一个不是首尾的基因进行交叉
  112. int k = rand.nextInt(4) + 1;
  113. int l = CroPopu.get(i).Path[k];
  114. CroPopu.get(i).Path[k] = CroPopu.get(j).Path[k];
  115. CroPopu.get(j).Path[k] = l;
  116. //更新length并添加到集合中
  117. Fitness(CroPopu.get(i));
  118. Fitness(CroPopu.get(j));
  119. NewSelPopu.add(CroPopu.get(i));
  120. NewSelPopu.add(CroPopu.get(j));
  121. //输出交叉产生的个体
  122. System.out.println("交叉产生的个体为:" + Arrays.toString(CroPopu.get(i).Path) + "和" + Arrays.toString(CroPopu.get(j).Path));
  123. //输出交叉后的个体适应度
  124. System.out.print("交叉后的个体的长度:");
  125. for(Individual a : NewSelPopu){
  126. System.out.print(a.length +" ");
  127. }
  128. System.out.println("\n"+"交叉完成!");
  129. return NewSelPopu;
  130. }
  131. //变异两个个体
  132. //输入:7个个体的种群
  133. //输出:10个个体的种群
  134. static List<Individual> Variation(List<Individual> NewCroPopu){
  135. //复制集合
  136. List<Individual> VarPopu = new ArrayList<>();
  137. for (int i = 0; i < 7; i++) {
  138. Individual ind = new Individual();
  139. System.arraycopy(NewCroPopu.get(i).Path, 0, ind.Path, 0, 6);
  140. ind.length = NewCroPopu.get(i).length;
  141. VarPopu.add(ind);
  142. }
  143. //变异三次
  144. for (int i = 0; i < 3; i++) {
  145. //随机选择一个个体的一个基因进行变异
  146. int j = rand.nextInt(7);
  147. VarPopu.get(j).Path[(rand.nextInt(4) + 1)] = rand.nextInt(5);
  148. //更新length并添加到集合中
  149. Fitness(VarPopu.get(j));
  150. NewCroPopu.add(VarPopu.get(j));
  151. //输出交叉产生的个体
  152. System.out.println("第"+ (i+1) +"次变异产生的个体为:" + Arrays.toString(VarPopu.get(i).Path));
  153. }
  154. //输出变异后的个体适应度
  155. System.out.print("变异后的个体的长度:");
  156. for(Individual a : NewCroPopu){
  157. System.out.print(a.length +" ");
  158. }
  159. System.out.println("\n"+"变异完成!");
  160. return NewCroPopu;
  161. }
  162. //更新适应度
  163. static void Fitness(Individual in){
  164. //计算路径长度
  165. in.length = matrix[in.Path[0]][in.Path[1]] + matrix[in.Path[1]][in.Path[2]] +
  166. matrix[in.Path[2]][in.Path[3]] + matrix[in.Path[3]][in.Path[4]] + matrix[in.Path[4]][in.Path[5]];
  167. }
  168. }
  1. public class Individual implements Comparable<Individual>{
  2. int[] Path = new int[6]; //存储路径
  3. int length; //表示适应度
  4. public int[] getPath() {
  5. return Path;
  6. }
  7. public void setPath(int[] path) {
  8. Path = path;
  9. }
  10. public int getLength() {
  11. return length;
  12. }
  13. public void setLength(int length) {
  14. this.length = length;
  15. }
  16. public int compareTo(Individual o) {
  17. if(this.getLength() > o.getLength())
  18. {
  19. return -1;
  20. }
  21. else if(this.getLength()<o.getLength())
  22. {
  23. return 1;
  24. }
  25. else
  26. {
  27. return 0;
  28. }
  29. }
  30. }
  1. 运行结果如下:
  1. 2000次迭代完成!
  2. 2000次迭代后集合中个体的长度:9 9 9 9 9 9 9 9 9 10003
  3. 2000次迭代后所有个体的最短路径长度为:9
  4. 最短路径为:[0, 2, 2, 2, 3, 5]
  1. 由于问题比较简单,一般迭代100次左右就已经求得最优解,为保证结果的最优性,本文对进行了2000次迭代,迭代结果与上一篇文章中通过Dijkstra方法求得的最优解一致。
  2. 在进行代码的编写时也遇到了一些比较经典的问题,总结如下:
  3. 1.初始版本的选择算子中,先将每个个体的适应度属性存储到一个新建的数组中进行排序,此方法舍近求远,因此对其进行改进,采用Collections.sort()对种群中的个体进行排序。
  4. 2.初始版本的选择算子中,采用for循环和while循环的方式删除适应度大的个体,此种方式导致程序运行时出现死循环且不能很好的实现删除5个适应度大的个体的目的,for循环中每次删除个体后种群数量发生变化,程序运行会出现异常,因此对其进行改进,采用迭代器对个体进行删除。
  5. 3.在交叉和变异算子中需要对集合进行复制,由于集合名代表的是集合存储的地址,直接赋值仍然会修改原集合中的数据,因此在对集合进行深层次的复制,新建个体并将原集合中的个体属性值分别赋给新个体后添加到复制集合中去。
  6. 本文适合于代码或者遗传算法的初学者阅读,也欢迎各位经验丰富的博主提出宝贵的意见!

本文转载自: https://blog.csdn.net/Xing_LG/article/details/125128518
版权归原作者 Xing_LG 所有, 如有侵权,请联系我们删除。

“java实现遗传算法求解最短路径问题”的评论:

还没有评论