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