- Timestamp:
- 02/17/10 00:30:46 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Selection/3.3/LinearRankSelector.cs
r1530 r2817 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-20 08Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 20 20 #endregion 21 21 22 using System ;22 using System.Linq; 23 23 using System.Collections.Generic; 24 using System.Text;25 24 using HeuristicLab.Core; 26 25 using HeuristicLab.Data; 26 using HeuristicLab.Parameters; 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 28 28 29 namespace HeuristicLab.Selection { 29 30 /// <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. 31 32 /// </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; 36 40 } 37 41 38 /// <summary>39 /// Initializes a new instance of <see cref="LinearRankSelector"/> with the <c>CopySelected</c> flag40 /// 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(); 45 49 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(); 62 57 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; 69 63 while (currentLot < selectedLot) { 70 64 index++; 71 currentLot += subScopes - index;65 currentLot += list[index].lots; 72 66 } 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()); 76 69 else { 77 s ource.RemoveSubScope(selectedScope);78 target.AddSubScope(selectedScope);79 subScopes--;80 l otSum = (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); 81 74 } 82 75 } 76 return selected; 83 77 } 84 78 }
Note: See TracChangeset
for help on using the changeset viewer.