#region License Information
/* HeuristicLab
* Copyright (C) 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.Collections.Generic;
using System.Linq;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
using HEAL.Attic;
namespace HeuristicLab.Selection {
[Item("CrowdedTournamentSelector", "Selects solutions using tournament selection by using the partial order defined in Deb et al. 2002. A Fast and Elitist Multiobjective Genetic Algorithm: NSGA-II. IEEE Transactions on Evolutionary Computation, 6(2), pp. 182-197.")]
[StorableType("343E8160-873B-4F76-BCA0-9F031CB6D1D7")]
public class CrowdedTournamentSelector : Selector, IMultiObjectiveSelector, IStochasticOperator {
public ILookupParameter MaximizationParameter {
get { return (ILookupParameter)Parameters["Maximization"]; }
}
public IValueLookupParameter NumberOfSelectedSubScopesParameter {
get { return (IValueLookupParameter)Parameters["NumberOfSelectedSubScopes"]; }
}
protected IValueParameter CopySelectedParameter {
get { return (IValueParameter)Parameters["CopySelected"]; }
}
public ILookupParameter RandomParameter {
get { return (ILookupParameter)Parameters["Random"]; }
}
public ILookupParameter> QualitiesParameter {
get { return (ILookupParameter>)Parameters["Qualities"]; }
}
public IScopeTreeLookupParameter RankParameter {
get { return (IScopeTreeLookupParameter)Parameters["Rank"]; }
}
public IScopeTreeLookupParameter CrowdingDistanceParameter {
get { return (IScopeTreeLookupParameter)Parameters["CrowdingDistance"]; }
}
public IValueLookupParameter GroupSizeParameter {
get { return (IValueLookupParameter)Parameters["GroupSize"]; }
}
public BoolValue CopySelected {
get { return CopySelectedParameter.Value; }
set { CopySelectedParameter.Value = value; }
}
[StorableConstructor]
protected CrowdedTournamentSelector(StorableConstructorFlag _) : base(_) { }
protected CrowdedTournamentSelector(CrowdedTournamentSelector original, Cloner cloner) : base(original, cloner) { }
public CrowdedTournamentSelector()
: base() {
Parameters.Add(new LookupParameter("Maximization", "For each objective determines whether it should be maximized or minimized."));
Parameters.Add(new ValueLookupParameter("NumberOfSelectedSubScopes", "The number of sub-scopes that should be selected."));
Parameters.Add(new ValueParameter("CopySelected", "True if the selected scopes are to be copied (cloned) otherwise they're moved."));
Parameters.Add(new LookupParameter("Random", "The random number generator."));
Parameters.Add(new ScopeTreeLookupParameter("Qualities", "The solutions' qualities vector."));
Parameters.Add(new ScopeTreeLookupParameter("Rank", "The solutions' domination rank."));
Parameters.Add(new ScopeTreeLookupParameter("CrowdingDistance", "The solutions' crowding distance values."));
Parameters.Add(new ValueLookupParameter("GroupSize", "The size of the group from which the best will be chosen.", new IntValue(2)));
CopySelectedParameter.Hidden = true;
}
protected override IScope[] Select(List scopes) {
IRandom random = RandomParameter.ActualValue;
List ranks = RankParameter.ActualValue.Select(x => x.Value).ToList();
List crowdingDistance = CrowdingDistanceParameter.ActualValue.Select(x => x.Value).ToList();
int count = NumberOfSelectedSubScopesParameter.ActualValue.Value;
int groupSize = GroupSizeParameter.ActualValue.Value;
bool copy = CopySelected.Value;
IScope[] selected = new IScope[count];
for (int i = 0; i < count; i++) {
int best = random.Next(scopes.Count);
int index;
for (int j = 1; j < groupSize; j++) {
index = random.Next(scopes.Count);
if (ranks[best] > ranks[index]
|| ranks[best] == ranks[index]
&& crowdingDistance[best] < crowdingDistance[index]) {
best = index;
}
}
if (copy)
selected[i] = (IScope)scopes[best].Clone();
else {
selected[i] = scopes[best];
scopes.RemoveAt(best);
ranks.RemoveAt(best);
crowdingDistance.RemoveAt(best);
}
}
return selected;
}
public override IDeepCloneable Clone(Cloner cloner) {
return new CrowdedTournamentSelector(this, cloner);
}
}
}