Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Evolutionary/CrossoverBase.cs @ 1063

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

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

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.Operators;
27
28namespace HeuristicLab.Evolutionary {
29  /// <summary>
30  /// Base class for crossing over operators.
31  /// </summary>
32  public abstract class CrossoverBase : OperatorBase {
33    /// <summary>
34    /// Initializes a new instance of <see cref="CrossoverBase"/> with one variable info (<c>Random</c>).
35    /// </summary>
36    public CrossoverBase()
37      : base() {
38      AddVariableInfo(new VariableInfo("Random", "Pseudo random number generator", typeof(IRandom), VariableKind.In));
39    }
40
41    /// <summary>
42    /// Replaces the parents (the sub scopes of the current <paramref name="scope"/>) with created children
43    /// by crossing over of two adjacent sub scopes.
44    /// </summary>
45    /// <exception cref="InvalidOperationException">Thrown when the size of the mating pool
46    /// is not even.</exception>
47    /// <param name="scope">The current scope whose sub scopes shall be parents.</param>
48    /// <returns><c>null</c>.</returns>
49    public override IOperation Apply(IScope scope) {
50      IRandom random = GetVariableValue<IRandom>("Random", scope, true);
51
52      if ((scope.SubScopes.Count % 2) != 0)
53        throw new InvalidOperationException("Size of mating pool is not even");
54
55      int children = scope.SubScopes.Count / 2;
56      for (int i = 0; i < children; i++) {
57        IScope parent1 = scope.SubScopes[0];
58        IScope parent2 = scope.SubScopes[1];
59        IScope child = new Scope(i.ToString());
60        scope.AddSubScope(child);
61        Cross(scope, random, parent1, parent2, child);
62        scope.RemoveSubScope(parent1);
63        scope.RemoveSubScope(parent2);
64      }
65
66      return null;
67    }
68
69    /// <summary>
70    /// Performs a cross over of <paramref name="parent1"/> and <paramref name="parent2"/>
71    /// to create a new <paramref name="child"/>.
72    /// </summary>
73    /// <param name="scope">The current scope.</param>
74    /// <param name="random">A random number generator.</param>
75    /// <param name="parent1">The parent scope 1 to cross over.</param>
76    /// <param name="parent2">The parent scope 2 to cross over.</param>
77    /// <param name="child">The resulting child of the cross over.</param>
78    protected abstract void Cross(IScope scope, IRandom random, IScope parent1, IScope parent2, IScope child);
79  }
80}
Note: See TracBrowser for help on using the repository browser.