Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1182 was 1159, checked in by abeham, 16 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.1 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("Random", "Pseudo random number generator", typeof(IRandom), VariableKind.In));
41    }
42
43    /// <summary>
44    /// Replaces the parents (sub scopes of the given <paramref name="scope"/>) with created children
45    /// by crossing over a specified number of parents.
46    /// </summary>
47    /// <remarks>Adds the children to the given <paramref name="scope"/> and removes the parents.</remarks>
48    /// <exception cref="InvalidOperationException">Thrown when the size of the mating pool and the
49    /// number of parents don't match.</exception>
50    /// <param name="scope">The scope whose sub scopes shall be crossed over.</param>
51    /// <returns><c>null</c>.</returns>
52    public override IOperation Apply(IScope scope) {
53      IRandom random = GetVariableValue<IRandom>("Random", scope, true);
54      int parents = scope.SubScopes.Count;
55
56      if (parents < 2)
57        throw new InvalidOperationException("ERROR: Number of parents is < 2");
58
59      IScope[] parentScopes = new IScope[parents];
60      for (int i = 0; i < parents; i++)
61        parentScopes[i] = scope.SubScopes[i];
62      IScope child = scope;
63      Cross(scope, random, parentScopes, child);
64
65      return null;
66    }
67    /// <summary>
68    /// Performs a cross over of a number of <paramref name="parents"/>
69    /// to create a new <paramref name="child"/>.
70    /// </summary>
71    /// <param name="scope">The current scope.</param>
72    /// <param name="random">A random number generator.</param>
73    /// <param name="parents">The scopes to cross over.</param>
74    /// <param name="child">The result of the cross over.</param>
75    protected abstract void Cross(IScope scope, IRandom random, IScope[] parents, IScope child);
76  }
77}
Note: See TracBrowser for help on using the repository browser.