Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ES/ESRandomSelector.cs @ 820

Last change on this file since 820 was 86, checked in by abeham, 17 years ago

Added the possibility to use Recombination in ES (ticket #64)

  • Added ES-specific RandomSelector that creates lambda parent groups of non repeating parents
  • Added the two recombination types: discrete and intermediate for RealVector
  • Updated the ES UI
File size: 3.2 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  public class ESRandomSelector : OperatorBase {
31    public override string Description {
32      get {
33        return @"Selects rho*lambda individuals with each rho successing individuals being selected from the parent population without repetitions.";
34      }
35    }
36
37    public ESRandomSelector()
38      : base() {
39      AddVariableInfo(new VariableInfo("Random", "Pseudo random number generator", typeof(IRandom), VariableKind.In));
40      AddVariableInfo(new VariableInfo("Lambda", "The amount of children to select", typeof(IntData), VariableKind.In));
41      AddVariableInfo(new VariableInfo("Rho", "The amount of parents per child", typeof(IntData), VariableKind.In));
42    }
43
44    public override IOperation Apply(IScope scope) {
45      IScope source = new Scope("Remaining");
46      while (scope.SubScopes.Count > 0) {
47        IScope s = scope.SubScopes[0];
48        scope.RemoveSubScope(s);
49        source.AddSubScope(s);
50      }
51      scope.AddSubScope(source);
52      IScope target = new Scope("Selected");
53      scope.AddSubScope(target);
54
55      Select(source, target);
56
57      return null;
58    }
59
60    protected virtual void Select(IScope source, IScope target) {
61      IRandom random = GetVariableValue<IRandom>("Random", source, true);
62      int children = GetVariableValue<IntData>("Lambda", source, true).Data;
63      int parents = GetVariableValue<IntData>("Rho", source, true).Data;
64      int parentsAvailable = source.SubScopes.Count;
65
66      if (parents > parentsAvailable)
67        throw new InvalidOperationException("Cannot select more parents per child than there are parents available");
68
69      IList<int> selectedParents = new List<int>(parentsAvailable);
70      for (int i = 0; i < children; i++) {
71        selectedParents.Clear();
72        for (int j = 0; j < parents; j++) {
73          int nextParent = j; // will be used in case parents == parentsAvailable
74          if (parents < parentsAvailable) {
75            do {
76              nextParent = random.Next(parentsAvailable);
77            } while (selectedParents.Contains(nextParent));
78          }
79
80          target.AddSubScope((IScope)source.SubScopes[nextParent].Clone());
81          selectedParents.Add(nextParent);
82        }
83      }
84    }
85  }
86}
Note: See TracBrowser for help on using the repository browser.