Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Selection/3.3/LinearRankSelector.cs @ 2818

Last change on this file since 2818 was 2818, checked in by swagner, 14 years ago

Operator architecture refactoring (#95)

  • corrected several bugs in order to get SGA working
File size: 3.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System.Linq;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26
27namespace HeuristicLab.Selection {
28  /// <summary>
29  /// A linear rank selection operator which considers the rank based on a single double quality value for selection.
30  /// </summary>
31  [Item("LinearRankSelector", "A linear rank selection operator which considers the rank based on a single double quality value for selection.")]
32  [EmptyStorableClass]
33  [Creatable("Test")]
34  public sealed class LinearRankSelector : StochasticSingleObjectiveSelector {
35    public LinearRankSelector()
36      : base() {
37      CopySelected.Value = true;
38    }
39
40    protected override ScopeList Select(ScopeList scopes) {
41      int count = NumberOfSelectedSubScopesParameter.ActualValue.Value;
42      bool copy = CopySelectedParameter.Value.Value;
43      IRandom random = RandomParameter.ActualValue;
44      bool maximization = MaximizationParameter.ActualValue.Value;
45      ItemArray<DoubleData> qualities = QualityParameter.ActualValue;
46      ScopeList selected = new ScopeList();
47
48      // create a list for each scope that contains the scope's index in the original scope list and its lots
49      var temp = qualities.Select((x, index) => new { index, x.Value });
50      if (maximization)
51        temp = temp.OrderBy(x => x.Value);
52      else
53        temp = temp.OrderByDescending(x => x.Value);
54      var list = temp.Select((x, index) => new { x.index, lots = index + 1 }).ToList();
55
56      int lotSum = list.Count * (list.Count + 1) / 2;
57      for (int i = 0; i < count; i++) {
58        int selectedLot = random.Next(lotSum) + 1;
59        int index = 0;
60        int currentLot = list[index].lots;
61        while (currentLot < selectedLot) {
62          index++;
63          currentLot += list[index].lots;
64        }
65        if (copy)
66          selected.Add((IScope)scopes[list[index].index].Clone());
67        else {
68          selected.Add(scopes[list[index].index]);
69          scopes.RemoveAt(list[index].index);
70          lotSum -= list[index].lots;
71          list.RemoveAt(index);
72        }
73      }
74      return selected;
75    }
76  }
77}
Note: See TracBrowser for help on using the repository browser.