Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1182 was 1178, checked in by abeham, 16 years ago

fixed documentation

File size: 4.1 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", "Whether the problem is a maximization or minimization problem", typeof(BoolData), VariableKind.In));
48      AddVariableInfo(new VariableInfo("Quality", "The variable that stores the quality value", typeof(DoubleData), VariableKind.In));
49      AddVariableInfo(new VariableInfo("SuccessfulChild", "If the child is successful", typeof(BoolData), VariableKind.New));
50      AddVariableInfo(new VariableInfo("ComparisonFactor", "The comparison factor that weighs between the parents qualities", 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 scope whose offspring should be analyzed.</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(GetVariableInfo("SuccessfulChild").FormalName), successful));
86
87      return null;
88    }
89  }
90}
Note: See TracBrowser for help on using the repository browser.