Free cookie consent management tool by TermsFeed Policy Generator

Changeset 3137


Ignore:
Timestamp:
03/20/10 03:49:15 (14 years ago)
Author:
swagner
Message:

Fixed IndexOutOfRangeExceptions in selectors (#930).

Location:
trunk/sources/HeuristicLab.Selection/3.3
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Selection/3.3/BestSelector.cs

    r3128 r3137  
    5353
    5454      if (copy) {
    55         for (int i = 0, j = 0; i < count; i++, j++) {
     55        int j = 0;
     56        for (int i = 0; i < count; i++) {
     57          selected[i] = (IScope)scopes[list[j].index].Clone();
     58          j++;
    5659          if (j >= list.Count) j = 0;
    57           selected[i] = (IScope)scopes[list[j].index].Clone();
    5860        }
    5961      } else {
    60         int i;
    61         for (i = 0; i < count; i++) {
    62           selected[i] = scopes[list[i].index];
     62        for (int i = 0; i < count; i++) {
     63          selected[i] = scopes[list[0].index];
     64          scopes[list[0].index] = null;
     65          list.RemoveAt(0);
    6366        }
    64         // remove the selected scopes starting from the scope with the highest index
    65         if (i < list.Count) list.RemoveRange(i, list.Count - i);
    66         list = list.OrderBy(x => x.index).ToList();
    67         do {
    68           i--;
    69           scopes.RemoveAt(list[i].index);
    70         } while (i > 0);
     67        scopes.RemoveAll(x => x == null);
    7168      }
    7269      return selected;
  • trunk/sources/HeuristicLab.Selection/3.3/LinearRankSelector.cs

    r3096 r3137  
    5555      for (int i = 0; i < count; i++) {
    5656        int selectedLot = random.Next(lotSum) + 1;
    57         int index = 0;
    58         int currentLot = list[index].lots;
     57        int j = 0;
     58        int currentLot = list[j].lots;
    5959        while (currentLot < selectedLot) {
    60           index++;
    61           currentLot += list[index].lots;
     60          j++;
     61          currentLot += list[j].lots;
    6262        }
    6363        if (copy)
    64           selected[i] = (IScope)scopes[list[index].index].Clone();
     64          selected[i] = (IScope)scopes[list[j].index].Clone();
    6565        else {
    66           selected[i] = scopes[list[index].index];
    67           scopes.RemoveAt(list[index].index);
    68           lotSum -= list[index].lots;
    69           list.RemoveAt(index);
     66          selected[i] = scopes[list[j].index];
     67          scopes[list[j].index] = null;
     68          lotSum -= list[j].lots;
     69          list.RemoveAt(j);
    7070        }
    7171      }
     72      if (!copy) scopes.RemoveAll(x => x == null);
    7273      return selected;
    7374    }
  • trunk/sources/HeuristicLab.Selection/3.3/WorstSelector.cs

    r3096 r3137  
    5151      var list = temp.ToList();
    5252
    53       int j = 0;
    54       for (int i = 0; i < count; i++) {
    55         if (copy) {
     53      if (copy) {
     54        int j = 0;
     55        for (int i = 0; i < count; i++) {
    5656          selected[i] = (IScope)scopes[list[j].index].Clone();
    5757          j++;
    5858          if (j >= list.Count) j = 0;
    59         } else {
    60           selected[i] = scopes[list[j].index];
    61           scopes.RemoveAt(list[j].index);
    62           list.RemoveAt(j);
    6359        }
     60      } else {
     61        for (int i = 0; i < count; i++) {
     62          selected[i] = scopes[list[0].index];
     63          scopes[list[0].index] = null;
     64          list.RemoveAt(0);
     65        }
     66        scopes.RemoveAll(x => x == null);
    6467      }
    6568      return selected;
Note: See TracChangeset for help on using the changeset viewer.