Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Selection/3.3/WorstSelector.cs @ 11316

Last change on this file since 11316 was 11171, checked in by ascheibe, 10 years ago

#2115 merged r11170 (copyright update) into trunk

File size: 3.4 KB
RevLine 
[2]1#region License Information
2/* HeuristicLab
[11171]3 * Copyright (C) 2002-2014 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;
[2805]29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[2]30
31namespace HeuristicLab.Selection {
[817]32  /// <summary>
[3096]33  /// A selection operator which considers a single double quality value and selects the worst.
[817]34  /// </summary>
[3096]35  [Item("WorstSelector", "A selection operator which considers a single double quality value and selects the worst.")]
[3017]36  [StorableClass]
[3138]37  public sealed class WorstSelector : SingleObjectiveSelector, ISingleObjectiveSelector {
[4722]38    [StorableConstructor]
39    private WorstSelector(bool deserializing) : base(deserializing) { }
40    private WorstSelector(WorstSelector original, Cloner cloner) : base(original, cloner) { }
41    public override IDeepCloneable Clone(Cloner cloner) {
42      return new WorstSelector(this, cloner);
43    }
[3096]44    public WorstSelector() : base() { }
[2]45
[2830]46    protected override IScope[] Select(List<IScope> scopes) {
[2805]47      int count = NumberOfSelectedSubScopesParameter.ActualValue.Value;
48      bool copy = CopySelectedParameter.Value.Value;
[3096]49      bool maximization = MaximizationParameter.ActualValue.Value;
50      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
[2830]51      IScope[] selected = new IScope[count];
[2805]52
[3096]53      // create a list for each scope that contains the scope's index in the original scope list
[7995]54      var temp = qualities.Where(x => IsValidQuality(x.Value)).Select((x, index) => new { index, x.Value });
[3096]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
[7995]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
[3137]67      if (copy) {
68        int j = 0;
69        for (int i = 0; i < count; i++) {
[3096]70          selected[i] = (IScope)scopes[list[j].index].Clone();
71          j++;
72          if (j >= list.Count) j = 0;
[2]73        }
[3137]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);
[2]81      }
[2817]82      return selected;
[2]83    }
84  }
85}
Note: See TracBrowser for help on using the repository browser.