See Address:
https://www.jianshu.com/p/ae5157c26af9
Code:
GA class {public Private ChrNum int = 10; // number of chromosomes private String [] ipop = new String [ChrNum]; // Number of chromosomes in a population private int generation = 0; // chromosome code public static final int GENE = 46 ; // number of genes private double bestfitness = Double.MAX_VALUE; // function optimal solution private int bestgenerations; // All the best parent and offspring chromosomes private String beststr; // chromosome optimum binary code / ** * initialize a chromosome (represented by binary strings) * / Private initChr string () { string RES = ""; for (int I = 0; I <the GENE; I ++) { IF (Math.random ()> 0.5) { RES + = "0"; } the else { RES = + ". 1"; } } return RES; } / ** * initialize a population (10 chromosomes) * / Private String [] initPop () { String [] iPOP = new new String [ChrNum]; for (int I = 0; I <ChrNum; I ++) { iPOP [I ] = initChr (); } return iPOP; } / ** * converted into a chromosome values of x, y variables * / Private Double [] calculatefitnessvalue (string STR) { // binary 23 before the binary string x after 23 to binary string y int a = the Integer.parseInt (str.substring (0, 23 is), 2); int B = the Integer.parseInt (str.substring (23 is, 46 is), 2); Double X = a * (6.0 - 0) / (Math.pow (2, 23) - 1); // gene of x double y = b * (6.0 - 0) / (Math.pow (2, 23) - 1) ; // Y gene // function to be optimized double fitness = 3 - Math.sin (2 * x) * Math.sin (2 * x) - Math.sin, (Y * 2) * Math.sin, (2 * Y); Double [] Returns = {X, Y, Fitness}; ; return Returns } / ** * roulette select each of the individual groups calculated * fitness value; * according to a rule determined by the selected individual fitness value of the individual entering the next generation; * / Private void sELECT () { Double evals [] = new new Double [ChrNum]; // all chromosomes adaptation value double p [] = new [ChrNum ] double; // the probability of selecting each chromosome double q [] = new double [ ChrNum]; // cumulative probability double F = 0; // integrated adaptation sum value for (int i = 0 ; I <ChrNum; I ++) { evals [I] = calculatefitnessvalue (iPOP [I]) [2]; IF (evals [I] <bestfitness) {// record the minimum value in the population, i.e. the optimal solution } bestfitness = evals [i]; bestgenerations = Generation; beststr = iPOP [I]; F. = F. + evals [I]; // all chromosomes adaptation sum value } for (int I = 0; I <ChrNum; I ++) { P [I] = evals [I ] / F.; IF (I == 0) Q [I] = P [I]; the else { Q [I] = Q [I -. 1] + P [I]; } } for (int I = 0; I <ChrNum; I ++) { Double = R & lt Math.random (); IF (R & lt <= Q [0]) { iPOP [I] = iPOP [0]; } the else { for (int. 1 = J; J <ChrNum; ++ J) { IF (R & lt <Q [J]) { iPOP [I] = iPOP [J]; } } } } } / ** * crossover intersection was 60%, with an average of 60% of the cross-chromosome * / private void cross() { String temp1, temp2; For (int I = 0; I <ChrNum; I ++) { IF (Math.random () <0.60) { int POS = (int) (Math.random () * the GENE) + 1'd; // POS site binary string longitudinal cross temp1 = iPOP [I] .substring (0, POS) + iPOP [(I +. 1)% ChrNum] .substring (POS); temp2 of iPOP = [(I +. 1)% ChrNum] .substring (0 , POS) + iPOP [I] .substring (POS); iPOP [I] = temp1; iPOP = temp2 of [(I +. 1) / ChrNum]; } } } / ** * mutation genetic variation operation 1% * / void mutation Private () { for (int I = 0; I <. 4; I ++) { int NUM = (int) (Math.random () * * ChrNum the GENE +. 1); int chromosomeNum = (int) (NUM / the GENE ) + 1; // chromosome number int mutationNum = num - (chromosomeNum - 1) * gENE; // number gene IF (mutationNum == 0) mutationNum =. 1; chromosomeNum chromosomeNum = -. 1; IF (chromosomeNum> = ChrNum) chromosomeNum =. 9; String TEMP; String A; // record the encoded mutation site variants if ([chromosomeNum] ipop .charAt (mutationNum - 1) == ' 0') {// when the mutation site is 0, A = ". 1"; } the else { A = "0"; } // if mutation site in the first, middle and mutation at the tail IF (mutationNum ==. 1) { TEMP = a + iPOP [chromosomeNum] .substring (mutationNum); } {the else IF (= mutationNum the GENE!) { } else { TEMP = iPOP [chromosomeNum] .substring (0, mutationNum -1) + A IPOP + [chromosomeNum] .substring (mutationNum); TEMP = iPOP [chromosomeNum] .substring (0, mutationNum -. 1) + A; } } // record Chromosomal Variation iPOP [chromosomeNum] = TEMP; } } public static main void (String args []) { GA GA tryer new new = (); Tryer.ipop Tryer.initPop = (); // initial population String STR = ""; // number of iterations for (int i = 0; i <1000000; I ++) { Tryer.select (); Tryer.cross (); Tryer.mutation (); Tryer.generation = I; } Double [] X = Tryer.calculatefitnessvalue (Tryer.beststr); STR = "Min "+ Tryer.bestfitness + '\ n' +" No. " + Tryer.bestgenerations + "chromosomes: <" + + Tryer.beststr ">" + '\ n-' + "= X" + X [0] + '\ n-'+ "y=" + x[1]; System.out.println (STR); } }