Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Selection/3.3/ProportionalSelector.cs @ 3096

Last change on this file since 3096 was 3096, checked in by swagner, 14 years ago

Replaced LeftSelector and RightSelector by BestSelector and WorstSelector (#926)

File size: 4.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Selection {
31  /// <summary>
32  /// A quality proportional selection operator which considers a single double quality value for selection.
33  /// </summary>
34  [Item("ProportionalSelector", "A quality proportional selection operator which considers a single double quality value for selection.")]
35  [StorableClass]
36  [Creatable("Test")]
37  public sealed class ProportionalSelector : StochasticSingleObjectiveSelector {
38    private ValueParameter<BoolValue> WindowingParameter {
39      get { return (ValueParameter<BoolValue>)Parameters["Windowing"]; }
40    }
41
42    public BoolValue Windowing {
43      get { return WindowingParameter.Value; }
44      set { WindowingParameter.Value = value; }
45    }
46
47    public ProportionalSelector()
48      : base() {
49      Parameters.Add(new ValueParameter<BoolValue>("Windowing", "Apply windowing strategy (selection probability is proportional to the quality differences and not to the total quality).", new BoolValue(true)));
50    }
51
52    protected override IScope[] Select(List<IScope> scopes) {
53      int count = NumberOfSelectedSubScopesParameter.ActualValue.Value;
54      bool copy = CopySelectedParameter.Value.Value;
55      IRandom random = RandomParameter.ActualValue;
56      bool maximization = MaximizationParameter.ActualValue.Value;
57      bool windowing = WindowingParameter.Value.Value;
58      IScope[] selected = new IScope[count];
59
60      // prepare qualities for proportional selection
61      var qualities = QualityParameter.ActualValue.Select(x => x.Value);
62      double minQuality = qualities.Min();
63      double maxQuality = qualities.Max();
64      if (minQuality == maxQuality) {  // all quality values are equal
65        qualities = qualities.Select(x => 1.0);
66      } else {
67        if (windowing) {
68          if (maximization)
69            qualities = qualities.Select(x => x - minQuality);
70          else
71            qualities = qualities.Select(x => maxQuality - x);
72        } else {
73          if (minQuality < 0.0) throw new InvalidOperationException("Proportional selection without windowing does not work with quality values < 0.");
74          if (!maximization) {
75            double limit = Math.Min(maxQuality * 2, double.MaxValue);
76            qualities = qualities.Select(x => limit - x);
77          }
78        }
79      }
80
81      List<double> list = qualities.ToList();
82      double qualitySum = qualities.Sum();
83      for (int i = 0; i < count; i++) {
84        double selectedQuality = random.NextDouble() * qualitySum;
85        int index = 0;
86        double currentQuality = list[index];
87        while (currentQuality < selectedQuality) {
88          index++;
89          currentQuality += list[index];
90        }
91        if (copy)
92          selected[i] = (IScope)scopes[index].Clone();
93        else {
94          selected[i] = scopes[index];
95          scopes.RemoveAt(index);
96          qualitySum -= list[index];
97          list.RemoveAt(index);
98        }
99      }
100      return selected;
101    }
102  }
103}
Note: See TracBrowser for help on using the repository browser.