Changeset 15779


Ignore:
Timestamp:
02/15/18 08:49:41 (18 months ago)
Author:
gkronber
Message:

#2901: implemented a static method for tournament selection

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2901_StaticSelectionMethods/HeuristicLab.Selection/3.3/TournamentSelector.cs

    r15583 r15779  
    6969
    7070      for (int i = 0; i < count; i++) {
    71         int best = random.Next(scopes.Count);
    72         int index;
    73         for (int j = 1; j < groupSize; j++) {
    74           index = random.Next(scopes.Count);
    75           if (((maximization) && (qualities[index] > qualities[best])) ||
    76               ((!maximization) && (qualities[index] < qualities[best]))) {
    77             best = index;
    78           }
    79         }
     71        var selectedIdx = SelectIdx(random, qualities, maximization, groupSize);
    8072
    8173        if (copy)
    82           selected[i] = (IScope)scopes[best].Clone();
     74          selected[i] = (IScope)scopes[selectedIdx].Clone();
    8375        else {
    84           selected[i] = scopes[best];
    85           scopes.RemoveAt(best);
    86           qualities.RemoveAt(best);
     76          selected[i] = scopes[selectedIdx];
     77          scopes.RemoveAt(selectedIdx);
     78          qualities.RemoveAt(selectedIdx);
    8779        }
    8880      }
    8981      return selected;
    9082    }
     83
     84    public static int SelectIdx(IRandom random, IList<double> qualities, bool maximization, int groupSize) {
     85      int best = random.Next(qualities.Count);
     86      int index;
     87      for (int j = 1; j < groupSize; j++) {
     88        index = random.Next(qualities.Count);
     89        if (((maximization) && (qualities[index] > qualities[best])) ||
     90            ((!maximization) && (qualities[index] < qualities[best]))) {
     91          best = index;
     92        }
     93      }
     94
     95      return best;
     96    }
    9197  }
    9298}
Note: See TracChangeset for help on using the changeset viewer.