Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/16/10 06:50:14 (14 years ago)
Author:
swagner
Message:

Operator architecture refactoring (#95)

  • worked on parameters, TSP and selection
File:
1 edited

Legend:

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

    r1530 r2805  
    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;
    2322using System.Collections.Generic;
    24 using System.Text;
    2523using HeuristicLab.Core;
    2624using HeuristicLab.Data;
     25using HeuristicLab.Parameters;
     26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2727
    2828namespace HeuristicLab.Selection {
    2929  /// <summary>
    30   /// Moves or copies a defined number of the best sub scopes from a source scope to a target scope.
     30  /// A tournament selection operator which considers a single double quality value for selection.
    3131  /// </summary>
    32   public class TournamentSelector : StochasticSelectorBase {
    33     /// <inheritdoc select="summary"/>
    34     public override string Description {
    35       get { return @"TODO\r\nOperator description still missing ..."; }
     32  [Item("TournamentSelector", "A tournament selection operator which considers a single double quality value for selection.")]
     33  [EmptyStorableClass]
     34  [Creatable("Test")]
     35  public sealed class TournamentSelector : StochasticSingleObjectiveSelector {
     36    public ValueLookupParameter<IntData> GroupSizeParameter {
     37      get { return (ValueLookupParameter<IntData>)Parameters["GroupSize"]; }
    3638    }
    3739
    38     /// <summary>
    39     /// Initializes a new instance of <see cref="TournamentSelector"/> with three variable infos
    40     /// (<c>Maximization</c>, <c>Quality</c> and <c>GroupSize</c>, being a local variable and set to
    41     /// <c>2</c>) with <c>CopySelected</c> set to <c>true</c>.
    42     /// </summary>
    43     public TournamentSelector() {
    44       AddVariableInfo(new VariableInfo("Maximization", "Maximization problem", typeof(BoolData), VariableKind.In));
    45       AddVariableInfo(new VariableInfo("Quality", "Quality value", typeof(DoubleData), VariableKind.In));
    46       AddVariableInfo(new VariableInfo("GroupSize", "Size of the tournament group", typeof(IntData), VariableKind.In));
    47       GetVariableInfo("GroupSize").Local = true;
    48       AddVariable(new Variable("GroupSize", new IntData(2)));
    49       GetVariable("CopySelected").GetValue<BoolData>().Data = true;
     40    public TournamentSelector() : base() {
     41      Parameters.Add(new ValueLookupParameter<IntData>("GroupSize", "The size of the tournament group.", new IntData(2)));
    5042    }
    5143
    52     /// <summary>
    53     /// Copies or moves the best sub scopes from the given <paramref name="source"/> to the specified
    54     /// <paramref name="target"/>.
    55     /// </summary>
    56     /// <exception cref="InvalidOperationException">Thrown when no source sub scopes are available.</exception>
    57     /// <param name="random">The random number generator.</param>
    58     /// <param name="source">The source scope from where to copy/move the sub scopes.</param>
    59     /// <param name="selected">The number of sub scopes to copy/move.</param>
    60     /// <param name="target">The target scope where to add the sub scopes.</param>
    61     /// <param name="copySelected">Boolean flag whether the sub scopes shall be moved or copied.</param>
    62     protected override void Select(IRandom random, IScope source, int selected, IScope target, bool copySelected) {
    63       IVariableInfo qualityInfo = GetVariableInfo("Quality");
    64       bool maximization = GetVariableValue<BoolData>("Maximization", source, true).Data;
    65       int groupSize = GetVariableValue<IntData>("GroupSize", source, true).Data;
    66       for (int i = 0; i < selected; i++) {
    67         if (source.SubScopes.Count < 1) throw new InvalidOperationException("No source scopes available to select.");
     44    protected override void Select(ScopeList source, ScopeList target) {
     45      int count = NumberOfSelectedSubScopesParameter.ActualValue.Value;
     46      bool copy = CopySelectedParameter.Value.Value;
     47      IRandom random = RandomParameter.ActualValue;
     48      bool maximization = MaximizationParameter.ActualValue.Value;
     49      List<DoubleData> qualities = new List<DoubleData>(QualityParameter.ActualValue);
     50      int groupSize = GroupSizeParameter.ActualValue.Value;
    6851
    69         IScope selectedScope = source.SubScopes[random.Next(source.SubScopes.Count)];
    70         double best = selectedScope.GetVariableValue<DoubleData>(qualityInfo.FormalName, false).Data;
     52      for (int i = 0; i < count; i++) {
     53        int best = random.Next(source.Count);
     54        int index;
    7155        for (int j = 1; j < groupSize; j++) {
    72           IScope scope = source.SubScopes[random.Next(source.SubScopes.Count)];
    73           double quality = scope.GetVariableValue<DoubleData>(qualityInfo.FormalName, false).Data;
    74           if (((maximization) && (quality > best)) ||
    75               ((!maximization) && (quality < best))) {
    76             best = quality;
    77             selectedScope = scope;
     56          index = random.Next(source.Count);
     57          if (((maximization) && (qualities[index].Value > qualities[best].Value)) ||
     58              ((!maximization) && (qualities[index].Value < qualities[best].Value))) {
     59            best = index;
    7860          }
    7961        }
    8062
    81         if (copySelected)
    82           target.AddSubScope((IScope)selectedScope.Clone());
     63        if (copy)
     64          target.Add((IScope)source[best].Clone());
    8365        else {
    84           source.RemoveSubScope(selectedScope);
    85           target.AddSubScope(selectedScope);
     66          target.Add(source[best]);
     67          source.RemoveAt(best);
     68          qualities.RemoveAt(best);
    8669        }
    8770      }
Note: See TracChangeset for help on using the changeset viewer.