#region License Information
/* HeuristicLab
* Copyright (C) 2002-2008 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 {
///
/// Moves or copies a defined number of the best sub scopes from a source scope to a target scope.
///
public class TournamentSelector : StochasticSelectorBase {
///
public override string Description {
get { return @"TODO\r\nOperator description still missing ..."; }
}
///
/// Initializes a new instance of with three variable infos
/// (Maximization, Quality and GroupSize, being a local variable and set to
/// 2) with CopySelected set to true.
///
public TournamentSelector() {
AddVariableInfo(new VariableInfo("Maximization", "Maximization problem", typeof(BoolData), VariableKind.In));
AddVariableInfo(new VariableInfo("Quality", "Quality value", typeof(DoubleData), VariableKind.In));
AddVariableInfo(new VariableInfo("GroupSize", "Size of the tournament group", typeof(IntData), VariableKind.In));
GetVariableInfo("GroupSize").Local = true;
AddVariable(new Variable("GroupSize", new IntData(2)));
GetVariable("CopySelected").GetValue().Data = true;
}
///
/// Copies or moves the best sub scopes from the given to the specified
/// .
///
/// Thrown when no source sub scopes are available.
/// The random number generator.
/// The source scope from where to copy/move the sub scopes.
/// The number of sub scopes to copy/move.
/// The target scope where to add the sub scopes.
/// Boolean flag whether the sub scopes shall be moved or copied.
protected override void Select(IRandom random, IScope source, int selected, IScope target, bool copySelected) {
IVariableInfo qualityInfo = GetVariableInfo("Quality");
bool maximization = GetVariableValue("Maximization", source, true).Data;
int groupSize = GetVariableValue("GroupSize", source, true).Data;
for (int i = 0; i < selected; i++) {
if (source.SubScopes.Count < 1) throw new InvalidOperationException("No source scopes available to select.");
IScope selectedScope = source.SubScopes[random.Next(source.SubScopes.Count)];
double best = selectedScope.GetVariableValue(qualityInfo.FormalName, false).Data;
for (int j = 1; j < groupSize; j++) {
IScope scope = source.SubScopes[random.Next(source.SubScopes.Count)];
double quality = scope.GetVariableValue(qualityInfo.FormalName, false).Data;
if (((maximization) && (quality > best)) ||
((!maximization) && (quality < best))) {
best = quality;
selectedScope = scope;
}
}
if (copySelected)
target.AddSubScope((IScope)selectedScope.Clone());
else {
source.RemoveSubScope(selectedScope);
target.AddSubScope(selectedScope);
}
}
}
}
}