1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Optimization;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 | using HEAL.Attic;
|
---|
31 |
|
---|
32 | namespace 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 | [StorableType("6423609D-9C11-4CC5-99A1-39257A306DF6")]
|
---|
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(StorableConstructorFlag _) : base(_) { }
|
---|
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.Where(x => IsValidQuality(x.Value)).Select((x, index) => new KeyValuePair<int, double>(index, x.Value)).OrderBy(x => x.Value).ToList();
|
---|
66 | if (maximization) ordered.Reverse();
|
---|
67 |
|
---|
68 | //check if list with indexes is as long as the original scope list
|
---|
69 | //otherwise invalid quality values were filtered
|
---|
70 | if (ordered.Count != scopes.Count) {
|
---|
71 | throw new ArgumentException("The scopes contain invalid quality values (either infinity or double.NaN) on which the selector cannot operate.");
|
---|
72 | }
|
---|
73 |
|
---|
74 | int m = scopes.Count;
|
---|
75 | for (int i = 0; i < count; i++) {
|
---|
76 | double rand = 1 + random.NextDouble() * (Math.Pow(m, 1.0 / pressure) - 1);
|
---|
77 | int selIdx = (int)Math.Floor(Math.Pow(rand, pressure) - 1);
|
---|
78 |
|
---|
79 | if (copy) {
|
---|
80 | selected[i] = (IScope)scopes[ordered[selIdx].Key].Clone();
|
---|
81 | } else {
|
---|
82 | int idx = ordered[selIdx].Key;
|
---|
83 | selected[i] = scopes[idx];
|
---|
84 | scopes.RemoveAt(idx);
|
---|
85 | ordered.RemoveAt(selIdx);
|
---|
86 | for (int j = 0; j < ordered.Count; j++) {
|
---|
87 | var o = ordered[j];
|
---|
88 | if (o.Key > idx) ordered[j] = new KeyValuePair<int, double>(o.Key - 1, o.Value);
|
---|
89 | }
|
---|
90 | m--;
|
---|
91 | }
|
---|
92 | }
|
---|
93 | return selected;
|
---|
94 | }
|
---|
95 | }
|
---|
96 | }
|
---|