#region License Information
/* HeuristicLab
* Copyright (C) 2002-2009 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using HeuristicLab.Core;
using HeuristicLab.Data;
namespace HeuristicLab.Selection.OffspringSelection {
///
/// Analyzes the offspring on whether it is successful or not based on its quality in comparison to its best and worst parents' qualities.
///
public class WeightedOffspringFitnessComparer : OperatorBase {
///
public override string Description {
get {
return @"Compares the quality values of the child with a weighted average of the best and worst parents' qualities.
Adds a variable SuccessfulChild into the current scope with the result of the comparison.";
}
}
///
/// Initializes a new instance of with four variable infos
/// (Maximization, Quality, SuccessfulChild, and ComparisonFactor).
///
public WeightedOffspringFitnessComparer()
: base() {
AddVariableInfo(new VariableInfo("Maximization", "True if the problem is a maximization problem", typeof(BoolData), VariableKind.In));
AddVariableInfo(new VariableInfo("Quality", "Quality value", typeof(DoubleData), VariableKind.In));
AddVariableInfo(new VariableInfo("SuccessfulChild", "True if the child is successful", typeof(BoolData), VariableKind.New));
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));
}
///
/// Weighs the worst and best parent quality with a given factor and decides whether the child is better than this threshold.
/// The result of this decision is added as variable "SuccessfulChild" into the scope.
///
/// The current scope which represents a new child.
/// null.
public override IOperation Apply(IScope scope) {
bool maximize = GetVariableValue("Maximization", scope, true).Data;
double compFactor = GetVariableValue("ComparisonFactor", scope, true).Data;
double child = GetVariableValue("Quality", scope, false).Data;
double lowParent = double.MaxValue; // lowest quality parent
double highParent = double.MinValue; // highest quality parent
for (int i = 0; i < scope.SubScopes.Count; i++) {
double parentQuality = scope.SubScopes[i].GetVariableValue("Quality", false).Data;
if (parentQuality < lowParent) lowParent = parentQuality;
if (parentQuality > highParent) highParent = parentQuality;
}
double threshold;
if (!maximize)
threshold = highParent + (lowParent - highParent) * compFactor;
else
threshold = lowParent + (highParent - lowParent) * compFactor;
BoolData successful;
if (((!maximize) && (child < threshold)) ||
((maximize) && (child > threshold)))
successful = new BoolData(true);
else
successful = new BoolData(false);
scope.AddVariable(new Variable(scope.TranslateName("SuccessfulChild"), successful));
return null;
}
}
}