Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1496

  • Added NonDiscoverableType attribute
File size: 4.6 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;
31using HeuristicLab.PluginInfrastructure;
32
33namespace HeuristicLab.Selection {
34  /// <summary>
35  /// 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.
36  /// </summary>
37  [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.")]
38  [StorableClass]
39  public sealed class GeneralizedRankSelector : StochasticSingleObjectiveSelector, ISelector {
40
41    public IValueLookupParameter<DoubleValue> PressureParameter {
42      get { return (IValueLookupParameter<DoubleValue>)Parameters["Pressure"]; }
43    }
44
45    [StorableConstructor]
46    private GeneralizedRankSelector(bool deserializing) : base(deserializing) { }
47    private GeneralizedRankSelector(GeneralizedRankSelector original, Cloner cloner) : base(original, cloner) { }
48    public GeneralizedRankSelector()
49      : base() {
50      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)));
51    }
52
53    public override IDeepCloneable Clone(Cloner cloner) {
54      return new GeneralizedRankSelector(this, cloner);
55    }
56
57    protected override IScope[] Select(List<IScope> scopes) {
58      int count = NumberOfSelectedSubScopesParameter.ActualValue.Value;
59      bool copy = CopySelectedParameter.Value.Value;
60      IRandom random = RandomParameter.ActualValue;
61      bool maximization = MaximizationParameter.ActualValue.Value;
62      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
63      IScope[] selected = new IScope[count];
64      double pressure = PressureParameter.ActualValue.Value;
65
66      var ordered = qualities.Select((x, index) => new IndexValuePair(index, x.Value)).OrderBy(x => x.Value).ToList();
67      if (maximization) ordered.Reverse();
68
69      int m = scopes.Count;
70      for (int i = 0; i < count; i++) {
71        double rand = 1 + random.NextDouble() * (Math.Pow(m, 1.0 / pressure) - 1);
72        int selIdx = (int)Math.Floor(Math.Pow(rand, pressure) - 1);
73
74        if (copy) {
75          selected[i] = (IScope)scopes[ordered[selIdx].Index].Clone();
76        } else {
77          int idx = ordered[selIdx].Index;
78          selected[i] = scopes[idx];
79          scopes.RemoveAt(idx);
80          ordered.RemoveAt(selIdx);
81          for (int j = 0; j < ordered.Count; j++) {
82            var o = ordered[j];
83            if (o.Index > idx) ordered[j] = o.DecrementIndex();
84          }
85          m--;
86        }
87      }
88      return selected;
89    }
90
91    [NonDiscoverableType]
92    private struct IndexValuePair {
93      private int index;
94      internal int Index { get { return index; } }
95      private double value;
96      internal double Value { get { return value; } }
97
98      internal IndexValuePair(int index, double value) {
99        this.index = index;
100        this.value = value;
101      }
102
103      internal IndexValuePair DecrementIndex() {
104        this.index--;
105        return this;
106      }
107    }
108  }
109}
Note: See TracBrowser for help on using the repository browser.