Changeset 2817
- Timestamp:
- 02/17/10 00:30:46 (15 years ago)
- 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 120 120 return; 121 121 122 if (!ViewCanShowContent(viewType, content) || !typeMenuItemTable.ContainsKey(viewType))122 if (!ViewCanShowContent(viewType, content)) 123 123 throw new ArgumentException(string.Format("View \"{0}\" cannot display content \"{1}\".", 124 124 viewType.GetPrettyName(), -
trunk/sources/HeuristicLab.Selection/3.3/HeuristicLab.Selection-3.3.csproj
r2805 r2817 82 82 <ItemGroup> 83 83 <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" /> 84 90 <Compile Include="RightSelector.cs" /> 85 91 <Compile Include="StochasticSelector.cs" /> -
trunk/sources/HeuristicLab.Selection/3.3/LeftReducer.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;23 22 using System.Collections.Generic; 24 using System.Text;25 23 using HeuristicLab.Core; 26 using HeuristicLab.Operators; 24 using HeuristicLab.Data; 25 using HeuristicLab.Parameters; 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 27 28 28 namespace HeuristicLab.Selection { 29 29 /// <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. 31 31 /// </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() { } 37 37 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; 49 42 } 50 43 } -
trunk/sources/HeuristicLab.Selection/3.3/LeftSelector.cs
r2805 r2817 36 36 public LeftSelector() : base() { } 37 37 38 protected override void Select(ScopeList source, ScopeList target) {38 protected override ScopeList Select(ScopeList scopes) { 39 39 int count = NumberOfSelectedSubScopesParameter.ActualValue.Value; 40 40 bool copy = CopySelectedParameter.Value.Value; 41 ScopeList selected = new ScopeList(); 41 42 42 43 int j = 0; 43 44 for (int i = 0; i < count; i++) { 44 45 if (copy) { 45 target.Add((IScope)source[j].Clone());46 selected.Add((IScope)scopes[j].Clone()); 46 47 j++; 47 if (j >= s ource.Count) j = 0;48 if (j >= scopes.Count) j = 0; 48 49 } else { 49 target.Add(source[0]);50 s ource.RemoveAt(0);50 selected.Add(scopes[0]); 51 scopes.RemoveAt(0); 51 52 } 52 53 } 54 return selected; 53 55 } 54 56 } -
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 } -
trunk/sources/HeuristicLab.Selection/3.3/MergingReducer.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;23 22 using System.Collections.Generic; 24 using System.Text;25 23 using HeuristicLab.Core; 26 using HeuristicLab.Operators; 24 using HeuristicLab.Data; 25 using HeuristicLab.Parameters; 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 27 28 28 namespace HeuristicLab.Selection { 29 29 /// <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. 31 31 /// </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() { } 37 37 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; 51 43 } 52 44 } -
trunk/sources/HeuristicLab.Selection/3.3/ProportionalSelector.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; 28 using System; 27 29 28 30 namespace HeuristicLab.Selection { 29 31 /// <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. 32 33 /// </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"]; } 37 40 } 38 41 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; } 51 45 } 52 46 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 } 67 52 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(); 73 61 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 } 92 79 } 93 80 } 94 }95 81 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; 134 90 } 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); 149 98 } 150 qualitySum += qualities[i];151 99 } 152 if (double.IsInfinity(qualitySum)) qualitySum = double.MaxValue;100 return selected; 153 101 } 154 102 } -
trunk/sources/HeuristicLab.Selection/3.3/RandomSelector.cs
r2805 r2817 36 36 public RandomSelector() : base() { } 37 37 38 protected override void Select(ScopeList source, ScopeList target) {38 protected override ScopeList Select(ScopeList scopes) { 39 39 int count = NumberOfSelectedSubScopesParameter.ActualValue.Value; 40 40 bool copy = CopySelectedParameter.Value.Value; 41 41 IRandom random = RandomParameter.ActualValue; 42 ScopeList selected = new ScopeList(); 42 43 43 44 for (int i = 0; i < count; i++) { 44 45 if (copy) 45 target.Add((IScope)source[random.Next(source.Count)].Clone());46 selected.Add((IScope)scopes[random.Next(scopes.Count)].Clone()); 46 47 else { 47 int index = random.Next(s ource.Count);48 target.Add(source[index]);49 s ource.RemoveAt(index);48 int index = random.Next(scopes.Count); 49 selected.Add(scopes[index]); 50 scopes.RemoveAt(index); 50 51 } 51 52 } 53 return selected; 52 54 } 53 55 } -
trunk/sources/HeuristicLab.Selection/3.3/RightReducer.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;23 22 using System.Collections.Generic; 24 using System.Text;25 23 using HeuristicLab.Core; 26 using HeuristicLab.Operators; 24 using HeuristicLab.Data; 25 using HeuristicLab.Parameters; 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 27 28 28 namespace HeuristicLab.Selection { 29 29 /// <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. 31 31 /// </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() { } 37 37 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; 49 42 } 50 43 } -
trunk/sources/HeuristicLab.Selection/3.3/RightSelector.cs
r2805 r2817 36 36 public RightSelector() : base() { } 37 37 38 protected override void Select(ScopeList source, ScopeList target) {38 protected override ScopeList Select(ScopeList scopes) { 39 39 int count = NumberOfSelectedSubScopesParameter.ActualValue.Value; 40 40 bool copy = CopySelectedParameter.Value.Value; 41 ScopeList selected = new ScopeList(); 41 42 42 int j = s ource.Count - 1;43 int j = scopes.Count - 1; 43 44 for (int i = 0; i < count; i++) { 44 45 if (copy) { 45 target.Add((IScope)source[j].Clone());46 selected.Add((IScope)scopes[j].Clone()); 46 47 j--; 47 if (j < 0) j = s ource.Count - 1;48 if (j < 0) j = scopes.Count - 1; 48 49 } else { 49 target.Add(source[source.Count - 1]);50 s ource.RemoveAt(source.Count - 1);50 selected.Add(scopes[scopes.Count - 1]); 51 scopes.RemoveAt(scopes.Count - 1); 51 52 } 52 53 } 54 return selected; 53 55 } 54 56 } -
trunk/sources/HeuristicLab.Selection/3.3/Selector.cs
r2805 r2817 60 60 61 61 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); 66 64 67 65 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); 77 72 78 73 return base.Apply(); 79 74 } 80 75 81 protected abstract void Select(ScopeList source, ScopeList target);76 protected abstract ScopeList Select(ScopeList scopes); 82 77 } 83 78 } -
trunk/sources/HeuristicLab.Selection/3.3/TournamentSelector.cs
r2805 r2817 40 40 public TournamentSelector() : base() { 41 41 Parameters.Add(new ValueLookupParameter<IntData>("GroupSize", "The size of the tournament group.", new IntData(2))); 42 CopySelected.Value = true; 42 43 } 43 44 44 protected override void Select(ScopeList source, ScopeList target) {45 protected override ScopeList Select(ScopeList scopes) { 45 46 int count = NumberOfSelectedSubScopesParameter.ActualValue.Value; 46 47 bool copy = CopySelectedParameter.Value.Value; … … 49 50 List<DoubleData> qualities = new List<DoubleData>(QualityParameter.ActualValue); 50 51 int groupSize = GroupSizeParameter.ActualValue.Value; 52 ScopeList selected = new ScopeList(); 51 53 52 54 for (int i = 0; i < count; i++) { 53 int best = random.Next(s ource.Count);55 int best = random.Next(scopes.Count); 54 56 int index; 55 57 for (int j = 1; j < groupSize; j++) { 56 index = random.Next(s ource.Count);58 index = random.Next(scopes.Count); 57 59 if (((maximization) && (qualities[index].Value > qualities[best].Value)) || 58 60 ((!maximization) && (qualities[index].Value < qualities[best].Value))) { … … 62 64 63 65 if (copy) 64 target.Add((IScope)source[best].Clone());66 selected.Add((IScope)scopes[best].Clone()); 65 67 else { 66 target.Add(source[best]);67 s ource.RemoveAt(best);68 selected.Add(scopes[best]); 69 scopes.RemoveAt(best); 68 70 qualities.RemoveAt(best); 69 71 } 70 72 } 73 return selected; 71 74 } 72 75 }
Note: See TracChangeset
for help on using the changeset viewer.