TournamentRankSelection.java 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package algorithms.geneticAlgorithm.Components.Selections;
  2. import java.util.ArrayList;
  3. import java.util.Collections;
  4. import java.util.Random;
  5. import algorithms.geneticAlgorithm.Components.GAIndividual;
  6. import algorithms.geneticAlgorithm.Components.GASelectionStrategy;
  7. public class TournamentRankSelection<I extends GAIndividual> implements GASelectionStrategy<I>{
  8. public class Range{
  9. public double min;
  10. public double max;
  11. public Range(double min, double max){
  12. this.min = min;
  13. this.max = max;
  14. }
  15. }
  16. private ArrayList<I> currentPop;
  17. public int tournamentSize;
  18. private Random rng;
  19. private ArrayList<Range> tournamentWheel;
  20. public ArrayList<I> competitors;
  21. //public double selectProb;
  22. private double maxRange;
  23. public TournamentRankSelection(){
  24. currentPop = new ArrayList<I>();
  25. tournamentSize = 1;
  26. rng = new Random();
  27. }
  28. public TournamentRankSelection(int tSize){
  29. currentPop = new ArrayList<I>();
  30. rng = new Random();
  31. tournamentSize = tSize;
  32. }
  33. @Override
  34. public void setCurrentPopulation(ArrayList<I> population) {
  35. currentPop.clear();
  36. currentPop.addAll(population);
  37. }
  38. public void setTournamentSize(int size){
  39. tournamentSize = size;
  40. }
  41. @Override
  42. public ArrayList<I> selectIndividuals() {
  43. ArrayList<I> parents = new ArrayList<I>();
  44. parents.add(selectSingleIndividual());
  45. parents.add(selectSingleIndividual());
  46. return parents;
  47. }
  48. public I selectSingleIndividual(){
  49. tournamentSelection();
  50. initWheel();
  51. double index = rng.nextDouble() * maxRange;
  52. for(int i = 0; i < tournamentWheel.size(); i++){
  53. if(index >= tournamentWheel.get(i).min && index <= tournamentWheel.get(i).max){
  54. return competitors.get(i);
  55. }
  56. }
  57. return competitors.get(rng.nextInt(competitors.size()));
  58. }
  59. public void tournamentSelection(){
  60. competitors = new ArrayList<I>();
  61. Collections.shuffle(currentPop);
  62. for(int i = 0; i < tournamentSize; i++){
  63. //I candidate = chooseCandidate();
  64. I candidate = currentPop.get(i);
  65. int size = competitors.size();
  66. for(int j = 0; j <= size; j++){
  67. if(j != size){
  68. if(competitors.get(j).getFittness() < candidate.getFittness()){
  69. competitors.add(j, candidate);
  70. break;
  71. }
  72. }else{
  73. competitors.add(candidate);
  74. }
  75. }
  76. }
  77. }
  78. public I chooseCandidate(){
  79. int index = rng.nextInt(currentPop.size());
  80. I candidate = currentPop.get(index);
  81. return candidate;
  82. }
  83. public void initWheel(){
  84. tournamentWheel = new ArrayList<Range>();
  85. int power = 0;
  86. maxRange = 0;
  87. for(int i = 0; i < tournamentSize; i++){
  88. Range range = new Range(maxRange, maxRange + tournamentSize - i);
  89. tournamentWheel.add(range);
  90. maxRange += tournamentSize - i;
  91. /*
  92. double currentProb = selectProb * Math.pow((1- selectProb), power);
  93. Range range = new Range(maxRange, maxRange + currentProb);
  94. tournamentWheel.add(range);
  95. maxRange += currentProb;
  96. power++;
  97. */
  98. }
  99. }
  100. }