Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2931_OR-Tools_LP_MIP/HeuristicLab.Selection/3.3/LinearRankSelector.cs @ 16300

Last change on this file since 16300 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 3.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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 linear rank selection operator which considers the rank based on a single double quality value for selection.
34  /// </summary>
35  [Item("LinearRankSelector", "A linear rank selection operator which considers the rank based on a single double quality value for selection.")]
36  [StorableClass]
37  public sealed class LinearRankSelector : StochasticSingleObjectiveSelector, ISingleObjectiveSelector {
38    [StorableConstructor]
39    private LinearRankSelector(bool deserializing) : base(deserializing) { }
40    private LinearRankSelector(LinearRankSelector original, Cloner cloner)
41      : base(original, cloner) {
42    }
43    public override IDeepCloneable Clone(Cloner cloner) {
44      return new LinearRankSelector(this, cloner);
45    }
46    public LinearRankSelector() : base() { }
47
48    protected override IScope[] Select(List<IScope> scopes) {
49      int count = NumberOfSelectedSubScopesParameter.ActualValue.Value;
50      bool copy = CopySelectedParameter.Value.Value;
51      IRandom random = RandomParameter.ActualValue;
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 and its lots
57      var temp = qualities.Where(x => IsValidQuality(x.Value)).Select((x, index) => new { index, x.Value });
58      if (maximization)
59        temp = temp.OrderBy(x => x.Value);
60      else
61        temp = temp.OrderByDescending(x => x.Value);
62      var list = temp.Select((x, index) => new { x.index, lots = index + 1 }).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      int lotSum = list.Count * (list.Count + 1) / 2;
71      for (int i = 0; i < count; i++) {
72        int selectedLot = random.Next(lotSum) + 1;
73        int j = 0;
74        int currentLot = list[j].lots;
75        while (currentLot < selectedLot) {
76          j++;
77          currentLot += list[j].lots;
78        }
79        if (copy)
80          selected[i] = (IScope)scopes[list[j].index].Clone();
81        else {
82          selected[i] = scopes[list[j].index];
83          scopes[list[j].index] = null;
84          lotSum -= list[j].lots;
85          list.RemoveAt(j);
86        }
87      }
88      if (!copy) scopes.RemoveAll(x => x == null);
89      return selected;
90    }
91  }
92}
Note: See TracBrowser for help on using the repository browser.