Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1223 was 1223, checked in by swagner, 16 years ago

Adapted some of the XML code comments and some of the VariableInfo descriptions. Added a local default variable for ComparisonFactor. (#475)

File size: 4.3 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 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 parents'.
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
51      VariableInfo compFactorInfo = 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);
52      compFactorInfo.Local = true;
53      AddVariableInfo(compFactorInfo);
54      AddVariable(new Variable("ComparisonFactor", new DoubleData(0.5)));
55    }
56
57    /// <summary>
58    /// Weighs the worst and best parent quality with a given factor and decides whether the child is better than this threshold.
59    /// The result of this decision is added as variable "SuccessfulChild" into the scope.
60    /// </summary>
61    /// <param name="scope">The current scope which represents a new child.</param>
62    /// <returns><c>null</c>.</returns>
63    public override IOperation Apply(IScope scope) {
64      bool maximize = GetVariableValue<BoolData>("Maximization", scope, true).Data;
65      double compFactor = GetVariableValue<DoubleData>("ComparisonFactor", scope, true).Data;
66      double child = GetVariableValue<DoubleData>("Quality", scope, false).Data;
67
68      double lowParent = double.MaxValue; // lowest quality parent
69      double highParent = double.MinValue; // highest quality parent
70      for (int i = 0; i < scope.SubScopes.Count; i++) {
71        double parentQuality = scope.SubScopes[i].GetVariableValue<DoubleData>("Quality", false).Data;
72        if (parentQuality < lowParent) lowParent = parentQuality;
73        if (parentQuality > highParent) highParent = parentQuality;
74      }
75
76      double threshold;
77      if (!maximize)
78        threshold = highParent + (lowParent - highParent) * compFactor;
79      else
80        threshold = lowParent + (highParent - lowParent) * compFactor;
81
82      BoolData successful;
83      if (((!maximize) && (child < threshold)) ||
84          ((maximize) && (child > threshold)))
85        successful = new BoolData(true);
86      else
87        successful = new BoolData(false);
88
89      scope.AddVariable(new Variable(scope.TranslateName("SuccessfulChild"), successful));
90      return null;
91    }
92  }
93}
Note: See TracBrowser for help on using the repository browser.