Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/17/10 00:30:46 (15 years ago)
Author:
swagner
Message:

Operator architecture refactoring (#95)

  • worked on selection
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Selection/3.3/LinearRankSelector.cs

    r1530 r2817  
    11#region License Information
    22/* HeuristicLab
    3  * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
    44 *
    55 * This file is part of HeuristicLab.
     
    2020#endregion
    2121
    22 using System;
     22using System.Linq;
    2323using System.Collections.Generic;
    24 using System.Text;
    2524using HeuristicLab.Core;
    2625using HeuristicLab.Data;
     26using HeuristicLab.Parameters;
     27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2728
    2829namespace HeuristicLab.Selection {
    2930  /// <summary>
    30   /// Selects scopes based on their rank, which has been determined through their quality.
     31  /// A linear rank selection operator which considers the rank based on a single double quality value for selection.
    3132  /// </summary>
    32   public class LinearRankSelector : StochasticSelectorBase {
    33     /// <inheritdoc select="summary"/>
    34     public override string Description {
    35       get { return @"TODO\r\nOperator description still missing ..."; }
     33  [Item("LinearRankSelector", "A linear rank selection operator which considers the rank based on a single double quality value for selection.")]
     34  [EmptyStorableClass]
     35  [Creatable("Test")]
     36  public sealed class LinearRankSelector : StochasticSingleObjectiveSelector {
     37    public LinearRankSelector()
     38      : base() {
     39      CopySelected.Value = true;
    3640    }
    3741
    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     }
     42    protected override ScopeList Select(ScopeList scopes) {
     43      int count = NumberOfSelectedSubScopesParameter.ActualValue.Value;
     44      bool copy = CopySelectedParameter.Value.Value;
     45      IRandom random = RandomParameter.ActualValue;
     46      bool maximization = MaximizationParameter.ActualValue.Value;
     47      ItemArray<DoubleData> qualities = QualityParameter.ActualValue;
     48      ScopeList selected = new ScopeList();
    4549
    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;
     50      // create a list for each scope that contains the scope's index in the original scope list and its lots
     51      var temp = qualities.Select((x, index) => new { index, x.Value });
     52      if (maximization)
     53        temp = temp.OrderBy(x => x.Value);
     54      else
     55        temp = temp.OrderByDescending(x => x.Value);
     56      var list = temp.Select((x, lots) => new { x.index, lots }).ToList();
    6257
    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;
     58      int lotSum = list.Count * (list.Count + 1) / 2;
     59      for (int i = 0; i < count; i++) {
     60        int selectedLot = random.Next(lotSum) + 1;
     61        int index = 0;
     62        int currentLot = list[index].lots;
    6963        while (currentLot < selectedLot) {
    7064          index++;
    71           currentLot += subScopes - index;
     65          currentLot += list[index].lots;
    7266        }
    73         IScope selectedScope = source.SubScopes[index];
    74         if (copySelected)
    75           target.AddSubScope((IScope)selectedScope.Clone());
     67        if (copy)
     68          selected.Add((IScope)scopes[list[index].index].Clone());
    7669        else {
    77           source.RemoveSubScope(selectedScope);
    78           target.AddSubScope(selectedScope);
    79           subScopes--;
    80           lotSum = (subScopes * (subScopes + 1)) / 2;
     70          selected.Add(scopes[list[index].index]);
     71          scopes.RemoveAt(list[index].index);
     72          lotSum -= list[index].lots;
     73          list.RemoveAt(index);
    8174        }
    8275      }
     76      return selected;
    8377    }
    8478  }
Note: See TracChangeset for help on using the changeset viewer.