Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ES/3.2/ESRandomSelector.cs @ 2476

Last change on this file since 2476 was 1529, checked in by gkronber, 16 years ago

Moved source files of plugins AdvancedOptimizationFrontEnd ... Grid into version-specific sub-folders. #576

File size: 4.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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;
23using System.Collections.Generic;
24using System.Text;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Selection;
28
29namespace HeuristicLab.ES {
30  /// <summary>
31  /// Selects rho*lambda individuals with each rho successing individuals being selected
32  /// from the parent population without repetitions.
33  /// </summary>
34  public class ESRandomSelector : OperatorBase {
35    /// <inheritdoc select="summary"/>
36    public override string Description {
37      get {
38        return @"Selects rho*lambda individuals with each rho successing individuals being selected from the parent population without repetitions.";
39      }
40    }
41
42    /// <summary>
43    /// Initializes a new instance of <see cref="ESRandomSelector"/> with three variable infos
44    /// (<c>Random</c>, <c>Lambda</c> and <c>Rho</c>).
45    /// </summary>
46    public ESRandomSelector()
47      : base() {
48      AddVariableInfo(new VariableInfo("Random", "Pseudo random number generator", typeof(IRandom), VariableKind.In));
49      AddVariableInfo(new VariableInfo("Lambda", "The amount of children to select", typeof(IntData), VariableKind.In));
50      AddVariableInfo(new VariableInfo("Rho", "The amount of parents per child", typeof(IntData), VariableKind.In));
51    }
52
53    /// <summary>
54    /// Separates the sub scopes of the given <paramref name="scope"/> in <c>Selected</c> and
55    /// <c>Remaining</c> and selects rho*lambda individuals with each rho successing individuals
56    /// being selected from the parent population without repetitions.
57    /// </summary>
58    /// <param name="scope">The current scope where to perform the selection.</param>
59    /// <returns><c>null</c>.</returns>
60    public override IOperation Apply(IScope scope) {
61      IScope source = new Scope("Remaining");
62      while (scope.SubScopes.Count > 0) {
63        IScope s = scope.SubScopes[0];
64        scope.RemoveSubScope(s);
65        source.AddSubScope(s);
66      }
67      scope.AddSubScope(source);
68      IScope target = new Scope("Selected");
69      scope.AddSubScope(target);
70
71      Select(source, target);
72
73      return null;
74    }
75
76    /// <summary>
77    /// Selects rho*lambda individuals with each rho successing individuals being selected
78    /// from the parent population (<paramref name="source"/>) without repetitions.
79    /// </summary>
80    /// <exception cref="InvalidOperationException">Thrown when more parents than available should be
81    /// selected per child.</exception>
82    /// <param name="source">The scope where to select the individuals.</param>
83    /// <param name="target">The target scope where to insert the selected individuals.</param>
84    protected virtual void Select(IScope source, IScope target) {
85      IRandom random = GetVariableValue<IRandom>("Random", source, true);
86      int children = GetVariableValue<IntData>("Lambda", source, true).Data;
87      int parents = GetVariableValue<IntData>("Rho", source, true).Data;
88      int parentsAvailable = source.SubScopes.Count;
89
90      if (parents > parentsAvailable)
91        throw new InvalidOperationException("Cannot select more parents per child than there are parents available");
92
93      IList<int> selectedParents = new List<int>(parentsAvailable);
94      for (int i = 0; i < children; i++) {
95        selectedParents.Clear();
96        for (int j = 0; j < parents; j++) {
97          int nextParent = j; // will be used in case parents == parentsAvailable
98          if (parents < parentsAvailable) {
99            do {
100              nextParent = random.Next(parentsAvailable);
101            } while (selectedParents.Contains(nextParent));
102          }
103
104          target.AddSubScope((IScope)source.SubScopes[nextParent].Clone());
105          selectedParents.Add(nextParent);
106        }
107      }
108    }
109  }
110}
Note: See TracBrowser for help on using the repository browser.