[95] | 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 |
|
---|
| 22 | using System;
|
---|
[70] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Text;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Evolutionary;
|
---|
| 28 |
|
---|
| 29 | namespace HeuristicLab.RealVector {
|
---|
[1184] | 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>
|
---|
[1218] | 34 | public class HeuristicCrossover : RealVectorCrossoverBase {
|
---|
[1184] | 35 | /// <summary>
|
---|
[1218] | 36 | /// Initializes a new instance of <see cref="HeuristicCrossover"/> with two variable infos
|
---|
| 37 | /// (<c>Maximization</c> and <c>Quality</c>).
|
---|
[1184] | 38 | /// </summary>
|
---|
[70] | 39 | public HeuristicCrossover()
|
---|
| 40 | : base() {
|
---|
[73] | 41 | AddVariableInfo(new VariableInfo("Maximization", "Maximization problem", typeof(BoolData), VariableKind.In));
|
---|
[70] | 42 | AddVariableInfo(new VariableInfo("Quality", "Quality value", typeof(DoubleData), VariableKind.In));
|
---|
| 43 | }
|
---|
| 44 |
|
---|
[1184] | 45 | /// <inheritdoc select="summary"/>
|
---|
[70] | 46 | public override string Description {
|
---|
| 47 | get { return "Heuristic crossover for real vectors."; }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
[1184] | 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>
|
---|
[73] | 60 | public static double[] Apply(IRandom random, bool maximization, double[] parent1, double quality1, double[] parent2, double quality2) {
|
---|
[70] | 61 | int length = parent1.Length;
|
---|
| 62 | double[] result = new double[length];
|
---|
[73] | 63 | double factor = random.NextDouble();
|
---|
[70] | 64 |
|
---|
| 65 | for (int i = 0; i < length; i++) {
|
---|
[73] | 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 | }
|
---|
[70] | 71 | return result;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[1184] | 74 | /// <summary>
|
---|
[1218] | 75 | /// Performs a heuristic crossover operation for two given parent real vectors.
|
---|
[1184] | 76 | /// </summary>
|
---|
[1218] | 77 | /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
|
---|
[1184] | 78 | /// <param name="scope">The current scope.</param>
|
---|
[1218] | 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");
|
---|
[73] | 84 | bool maximization = GetVariableValue<BoolData>("Maximization", scope, true).Data;
|
---|
[1218] | 85 | double quality1 = scope.SubScopes[0].GetVariableValue<DoubleData>("Quality", false).Data;
|
---|
| 86 | double quality2 = scope.SubScopes[1].GetVariableValue<DoubleData>("Quality", false).Data;
|
---|
[70] | 87 |
|
---|
[1218] | 88 | return Apply(random, maximization, parents[0], quality1, parents[1], quality2);
|
---|
[70] | 89 | }
|
---|
| 90 | }
|
---|
| 91 | }
|
---|