Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Evolutionary/MultiCrossoverBase.cs @ 1143

Last change on this file since 1143 was 881, checked in by vdorfer, 16 years ago

Created API documentation for HeuristicLab.Evolutionary namespace (#331)

File size: 3.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.Operators;
28
29namespace HeuristicLab.Evolutionary {
30  /// <summary>
31  /// Base class for cross over operators that use more than two parents.
32  /// </summary>
33  public abstract class MultiCrossoverBase : OperatorBase {
34    /// <summary>
35    /// Initializes a new instance of <see cref="MultiCrossoverBase"/> with two variable infos
36    /// (<c>Parents</c> and <c>Random</c>).
37    /// </summary>
38    public MultiCrossoverBase()
39      : base() {
40      AddVariableInfo(new VariableInfo("Parents", "Number of parents that should be crossed", typeof(IntData), VariableKind.In));
41      AddVariableInfo(new VariableInfo("Random", "Pseudo random number generator", typeof(IRandom), VariableKind.In));
42    }
43
44    /// <summary>
45    /// Replaces the parents (sub scopes of the given <paramref name="scope"/>) with created children
46    /// by crossing over a specified number of parents.
47    /// </summary>
48    /// <remarks>Adds the children to the given <paramref name="scope"/> and removes the parents.</remarks>
49    /// <exception cref="InvalidOperationException">Thrown when the size of the mating pool and the
50    /// number of parents don't match.</exception>
51    /// <param name="scope">The scope whose sub scopes shall be crossed over.</param>
52    /// <returns><c>null</c>.</returns>
53    public override IOperation Apply(IScope scope) {
54      IRandom random = GetVariableValue<IRandom>("Random", scope, true);
55      int parents = GetVariableValue<IntData>("Parents", scope, true).Data;
56
57      if ((scope.SubScopes.Count % parents) != 0)
58        throw new InvalidOperationException("Size of mating pool and number of parents don't match");
59
60      int children = scope.SubScopes.Count / parents;
61      for (int i = 0; i < children; i++) {
62        IScope[] parentScopes = new IScope[parents];
63        for (int j = 0; j < parentScopes.Length; j++)
64          parentScopes[j] = scope.SubScopes[j];
65
66        IScope child = new Scope(i.ToString());
67        scope.AddSubScope(child);
68        Cross(scope, random, parentScopes, child);
69
70        for (int j = 0; j < parentScopes.Length; j++)
71          scope.RemoveSubScope(parentScopes[j]);
72      }
73      return null;
74    }
75    /// <summary>
76    /// Performs a cross over of a number of <paramref name="parents"/>
77    /// to create a new <paramref name="child"/>.
78    /// </summary>
79    /// <param name="scope">The current scope.</param>
80    /// <param name="random">A random number generator.</param>
81    /// <param name="parents">The scopes to cross over.</param>
82    /// <param name="child">The result of the cross over.</param>
83    protected abstract void Cross(IScope scope, IRandom random, IScope[] parents, IScope child);
84  }
85}
Note: See TracBrowser for help on using the repository browser.