[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[9456] | 3 | * Copyright (C) 2002-2013 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 |
|
---|
[7995] | 22 | using System;
|
---|
[2830] | 23 | using System.Collections.Generic;
|
---|
[2817] | 24 | using System.Linq;
|
---|
[4722] | 25 | using HeuristicLab.Common;
|
---|
[2] | 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
[3138] | 28 | using HeuristicLab.Optimization;
|
---|
[2817] | 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2] | 30 |
|
---|
| 31 | namespace HeuristicLab.Selection {
|
---|
[817] | 32 | /// <summary>
|
---|
[2817] | 33 | /// A linear rank selection operator which considers the rank based on a single double quality value for selection.
|
---|
[817] | 34 | /// </summary>
|
---|
[2817] | 35 | [Item("LinearRankSelector", "A linear rank selection operator which considers the rank based on a single double quality value for selection.")]
|
---|
[3017] | 36 | [StorableClass]
|
---|
[3138] | 37 | public sealed class LinearRankSelector : StochasticSingleObjectiveSelector, ISingleObjectiveSelector {
|
---|
[4722] | 38 | [StorableConstructor]
|
---|
| 39 | private LinearRankSelector(bool deserializing) : base(deserializing) { }
|
---|
| 40 | private LinearRankSelector(LinearRankSelector original, Cloner cloner)
|
---|
| 41 | : base(original, cloner) {
|
---|
| 42 | }
|
---|
| 43 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 44 | return new LinearRankSelector(this, cloner);
|
---|
| 45 | }
|
---|
[3096] | 46 | public LinearRankSelector() : base() { }
|
---|
[2] | 47 |
|
---|
[2830] | 48 | protected override IScope[] Select(List<IScope> scopes) {
|
---|
[2817] | 49 | int count = NumberOfSelectedSubScopesParameter.ActualValue.Value;
|
---|
| 50 | bool copy = CopySelectedParameter.Value.Value;
|
---|
| 51 | IRandom random = RandomParameter.ActualValue;
|
---|
| 52 | bool maximization = MaximizationParameter.ActualValue.Value;
|
---|
[3048] | 53 | ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
|
---|
[2830] | 54 | IScope[] selected = new IScope[count];
|
---|
[2] | 55 |
|
---|
[2817] | 56 | // create a list for each scope that contains the scope's index in the original scope list and its lots
|
---|
[7995] | 57 | var temp = qualities.Where(x => IsValidQuality(x.Value)).Select((x, index) => new { index, x.Value });
|
---|
[2817] | 58 | if (maximization)
|
---|
| 59 | temp = temp.OrderBy(x => x.Value);
|
---|
| 60 | else
|
---|
| 61 | temp = temp.OrderByDescending(x => x.Value);
|
---|
[2818] | 62 | var list = temp.Select((x, index) => new { x.index, lots = index + 1 }).ToList();
|
---|
[2] | 63 |
|
---|
[7995] | 64 | //check if list with indexes is as long as the original scope list
|
---|
| 65 | //otherwise invalid quality values were filtered
|
---|
| 66 | if (list.Count != scopes.Count) {
|
---|
| 67 | throw new ArgumentException("The scopes contain invalid quality values (either infinity or double.NaN) on which the selector cannot operate.");
|
---|
| 68 | }
|
---|
| 69 |
|
---|
[2817] | 70 | int lotSum = list.Count * (list.Count + 1) / 2;
|
---|
| 71 | for (int i = 0; i < count; i++) {
|
---|
| 72 | int selectedLot = random.Next(lotSum) + 1;
|
---|
[3137] | 73 | int j = 0;
|
---|
| 74 | int currentLot = list[j].lots;
|
---|
[2] | 75 | while (currentLot < selectedLot) {
|
---|
[3137] | 76 | j++;
|
---|
| 77 | currentLot += list[j].lots;
|
---|
[2] | 78 | }
|
---|
[2817] | 79 | if (copy)
|
---|
[3137] | 80 | selected[i] = (IScope)scopes[list[j].index].Clone();
|
---|
[2] | 81 | else {
|
---|
[3137] | 82 | selected[i] = scopes[list[j].index];
|
---|
| 83 | scopes[list[j].index] = null;
|
---|
| 84 | lotSum -= list[j].lots;
|
---|
| 85 | list.RemoveAt(j);
|
---|
[2] | 86 | }
|
---|
| 87 | }
|
---|
[3137] | 88 | if (!copy) scopes.RemoveAll(x => x == null);
|
---|
[2817] | 89 | return selected;
|
---|
[2] | 90 | }
|
---|
| 91 | }
|
---|
| 92 | }
|
---|