Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Selection/3.3/ProportionalSelector.cs @ 7940

Last change on this file since 7940 was 7779, checked in by bburlacu, 12 years ago

#1772: Implemented new View, improved functionality (tracking of fragments and operators)

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