赞
踩
算法思想不再赘述。
直接贴完整代码。
求二元函数f(x,y) = x^2 + y^2, x,y∈[0,7] 的最大值
可以看到,在100次迭代中,算法找到的最大的最佳适应度为98,可以认为98就是该二元函数的最大值。
可以看到,在两个因变量的取值都为7时,函数有最大值98
- import java.util.Random;
-
- public class YiChuanSuanFa {
- static int generation = 0;
-
- public static int x1Init(){
- Random random = new Random();
- int x1 = random.nextInt(8);
- return x1;
- }
- public static int x2Init(){
- Random random = new Random();
- int x2 = random.nextInt(8);
- return x2;
- }
-
- //选择操作 采用与适应度成正比的概率
- public static int[] Choice(int degree_one, int degree_two, int degree_three, int degree_four){
- int[] result = new int[4];
- int sumDegree = degree_one + degree_two + degree_three + degree_four;
- int interval_one = degree_one;
- int interval_two = interval_one + degree_two;
- int interval_three = interval_two+ degree_three;
- int interval_four = interval_three + degree_four;
- Random random = new Random();
- for (int i = 0; i < 4; i++){
- int n = random.nextInt(sumDegree);
- if (n <= interval_one){
- result[i] = 1;
- }
- else if (n <= interval_two){
- result[i] = 2;
- }
- else if (n <= interval_three){
- result[i] = 3;
- }
- else if (n <= interval_four){
- result[i] = 4;
- }
- }
- return result;
- }
-
- //交叉操作
- public static String[] mix(int []indivXY){
- String []transBinary = new String[8];
- String []resGene = new String[4];
- int n = indivXY.length;
- for (int i = 0; i < n ;i++){
- if (indivXY[i]<=1){
- transBinary[i] = "000"+Integer.toBinaryString(indivXY[i]);
- }
- else if (indivXY[i]<=3){
- transBinary[i] = "00"+Integer.toBinaryString(indivXY[i]);
- }
- else if (indivXY[i]<=7){
- transBinary[i] = "0"+Integer.toBinaryString(indivXY[i]);
- }
- }
- //拼接
- for (int j = 0 ,i = 0; i < n; j++,i += 2){
- resGene[j] = transBinary[i]+transBinary[i+1];
- }
- Random transPos = new Random();
- //两两交叉
- for (int i = 0; i < resGene.length; i+=2){
- int pos = transPos.nextInt(8);
- String temp = resGene[i].substring(pos,8);//临时保存子串
- resGene[i] = resGene[i].substring(0,pos) + resGene[i+1].substring(pos,8);
- resGene[i+1] = resGene[i+1].substring(0,pos)+temp;
- }
- //交叉结果
- return resGene;
- }
- //变异操作
- public static int[][] variation(String []indiv){
- int [][]res = new int[4][2];
- int vary_prob = 20;//变异概率
- Random n = new Random();
- for (int i = 0; i < indiv.length; i++) {
- int j = n.nextInt(100);
- if (j <= vary_prob){
- int pos = n.nextInt(8);
- char []temp = indiv[i].toCharArray();
- if (temp[pos] == '0'){
- temp[pos] = '1';
- }
- else {
- temp[pos] = '0';
- }
- indiv[i] = String.valueOf(temp);
- }
- }
- for (int i = 0;i<indiv.length;i++){
- res[i][0] = Integer.parseInt(indiv[i].substring(1,4),2);
- res[i][1] = Integer.parseInt(indiv[i].substring(5,8),2);
- }
- return res;
- }
- //适应度函数
- public static int FitDegree(int x1,int x2) {
- return x1*x1 + x2*x2;
- }
-
- public static int TheBestDegree(int []Degree) {
- int bestDegree = Degree[1];
- for (int i = 0;i<Degree.length;i++){
- if (bestDegree < Degree[i]){
- bestDegree = Degree[i];
- }
- }
- return bestDegree;
- }
-
- public static String TheBestGene(int [][]indivXY) {
- String res = "";
- int []degree = new int[4];
- for (int i =0;i<4;i++){
- degree[i] = FitDegree(indivXY[i][0],indivXY[i][1]);
- }
- int bestDegree = degree[1];
- int bestGeneNum = 1;
- for (int i = 0;i<degree.length;i++){
- if (bestDegree < degree[i]){
- bestDegree = degree[i];
- bestGeneNum = i;
- }
- }
- for (int i =0;i<indivXY[i].length;i++){
- if (indivXY[bestGeneNum][i]<=1){
- res += "000"+Integer.toBinaryString(indivXY[bestGeneNum][i]);
- }
- else if (indivXY[bestGeneNum][i]<=3){
- res += "00"+Integer.toBinaryString(indivXY[bestGeneNum][i]);
- }
- else if (indivXY[bestGeneNum][i]<=7){
- res += "0"+Integer.toBinaryString(indivXY[bestGeneNum][i]);
- }
- }
-
- return res;
- }
-
- public static void main(String[] args) {
- int [][]parent = new int[4][2];
-
- parent[0][0] = x1Init();
- parent[0][1] = x2Init();
-
- parent[1][0] = x1Init();
- parent[1][1] = x2Init();
-
- parent[2][0] = x1Init();
- parent[2][1] = x2Init();
-
- parent[3][0] = x1Init();
- parent[3][1] = x2Init();
-
- int [][]progeny = parent;
-
- System.out.println("初代种群:");
- for (int i=0;i<parent.length;i++){
- for (int j = 0;j< parent[i].length;j++){
- System.out.print(parent[i][j]+" ");
- }
- System.out.println();
- }
- while (generation < 100){
- generation++;
- int []res = new int[8];
- int []progenyFit = new int[4];
- //计算适应度
- progenyFit[0] = FitDegree(progeny[0][0],progeny[0][1]);
- progenyFit[1] = FitDegree(progeny[1][0],progeny[1][1]);
- progenyFit[2] = FitDegree(progeny[2][0],progeny[2][1]);
- progenyFit[3] = FitDegree(progeny[3][0],progeny[3][1]);
-
- int TheBestFit = TheBestDegree(progenyFit);
- String TheBestGene = TheBestGene(progeny);
- //根据适应度概率进行选择
- int []choiceRes = Choice(progenyFit[0],progenyFit[1],progenyFit[2],progenyFit[3]);
- for (int i = 0,j = 0;i < choiceRes.length;i++,j+=2){
- if (choiceRes[i] == 1){
- res[j] = progeny[0][0];
- res[j+1] = progeny[0][1];
- }
- else if (choiceRes[i] == 2){
- res[j] = progeny[1][0];
- res[j+1] = progeny[1][1];
- }
- else if (choiceRes[i] == 3){
- res[j] = progeny[2][0];
- res[j+1] = progeny[2][1];
- }
- else if (choiceRes[i] == 4){
- res[j] = progeny[3][0];
- res[j+1] = progeny[3][1];
- }
- }
-
- String []indiv = mix(res);//交叉操作
- progeny = variation(indiv);//变异操作
- System.out.println("当前代数:"+generation+" 最佳适应度:"+TheBestFit+" 子代基因型:"+TheBestGene);
- // if (TheBestFit == 98 ){
- // break;
- // }
- }
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。