Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2817 was 2817, checked in by swagner, 15 years ago

Operator architecture refactoring (#95)

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