[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;
|
---|
[3138] | 23 | using System.Collections.Generic;
|
---|
[3096] | 24 | using System.Linq;
|
---|
[4722] | 25 | using HeuristicLab.Common;
|
---|
[2] | 26 | using HeuristicLab.Core;
|
---|
[3138] | 27 | using HeuristicLab.Data;
|
---|
| 28 | using HeuristicLab.Optimization;
|
---|
[2805] | 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2] | 30 |
|
---|
| 31 | namespace HeuristicLab.Selection {
|
---|
[817] | 32 | /// <summary>
|
---|
[3096] | 33 | /// A selection operator which considers a single double quality value and selects the best.
|
---|
[817] | 34 | /// </summary>
|
---|
[3096] | 35 | [Item("BestSelector", "A selection operator which considers a single double quality value and selects the best.")]
|
---|
[3017] | 36 | [StorableClass]
|
---|
[3138] | 37 | public sealed class BestSelector : SingleObjectiveSelector, ISingleObjectiveSelector {
|
---|
[3096] | 38 | public BestSelector() : base() { }
|
---|
[4722] | 39 | [StorableConstructor]
|
---|
| 40 | private BestSelector(bool deserializing) : base(deserializing) { }
|
---|
| 41 | private BestSelector(BestSelector original, Cloner cloner)
|
---|
| 42 | : base(original, cloner) {
|
---|
| 43 | }
|
---|
| 44 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 45 | return new BestSelector(this, cloner);
|
---|
| 46 | }
|
---|
[2] | 47 |
|
---|
[4722] | 48 |
|
---|
[2830] | 49 | protected override IScope[] Select(List<IScope> scopes) {
|
---|
[2805] | 50 | int count = NumberOfSelectedSubScopesParameter.ActualValue.Value;
|
---|
| 51 | bool copy = CopySelectedParameter.Value.Value;
|
---|
[3096] | 52 | bool maximization = MaximizationParameter.ActualValue.Value;
|
---|
| 53 | ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
|
---|
[2830] | 54 | IScope[] selected = new IScope[count];
|
---|
[2805] | 55 |
|
---|
[3096] | 56 | // create a list for each scope that contains the scope's index in the original scope list
|
---|
[7995] | 57 | var temp = qualities.Where(x => IsValidQuality(x.Value)).Select((x, index) => new { index, x.Value });
|
---|
[3096] | 58 | if (maximization)
|
---|
| 59 | temp = temp.OrderByDescending(x => x.Value);
|
---|
| 60 | else
|
---|
| 61 | temp = temp.OrderBy(x => x.Value);
|
---|
| 62 | var list = temp.ToList();
|
---|
| 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 |
|
---|
[3128] | 70 | if (copy) {
|
---|
[3137] | 71 | int j = 0;
|
---|
| 72 | for (int i = 0; i < count; i++) {
|
---|
| 73 | selected[i] = (IScope)scopes[list[j].index].Clone();
|
---|
| 74 | j++;
|
---|
[3128] | 75 | if (j >= list.Count) j = 0;
|
---|
[2] | 76 | }
|
---|
[3128] | 77 | } else {
|
---|
[3137] | 78 | for (int i = 0; i < count; i++) {
|
---|
| 79 | selected[i] = scopes[list[0].index];
|
---|
| 80 | scopes[list[0].index] = null;
|
---|
| 81 | list.RemoveAt(0);
|
---|
[3128] | 82 | }
|
---|
[3137] | 83 | scopes.RemoveAll(x => x == null);
|
---|
[2] | 84 | }
|
---|
[2817] | 85 | return selected;
|
---|
[2] | 86 | }
|
---|
| 87 | }
|
---|
| 88 | }
|
---|