Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Selection/3.3/GeneralizedRankSelector.cs @ 6675

Last change on this file since 6675 was 6511, checked in by abeham, 13 years ago

#1496

  • a little undecided today, I removed the private struct for using KeyValuePair<U, V>
File size: 4.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Selection {
33  /// <summary>
34  /// 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.
35  /// </summary>
36  [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.")]
37  [StorableClass]
38  public sealed class GeneralizedRankSelector : StochasticSingleObjectiveSelector, ISelector {
39
40    public IValueLookupParameter<DoubleValue> PressureParameter {
41      get { return (IValueLookupParameter<DoubleValue>)Parameters["Pressure"]; }
42    }
43
44    [StorableConstructor]
45    private GeneralizedRankSelector(bool deserializing) : base(deserializing) { }
46    private GeneralizedRankSelector(GeneralizedRankSelector original, Cloner cloner) : base(original, cloner) { }
47    public GeneralizedRankSelector()
48      : base() {
49      Parameters.Add(new ValueLookupParameter<DoubleValue>("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)));
50    }
51
52    public override IDeepCloneable Clone(Cloner cloner) {
53      return new GeneralizedRankSelector(this, cloner);
54    }
55
56    protected override IScope[] Select(List<IScope> scopes) {
57      int count = NumberOfSelectedSubScopesParameter.ActualValue.Value;
58      bool copy = CopySelectedParameter.Value.Value;
59      IRandom random = RandomParameter.ActualValue;
60      bool maximization = MaximizationParameter.ActualValue.Value;
61      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
62      IScope[] selected = new IScope[count];
63      double pressure = PressureParameter.ActualValue.Value;
64
65      var ordered = qualities.Select((x, index) => new KeyValuePair<int, double>(index, x.Value)).OrderBy(x => x.Value).ToList();
66      if (maximization) ordered.Reverse();
67
68      int m = scopes.Count;
69      for (int i = 0; i < count; i++) {
70        double rand = 1 + random.NextDouble() * (Math.Pow(m, 1.0 / pressure) - 1);
71        int selIdx = (int)Math.Floor(Math.Pow(rand, pressure) - 1);
72
73        if (copy) {
74          selected[i] = (IScope)scopes[ordered[selIdx].Key].Clone();
75        } else {
76          int idx = ordered[selIdx].Key;
77          selected[i] = scopes[idx];
78          scopes.RemoveAt(idx);
79          ordered.RemoveAt(selIdx);
80          for (int j = 0; j < ordered.Count; j++) {
81            var o = ordered[j];
82            if (o.Key > idx) ordered[j] = new KeyValuePair<int, double>(o.Key - 1, o.Value);
83          }
84          m--;
85        }
86      }
87      return selected;
88    }
89  }
90}
Note: See TracBrowser for help on using the repository browser.