Free cookie consent management tool by TermsFeed Policy Generator

Changeset 2817


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

Operator architecture refactoring (#95)

  • worked on selection
Location:
trunk/sources
Files:
1 added
2 deleted
12 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Core.Views/3.3/ViewHost.cs

    r2809 r2817  
    120120        return;
    121121
    122       if (!ViewCanShowContent(viewType, content) || !typeMenuItemTable.ContainsKey(viewType))
     122      if (!ViewCanShowContent(viewType, content))
    123123        throw new ArgumentException(string.Format("View \"{0}\" cannot display content \"{1}\".",
    124124                                                  viewType.GetPrettyName(),
  • trunk/sources/HeuristicLab.Selection/3.3/HeuristicLab.Selection-3.3.csproj

    r2805 r2817  
    8282  <ItemGroup>
    8383    <None Include="HeuristicLabSelectionPlugin.cs.frame" />
     84    <Compile Include="LeftReducer.cs" />
     85    <Compile Include="MergingReducer.cs" />
     86    <Compile Include="Reducer.cs" />
     87    <Compile Include="LinearRankSelector.cs" />
     88    <Compile Include="ProportionalSelector.cs" />
     89    <Compile Include="RightReducer.cs" />
    8490    <Compile Include="RightSelector.cs" />
    8591    <Compile Include="StochasticSelector.cs" />
  • trunk/sources/HeuristicLab.Selection/3.3/LeftReducer.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;
    2322using System.Collections.Generic;
    24 using System.Text;
    2523using HeuristicLab.Core;
    26 using HeuristicLab.Operators;
     24using HeuristicLab.Data;
     25using HeuristicLab.Parameters;
     26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2727
    2828namespace HeuristicLab.Selection {
    2929  /// <summary>
    30   /// Takes only sub scopes from the left child of the tree.
     30  /// An operator which reduces to the sub-scopes of the leftmost sub-scope of the current scope.
    3131  /// </summary>
    32   public class LeftReducer : ReducerBase {
    33     /// <inheritdoc select="summary"/>
    34     public override string Description {
    35       get { return @"TODO\r\nOperator description still missing ..."; }
    36     }
     32  [Item("LeftReducer", "An operator which reduces to the sub-scopes of the leftmost sub-scope of the current scope.")]
     33  [EmptyStorableClass]
     34  [Creatable("Test")]
     35  public sealed class LeftReducer : Reducer {
     36    public LeftReducer() : base() { }
    3737
    38     /// <summary>
    39     /// Takes only the sub scopes from the left sub scope of the tree.
    40     /// </summary>
    41     /// <param name="scope">The current scope.</param>
    42     /// <returns>All sub scopes from the left part of the tree.</returns>
    43     protected override ICollection<IScope> Reduce(IScope scope) {
    44       List<IScope> subScopes = new List<IScope>();
    45 
    46       if (scope.SubScopes.Count > 0)
    47         subScopes.AddRange(scope.SubScopes[0].SubScopes);
    48       return subScopes;
     38    protected override ScopeList Reduce(ScopeList scopes) {
     39      ScopeList reduced = new ScopeList();
     40      if (scopes.Count > 0) reduced.AddRange(scopes[0].SubScopes);
     41      return reduced;
    4942    }
    5043  }
  • trunk/sources/HeuristicLab.Selection/3.3/LeftSelector.cs

    r2805 r2817  
    3636    public LeftSelector() : base() { }
    3737
    38     protected override void Select(ScopeList source, ScopeList target) {
     38    protected override ScopeList Select(ScopeList scopes) {
    3939      int count = NumberOfSelectedSubScopesParameter.ActualValue.Value;
    4040      bool copy = CopySelectedParameter.Value.Value;
     41      ScopeList selected = new ScopeList();
    4142
    4243      int j = 0;
    4344      for (int i = 0; i < count; i++) {
    4445        if (copy) {
    45           target.Add((IScope)source[j].Clone());
     46          selected.Add((IScope)scopes[j].Clone());
    4647          j++;
    47           if (j >= source.Count) j = 0;
     48          if (j >= scopes.Count) j = 0;
    4849        } else {
    49           target.Add(source[0]);
    50           source.RemoveAt(0);
     50          selected.Add(scopes[0]);
     51          scopes.RemoveAt(0);
    5152        }
    5253      }
     54      return selected;
    5355    }
    5456  }
  • 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  }
  • trunk/sources/HeuristicLab.Selection/3.3/MergingReducer.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;
    2322using System.Collections.Generic;
    24 using System.Text;
    2523using HeuristicLab.Core;
    26 using HeuristicLab.Operators;
     24using HeuristicLab.Data;
     25using HeuristicLab.Parameters;
     26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2727
    2828namespace HeuristicLab.Selection {
    2929  /// <summary>
    30   /// Merges all sub scopes of the children to one list.
     30  /// An operator which reduces to the sub-scopes of all sub-scopes of the current scope.
    3131  /// </summary>
    32   public class MergingReducer : ReducerBase {
    33     /// <inheritdoc select="summary"/>
    34     public override string Description {
    35       get { return @"TODO\r\nOperator description still missing ..."; }
    36     }
     32  [Item("MergingReducer", "An operator which reduces to the sub-scopes of all sub-scopes of the current scope.")]
     33  [EmptyStorableClass]
     34  [Creatable("Test")]
     35  public sealed class MergingReducer : Reducer {
     36    public MergingReducer() : base() { }
    3737
    38     /// <summary>
    39     /// Merges all sub scopes of the sub scopes of the current <paramref name="scope"/>.
    40     /// </summary>
    41     /// <param name="scope">The current scope whose sub scopes to merge.</param>
    42     /// <returns>A list of all merged subscopes of the given <paramref name="scope"/>.</returns>
    43     protected override ICollection<IScope> Reduce(IScope scope) {
    44       List<IScope> subScopes = new List<IScope>();
    45 
    46       for (int i = 0; i < scope.SubScopes.Count; i++) {
    47         for (int j = 0; j < scope.SubScopes[i].SubScopes.Count; j++)
    48           subScopes.Add(scope.SubScopes[i].SubScopes[j]);
    49       }
    50       return subScopes;
     38    protected override ScopeList Reduce(ScopeList scopes) {
     39      ScopeList reduced = new ScopeList();
     40      for (int i = 0; i < scopes.Count; i++)
     41        reduced.AddRange(scopes[i].SubScopes);
     42      return reduced;
    5143    }
    5244  }
  • trunk/sources/HeuristicLab.Selection/3.3/ProportionalSelector.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;
     28using System;
    2729
    2830namespace HeuristicLab.Selection {
    2931  /// <summary>
    30   /// Copies or moves a number of sub scopes from a source scope to a target scope, their probability
    31   /// to be selected depending on their quality.
     32  /// A quality proportional selection operator which considers a single double quality value for selection.
    3233  /// </summary>
    33   public class ProportionalSelector : StochasticSelectorBase {
    34     /// <inheritdoc select="summary"/>
    35     public override string Description {
    36       get { return @"TODO\r\nOperator description still missing ..."; }
     34  [Item("ProportionalSelector", "A quality proportional selection operator which considers a single double quality value for selection.")]
     35  [EmptyStorableClass]
     36  [Creatable("Test")]
     37  public sealed class ProportionalSelector : StochasticSingleObjectiveSelector {
     38    private ValueParameter<BoolData> WindowingParameter {
     39      get { return (ValueParameter<BoolData>)Parameters["Windowing"]; }
    3740    }
    3841
    39     /// <summary>
    40     /// Initializes a new instance of <see cref="ProportionalSelector"/> with three variable infos
    41     /// (<c>Maximization</c>, <c>Quality</c> and <c>Windowing</c>, being a local variable and initialized
    42     /// with <c>true</c>) and the <c>CopySelected</c> flag set to <c>true</c>.
    43     /// </summary>
    44     public ProportionalSelector() {
    45       AddVariableInfo(new VariableInfo("Maximization", "Maximization problem", typeof(BoolData), VariableKind.In));
    46       AddVariableInfo(new VariableInfo("Quality", "Quality value", typeof(DoubleData), VariableKind.In));
    47       AddVariableInfo(new VariableInfo("Windowing", "Apply windowing strategy (selection probability is proportional to the quality differences and not to the total quality)", typeof(BoolData), VariableKind.In));
    48       GetVariableInfo("Windowing").Local = true;
    49       AddVariable(new Variable("Windowing", new BoolData(true)));
    50       GetVariable("CopySelected").GetValue<BoolData>().Data = true;
     42    public BoolData Windowing {
     43      get { return WindowingParameter.Value; }
     44      set { WindowingParameter.Value = value; }
    5145    }
    5246
    53     /// <summary>
    54     /// Copies or movies a number of sub scopes (<paramref name="selected"/>) in the given
    55     /// <paramref name="source"/> to the given <paramref name="target"/>, selection takes place with respect
    56     /// to the quality of the scope.
    57     /// </summary>
    58     /// <param name="random">The random number generator.</param>
    59     /// <param name="source">The source scope from where to copy/move the sub scopes.</param>
    60     /// <param name="selected">The number of sub scopes to copy/move.</param>
    61     /// <param name="target">The target scope where to add the sub scopes.</param>
    62     /// <param name="copySelected">Boolean flag whether the sub scopes shall be moved or copied.</param>
    63     protected override void Select(IRandom random, IScope source, int selected, IScope target, bool copySelected) {
    64       bool maximization = GetVariableValue<BoolData>("Maximization", source, true).Data;
    65       IVariableInfo qualityInfo = GetVariableInfo("Quality");
    66       bool windowing = GetVariableValue<BoolData>("Windowing", source, true).Data;
     47    public ProportionalSelector()
     48      : base() {
     49      Parameters.Add(new ValueParameter<BoolData>("Windowing", "Apply windowing strategy (selection probability is proportional to the quality differences and not to the total quality).", new BoolData(true)));
     50      CopySelected.Value = true;
     51    }
    6752
    68       double[] qualities;
    69       double qualitySum;
    70       double selectedQuality;
    71       double sum;
    72       int j;
     53    protected override ScopeList Select(ScopeList scopes) {
     54      int count = NumberOfSelectedSubScopesParameter.ActualValue.Value;
     55      bool copy = CopySelectedParameter.Value.Value;
     56      IRandom random = RandomParameter.ActualValue;
     57      bool maximization = MaximizationParameter.ActualValue.Value;
     58      List<DoubleData> qualities = new List<DoubleData>(QualityParameter.ActualValue);
     59      bool windowing = WindowingParameter.Value.Value;
     60      ScopeList selected = new ScopeList();
    7361
    74       GenerateQualitiesArray(source, maximization, qualityInfo, windowing, out qualities, out qualitySum);
    75 
    76       // perform selection
    77       for (int i = 0; i < selected; i++) {
    78         selectedQuality = random.NextDouble() * qualitySum;
    79         sum = 0;
    80         j = 0;
    81         while ((j < qualities.Length) && (sum <= selectedQuality)) {
    82           sum += qualities[j];
    83           j++;
    84         }
    85         IScope selectedScope = source.SubScopes[j - 1];
    86         if (copySelected)
    87           target.AddSubScope((IScope)selectedScope.Clone());
    88         else {
    89           source.RemoveSubScope(selectedScope);
    90           target.AddSubScope(selectedScope);
    91           GenerateQualitiesArray(source, maximization, qualityInfo, windowing, out qualities, out qualitySum);
     62      // prepare qualities for proportional selection
     63      double minQuality = qualities.Min(x => x.Value);
     64      double maxQuality = qualities.Max(x => x.Value);
     65      if (minQuality == maxQuality) {  // all quality values are equal
     66        qualities.ForEach(x => x.Value = 1);
     67      } else {
     68        if (windowing) {
     69          if (maximization)
     70            qualities.ForEach(x => x.Value = x.Value - minQuality);
     71          else
     72            qualities.ForEach(x => x.Value = maxQuality - x.Value);
     73        } else {
     74          if (minQuality < 0.0) throw new InvalidOperationException("Proportional selection without windowing does not work with quality values < 0.");
     75          if (!maximization) {
     76            double limit = Math.Min(maxQuality * 2, double.MaxValue);
     77            qualities.ForEach(x => x.Value = limit - x.Value);
     78          }
    9279        }
    9380      }
    94     }
    9581
    96     /// <summary>
    97     /// Calculates the qualities of the sub scopes of the given <paramref name="source"/>.
    98     /// </summary>
    99     /// <exception cref="InvalidOperationException">Thrown when the sub scopes are not sorted according
    100     /// to their solution qualities or if the quality value is beyond zero and the <c>windowing</c>
    101     /// flag is set to <c>false</c>.</exception>
    102     /// <param name="source">The scource scope where to calculate the qualities.</param>
    103     /// <param name="maximization">Boolean flag whether is a maximization problem.</param>
    104     /// <param name="qualityInfo">The quality variable info.</param>
    105     /// <param name="windowing">Boolean flag whether the windowing strategy shall be applied.</param>
    106     /// <param name="qualities">Output parameter; contains all qualities of the sub scopes.</param>
    107     /// <param name="qualitySum">Output parameter; the sum of all qualities.</param>
    108     private void GenerateQualitiesArray(IScope source, bool maximization, IVariableInfo qualityInfo, bool windowing, out double[] qualities, out double qualitySum) {
    109       int subScopes = source.SubScopes.Count;
    110       qualities = new double[subScopes];
    111       qualitySum = 0;
    112 
    113       if (subScopes < 1) throw new InvalidOperationException("No source scopes to select available.");
    114 
    115       double best = source.SubScopes[0].GetVariableValue<DoubleData>(qualityInfo.FormalName, false).Data;
    116       double worst = source.SubScopes[subScopes - 1].GetVariableValue<DoubleData>(qualityInfo.FormalName, false).Data;
    117       double limit = Math.Min(worst * 2, double.MaxValue);
    118       double min = Math.Min(best, worst);
    119       double max = Math.Max(best, worst);
    120       double solutionQuality;
    121 
    122       // preprocess fitness values, apply windowing if desired
    123       for (int i = 0; i < qualities.Length; i++) {
    124         solutionQuality = source.SubScopes[i].GetVariableValue<DoubleData>(qualityInfo.FormalName, false).Data;
    125         if (solutionQuality < min || solutionQuality > max) {
    126           // something has obviously gone wrong here
    127           string errorMessage = "There is a problem with the ordering of the source sub-scopes in " + Name + ".\r\n" +
    128             "The quality of solution number " + i.ToString() + " is ";
    129           if (solutionQuality < min) errorMessage += "below";
    130           else errorMessage += "greater than";
    131           errorMessage += " the calculated qualities range:\r\n";
    132           errorMessage += solutionQuality.ToString() + " is outside the interval [ " + min.ToString() + " ; " + max.ToString() + " ].";
    133           throw new InvalidOperationException(errorMessage);
     82      double qualitySum = qualities.Sum(x => x.Value);
     83      for (int i = 0; i < count; i++) {
     84        double selectedQuality = random.NextDouble() * qualitySum;
     85        int index = 0;
     86        double currentQuality = qualities[index].Value;
     87        while (currentQuality < selectedQuality) {
     88          index++;
     89          currentQuality += qualities[index].Value;
    13490        }
    135         if (best != worst) {  // prevent division by zero
    136           if (windowing) {
    137             if (!maximization) {
    138               qualities[i] = 1 - ((solutionQuality - best) / (worst - best));
    139             } else {
    140               qualities[i] = (solutionQuality - worst) / (best - worst);
    141             }
    142           } else {
    143             if (solutionQuality < 0.0) throw new InvalidOperationException("ERROR in ProportionalSelector: Non-windowing is not working with quality values < 0. Use windowing.");
    144             if (!maximization) qualities[i] = limit - solutionQuality;
    145             else qualities[i] = solutionQuality;
    146           }
    147         } else {  // best == worst -> all fitness values are equal
    148           qualities[i] = 1;
     91        if (copy)
     92          selected.Add((IScope)scopes[index].Clone());
     93        else {
     94          selected.Add(scopes[index]);
     95          scopes.RemoveAt(index);
     96          qualitySum -= qualities[index].Value;
     97          qualities.RemoveAt(index);
    14998        }
    150         qualitySum += qualities[i];
    15199      }
    152       if (double.IsInfinity(qualitySum)) qualitySum = double.MaxValue;
     100      return selected;
    153101    }
    154102  }
  • trunk/sources/HeuristicLab.Selection/3.3/RandomSelector.cs

    r2805 r2817  
    3636    public RandomSelector() : base() { }
    3737
    38     protected override void Select(ScopeList source, ScopeList target) {
     38    protected override ScopeList Select(ScopeList scopes) {
    3939      int count = NumberOfSelectedSubScopesParameter.ActualValue.Value;
    4040      bool copy = CopySelectedParameter.Value.Value;
    4141      IRandom random = RandomParameter.ActualValue;
     42      ScopeList selected = new ScopeList();
    4243
    4344      for (int i = 0; i < count; i++) {
    4445        if (copy)
    45           target.Add((IScope)source[random.Next(source.Count)].Clone());
     46          selected.Add((IScope)scopes[random.Next(scopes.Count)].Clone());
    4647        else {
    47           int index = random.Next(source.Count);
    48           target.Add(source[index]);
    49           source.RemoveAt(index);
     48          int index = random.Next(scopes.Count);
     49          selected.Add(scopes[index]);
     50          scopes.RemoveAt(index);
    5051        }
    5152      }
     53      return selected;
    5254    }
    5355  }
  • trunk/sources/HeuristicLab.Selection/3.3/RightReducer.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;
    2322using System.Collections.Generic;
    24 using System.Text;
    2523using HeuristicLab.Core;
    26 using HeuristicLab.Operators;
     24using HeuristicLab.Data;
     25using HeuristicLab.Parameters;
     26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2727
    2828namespace HeuristicLab.Selection {
    2929  /// <summary>
    30   /// Takes only sub scopes from the right child of the tree.
     30  /// An operator which reduces to the sub-scopes of the rightmost sub-scope of the current scope.
    3131  /// </summary>
    32   public class RightReducer : ReducerBase {
    33     /// <inheritdoc select="summary"/>
    34     public override string Description {
    35       get { return @"TODO\r\nOperator description still missing ..."; }
    36     }
     32  [Item("RightReducer", "An operator which reduces to the sub-scopes of the rightmost sub-scope of the current scope.")]
     33  [EmptyStorableClass]
     34  [Creatable("Test")]
     35  public sealed class RightReducer : Reducer {
     36    public RightReducer() : base() { }
    3737
    38     /// <summary>
    39     /// Takes only the sub scopes from the right part of the tree.
    40     /// </summary>
    41     /// <param name="scope">The current scope.</param>
    42     /// <returns>All sub scopes from the right sub scope of the tree.</returns>
    43     protected override ICollection<IScope> Reduce(IScope scope) {
    44       List<IScope> subScopes = new List<IScope>();
    45 
    46       if (scope.SubScopes.Count > 0)
    47         subScopes.AddRange(scope.SubScopes[scope.SubScopes.Count - 1].SubScopes);
    48       return subScopes;
     38    protected override ScopeList Reduce(ScopeList scopes) {
     39      ScopeList reduced = new ScopeList();
     40      if (scopes.Count > 0) reduced.AddRange(scopes[scopes.Count - 1].SubScopes);
     41      return reduced;
    4942    }
    5043  }
  • trunk/sources/HeuristicLab.Selection/3.3/RightSelector.cs

    r2805 r2817  
    3636    public RightSelector() : base() { }
    3737
    38     protected override void Select(ScopeList source, ScopeList target) {
     38    protected override ScopeList Select(ScopeList scopes) {
    3939      int count = NumberOfSelectedSubScopesParameter.ActualValue.Value;
    4040      bool copy = CopySelectedParameter.Value.Value;
     41      ScopeList selected = new ScopeList();
    4142
    42       int j = source.Count - 1;
     43      int j = scopes.Count - 1;
    4344      for (int i = 0; i < count; i++) {
    4445        if (copy) {
    45           target.Add((IScope)source[j].Clone());
     46          selected.Add((IScope)scopes[j].Clone());
    4647          j--;
    47           if (j < 0) j = source.Count - 1;
     48          if (j < 0) j = scopes.Count - 1;
    4849        } else {
    49           target.Add(source[source.Count - 1]);
    50           source.RemoveAt(source.Count - 1);
     50          selected.Add(scopes[scopes.Count - 1]);
     51          scopes.RemoveAt(scopes.Count - 1);
    5152        }
    5253      }
     54      return selected;
    5355    }
    5456  }
  • trunk/sources/HeuristicLab.Selection/3.3/Selector.cs

    r2805 r2817  
    6060
    6161    public sealed override IExecutionSequence Apply() {
    62       ScopeList source = new ScopeList(CurrentScope.SubScopes);
    63       ScopeList target = new ScopeList();
    64 
    65       Select(source, target);
     62      ScopeList scopes = new ScopeList(CurrentScope.SubScopes);
     63      ScopeList selected = Select(scopes);
    6664
    6765      CurrentScope.SubScopes.Clear();
    68       IScope remaining = new Scope("Remaining");
    69       CurrentScope.SubScopes.Add(remaining);
    70       IScope selected = new Scope("Selected");
    71       CurrentScope.SubScopes.Add(selected);
    72 
    73       for (int i = 0; i < source.Count; i++)
    74         remaining.SubScopes.Add(source[i]);
    75       for (int i = 0; i < target.Count; i++)
    76         selected.SubScopes.Add(target[i]);
     66      IScope remainingScope = new Scope("Remaining");
     67      remainingScope.SubScopes.AddRange(scopes);
     68      CurrentScope.SubScopes.Add(remainingScope);
     69      IScope selectedScope = new Scope("Selected");
     70      selectedScope.SubScopes.AddRange(selected);
     71      CurrentScope.SubScopes.Add(selectedScope);
    7772
    7873      return base.Apply();
    7974    }
    8075
    81     protected abstract void Select(ScopeList source, ScopeList target);
     76    protected abstract ScopeList Select(ScopeList scopes);
    8277  }
    8378}
  • trunk/sources/HeuristicLab.Selection/3.3/TournamentSelector.cs

    r2805 r2817  
    4040    public TournamentSelector() : base() {
    4141      Parameters.Add(new ValueLookupParameter<IntData>("GroupSize", "The size of the tournament group.", new IntData(2)));
     42      CopySelected.Value = true;
    4243    }
    4344
    44     protected override void Select(ScopeList source, ScopeList target) {
     45    protected override ScopeList Select(ScopeList scopes) {
    4546      int count = NumberOfSelectedSubScopesParameter.ActualValue.Value;
    4647      bool copy = CopySelectedParameter.Value.Value;
     
    4950      List<DoubleData> qualities = new List<DoubleData>(QualityParameter.ActualValue);
    5051      int groupSize = GroupSizeParameter.ActualValue.Value;
     52      ScopeList selected = new ScopeList();
    5153
    5254      for (int i = 0; i < count; i++) {
    53         int best = random.Next(source.Count);
     55        int best = random.Next(scopes.Count);
    5456        int index;
    5557        for (int j = 1; j < groupSize; j++) {
    56           index = random.Next(source.Count);
     58          index = random.Next(scopes.Count);
    5759          if (((maximization) && (qualities[index].Value > qualities[best].Value)) ||
    5860              ((!maximization) && (qualities[index].Value < qualities[best].Value))) {
     
    6264
    6365        if (copy)
    64           target.Add((IScope)source[best].Clone());
     66          selected.Add((IScope)scopes[best].Clone());
    6567        else {
    66           target.Add(source[best]);
    67           source.RemoveAt(best);
     68          selected.Add(scopes[best]);
     69          scopes.RemoveAt(best);
    6870          qualities.RemoveAt(best);
    6971        }
    7072      }
     73      return selected;
    7174    }
    7275  }
Note: See TracChangeset for help on using the changeset viewer.