Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Selection.OffspringSelection/3.2/WeightedOffspringFitnessComparer.cs @ 2080

Last change on this file since 2080 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.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2009 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;
27
28namespace HeuristicLab.Selection.OffspringSelection {
29  /// <summary>
30  /// Analyzes the offspring on whether it is successful or not based on its quality in comparison to its best and worst parents' qualities.
31  /// </summary>
32  public class WeightedOffspringFitnessComparer : OperatorBase  {
33    /// <inheritdoc select="summary"/>
34    public override string Description {
35      get {
36        return @"Compares the quality values of the child with a weighted average of the best and worst parents' qualities.
37Adds a variable SuccessfulChild into the current scope with the result of the comparison.";
38      }
39    }
40
41    /// <summary>
42    /// Initializes a new instance of <see cref="WeightedOffspringFitnessComparer"/> with four variable infos
43    /// (<c>Maximization</c>, <c>Quality</c>, <c>SuccessfulChild</c>, and <c>ComparisonFactor</c>).
44    /// </summary>
45    public WeightedOffspringFitnessComparer()
46      : base() {
47      AddVariableInfo(new VariableInfo("Maximization", "True if the problem is a maximization problem", typeof(BoolData), VariableKind.In));
48      AddVariableInfo(new VariableInfo("Quality", "Quality value", typeof(DoubleData), VariableKind.In));
49      AddVariableInfo(new VariableInfo("SuccessfulChild", "True if the child is successful", typeof(BoolData), VariableKind.New));
50      AddVariableInfo(new VariableInfo("ComparisonFactor", "Factor for comparing the quality of a child with the qualities of its parents (0 = better than worst parent, 1 = better than best parent)", typeof(DoubleData), VariableKind.In));
51    }
52
53    /// <summary>
54    /// Weighs the worst and best parent quality with a given factor and decides whether the child is better than this threshold.
55    /// The result of this decision is added as variable "SuccessfulChild" into the scope.
56    /// </summary>
57    /// <param name="scope">The current scope which represents a new child.</param>
58    /// <returns><c>null</c>.</returns>
59    public override IOperation Apply(IScope scope) {
60      bool maximize = GetVariableValue<BoolData>("Maximization", scope, true).Data;
61      double compFactor = GetVariableValue<DoubleData>("ComparisonFactor", scope, true).Data;
62      double child = GetVariableValue<DoubleData>("Quality", scope, false).Data;
63
64      double lowParent = double.MaxValue; // lowest quality parent
65      double highParent = double.MinValue; // highest quality parent
66      for (int i = 0; i < scope.SubScopes.Count; i++) {
67        double parentQuality = scope.SubScopes[i].GetVariableValue<DoubleData>("Quality", false).Data;
68        if (parentQuality < lowParent) lowParent = parentQuality;
69        if (parentQuality > highParent) highParent = parentQuality;
70      }
71
72      double threshold;
73      if (!maximize)
74        threshold = highParent + (lowParent - highParent) * compFactor;
75      else
76        threshold = lowParent + (highParent - lowParent) * compFactor;
77
78      BoolData successful;
79      if (((!maximize) && (child < threshold)) ||
80          ((maximize) && (child > threshold)))
81        successful = new BoolData(true);
82      else
83        successful = new BoolData(false);
84
85      scope.AddVariable(new Variable(scope.TranslateName("SuccessfulChild"), successful));
86      return null;
87    }
88  }
89}
Note: See TracBrowser for help on using the repository browser.