Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 170 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.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  public abstract class CrossoverBase : OperatorBase {
30    public CrossoverBase()
31      : base() {
32      AddVariableInfo(new VariableInfo("Random", "Pseudo random number generator", typeof(IRandom), VariableKind.In));
33    }
34
35    public override IOperation Apply(IScope scope) {
36      IRandom random = GetVariableValue<IRandom>("Random", scope, true);
37
38      if ((scope.SubScopes.Count % 2) != 0)
39        throw new InvalidOperationException("Size of mating pool is not even");
40
41      int children = scope.SubScopes.Count / 2;
42      for (int i = 0; i < children; i++) {
43        IScope parent1 = scope.SubScopes[0];
44        IScope parent2 = scope.SubScopes[1];
45        IScope child = new Scope(i.ToString());
46        scope.AddSubScope(child);
47        Cross(scope, random, parent1, parent2, child);
48        scope.RemoveSubScope(parent1);
49        scope.RemoveSubScope(parent2);
50      }
51
52      return null;
53    }
54
55    protected abstract void Cross(IScope scope, IRandom random, IScope parent1, IScope parent2, IScope child);
56  }
57}
Note: See TracBrowser for help on using the repository browser.