Advertisement
dimon2242

GenAlg

Jun 29th, 2017
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.92 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.Random;
  4.  
  5. /**
  6.  * Created by Dmitry Teplyakov on 28.06.17.
  7.  */
  8. public class GenAlg {
  9.     private static double fPow(float a, float b) {
  10.         if(b == 0)  return 1;
  11.         return a * fPow(a, b-1);
  12.     }
  13.     private static double calcFunc(int a, int b, int c, int d, Individ individForCalc) {
  14.         int x = individForCalc.getFenotype();
  15.         double res =  a+b*x + c*fPow(x, 2) + d*fPow(x, 3);
  16.         //System.out.println(res + " " + x);
  17.         return res;
  18.     }
  19.  
  20.     public static void main(String args[]) {
  21.         Random random = new Random();
  22.         ArrayList<Individ> populationList = new ArrayList<>();
  23.         int a, b, c, d, iterations;
  24.         a = 28;
  25.         b = -28;
  26.         c = -64;
  27.         d = 1;
  28.         iterations = 50;
  29.         boolean isFirst = true;
  30.         //for(int i = 0; i < 4; i++)
  31.         //    populationList.add(new Individ(random.nextInt()));
  32.         ArrayList<Individ> childrenIndivid = new ArrayList<>();
  33.         //System.out.println(String.format("%8s|%8s", Integer.toBinaryString(populationList.get(0).getChromosome()), Integer.toBinaryString(populationList.get(1).getChromosome())).replace(' ', '0'));
  34.         Individ min = null;
  35.         Individ max = null;
  36.         int num = 4;
  37.         int counter = 0;
  38.         int locRand;
  39.         while(counter != iterations) {
  40.             int firstRand, secondRand, count = 0;
  41.             while (count < 2) {
  42.                 firstRand = random.nextInt(4);
  43.                 secondRand = random.nextInt(4);
  44.                 if(isFirst) {
  45.                     for(int i = 0; i < 2; i++)
  46.                         populationList.add(new Individ(random.nextInt()));
  47.                     System.out.println("FIRST");
  48.                     count++;
  49.  
  50.                 }
  51.                 else {
  52.                     if (populationList.get(firstRand).getId() != populationList.get(secondRand).getId()) {
  53.                         childrenIndivid.add(new Individ(populationList.get(firstRand), populationList.get(secondRand)));
  54.                         childrenIndivid.add(new Individ(populationList.get(secondRand), populationList.get(firstRand)));
  55.                         count++;
  56.                     }
  57.                 }
  58.  
  59.                 if(count == 2) { System.out.println(String.format("%6s|%6s|%6s|%6s", Integer.toBinaryString(populationList.get(0).getChromosome()), Integer.toBinaryString(populationList.get(1).getChromosome()),
  60.                         Integer.toBinaryString(populationList.get(2).getChromosome()), Integer.toBinaryString(populationList.get(3).getChromosome())).replace(' ', '0'));
  61.                 if(!isFirst) {
  62.                     locRand = random.nextInt(num); childrenIndivid.get(locRand).mutation(); System.out.println("MUTATION in element: " + locRand + " After mutation: " + String.format("%6s", Integer.toBinaryString(childrenIndivid.get(locRand).getChromosome())).replace(' ', '0'));
  63.                 }
  64.                 else {
  65.                     locRand = random.nextInt(num); populationList.get(locRand).mutation(); System.out.println("MUTATION in element: " + locRand + " After mutation: " + String.format("%6s", Integer.toBinaryString(populationList.get(locRand).getChromosome())).replace(' ', '0'));
  66.                 }
  67.  
  68.                 }
  69.             }
  70.  
  71.                 for (Individ ind : populationList)
  72.                     ind.setResult(calcFunc(a, b, c, d, ind));
  73.  
  74.                 for (Individ ind : childrenIndivid)
  75.                     ind.setResult(calcFunc(a, b, c, d, ind));
  76.  
  77.                 if(counter < iterations/2) {
  78.                     int ind;
  79.                     for (int i = 1; i < populationList.size(); i++) {
  80.                         ind = i - 1;
  81.                         while ((ind >= 0) && (populationList.get(ind).getResult() > populationList.get(ind + 1).getResult())) {
  82.                             Collections.swap(populationList, ind, ind + 1);
  83.                             ind--;
  84.                         }
  85.                     }
  86.                     if (!isFirst) {
  87.                         for (int i = 1; i < childrenIndivid.size(); i++) {
  88.  
  89.                             ind = i - 1;
  90.                             while ((ind >= 0) && (childrenIndivid.get(ind).getResult() > childrenIndivid.get(ind + 1).getResult())) {
  91.                                 Collections.swap(childrenIndivid, ind, ind + 1);
  92.                                 ind--;
  93.                             }
  94.                         }
  95.                         max = populationList.get(3);
  96.                     }
  97.                 }
  98.                 else {
  99.                     int ind;
  100.                     for (int i = 1; i < populationList.size(); i++) {
  101.  
  102.                         ind = i - 1;
  103.                         while ((ind >= 0) && (populationList.get(ind).getResult() < populationList.get(ind + 1).getResult())) {
  104.                             Collections.swap(populationList, ind, ind + 1);
  105.                             ind--;
  106.                         }
  107.                     }
  108.                     if (!isFirst) {
  109.                         for (int i = 1; i < childrenIndivid.size(); i++) {
  110.                             ind = i - 1;
  111.                             while ((ind >= 0) && (childrenIndivid.get(ind).getResult() < childrenIndivid.get(ind + 1).getResult())) {
  112.                                 Collections.swap(childrenIndivid, ind, ind + 1);
  113.                                 ind--;
  114.                             }
  115.                         }
  116.                         min = populationList.get(3);
  117.                     }
  118.                 }
  119.  
  120.                 if(!isFirst)
  121.                     for (int i = 0; i < 2; i++) {
  122.                            populationList.remove(0);
  123.                            childrenIndivid.remove(0);
  124.                     }
  125.                 for (Individ indiv : populationList)
  126.                     System.out.println("PopulationList Sorted: " + indiv.getResult() + " Fenotype: " + indiv.getFenotype());
  127.                 if(!isFirst)
  128.                     for (Individ indiv : childrenIndivid)
  129.                         System.out.println("ChildrenIndivid Sorted: " + indiv.getResult() + " Fenotype: " + indiv.getFenotype());
  130.                 if(!isFirst) {
  131.                     populationList.add(childrenIndivid.get(0));
  132.                     populationList.add(childrenIndivid.get(1));
  133.  
  134.                     for (int i = 0; i < 2; i++) {
  135.                         childrenIndivid.remove(0);
  136.                     }
  137.                 }
  138.                 isFirst = false;
  139.             counter++;
  140.         }
  141.         System.out.println("MIN Func: " + min.getResult() + " Chromosome: " + String.format("%6s", Integer.toBinaryString(min.getChromosome())).replace(' ', '0') + " Fenotype: " + min.getFenotype());
  142.         System.out.println("MAX Func: " + max.getResult() + " Chromosome: " + String.format("%6s", Integer.toBinaryString(max.getChromosome())).replace(' ', '0') + " Fenotype: " + max.getFenotype());
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement