[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[2817] | 3 | * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2] | 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 |
|
---|
[2818] | 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
[2817] | 24 | using System.Linq;
|
---|
[2] | 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
[2817] | 27 | using HeuristicLab.Parameters;
|
---|
| 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2] | 29 |
|
---|
| 30 | namespace HeuristicLab.Selection {
|
---|
[817] | 31 | /// <summary>
|
---|
[2817] | 32 | /// A quality proportional selection operator which considers a single double quality value for selection.
|
---|
[817] | 33 | /// </summary>
|
---|
[2817] | 34 | [Item("ProportionalSelector", "A quality proportional selection operator which considers a single double quality value for selection.")]
|
---|
[3017] | 35 | [StorableClass]
|
---|
[2817] | 36 | [Creatable("Test")]
|
---|
| 37 | public sealed class ProportionalSelector : StochasticSingleObjectiveSelector {
|
---|
[2891] | 38 | private ValueParameter<BoolData> WindowingParameter {
|
---|
| 39 | get { return (ValueParameter<BoolData>)Parameters["Windowing"]; }
|
---|
[2] | 40 | }
|
---|
| 41 |
|
---|
[2817] | 42 | public BoolData Windowing {
|
---|
| 43 | get { return WindowingParameter.Value; }
|
---|
| 44 | set { WindowingParameter.Value = value; }
|
---|
[2] | 45 | }
|
---|
| 46 |
|
---|
[2817] | 47 | public ProportionalSelector()
|
---|
| 48 | : base() {
|
---|
[2891] | 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)));
|
---|
[2817] | 50 | CopySelected.Value = true;
|
---|
| 51 | }
|
---|
[2] | 52 |
|
---|
[2830] | 53 | protected override IScope[] Select(List<IScope> scopes) {
|
---|
[2817] | 54 | int count = NumberOfSelectedSubScopesParameter.ActualValue.Value;
|
---|
| 55 | bool copy = CopySelectedParameter.Value.Value;
|
---|
| 56 | IRandom random = RandomParameter.ActualValue;
|
---|
| 57 | bool maximization = MaximizationParameter.ActualValue.Value;
|
---|
| 58 | bool windowing = WindowingParameter.Value.Value;
|
---|
[2830] | 59 | IScope[] selected = new IScope[count];
|
---|
[2] | 60 |
|
---|
[2817] | 61 | // prepare qualities for proportional selection
|
---|
[2818] | 62 | var qualities = QualityParameter.ActualValue.Select(x => x.Value);
|
---|
| 63 | double minQuality = qualities.Min();
|
---|
| 64 | double maxQuality = qualities.Max();
|
---|
[2817] | 65 | if (minQuality == maxQuality) { // all quality values are equal
|
---|
[2818] | 66 | qualities = qualities.Select(x => 1.0);
|
---|
[2817] | 67 | } else {
|
---|
| 68 | if (windowing) {
|
---|
| 69 | if (maximization)
|
---|
[2818] | 70 | qualities = qualities.Select(x => x - minQuality);
|
---|
[2817] | 71 | else
|
---|
[2818] | 72 | qualities = qualities.Select(x => maxQuality - x);
|
---|
[2817] | 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);
|
---|
[2818] | 77 | qualities = qualities.Select(x => limit - x);
|
---|
[2817] | 78 | }
|
---|
[2] | 79 | }
|
---|
| 80 | }
|
---|
| 81 |
|
---|
[2818] | 82 | List<double> list = qualities.ToList();
|
---|
| 83 | double qualitySum = qualities.Sum();
|
---|
[2817] | 84 | for (int i = 0; i < count; i++) {
|
---|
| 85 | double selectedQuality = random.NextDouble() * qualitySum;
|
---|
| 86 | int index = 0;
|
---|
[2818] | 87 | double currentQuality = list[index];
|
---|
[2817] | 88 | while (currentQuality < selectedQuality) {
|
---|
| 89 | index++;
|
---|
[2818] | 90 | currentQuality += list[index];
|
---|
[2] | 91 | }
|
---|
[2817] | 92 | if (copy)
|
---|
[2830] | 93 | selected[i] = (IScope)scopes[index].Clone();
|
---|
[2817] | 94 | else {
|
---|
[2830] | 95 | selected[i] = scopes[index];
|
---|
[2817] | 96 | scopes.RemoveAt(index);
|
---|
[2818] | 97 | qualitySum -= list[index];
|
---|
| 98 | list.RemoveAt(index);
|
---|
[2] | 99 | }
|
---|
| 100 | }
|
---|
[2817] | 101 | return selected;
|
---|
[2] | 102 | }
|
---|
| 103 | }
|
---|
| 104 | }
|
---|