Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Evolutionary/ChildrenInitializer.cs @ 1159

Last change on this file since 1159 was 1159, checked in by abeham, 15 years ago

implemented changes to crossover described in ticket #470

  • added ChildrenInitializer operator
  • modified CrossoverBase and MultiCrossoverBase
  • removed unnecessary operators in HeuristicLab.RealVector
File size: 2.4 KB
RevLine 
[1159]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2009 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.Operators;
28
29namespace HeuristicLab.Evolutionary {
30  public class ChildrenInitializer : OperatorBase {
31    public override string Description {
32      get {
33        return @"This operator prepares the individuals for crossover.";
34      }
35    }
36
37    public ChildrenInitializer()
38      : base() {
39      AddVariableInfo(new VariableInfo("ParentsPerChild", "Denotes the number of parents that should be crossed per child. Note that some of the typical crossover operators can only work with 2 parents.", typeof(IntData), VariableKind.In));
40      GetVariableInfo("ParentsPerChild").Local = true;
41      AddVariable(new Variable("ParentsPerChild", new IntData(2)));
42    }
43
44    public override IOperation Apply(IScope scope) {
45      int parents = GetVariableValue<IntData>("ParentsPerChild", scope, true).Data;
46      int children = scope.SubScopes.Count;
47      if (children % parents > 0) throw new InvalidOperationException("ERROR in ChildrenInitializer: The number of selected parents is not a natural multiple of ParentsPerChild");
48      children /= parents;
49
50      for (int i = 0; i < children; i++) {
51        IScope child = new Scope(i.ToString());
52        for (int y = 0; y < parents; y++) {
53          IScope parenty = scope.SubScopes[0];
54          scope.RemoveSubScope(parenty);
55          child.AddSubScope(parenty);
56        }
57        scope.AddSubScope(child);
58      }
59
60      return null;
61    }
62  }
63}
Note: See TracBrowser for help on using the repository browser.