Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CEDMA-Exporter-715/sources/HeuristicLab.RealVector/3.2/SelfAdaptiveDiscreteCrossover.cs @ 2498

Last change on this file since 2498 was 1530, checked in by gkronber, 16 years ago

Moved source files of plugins Hive ... Visualization.Test into version-specific sub-folders. #576

File size: 4.9 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  /// <summary>
30  /// Discrete crossover for real vectors: creates a new offspring by combining the alleles in the parents such that each allele is
31  /// randomly selected from one parent. It will also use the same strategy to combine the endogenous
32  /// strategy parameter vector.
33  /// </summary>
34  public class SelfAdaptiveDiscreteCrossover : RealVectorCrossoverBase {
35    /// <inheritdoc select="summary"/>
36    public override string Description {
37      get {
38        return @"Discrete crossover for real vectors: creates a new offspring by combining the alleles in the parents such that each allele is randomly selected from one parent. It will also use the same strategy to combine the endogenous strategy parameter vector.";
39      }
40    }
41
42    /// <summary>
43    /// Initializes a new instance of <see cref="SelfAdaptiveDiscreteCrossover"/> with one additional
44    /// variable infos (<c>StrategyVector</c>).
45    /// </summary>
46    public SelfAdaptiveDiscreteCrossover()
47      : base() {
48      AddVariableInfo(new VariableInfo("StrategyVector", "Endogenous strategy parameter vector", typeof(DoubleArrayData), VariableKind.In | VariableKind.New));
49    }
50
51    /// <summary>
52    /// Performs a discrete crossover operation for multiple given parent real vectors that combines the strategy vectors in the same way.
53    /// </summary>
54    /// <param name="random">A random number generator.</param>
55    /// <param name="parents">An array containing the parents that should be crossed.</param>
56    /// <param name="strategies">An array containing the strategy vectors of the parents.</param>
57    /// <param name="child">The newly created real vector, resulting from the crossover operation (output parameter).</param>
58    /// <param name="strategy">The newly created strategy vector, resulting from the crossover operation (output parameter).</param>
59    public static void Apply(IRandom random, double[][] parents, double[][] strategies, out double[] child, out double[] strategy) {
60      child = new double[parents[0].Length];
61      strategy = new double[strategies[0].Length];
62      for (int i = 0; i < child.Length; i++) {
63        int nextParent = random.Next(parents.Length);
64        child[i] = parents[nextParent][i];
65        strategy[i] = strategies[nextParent][i];
66      }
67    }
68
69    /// <summary>
70    /// Performs a discrete crossover operation for multiple given parent real vectors that combines the strategy vectors in the same way.
71    /// </summary>
72    /// <exception cref="InvalidOperationException">Thrown if there are less than two parents or if the length of the strategy vectors is not the same as the length of the real vectors.</exception>
73    /// <param name="scope">The current scope.</param>
74    /// <param name="random">A random number generator.</param>
75    /// <param name="parents">An array containing the real vectors that should be crossed.</param>
76    /// <returns>The newly created real vector, resulting from the crossover operation.</returns>
77    protected override double[] Cross(IScope scope, IRandom random, double[][] parents) {
78      if (parents.Length < 2) throw new InvalidOperationException("ERROR in SelfAdaptiveDiscreteCrossover: The number of parents is less than 2");
79      double[][] strategies = new double[scope.SubScopes.Count][];
80      int length = parents[0].Length;
81      for (int i = 0; i < scope.SubScopes.Count; i++) {
82        strategies[i] = scope.SubScopes[i].GetVariableValue<DoubleArrayData>("StrategyVector", false).Data;
83        if (strategies[i].Length != length) throw new InvalidOperationException("ERROR in SelfAdaptiveDiscreteCrossover: Strategy vectors do not have the same length as the real vectors");
84      }
85
86      double[] child, strategy;
87      Apply(random, parents, strategies, out child, out strategy);
88      scope.AddVariable(new Variable(scope.TranslateName("StrategyVector"), new DoubleArrayData(strategy)));
89      return child;
90    }
91  }
92}
Note: See TracBrowser for help on using the repository browser.