Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3027-NormalDistribution/HeuristicLab.Selection/3.3/WorstSelector.cs @ 17456

Last change on this file since 17456 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 3.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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.Optimization;
29using HEAL.Attic;
30
31namespace HeuristicLab.Selection {
32  /// <summary>
33  /// A selection operator which considers a single double quality value and selects the worst.
34  /// </summary>
35  [Item("WorstSelector", "A selection operator which considers a single double quality value and selects the worst.")]
36  [StorableType("FD37F242-2CB7-4528-9047-4011A61CE24F")]
37  public sealed class WorstSelector : SingleObjectiveSelector, ISingleObjectiveSelector {
38    [StorableConstructor]
39    private WorstSelector(StorableConstructorFlag _) : base(_) { }
40    private WorstSelector(WorstSelector original, Cloner cloner) : base(original, cloner) { }
41    public override IDeepCloneable Clone(Cloner cloner) {
42      return new WorstSelector(this, cloner);
43    }
44    public WorstSelector() : base() { }
45
46    protected override IScope[] Select(List<IScope> scopes) {
47      int count = NumberOfSelectedSubScopesParameter.ActualValue.Value;
48      bool copy = CopySelectedParameter.Value.Value;
49      bool maximization = MaximizationParameter.ActualValue.Value;
50      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
51      IScope[] selected = new IScope[count];
52
53      // create a list for each scope that contains the scope's index in the original scope list
54      var temp = qualities.Where(x => IsValidQuality(x.Value)).Select((x, index) => new { index, x.Value });
55      if (maximization)
56        temp = temp.OrderBy(x => x.Value);
57      else
58        temp = temp.OrderByDescending(x => x.Value);
59      var list = temp.ToList();
60
61      //check if list with indexes is as long as the original scope list
62      //otherwise invalid quality values were filtered
63      if (list.Count != scopes.Count) {
64        throw new ArgumentException("The scopes contain invalid quality values (either infinity or double.NaN) on which the selector cannot operate.");
65      }
66
67      if (copy) {
68        int j = 0;
69        for (int i = 0; i < count; i++) {
70          selected[i] = (IScope)scopes[list[j].index].Clone();
71          j++;
72          if (j >= list.Count) j = 0;
73        }
74      } else {
75        for (int i = 0; i < count; i++) {
76          selected[i] = scopes[list[0].index];
77          scopes[list[0].index] = null;
78          list.RemoveAt(0);
79        }
80        scopes.RemoveAll(x => x == null);
81      }
82      return selected;
83    }
84  }
85}
Note: See TracBrowser for help on using the repository browser.