TournamentSelectionStrategy.java 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package algorithms.geneticAlgorithm.Components.Selections;
  2. import java.util.ArrayList;
  3. import java.util.Random;
  4. import algorithms.geneticAlgorithm.Components.GAIndividual;
  5. import algorithms.geneticAlgorithm.Components.GASelectionStrategy;
  6. public class TournamentSelectionStrategy<I extends GAIndividual> implements GASelectionStrategy<I> {
  7. public class Range{
  8. public double min;
  9. public double max;
  10. public Range(double min, double max){
  11. this.min = min;
  12. this.max = max;
  13. }
  14. }
  15. private ArrayList<I> currentPop;
  16. public int tournamentSize;
  17. private Random rng;
  18. private ArrayList<Range> tournamentWheel;
  19. public ArrayList<I> competitors;
  20. public double selectProb;
  21. private double maxRange;
  22. public TournamentSelectionStrategy(){
  23. currentPop = new ArrayList<I>();
  24. tournamentSize = 1;
  25. rng = new Random();
  26. selectProb = 1;
  27. }
  28. public TournamentSelectionStrategy(int tSize, double selectProb){
  29. currentPop = new ArrayList<I>();
  30. rng = new Random();
  31. tournamentSize = tSize;
  32. this.selectProb = selectProb;
  33. }
  34. @Override
  35. public void setCurrentPopulation(ArrayList<I> population) {
  36. currentPop = 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. for(int i = 0; i < tournamentSize; i++){
  62. I candidate = chooseCandidate();
  63. int size = competitors.size();
  64. for(int j = 0; j <= size; j++){
  65. if(j != size){
  66. if(competitors.get(j).getFittness() < candidate.getFittness()){
  67. competitors.add(j, candidate);
  68. break;
  69. }
  70. }else{
  71. competitors.add(candidate);
  72. }
  73. }
  74. }
  75. }
  76. public I chooseCandidate(){
  77. int index = rng.nextInt(currentPop.size());
  78. I candidate = currentPop.get(index);
  79. return candidate;
  80. }
  81. public void initWheel(){
  82. tournamentWheel = new ArrayList<Range>();
  83. int power = 0;
  84. maxRange = 0;
  85. for(int i = 0; i < tournamentSize; i++){
  86. double currentProb = selectProb * Math.pow((1- selectProb), power);
  87. Range range = new Range(maxRange, maxRange + currentProb);
  88. tournamentWheel.add(range);
  89. maxRange += currentProb;
  90. power++;
  91. }
  92. }
  93. }