Changeset 14429 for branches/ScopedAlgorithms/HeuristicLab.Selection
- Timestamp:
- 11/29/16 15:46:48 (8 years ago)
- Location:
- branches/ScopedAlgorithms
- Files:
-
- 1 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/ScopedAlgorithms/HeuristicLab.Selection/3.3/TournamentSelector.cs
r12012 r14429 27 27 using HeuristicLab.Data; 28 28 using HeuristicLab.Optimization; 29 using HeuristicLab.Optimization.Selection; 29 30 using HeuristicLab.Parameters; 30 31 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 90 91 } 91 92 } 93 94 [Item("Tournament Selector", "", ExcludeGenericTypeInfo = true)] 95 [StorableClass] 96 public sealed class TournamentSelector<TContext, TProblem, TEncoding, TSolution> : ParameterizedNamedItem, ISelector<TContext> 97 where TContext : ISingleObjectivePopulationContext<TSolution>, IMatingpoolContext<TSolution>, IStochasticContext, 98 IProblemContext<TProblem, TEncoding, TSolution> 99 where TProblem : class, ISingleObjectiveProblem<TEncoding, TSolution>, ISingleObjectiveProblemDefinition<TEncoding, TSolution> 100 where TEncoding : class, IEncoding<TSolution> 101 where TSolution : class, ISolution { 102 103 [Storable] 104 private IValueParameter<IntValue> groupSizeParameter; 105 public int GroupSize { 106 get { return groupSizeParameter.Value.Value; } 107 set { 108 if (value < 1) throw new ArgumentException("Cannot use a group size less than 1 in tournament selection."); 109 groupSizeParameter.Value.Value = value; 110 } 111 } 112 113 [StorableConstructor] 114 private TournamentSelector(bool deserializing) : base(deserializing) { } 115 private TournamentSelector(TournamentSelector<TContext, TProblem, TEncoding, TSolution> original, Cloner cloner) 116 : base(original, cloner) { 117 groupSizeParameter = cloner.Clone(groupSizeParameter); 118 } 119 public TournamentSelector() { 120 Parameters.Add(groupSizeParameter = new ValueParameter<IntValue>("GroupSize", "The group size that competes in the tournament.", new IntValue(2))); 121 } 122 123 public override IDeepCloneable Clone(Cloner cloner) { 124 return new TournamentSelector<TContext, TProblem, TEncoding, TSolution>(this, cloner); 125 } 126 127 public void Select(TContext context, int n, bool withRepetition) { 128 context.MatingPool = Select(context.Random, context.Problem.IsBetter, context.Population, GroupSize, n, withRepetition); 129 } 130 131 public static IEnumerable<ISingleObjectiveSolutionScope<TSolution>> Select(IRandom random, Func<double, double, bool> isBetterFunc, IEnumerable<ISingleObjectiveSolutionScope<TSolution>> population, int groupSize, int n, bool withRepetition) { 132 var pop = population.Where(x => !double.IsNaN(x.Fitness)).ToList(); 133 134 var i = n; 135 while (i > 0 && pop.Count > 0) { 136 var best = random.Next(pop.Count); 137 for (var j = 1; j < groupSize; j++) { 138 var index = random.Next(pop.Count); 139 if (isBetterFunc(pop[index].Fitness, pop[best].Fitness)) { 140 best = index; 141 } 142 } 143 144 yield return pop[best]; 145 i--; 146 if (!withRepetition) { 147 pop.RemoveAt(i); 148 } 149 } 150 } 151 } 92 152 }
Note: See TracChangeset
for help on using the changeset viewer.