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

Last change on this file since 1159 was 1159, checked in by abeham, 12 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: 3.0 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)
53        throw new InvalidOperationException("ERROR: Number of parents is != 2");
54
55      IScope parent1 = scope.SubScopes[0];
56      IScope parent2 = scope.SubScopes[1];
57      IScope child = scope;
58      Cross(scope, random, parent1, parent2, child);
59
60      return null;
61    }
62
63    /// <summary>
64    /// Performs a cross over of <paramref name="parent1"/> and <paramref name="parent2"/>
65    /// to create a new <paramref name="child"/>.
66    /// </summary>
67    /// <param name="scope">The current scope.</param>
68    /// <param name="random">A random number generator.</param>
69    /// <param name="parent1">The parent scope 1 to cross over.</param>
70    /// <param name="parent2">The parent scope 2 to cross over.</param>
71    /// <param name="child">The resulting child of the cross over.</param>
72    protected abstract void Cross(IScope scope, IRandom random, IScope parent1, IScope parent2, IScope child);
73  }
74}
Note: See TracBrowser for help on using the repository browser.