Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Selection/3.3/BestSelector.cs @ 17712

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

#2875: Removed years in copyrights

File size: 3.3 KB
RevLine 
[2]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) 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]22using System;
[3138]23using System.Collections.Generic;
[3096]24using System.Linq;
[4722]25using HeuristicLab.Common;
[2]26using HeuristicLab.Core;
[3138]27using HeuristicLab.Data;
28using HeuristicLab.Optimization;
[16565]29using HEAL.Attic;
[2]30
31namespace 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.")]
[16565]36  [StorableType("D0D03F30-C2A2-488B-81B0-856CA259AA03")]
[3138]37  public sealed class BestSelector : SingleObjectiveSelector, ISingleObjectiveSelector {
[3096]38    public BestSelector() : base() { }
[4722]39    [StorableConstructor]
[16565]40    private BestSelector(StorableConstructorFlag _) : base(_) { }
[4722]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}
Note: See TracBrowser for help on using the repository browser.