Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone2/sources/HeuristicLab.RealVector/3.2/BlendAlphaBetaCrossover.cs @ 6004

Last change on this file since 6004 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: 6.5 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 : RealVectorCrossoverBase {
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>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      VariableInfo alphaVarInfo = new VariableInfo("Alpha", "Value for alpha", typeof(DoubleData), VariableKind.In);
54      alphaVarInfo.Local = true;
55      AddVariableInfo(alphaVarInfo);
56      AddVariable(new Variable("Alpha", new DoubleData(0.75)));
57      VariableInfo betaVarInfo = new VariableInfo("Beta", "Value for beta", typeof(DoubleData), VariableKind.In);
58      betaVarInfo.Local = true;
59      AddVariableInfo(betaVarInfo);
60      AddVariable(new Variable("Beta", new DoubleData(0.25)));
61    }
62
63    /// <summary>
64    /// Performs a blend alpha beta crossover of two real vectors.
65    /// </summary>
66    /// <param name="random">The random number generator.</param>
67    /// <param name="maximization">Boolean flag whether it is a maximization problem.</param>
68    /// <param name="parent1">The first parent for the crossover.</param>
69    /// <param name="quality1">The quality of the first parent.</param>
70    /// <param name="parent2">The second parent for the crossover.</param>
71    /// <param name="quality2">The quality of the second parent.</param>
72    /// <param name="alpha">The alpha value for the crossover.</param>
73    /// <param name="beta">The beta value for the crossover operation.</param>
74    /// <returns>The newly created real vector resulting from the crossover.</returns>
75    public static double[] Apply(IRandom random, bool maximization, double[] parent1, double quality1, double[] parent2, double quality2, double alpha, double beta) {
76      int length = parent1.Length;
77      double[] result = new double[length];
78
79      for (int i = 0; i < length; i++) {
80        double interval = Math.Abs(parent1[i] - parent2[i]);
81
82        if ((maximization && (quality1 > quality2)) || ((!maximization) && (quality1 < quality2))) {
83          result[i] = SelectFromInterval(random, interval, parent1[i], parent2[i], alpha, beta);
84        } else {
85          result[i] = SelectFromInterval(random, interval, parent2[i], parent1[i], alpha, beta);
86        }
87      }
88      return result;
89    }
90
91    private static double SelectFromInterval(IRandom random, double interval, double val1, double val2, double alpha, double beta) {
92      double resMin = 0;
93      double resMax = 0;
94
95      if (val1 <= val2) {
96        resMin = val1 - interval * alpha;
97        resMax = val2 + interval * beta;
98      } else {
99        resMin = val2 - interval * beta;
100        resMax = val1 + interval * alpha;
101      }
102
103      return SelectRandomFromInterval(random, resMin, resMax);
104    }
105
106    private static double SelectRandomFromInterval(IRandom random, double resMin, double resMax) {
107      return resMin + random.NextDouble() * Math.Abs(resMax - resMin);
108    }
109
110    /// <summary>
111    /// Performs a blend alpha beta crossover operation for two given parent real vectors.
112    /// </summary>
113    /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
114    /// <param name="scope">The current scope.</param>
115    /// <param name="random">A random number generator.</param>
116    /// <param name="parents">An array containing the two real vectors that should be crossed.</param>
117    /// <returns>The newly created real vector, resulting from the crossover operation.</returns>
118    protected override double[] Cross(IScope scope, IRandom random, double[][] parents) {
119      if (parents.Length != 2) throw new InvalidOperationException("ERROR in BlendAlphaBetaCrossover: The number of parents is not equal to 2");
120      bool maximization = GetVariableValue<BoolData>("Maximization", scope, true).Data;
121      double quality1 = scope.SubScopes[0].GetVariableValue<DoubleData>("Quality", false).Data;
122      double quality2 = scope.SubScopes[1].GetVariableValue<DoubleData>("Quality", false).Data;
123      double alpha = GetVariableValue<DoubleData>("Alpha", scope, true).Data;
124      double beta = GetVariableValue<DoubleData>("Beta", scope, true).Data;
125
126      return Apply(random, maximization, parents[0], quality1, parents[1], quality2, alpha, beta);
127    }
128  }
129}
Note: See TracBrowser for help on using the repository browser.