Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-MoveOperators/HeuristicLab.Selection/3.3/BestSelector.cs @ 12438

Last change on this file since 12438 was 8085, checked in by gkronber, 12 years ago

#1847: merged trunk changes r7800:HEAD into gp move operators branch

File size: 3.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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 HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Selection {
32  /// <summary>
33  /// A selection operator which considers a single double quality value and selects the best.
34  /// </summary>
35  [Item("BestSelector", "A selection operator which considers a single double quality value and selects the best.")]
36  [StorableClass]
37  public sealed class BestSelector : SingleObjectiveSelector, ISingleObjectiveSelector {
38    public BestSelector() : base() { }
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    }
47
48
49    protected override IScope[] Select(List<IScope> scopes) {
50      int count = NumberOfSelectedSubScopesParameter.ActualValue.Value;
51      bool copy = CopySelectedParameter.Value.Value;
52      bool maximization = MaximizationParameter.ActualValue.Value;
53      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
54      IScope[] selected = new IScope[count];
55
56      // create a list for each scope that contains the scope's index in the original scope list
57      var temp = qualities.Where(x => IsValidQuality(x.Value)).Select((x, index) => new { index, x.Value });
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
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
70      if (copy) {
71        int j = 0;
72        for (int i = 0; i < count; i++) {
73          selected[i] = (IScope)scopes[list[j].index].Clone();
74          j++;
75          if (j >= list.Count) j = 0;
76        }
77      } else {
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);
82        }
83        scopes.RemoveAll(x => x == null);
84      }
85      return selected;
86    }
87  }
88}
Note: See TracBrowser for help on using the repository browser.