Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.RealVector/DiscreteRecombination.cs @ 102

Last change on this file since 102 was 86, checked in by abeham, 17 years ago

Added the possibility to use Recombination in ES (ticket #64)

  • Added ES-specific RandomSelector that creates lambda parent groups of non repeating parents
  • Added the two recombination types: discrete and intermediate for RealVector
  • Updated the ES UI
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;
27
28namespace HeuristicLab.RealVector {
29  public class DiscreteRecombination : OperatorBase {
30    public override string Description {
31      get {
32        return @"Discrete/dominant recombination creates a new offspring by combining the alleles in the parents such that each allele is randomly selected from one parent";
33      }
34    }
35
36    public DiscreteRecombination()
37      : base() {
38      AddVariableInfo(new VariableInfo("Random", "Pseudo random number generator", typeof(IRandom), VariableKind.In));
39      AddVariableInfo(new VariableInfo("Rho", "Amount of parents to recombine", typeof(IntData), VariableKind.In));
40      AddVariableInfo(new VariableInfo("RealVector", "Parent and child real vector", typeof(DoubleArrayData), VariableKind.In | VariableKind.New));
41    }
42
43    public override IOperation Apply(IScope scope) {
44      int rho = GetVariableValue<IntData>("Rho", scope, true).Data;
45      // with just 1 parent no recombination is necessary/possible
46      if (rho == 1) return null;
47      IRandom random = GetVariableValue<IRandom>("Random", scope, true);
48
49      if (scope.SubScopes.Count % rho != 0)
50        throw new InvalidOperationException("Number of parents is not a multiple of rho");
51      int lambda = scope.SubScopes.Count / rho;
52      IList<double[]> parents = new List<double[]>(rho);
53
54      for (int i = 0; i < lambda; i++) {
55        IScope childScope = new Scope(i.ToString());
56        double[] childGene = (double[])scope.SubScopes[0].GetVariableValue<DoubleArrayData>("RealVector", false).Data.Clone();
57        parents.Clear();
58        for (int j = 0; j < rho; j++) {
59          IScope parent = scope.SubScopes[0];
60          parents.Add(parent.GetVariableValue<DoubleArrayData>("RealVector", false).Data);
61          scope.RemoveSubScope(parent);
62        }
63        // actual discrete recombination
64        for (int x = 0; x < childGene.Length; x++) {
65          childGene[x] = parents[random.Next(rho)][x];
66        }
67        childScope.AddVariable(new Variable(scope.SubScopes[0].TranslateName("RealVector"), new DoubleArrayData(childGene)));
68        scope.AddSubScope(childScope);
69      }
70      return null;
71    }
72  }
73}
Note: See TracBrowser for help on using the repository browser.