Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.1/sources/HeuristicLab.Evolutionary/MultiCrossoverBase.cs @ 592

Last change on this file since 592 was 108, checked in by swagner, 17 years ago

Closed ticket #90

  • added base class MultiCrossoverBase for crossover operators crossing more than two parents
File size: 2.4 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  public abstract class MultiCrossoverBase : OperatorBase {
31    public MultiCrossoverBase()
32      : base() {
33      AddVariableInfo(new VariableInfo("Parents", "Number of parents that should be crossed", typeof(IntData), VariableKind.In));
34      AddVariableInfo(new VariableInfo("Random", "Pseudo random number generator", typeof(IRandom), VariableKind.In));
35    }
36
37    public override IOperation Apply(IScope scope) {
38      IRandom random = GetVariableValue<IRandom>("Random", scope, true);
39      int parents = GetVariableValue<IntData>("Parents", scope, true).Data;
40
41      if ((scope.SubScopes.Count % parents) != 0)
42        throw new InvalidOperationException("Size of mating pool and number of parents don't match");
43
44      int children = scope.SubScopes.Count / parents;
45      for (int i = 0; i < children; i++) {
46        IScope[] parentScopes = new IScope[parents];
47        for (int j = 0; j < parentScopes.Length; j++)
48          parentScopes[j] = scope.SubScopes[j];
49
50        IScope child = new Scope(i.ToString());
51        scope.AddSubScope(child);
52        Cross(scope, random, parentScopes, child);
53
54        for (int j = 0; j < parentScopes.Length; j++)
55          scope.RemoveSubScope(parentScopes[j]);
56      }
57      return null;
58    }
59
60    protected abstract void Cross(IScope scope, IRandom random, IScope[] parents, IScope child);
61  }
62}
Note: See TracBrowser for help on using the repository browser.