sandeshMC

Genetic Algorithm

Apr 10th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.56 KB | None | 0 0
  1. package codechef;
  2.  
  3. import java.util.*;
  4.  
  5. public class Demo {
  6.  
  7.     static ArrayList<Integer> a = new ArrayList<Integer>();
  8.     static ArrayList<Integer> fx = new ArrayList<Integer>();
  9.     static ArrayList<Double> probi = new ArrayList<Double>();
  10.     static ArrayList<Double> ei = new ArrayList<Double>();
  11.     static ArrayList<Integer> ac = new ArrayList<Integer>();
  12.  
  13.     public static void main(String args[]) {
  14.         Scanner sc = new Scanner(System.in);
  15.  
  16.         System.out.println("Enter the number of initial chromosomes");
  17.         int n = sc.nextInt();
  18.         System.out.println("Enter the initial population");
  19.  
  20.         for (int i = 0; i < n; i++) {
  21.             a.add(sc.nextInt());
  22.         }
  23.  
  24.         int sum = 0;
  25.         int max = 0;
  26.         double avg = 0;
  27.         for (int i = 0; i < n; i++) {
  28.             int temp = a.get(i) * a.get(i);
  29.             fx.add(temp);
  30.             sum = sum + temp;
  31.             if (temp >= max) {
  32.                 max = temp;
  33.             }
  34.         }
  35.  
  36.         avg = sum / n;
  37.  
  38.         for (int i = 0; i < n; i++) {
  39.             double temp = (double) fx.get(i) / sum;
  40.             probi.add(temp);
  41.  
  42.         }
  43.  
  44.         for (int i = 0; i < n; i++) {
  45.             double temp = fx.get(i) / avg;
  46.             ei.add(temp);
  47.             if (Math.ceil(temp) - temp <= temp - Math.floor(temp)) {
  48.                 ac.add((int) Math.ceil(temp));
  49.             } else {
  50.                 ac.add((int) Math.floor(temp));
  51.             }
  52.  
  53.         }
  54.  
  55.         print();
  56.  
  57.         //Step 2
  58.         int k = 0;
  59.         for (int i = 0; i < n; i++) {
  60.             if (ac.get(i) != 0) {
  61.                 for (int j = 0; j < ac.get(i); j++) {
  62.                     int temp = a.get(i);
  63.                     a.set(k, temp);
  64.                     k++;
  65.                 }
  66.             }
  67.         }
  68.  
  69.         print();
  70.         //Cross over rate = 50%
  71.  
  72.         char finalc1[] = new char[4];
  73.         char finalc2[] = new char[4];
  74.  
  75.         for (int i = 0; i < finalc1.length; i++) {
  76.             finalc1[i] = '0';
  77.         }
  78.  
  79.         for (int i = 0; i < finalc2.length; i++) {
  80.             finalc2[i] = '0';
  81.         }
  82.  
  83.         for (int i = 0; i < n / 2; i++) {
  84.             String s1 = Integer.toBinaryString(a.get(i));
  85.             char c1[] = s1.toCharArray();
  86.  
  87.             if (c1.length < 4) {
  88.                 for (int j = c1.length - 1; j >= 0; j--) {
  89.                     finalc1[4 - 1 - j] = c1[j];
  90.                 }
  91.             } else {
  92.                 finalc1 = c1;
  93.             }
  94.  
  95.             i++;
  96.  
  97.             String s2 = Integer.toBinaryString(a.get(i));
  98.             char c2[] = s2.toCharArray();
  99.  
  100.             if (c2.length < 4) {
  101.                 for (int j = c2.length - 1; j >= 0; j--) {
  102.                     finalc2[4 - 1 - j] = c2[j];
  103.                 }
  104.             } else {
  105.                 finalc2 = c2;
  106.             }
  107.  
  108.         }
  109.  
  110.         //Cross over point = 2
  111.         System.out.println("After crossover:");
  112.  
  113.         for (int i = 2; i < finalc1.length; i++) {
  114.             char temp = finalc1[i];
  115.             finalc1[i] = finalc2[i];
  116.             finalc2[i] = temp;
  117.         }
  118.  
  119.         for (int i = 0; i < finalc1.length; i++) {
  120.             System.out.print(finalc1[i]);
  121.         }
  122.  
  123.         System.out.println();
  124.  
  125.         for (int i = 0; i < finalc2.length; i++) {
  126.             System.out.print(finalc2[i]);
  127.         }
  128.  
  129.         System.out.println();
  130.  
  131.         //Mutation point = 3 and mutation rate = 12.5% ;
  132.         if (finalc1[2] == '0') {
  133.             finalc1[2] = '1';
  134.         } else {
  135.             finalc1[2] = '0';
  136.         }
  137.  
  138.         if (finalc2[2] == '0') {
  139.             finalc2[2] = '1';
  140.         } else {
  141.             finalc2[2] = '0';
  142.         }
  143.  
  144.         System.out.println("After Mutation:");
  145.  
  146.         for (int i = 0; i < finalc1.length; i++) {
  147.             System.out.print(finalc1[i]);
  148.         }
  149.  
  150.         System.out.println();
  151.  
  152.         for (int i = 0; i < finalc2.length; i++) {
  153.             System.out.print(finalc2[i]);
  154.         }
  155.  
  156.         System.out.println();
  157.  
  158.         a.set(0, Integer.parseInt(String.valueOf(finalc1), 2));
  159.         a.set(1, Integer.parseInt(String.valueOf(finalc2), 2));
  160.         print();
  161.  
  162.         Collections.sort(a);
  163.         System.out.println(a.get(a.size() - 1) + " will yield the maximum value");
  164.  
  165.     }
  166.  
  167.     public static void print() {
  168.         System.out.println("a is " + "\t" + a);
  169.         System.out.println("fx is " + "\t" + fx);
  170.         System.out.println("pi is " + "\t" + probi);
  171.         System.out.println("ei is " + "\t" + ei);
  172.         System.out.println("ac is " + "\t" + ac);
  173.         //System.out.println("The sum is "+ sum + ", the average is "+avg + " and the max is "+max);
  174.  
  175.     }
  176.  
  177. }
  178.  
  179.  
  180. /*OUTPUT:
  181. Enter the number of initial chromosomes
  182. 4
  183. Enter the initial population
  184. 1
  185. 2
  186. 5
  187. 8
  188. a is    [1, 2, 5, 8]
  189. fx is   [1, 4, 25, 64]
  190. pi is   [0.010638297872340425, 0.0425531914893617, 0.26595744680851063, 0.6808510638297872]
  191. ei is   [0.043478260869565216, 0.17391304347826086, 1.0869565217391304, 2.782608695652174]
  192. ac is   [0, 0, 1, 3]
  193. a is    [5, 8, 8, 8]
  194. fx is   [1, 4, 25, 64]
  195. pi is   [0.010638297872340425, 0.0425531914893617, 0.26595744680851063, 0.6808510638297872]
  196. ei is   [0.043478260869565216, 0.17391304347826086, 1.0869565217391304, 2.782608695652174]
  197. ac is   [0, 0, 1, 3]
  198. After crossover:
  199. 0100
  200. 1001
  201. After Mutation:
  202. 0110
  203. 1011
  204. a is    [6, 11, 8, 8]
  205. fx is   [1, 4, 25, 64]
  206. pi is   [0.010638297872340425, 0.0425531914893617, 0.26595744680851063, 0.6808510638297872]
  207. ei is   [0.043478260869565216, 0.17391304347826086, 1.0869565217391304, 2.782608695652174]
  208. ac is   [0, 0, 1, 3]
  209. 11 will yield the maximum value
  210. */
Add Comment
Please, Sign In to add comment