Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone3/sources/HeuristicLab.RealVector/3.2/BlendAlphaCrossover.cs @ 5726

Last change on this file since 5726 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.2 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  /// Blend alpha crossover for real vectors. Creates a new offspring by selecting a random value
31  /// from the interval between the two alleles of the parent solutions. The interval is increased
32  /// in both directions by the factor alpha.
33  /// </summary>
34  public class BlendAlphaCrossover : RealVectorCrossoverBase {
35    /// <inheritdoc select="summary"/>
36    public override string Description {
37      get { return
38@"Blend alpha crossover for real vectors. Creates a new offspring by selecting a random value from the interval between the two alleles of the parent solutions. The interval is increased in both directions by the factor alpha.
39Please use the operator BoundsChecker if necessary.";
40      }
41    }
42
43    /// <summary>
44    /// Initializes a new instance of <see cref="BlendAlphaCrossover"/> with one variable info (<c>Alpha</c>).
45    /// </summary>
46    public BlendAlphaCrossover()
47      : base() {
48      VariableInfo alphaVarInfo = new VariableInfo("Alpha", "Value for alpha", typeof(DoubleData), VariableKind.In);
49      alphaVarInfo.Local = true;
50      AddVariableInfo(alphaVarInfo);
51      AddVariable(new Variable("Alpha", new DoubleData(0.5)));
52    }
53
54    /// <summary>
55    /// Performs a blend alpha crossover of two real vectors.
56    /// </summary>
57    /// <param name="random">The random number generator.</param>
58    /// <param name="parent1">The first parent for the crossover operation.</param>
59    /// <param name="parent2">The second parent for the crossover operation.</param>
60    /// <param name="alpha">The alpha value for the crossover.</param>
61    /// <returns>The newly created real vector resulting from the crossover operation.</returns>
62    public static double[] Apply(IRandom random, double[] parent1, double[] parent2, double alpha) {
63      int length = parent1.Length;
64      double[] result = new double[length];
65      double cMax = 0, cMin = 0, interval = 0, resMin = 0, resMax = 0;
66
67      for (int i = 0; i < length; i++) {
68        cMax = Math.Max(parent1[i], parent2[i]);
69        cMin = Math.Min(parent1[i], parent2[i]);
70        interval = Math.Abs(cMax - cMin);
71        resMin = cMin - interval * alpha;
72        resMax = cMax + interval * alpha;
73
74        result[i] = resMin + random.NextDouble() * Math.Abs(resMax - resMin);
75      }
76      return result;
77    }
78
79    /// <summary>
80    /// Performs a blend alpha crossover operation for two given parent real vectors.
81    /// </summary>
82    /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
83    /// <param name="scope">The current scope.</param>
84    /// <param name="random">A random number generator.</param>
85    /// <param name="parents">An array containing the two real vectors that should be crossed.</param>
86    /// <returns>The newly created real vector, resulting from the crossover operation.</returns>
87    protected override double[] Cross(IScope scope, IRandom random, double[][] parents) {
88      if (parents.Length != 2) throw new InvalidOperationException("ERROR in BlendAlphaCrossover: The number of parents is not equal to 2");
89      double alpha = GetVariableValue<DoubleData>("Alpha", scope, true).Data;
90      return Apply(random, parents[0], parents[1], alpha);
91    }
92  }
93}
Note: See TracBrowser for help on using the repository browser.