Posted By

a2-hopewell on 03/21/14


Tagged


Versions (?)

Population


 / Published in: Java
 

Population class for Genetic Algorithm

  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package simplega;
  6.  
  7. import java.util.Random;
  8.  
  9. /**
  10.  *
  11.  * @author a2-hopewell
  12.  */
  13. public class Population {
  14.  
  15. final static int popsize = 1000;
  16. final static int tourSize = 12;
  17. final static double crossRate = 0.7;
  18. final static double uniformRate = 0.5;
  19. final static double mutateRate = 0.02;
  20.  
  21. Random rand = new Random();
  22. public Gene[] initialPopulation;
  23. public Gene[] postSelection;
  24. public Gene[] postCrossover;
  25. public Gene[] postMutation;
  26. double total;
  27.  
  28. public Population()
  29. {
  30. this.initialPopulation = new Gene[popsize];
  31. this.postSelection = new Gene[popsize];
  32. this.postCrossover = new Gene[popsize];
  33. this.postMutation = new Gene[popsize];
  34. this.total = 0.0;
  35. }
  36.  
  37. public Gene[] generatePop(Gene dataset)
  38. {
  39. for(int p = 0; p < popsize; p++)
  40. {
  41. initialPopulation[p] = new Gene();
  42. initialPopulation[p].generateGene();
  43. initialPopulation[p].evalFitness(initialPopulation[p], dataset);
  44. total += initialPopulation[p].getFitness();
  45. //System.out.println(initialPopulation[p]);
  46. }
  47. System.out.print("Total: " + total);
  48.  
  49. return initialPopulation;
  50. }
  51.  
  52. public Gene[] newOffspring(Gene dataset)
  53. {
  54. for(int p = 0; p < popsize; p++)
  55. {
  56. postMutation[p].evalFitness(postMutation[p], dataset);
  57. total += postMutation[p].getFitness();
  58. }
  59. System.out.print("Total: " + total);
  60.  
  61. return postMutation;
  62. }
  63.  
  64. public Gene getIndividual(int index)
  65. {
  66. return initialPopulation[index];
  67. }
  68.  
  69. public int getBestFitness(Gene[] population)
  70. {
  71. int fitness = 0;
  72.  
  73. for(int i = 0; i < popsize; i++)
  74. {
  75. if(population[i].getFitness() > fitness)
  76. {
  77. fitness = population[i].getFitness();
  78. }
  79. }
  80. System.out.print(" | Best: " + fitness);
  81.  
  82. return fitness;
  83. }
  84.  
  85. public double getMeanFitness()
  86. {
  87. double meanFitness;
  88.  
  89. meanFitness = (total / popsize);
  90. System.out.print(" | Mean: " + meanFitness + "\n");
  91.  
  92. return meanFitness;
  93. }
  94.  
  95. public Gene[] rouletteWheel(Gene[] population)
  96. {
  97. initialPopulation = population;
  98.  
  99. int r, tempTotal = 0;
  100.  
  101. for(int i = 0; i < popsize; i++)
  102. {
  103. int roulette = rand.nextInt((int)total + 1);
  104.  
  105. if(roulette == 0)
  106. {
  107. roulette = rand.nextInt((int)total + 1);
  108. }
  109. for(r = 0; tempTotal < roulette; r++)
  110. {
  111. tempTotal += initialPopulation[r].getFitness();
  112. }
  113. postSelection[i] = initialPopulation[r-1];
  114. tempTotal = 0;
  115. }
  116.  
  117. return postSelection;
  118. }
  119.  
  120. public Gene[] tournament(Gene[] population)
  121. {
  122. initialPopulation = population;
  123. int fitness = 0;
  124.  
  125. for(int i = 0; i < popsize; i++)
  126. {
  127. for(int t = 0; t < tourSize; t++)
  128. {
  129. int tour = rand.nextInt(popsize);
  130. if(initialPopulation[tour].getFitness() > fitness)
  131. {
  132. postSelection[i] = initialPopulation[tour];
  133. fitness = initialPopulation[tour].getFitness();
  134. }
  135. }
  136. fitness = 0;
  137. }
  138.  
  139. return postSelection;
  140. }
  141.  
  142. public Gene[] crossover()
  143. {
  144. for(int c = 0; c < popsize; c += 2)
  145. {
  146. if(rand.nextDouble() <= crossRate)
  147. {
  148. postCrossover[c] = new Gene();
  149. postCrossover[c+1] = new Gene();
  150.  
  151. int i;
  152. int crossPoint = rand.nextInt(Gene.genelength);
  153.  
  154. for(i = 0; i < crossPoint; i++)
  155. {
  156. postCrossover[c].setGene(i, postSelection[c].getGene(i));
  157. postCrossover[c+1].setGene(i, postSelection[c+1].getGene(i));
  158. }
  159. for( ; i < Gene.genelength; i++)
  160. {
  161. postCrossover[c].setGene(i, postSelection[c+1].getGene(i));
  162. postCrossover[c+1].setGene(i, postSelection[c].getGene(i));
  163. }
  164. }
  165. else
  166. {
  167. postCrossover[c] = postSelection[c];
  168. postCrossover[c+1] = postSelection[c+1];
  169. }
  170. }
  171.  
  172. return postCrossover;
  173. }
  174.  
  175. public Gene[] uniform()
  176. {
  177. for(int c = 0; c < popsize; c += 2)
  178. {
  179. if(rand.nextDouble() <= crossRate)
  180. {
  181. postCrossover[c] = new Gene();
  182. postCrossover[c+1] = new Gene();
  183.  
  184. for(int i = 0; i < Gene.genelength; i += 6)
  185. {
  186. if(rand.nextDouble() <= uniformRate)
  187. {
  188. postCrossover[c].setGene(i, postSelection[c+1].getGene(i));
  189. postCrossover[c+1].setGene(i, postSelection[c].getGene(i));
  190. postCrossover[c].setGene(i+1, postSelection[c+1].getGene(i+1));
  191. postCrossover[c+1].setGene(i+1, postSelection[c].getGene(i+1));
  192. postCrossover[c].setGene(i+2, postSelection[c+1].getGene(i+2));
  193. postCrossover[c+1].setGene(i+2, postSelection[c].getGene(i+2));
  194. postCrossover[c].setGene(i+3, postSelection[c+1].getGene(i+3));
  195. postCrossover[c+1].setGene(i+3, postSelection[c].getGene(i+3));
  196. postCrossover[c].setGene(i+4, postSelection[c+1].getGene(i+4));
  197. postCrossover[c+1].setGene(i+4, postSelection[c].getGene(i+4));
  198. postCrossover[c].setGene(i+5, postSelection[c+1].getGene(i+5));
  199. postCrossover[c+1].setGene(i+5, postSelection[c].getGene(i+5));
  200. }
  201. else
  202. {
  203. postCrossover[c].setGene(i, postSelection[c].getGene(i));
  204. postCrossover[c+1].setGene(i, postSelection[c+1].getGene(i));
  205. postCrossover[c].setGene(i+1, postSelection[c].getGene(i+1));
  206. postCrossover[c+1].setGene(i+1, postSelection[c+1].getGene(i+1));
  207. postCrossover[c].setGene(i+2, postSelection[c].getGene(i+2));
  208. postCrossover[c+1].setGene(i+2, postSelection[c+1].getGene(i+2));
  209. postCrossover[c].setGene(i+3, postSelection[c].getGene(i+3));
  210. postCrossover[c+1].setGene(i+3, postSelection[c+1].getGene(i+3));
  211. postCrossover[c].setGene(i+4, postSelection[c].getGene(i+4));
  212. postCrossover[c+1].setGene(i+4, postSelection[c+1].getGene(i+4));
  213. postCrossover[c].setGene(i+5, postSelection[c].getGene(i+5));
  214. postCrossover[c+1].setGene(i+5, postSelection[c+1].getGene(i+5));
  215. }
  216. }
  217. }
  218. else
  219. {
  220. postCrossover[c] = postSelection[c];
  221. postCrossover[c+1] = postSelection[c+1];
  222. }
  223. }
  224.  
  225. return postCrossover;
  226. }
  227.  
  228. public Gene[] mutation()
  229. {
  230. for(int i = 0; i < popsize; i++)
  231. {
  232. postMutation[i] = new Gene();
  233.  
  234. for(int m = 0; m < Gene.genelength; m += 6)
  235. {
  236. if(rand.nextDouble() <= mutateRate)
  237. {
  238. postMutation[i].setGene(m, 1 - postCrossover[i].getGene(m));
  239. postMutation[i].setGene(m+1, 1 - postCrossover[i].getGene(m+1));
  240. postMutation[i].setGene(m+2, 1 - postCrossover[i].getGene(m+2));
  241. postMutation[i].setGene(m+3, 1 - postCrossover[i].getGene(m+3));
  242. postMutation[i].setGene(m+4, 1 - postCrossover[i].getGene(m+4));
  243. postMutation[i].setGene(m+5, 1 - postCrossover[i].getGene(m+5));
  244. }
  245. else
  246. {
  247. postMutation[i].setGene(m, postCrossover[i].getGene(m));
  248. postMutation[i].setGene(m+1, postCrossover[i].getGene(m+1));
  249. postMutation[i].setGene(m+2, postCrossover[i].getGene(m+2));
  250. postMutation[i].setGene(m+3, postCrossover[i].getGene(m+3));
  251. postMutation[i].setGene(m+4, postCrossover[i].getGene(m+4));
  252. postMutation[i].setGene(m+5, postCrossover[i].getGene(m+5));
  253. }
  254. }
  255. }
  256.  
  257. return postMutation;
  258. }
  259.  
  260. }

Report this snippet  

You need to login to post a comment.