#region License Information /* HeuristicLab * Copyright (C) 2002-2011 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.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Optimization; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.PluginInfrastructure; namespace HeuristicLab.Selection { /// /// The generalized rank selection operator selects qualities by rank with a varying focus on better qualities. It is implemented as described in Tate, D. M. and Smith, A. E. 1995. A genetic approach to the quadratic assignment problem. Computers & Operations Research, vol. 22, pp. 73-83. /// [Item("GeneralizedRankSelector", "The generalized rank selection operator selects qualities by rank with a varying focus on better qualities. It is implemented as described in Tate, D. M. and Smith, A. E. 1995. A genetic approach to the quadratic assignment problem. Computers & Operations Research, vol. 22, pp. 73-83.")] [StorableClass] public sealed class GeneralizedRankSelector : StochasticSingleObjectiveSelector, ISelector { public IValueLookupParameter PressureParameter { get { return (IValueLookupParameter)Parameters["Pressure"]; } } [StorableConstructor] private GeneralizedRankSelector(bool deserializing) : base(deserializing) { } private GeneralizedRankSelector(GeneralizedRankSelector original, Cloner cloner) : base(original, cloner) { } public GeneralizedRankSelector() : base() { Parameters.Add(new ValueLookupParameter("Pressure", "The selection pressure that is applied, must lie in the interval [1;infinity). A pressure of 1 equals random selection, higher pressure values focus on selecting better qualities.", new DoubleValue(2))); } public override IDeepCloneable Clone(Cloner cloner) { return new GeneralizedRankSelector(this, cloner); } protected override IScope[] Select(List scopes) { int count = NumberOfSelectedSubScopesParameter.ActualValue.Value; bool copy = CopySelectedParameter.Value.Value; IRandom random = RandomParameter.ActualValue; bool maximization = MaximizationParameter.ActualValue.Value; ItemArray qualities = QualityParameter.ActualValue; IScope[] selected = new IScope[count]; double pressure = PressureParameter.ActualValue.Value; var ordered = qualities.Select((x, index) => new IndexValuePair(index, x.Value)).OrderBy(x => x.Value).ToList(); if (maximization) ordered.Reverse(); int m = scopes.Count; for (int i = 0; i < count; i++) { double rand = 1 + random.NextDouble() * (Math.Pow(m, 1.0 / pressure) - 1); int selIdx = (int)Math.Floor(Math.Pow(rand, pressure) - 1); if (copy) { selected[i] = (IScope)scopes[ordered[selIdx].Index].Clone(); } else { int idx = ordered[selIdx].Index; selected[i] = scopes[idx]; scopes.RemoveAt(idx); ordered.RemoveAt(selIdx); for (int j = 0; j < ordered.Count; j++) { var o = ordered[j]; if (o.Index > idx) ordered[j] = o.DecrementIndex(); } m--; } } return selected; } [NonDiscoverableType] private struct IndexValuePair { private int index; internal int Index { get { return index; } } private double value; internal double Value { get { return value; } } internal IndexValuePair(int index, double value) { this.index = index; this.value = value; } internal IndexValuePair DecrementIndex() { this.index--; return this; } } } }