Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Selection/3.2/LinearRankSelector.cs @ 2174

Last change on this file since 2174 was 1530, checked in by gkronber, 16 years ago

Moved source files of plugins Hive ... Visualization.Test into version-specific sub-folders. #576

File size: 3.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.Text;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27
28namespace HeuristicLab.Selection {
29  /// <summary>
30  /// Selects scopes based on their rank, which has been determined through their quality.
31  /// </summary>
32  public class LinearRankSelector : StochasticSelectorBase {
33    /// <inheritdoc select="summary"/>
34    public override string Description {
35      get { return @"TODO\r\nOperator description still missing ..."; }
36    }
37
38    /// <summary>
39    /// Initializes a new instance of <see cref="LinearRankSelector"/> with the <c>CopySelected</c> flag
40    /// set to <c>true</c>.
41    /// </summary>
42    public LinearRankSelector() {
43      GetVariable("CopySelected").GetValue<BoolData>().Data = true;
44    }
45
46    /// <summary>
47    /// Copies or moves sub scopes from the given <paramref name="source"/> to the specified
48    /// <paramref name="target"/> according to their rank which is determined through their quality.
49    /// </summary>
50    /// <exception cref="InvalidOperationException">Thrown when no source sub scopes are available.</exception>
51    /// <param name="random">The random number generator.</param>
52    /// <param name="source">The source scope from where to copy/move the sub scopes.</param>
53    /// <param name="selected">The number of sub scopes to copy/move.</param>
54    /// <param name="target">The target scope where to add the sub scopes.</param>
55    /// <param name="copySelected">Boolean flag whether the sub scopes shall be moved or copied.</param>
56    protected override void Select(IRandom random, IScope source, int selected, IScope target, bool copySelected) {
57      int subScopes = source.SubScopes.Count;
58      int lotSum = (subScopes * (subScopes + 1)) / 2;
59      int selectedLot;
60      int currentLot;
61      int index;
62
63      for (int i = 0; i < selected; i++) {
64        if (subScopes < 1) throw new InvalidOperationException("No source scopes available to select.");
65
66        selectedLot = random.Next(1, lotSum + 1);
67        currentLot = subScopes;  // first individual is the best one
68        index = 0;
69        while (currentLot < selectedLot) {
70          index++;
71          currentLot += subScopes - index;
72        }
73        IScope selectedScope = source.SubScopes[index];
74        if (copySelected)
75          target.AddSubScope((IScope)selectedScope.Clone());
76        else {
77          source.RemoveSubScope(selectedScope);
78          target.AddSubScope(selectedScope);
79          subScopes--;
80          lotSum = (subScopes * (subScopes + 1)) / 2;
81        }
82      }
83    }
84  }
85}
Note: See TracBrowser for help on using the repository browser.