Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.RealVector/BlendAlphaBetaCrossover.cs @ 1184

Last change on this file since 1184 was 1184, checked in by vdorfer, 15 years ago

Created API documentation for HeuristicLab.RealVector namespace (#331)

File size: 6.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.Evolutionary;
27using HeuristicLab.Data;
28
29namespace HeuristicLab.RealVector {
30  /// <summary>
31  /// Blend alpha-beta crossover for real vectors. Creates a new offspring by selecting a
32  /// random value from the interval between the two alleles of the parent solutions.
33  /// The interval is increased in both directions as follows: Into the direction of the 'better'
34  /// solution by the factor alpha, into the direction of the 'worse' solution by the factor beta.
35  /// </summary>
36  public class BlendAlphaBetaCrossover : CrossoverBase {
37    /// <inheritdoc select="summary"/>
38    public override string Description {
39      get { return
40@"Blend alpha-beta 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 as follows: Into the direction of the 'better' solution by the factor alpha, into the direction of the 'worse' solution by the factor beta.
41Please use the operator BoundsChecker if necessary.";
42      }
43    }
44
45    /// <summary>
46    /// Initializes a new instance of <see cref="BlendAlphaBetaCrossover"/> with five variable infos
47    /// (<c>Maximization</c>, <c>Quality</c>, <c>RealVector</c>, <c>Alpha</c> and <c>Beta</c>).
48    /// </summary>
49    public BlendAlphaBetaCrossover()
50      : base() {
51      AddVariableInfo(new VariableInfo("Maximization", "Maximization problem", typeof(BoolData), VariableKind.In));
52      AddVariableInfo(new VariableInfo("Quality", "Quality value", typeof(DoubleData), VariableKind.In));
53      AddVariableInfo(new VariableInfo("RealVector", "Parent and child real vector", typeof(DoubleArrayData), VariableKind.In | VariableKind.New));
54      VariableInfo alphaVarInfo = new VariableInfo("Alpha", "Value for alpha", typeof(DoubleData), VariableKind.In);
55      alphaVarInfo.Local = true;
56      AddVariableInfo(alphaVarInfo);
57      AddVariable(new Variable("Alpha", new DoubleData(0.75)));
58      VariableInfo betaVarInfo = new VariableInfo("Beta", "Value for beta", typeof(DoubleData), VariableKind.In);
59      betaVarInfo.Local = true;
60      AddVariableInfo(betaVarInfo);
61      AddVariable(new Variable("Beta", new DoubleData(0.25)));
62    }
63
64    /// <summary>
65    /// Performs a blend alpha beta crossover of two real vectors.
66    /// </summary>
67    /// <param name="random">The random number generator.</param>
68    /// <param name="maximization">Boolean flag whether it is a maximization problem.</param>
69    /// <param name="parent1">The first parent for the crossover.</param>
70    /// <param name="quality1">The quality of the first parent.</param>
71    /// <param name="parent2">The second parent for the crossover.</param>
72    /// <param name="quality2">The quality of the second parent.</param>
73    /// <param name="alpha">The alpha value for the crossover.</param>
74    /// <param name="beta">The beta value for the crossover operation.</param>
75    /// <returns>The newly created real vector resulting from the crossover.</returns>
76    public static double[] Apply(IRandom random, bool maximization, double[] parent1, double quality1, double[] parent2, double quality2, double alpha, double beta) {
77      int length = parent1.Length;
78      double[] result = new double[length];
79
80      for (int i = 0; i < length; i++) {
81        double interval = Math.Abs(parent1[i] - parent2[i]);
82
83        if ((maximization && (quality1 > quality2)) || ((!maximization) && (quality1 < quality2))) {
84          result[i] = SelectFromInterval(random, interval, parent1[i], parent2[i], alpha, beta);
85        } else {
86          result[i] = SelectFromInterval(random, interval, parent2[i], parent1[i], alpha, beta);
87        }
88      }
89      return result;
90    }
91
92    private static double SelectFromInterval(IRandom random, double interval, double val1, double val2, double alpha, double beta) {
93      double resMin = 0;
94      double resMax = 0;
95
96      if (val1 <= val2) {
97        resMin = val1 - interval * alpha;
98        resMax = val2 + interval * beta;
99      } else {
100        resMin = val2 - interval * beta;
101        resMax = val1 + interval * alpha;
102      }
103
104      return SelectRandomFromInterval(random, resMin, resMax);
105    }
106
107    private static double SelectRandomFromInterval(IRandom random, double resMin, double resMax) {
108      return resMin + random.NextDouble() * Math.Abs(resMax - resMin);
109    }
110
111    /// <summary>
112    /// Performs a blend alpha beta crossover of two real vectors.
113    /// </summary>
114    /// <param name="scope">The current scope.</param>
115    /// <param name="random">The random number generator.</param>
116    /// <param name="parent1">The first parent for the crossover.</param>
117    /// <param name="parent2">The second parent for the crossover.</param>
118    /// <param name="child">The created child generated through the blend alpha beta crossover.</param>
119    protected sealed override void Cross(IScope scope, IRandom random, IScope parent1, IScope parent2, IScope child) {
120      bool maximization = GetVariableValue<BoolData>("Maximization", scope, true).Data;
121      DoubleArrayData vector1 = parent1.GetVariableValue<DoubleArrayData>("RealVector", false);
122      DoubleData quality1 = parent1.GetVariableValue<DoubleData>("Quality", false);
123      DoubleArrayData vector2 = parent2.GetVariableValue<DoubleArrayData>("RealVector", false);
124      DoubleData quality2 = parent2.GetVariableValue<DoubleData>("Quality", false);
125      double alpha = GetVariableValue<DoubleData>("Alpha", scope, true).Data;
126      double beta = GetVariableValue<DoubleData>("Beta", scope, true).Data;
127
128      if (vector1.Data.Length != vector2.Data.Length) throw new InvalidOperationException("Cannot apply crossover to real vectors of different length.");
129
130      double[] result = Apply(random, maximization, vector1.Data, quality1.Data, vector2.Data, quality2.Data, alpha, beta);
131      child.AddVariable(new Variable(child.TranslateName("RealVector"), new DoubleArrayData(result)));
132    }
133  }
134}
Note: See TracBrowser for help on using the repository browser.