Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.RealVector/3.2/HeuristicCrossover.cs @ 3472

Last change on this file since 3472 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.3 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 : RealVectorCrossoverBase {
35    /// <summary>
36    /// Initializes a new instance of <see cref="HeuristicCrossover"/> with two variable infos
37    /// (<c>Maximization</c> and <c>Quality</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    }
44
45    /// <inheritdoc select="summary"/>
46    public override string Description {
47      get { return "Heuristic crossover for real vectors."; }
48    }
49
50    /// <summary>
51    /// Perfomrs a heuristic crossover on the two given parents.
52    /// </summary>
53    /// <param name="random">The random number generator.</param>
54    /// <param name="maximization">Boolean flag whether it is a maximization problem.</param>
55    /// <param name="parent1">The first parent for the crossover operation.</param>
56    /// <param name="quality1">The quality of the first parent.</param>
57    /// <param name="parent2">The second parent for the crossover operation.</param>
58    /// <param name="quality2">The quality of the second parent.</param>
59    /// <returns>The newly created real vector, resulting from the heuristic crossover.</returns>
60    public static double[] Apply(IRandom random, bool maximization, double[] parent1, double quality1, double[] parent2, double quality2) {
61      int length = parent1.Length;
62      double[] result = new double[length];
63      double factor = random.NextDouble();
64
65      for (int i = 0; i < length; i++) {
66        if ((maximization && (quality1 > quality2)) || ((!maximization) && (quality1 < quality2)))
67          result[i] = parent1[i] + factor * (parent1[i] - parent2[i]);
68        else
69          result[i] = parent2[i] + factor * (parent2[i] - parent1[i]);
70      }
71      return result;
72    }
73
74    /// <summary>
75    /// Performs a heuristic crossover operation for two given parent real vectors.
76    /// </summary>
77    /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
78    /// <param name="scope">The current scope.</param>
79    /// <param name="random">A random number generator.</param>
80    /// <param name="parents">An array containing the two real vectors that should be crossed.</param>
81    /// <returns>The newly created real vector, resulting from the crossover operation.</returns>
82    protected override double[] Cross(IScope scope, IRandom random, double[][] parents) {
83      if (parents.Length != 2) throw new InvalidOperationException("ERROR in HeuristicCrossover: The number of parents is not equal to 2");
84      bool maximization = GetVariableValue<BoolData>("Maximization", scope, true).Data;
85      double quality1 = scope.SubScopes[0].GetVariableValue<DoubleData>("Quality", false).Data;
86      double quality2 = scope.SubScopes[1].GetVariableValue<DoubleData>("Quality", false).Data;
87
88      return Apply(random, maximization, parents[0], quality1, parents[1], quality2);
89    }
90  }
91}
Note: See TracBrowser for help on using the repository browser.