Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Evolutionary/3.2/ChildrenInitializer.cs @ 3527

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

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

File size: 3.1 KB
Line 
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  /// <summary>
31  /// Initializes and prepares the children for crossover
32  /// </summary>
33  public class ChildrenInitializer : OperatorBase {
34    /// <inheritdoc select="summary"/>
35    public override string Description {
36      get {
37        return @"This operator prepares the individuals for crossover.";
38      }
39    }
40
41    /// <summary>
42    /// Initializes a new instance of <see cref="ChildrenInitializer"/> with one variable info: <c>ParentsPerChild</c>.
43    /// </summary>
44    public ChildrenInitializer()
45      : base() {
46      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));
47      GetVariableInfo("ParentsPerChild").Local = true;
48      AddVariable(new Variable("ParentsPerChild", new IntData(2)));
49    }
50
51    /// <summary>
52    /// Initializes the children as new scopes and inserts their parents as subscopes.
53    /// </summary>
54    /// <exception cref="InvalidOperationException">Thrown when the number of selected parents is not a natural multiple of <c>ParentsPerChild</c>.</exception>
55    /// <param name="scope">The scope that houses all the selected parents as subscopes.</param>
56    /// <returns><c>null</c>.</returns>
57    public override IOperation Apply(IScope scope) {
58      int parents = GetVariableValue<IntData>("ParentsPerChild", scope, true).Data;
59      int children = scope.SubScopes.Count;
60      if (children % parents > 0) throw new InvalidOperationException("ERROR in ChildrenInitializer: The number of selected parents is not a natural multiple of ParentsPerChild");
61      children /= parents;
62
63      for (int i = 0; i < children; i++) {
64        IScope child = new Scope(i.ToString());
65        for (int y = 0; y < parents; y++) {
66          IScope parenty = scope.SubScopes[0];
67          scope.RemoveSubScope(parenty);
68          child.AddSubScope(parenty);
69        }
70        scope.AddSubScope(child);
71      }
72
73      return null;
74    }
75  }
76}
Note: See TracBrowser for help on using the repository browser.