当前位置:   article > 正文

遗传算法的Java实现_遗传算法java代码

遗传算法java代码

算法思想不再赘述。

直接贴完整代码。


问题描述:

求二元函数f(x,y) = x^2 + y^2, x,y∈[0,7] 的最大值


运行结果:

可以看到,在100次迭代中,算法找到的最大的最佳适应度为98,可以认为98就是该二元函数的最大值。


 函数图像:

可以看到,在两个因变量的取值都为7时,函数有最大值98


 完整代码如下:

  1. import java.util.Random;
  2. public class YiChuanSuanFa {
  3. static int generation = 0;
  4. public static int x1Init(){
  5. Random random = new Random();
  6. int x1 = random.nextInt(8);
  7. return x1;
  8. }
  9. public static int x2Init(){
  10. Random random = new Random();
  11. int x2 = random.nextInt(8);
  12. return x2;
  13. }
  14. //选择操作 采用与适应度成正比的概率
  15. public static int[] Choice(int degree_one, int degree_two, int degree_three, int degree_four){
  16. int[] result = new int[4];
  17. int sumDegree = degree_one + degree_two + degree_three + degree_four;
  18. int interval_one = degree_one;
  19. int interval_two = interval_one + degree_two;
  20. int interval_three = interval_two+ degree_three;
  21. int interval_four = interval_three + degree_four;
  22. Random random = new Random();
  23. for (int i = 0; i < 4; i++){
  24. int n = random.nextInt(sumDegree);
  25. if (n <= interval_one){
  26. result[i] = 1;
  27. }
  28. else if (n <= interval_two){
  29. result[i] = 2;
  30. }
  31. else if (n <= interval_three){
  32. result[i] = 3;
  33. }
  34. else if (n <= interval_four){
  35. result[i] = 4;
  36. }
  37. }
  38. return result;
  39. }
  40. //交叉操作
  41. public static String[] mix(int []indivXY){
  42. String []transBinary = new String[8];
  43. String []resGene = new String[4];
  44. int n = indivXY.length;
  45. for (int i = 0; i < n ;i++){
  46. if (indivXY[i]<=1){
  47. transBinary[i] = "000"+Integer.toBinaryString(indivXY[i]);
  48. }
  49. else if (indivXY[i]<=3){
  50. transBinary[i] = "00"+Integer.toBinaryString(indivXY[i]);
  51. }
  52. else if (indivXY[i]<=7){
  53. transBinary[i] = "0"+Integer.toBinaryString(indivXY[i]);
  54. }
  55. }
  56. //拼接
  57. for (int j = 0 ,i = 0; i < n; j++,i += 2){
  58. resGene[j] = transBinary[i]+transBinary[i+1];
  59. }
  60. Random transPos = new Random();
  61. //两两交叉
  62. for (int i = 0; i < resGene.length; i+=2){
  63. int pos = transPos.nextInt(8);
  64. String temp = resGene[i].substring(pos,8);//临时保存子串
  65. resGene[i] = resGene[i].substring(0,pos) + resGene[i+1].substring(pos,8);
  66. resGene[i+1] = resGene[i+1].substring(0,pos)+temp;
  67. }
  68. //交叉结果
  69. return resGene;
  70. }
  71. //变异操作
  72. public static int[][] variation(String []indiv){
  73. int [][]res = new int[4][2];
  74. int vary_prob = 20;//变异概率
  75. Random n = new Random();
  76. for (int i = 0; i < indiv.length; i++) {
  77. int j = n.nextInt(100);
  78. if (j <= vary_prob){
  79. int pos = n.nextInt(8);
  80. char []temp = indiv[i].toCharArray();
  81. if (temp[pos] == '0'){
  82. temp[pos] = '1';
  83. }
  84. else {
  85. temp[pos] = '0';
  86. }
  87. indiv[i] = String.valueOf(temp);
  88. }
  89. }
  90. for (int i = 0;i<indiv.length;i++){
  91. res[i][0] = Integer.parseInt(indiv[i].substring(1,4),2);
  92. res[i][1] = Integer.parseInt(indiv[i].substring(5,8),2);
  93. }
  94. return res;
  95. }
  96. //适应度函数
  97. public static int FitDegree(int x1,int x2) {
  98. return x1*x1 + x2*x2;
  99. }
  100. public static int TheBestDegree(int []Degree) {
  101. int bestDegree = Degree[1];
  102. for (int i = 0;i<Degree.length;i++){
  103. if (bestDegree < Degree[i]){
  104. bestDegree = Degree[i];
  105. }
  106. }
  107. return bestDegree;
  108. }
  109. public static String TheBestGene(int [][]indivXY) {
  110. String res = "";
  111. int []degree = new int[4];
  112. for (int i =0;i<4;i++){
  113. degree[i] = FitDegree(indivXY[i][0],indivXY[i][1]);
  114. }
  115. int bestDegree = degree[1];
  116. int bestGeneNum = 1;
  117. for (int i = 0;i<degree.length;i++){
  118. if (bestDegree < degree[i]){
  119. bestDegree = degree[i];
  120. bestGeneNum = i;
  121. }
  122. }
  123. for (int i =0;i<indivXY[i].length;i++){
  124. if (indivXY[bestGeneNum][i]<=1){
  125. res += "000"+Integer.toBinaryString(indivXY[bestGeneNum][i]);
  126. }
  127. else if (indivXY[bestGeneNum][i]<=3){
  128. res += "00"+Integer.toBinaryString(indivXY[bestGeneNum][i]);
  129. }
  130. else if (indivXY[bestGeneNum][i]<=7){
  131. res += "0"+Integer.toBinaryString(indivXY[bestGeneNum][i]);
  132. }
  133. }
  134. return res;
  135. }
  136. public static void main(String[] args) {
  137. int [][]parent = new int[4][2];
  138. parent[0][0] = x1Init();
  139. parent[0][1] = x2Init();
  140. parent[1][0] = x1Init();
  141. parent[1][1] = x2Init();
  142. parent[2][0] = x1Init();
  143. parent[2][1] = x2Init();
  144. parent[3][0] = x1Init();
  145. parent[3][1] = x2Init();
  146. int [][]progeny = parent;
  147. System.out.println("初代种群:");
  148. for (int i=0;i<parent.length;i++){
  149. for (int j = 0;j< parent[i].length;j++){
  150. System.out.print(parent[i][j]+" ");
  151. }
  152. System.out.println();
  153. }
  154. while (generation < 100){
  155. generation++;
  156. int []res = new int[8];
  157. int []progenyFit = new int[4];
  158. //计算适应度
  159. progenyFit[0] = FitDegree(progeny[0][0],progeny[0][1]);
  160. progenyFit[1] = FitDegree(progeny[1][0],progeny[1][1]);
  161. progenyFit[2] = FitDegree(progeny[2][0],progeny[2][1]);
  162. progenyFit[3] = FitDegree(progeny[3][0],progeny[3][1]);
  163. int TheBestFit = TheBestDegree(progenyFit);
  164. String TheBestGene = TheBestGene(progeny);
  165. //根据适应度概率进行选择
  166. int []choiceRes = Choice(progenyFit[0],progenyFit[1],progenyFit[2],progenyFit[3]);
  167. for (int i = 0,j = 0;i < choiceRes.length;i++,j+=2){
  168. if (choiceRes[i] == 1){
  169. res[j] = progeny[0][0];
  170. res[j+1] = progeny[0][1];
  171. }
  172. else if (choiceRes[i] == 2){
  173. res[j] = progeny[1][0];
  174. res[j+1] = progeny[1][1];
  175. }
  176. else if (choiceRes[i] == 3){
  177. res[j] = progeny[2][0];
  178. res[j+1] = progeny[2][1];
  179. }
  180. else if (choiceRes[i] == 4){
  181. res[j] = progeny[3][0];
  182. res[j+1] = progeny[3][1];
  183. }
  184. }
  185. String []indiv = mix(res);//交叉操作
  186. progeny = variation(indiv);//变异操作
  187. System.out.println("当前代数:"+generation+" 最佳适应度:"+TheBestFit+" 子代基因型:"+TheBestGene);
  188. // if (TheBestFit == 98 ){
  189. // break;
  190. // }
  191. }
  192. }
  193. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/429973
推荐阅读
相关标签
  

闽ICP备14008679号