Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.RealVector/HeuristicCrossover.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: 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;
27using HeuristicLab.Evolutionary;
28
29namespace HeuristicLab.RealVector {
30  /// <summary>
31  /// Heuristic crossover for real vectors: Takes for each position the better parent and adds the difference
32  /// of the two parents times a randomly chosen factor.
33  /// </summary>
34  public class HeuristicCrossover : CrossoverBase {
35    /// <summary>
36    /// Initializes a new instance of <see cref="HeuristicCrossover"/> with three variable infos
37    /// (<c>Maximization</c>, <c>Quality</c> and <c>RealVector</c>).
38    /// </summary>
39    public HeuristicCrossover()
40      : base() {
41      AddVariableInfo(new VariableInfo("Maximization", "Maximization problem", typeof(BoolData), VariableKind.In));
42      AddVariableInfo(new VariableInfo("Quality", "Quality value", typeof(DoubleData), VariableKind.In));
43      AddVariableInfo(new VariableInfo("RealVector", "Parent and child real vector", typeof(DoubleArrayData), VariableKind.In | VariableKind.New));
44    }
45
46    /// <inheritdoc select="summary"/>
47    public override string Description {
48      get { return "Heuristic crossover for real vectors."; }
49    }
50
51    /// <summary>
52    /// Perfomrs a heuristic crossover on the two given parents.
53    /// </summary>
54    /// <param name="random">The random number generator.</param>
55    /// <param name="maximization">Boolean flag whether it is a maximization problem.</param>
56    /// <param name="parent1">The first parent for the crossover operation.</param>
57    /// <param name="quality1">The quality of the first parent.</param>
58    /// <param name="parent2">The second parent for the crossover operation.</param>
59    /// <param name="quality2">The quality of the second parent.</param>
60    /// <returns>The newly created real vector, resulting from the heuristic crossover.</returns>
61    public static double[] Apply(IRandom random, bool maximization, double[] parent1, double quality1, double[] parent2, double quality2) {
62      int length = parent1.Length;
63      double[] result = new double[length];
64      double factor = random.NextDouble();
65
66      for (int i = 0; i < length; i++) {
67        if ((maximization && (quality1 > quality2)) || ((!maximization) && (quality1 < quality2)))
68          result[i] = parent1[i] + factor * (parent1[i] - parent2[i]);
69        else
70          result[i] = parent2[i] + factor * (parent2[i] - parent1[i]);
71      }
72      return result;
73    }
74
75    /// <summary>
76    /// Perfomrs a heuristic crossover on the two given parents.
77    /// </summary>
78    /// <exception cref="InvalidOperationException">Thrown when the parent vectors have different lengths.</exception>
79    /// <param name="scope">The current scope.</param>
80    /// <param name="random">The random number generator.</param>
81    /// <param name="parent1">The first parent for the crossover operation.</param>
82    /// <param name="parent2">The second parent for the crossover operation.</param>
83    /// <param name="child">The newly created real vector, resulting from the heuristic crossover.</param>
84    protected sealed override void Cross(IScope scope, IRandom random, IScope parent1, IScope parent2, IScope child) {
85      bool maximization = GetVariableValue<BoolData>("Maximization", scope, true).Data;
86      DoubleArrayData vector1 = parent1.GetVariableValue<DoubleArrayData>("RealVector", false);
87      DoubleData quality1 = parent1.GetVariableValue<DoubleData>("Quality", false);
88      DoubleArrayData vector2 = parent2.GetVariableValue<DoubleArrayData>("RealVector", false);
89      DoubleData quality2 = parent2.GetVariableValue<DoubleData>("Quality", false);
90
91      if (vector1.Data.Length != vector2.Data.Length) throw new InvalidOperationException("Cannot apply crossover to real vectors of different length.");
92
93      double[] result = Apply(random, maximization, vector1.Data, quality1.Data, vector2.Data, quality2.Data);
94      child.AddVariable(new Variable(child.TranslateName("RealVector"), new DoubleArrayData(result)));
95    }
96  }
97}
Note: See TracBrowser for help on using the repository browser.