Free cookie consent management tool by TermsFeed Policy Generator

source: branches/histogram/HeuristicLab.Selection/3.3/GeneralizedRankSelector.cs @ 6081

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

#1496

  • Implemented GeneralizedRankSelector
File size: 3.8 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;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Selection {
32  /// <summary>
33  /// 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.
34  /// </summary>
35  [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.")]
36  [StorableClass]
37  public sealed class GeneralizedRankSelector : StochasticSingleObjectiveSelector {
38
39    public IValueLookupParameter<DoubleValue> PressureParameter {
40      get { return (IValueLookupParameter<DoubleValue>)Parameters["Pressure"]; }
41    }
42
43    [StorableConstructor]
44    private GeneralizedRankSelector(bool deserializing) : base(deserializing) { }
45    private GeneralizedRankSelector(GeneralizedRankSelector original, Cloner cloner) : base(original, cloner) { }
46    public GeneralizedRankSelector()
47      : base() {
48      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)));
49    }
50
51    public override IDeepCloneable Clone(Cloner cloner) {
52      return new GeneralizedRankSelector(this, cloner);
53    }
54
55    protected override IScope[] Select(List<IScope> scopes) {
56      int count = NumberOfSelectedSubScopesParameter.ActualValue.Value;
57      bool copy = CopySelectedParameter.Value.Value;
58      IRandom random = RandomParameter.ActualValue;
59      bool maximization = MaximizationParameter.ActualValue.Value;
60      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
61      IScope[] selected = new IScope[count];
62      double pressure = PressureParameter.ActualValue.Value;
63
64      var ordered = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).ToList();
65      if (maximization) ordered.Reverse();
66
67      int m = scopes.Count;
68      for (int i = 0; i < count; i++) {
69        double rand = 1 + random.NextDouble() * (Math.Pow(m, 1.0 / pressure) - 1);
70        int selIdx = (int)Math.Floor(Math.Pow(rand, pressure) - 1);
71
72        if (copy) {
73          selected[i] = (IScope)scopes[ordered[selIdx].index].Clone();
74        } else {
75          selected[i] = scopes[ordered[selIdx].index];
76          scopes.Remove(selected[i]);
77          m--;
78        }
79      }
80      return selected;
81    }
82  }
83}
Note: See TracBrowser for help on using the repository browser.