Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1496

  • reverted last change ...
File size: 4.5 KB
RevLine 
[6081]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;
[6082]28using HeuristicLab.Optimization;
[6081]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]
[6082]38  public sealed class GeneralizedRankSelector : StochasticSingleObjectiveSelector, ISelector {
[6081]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
[6508]65      var ordered = qualities.Select((x, index) => new IndexValuePair(index, x.Value)).OrderBy(x => x.Value).ToList();
[6081]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) {
[6508]74          selected[i] = (IScope)scopes[ordered[selIdx].Index].Clone();
[6081]75        } else {
[6508]76          int idx = ordered[selIdx].Index;
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.Index > idx) ordered[j] = o.DecrementIndex();
83          }
[6081]84          m--;
85        }
86      }
87      return selected;
88    }
[6508]89
90    private struct IndexValuePair {
91      private int index;
92      internal int Index { get { return index; } }
93      private double value;
94      internal double Value { get { return value; } }
95
96      internal IndexValuePair(int index, double value) {
97        this.index = index;
98        this.value = value;
99      }
100
101      internal IndexValuePair DecrementIndex() {
102        this.index--;
103        return this;
104      }
105    }
[6081]106  }
107}
Note: See TracBrowser for help on using the repository browser.