Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/29/16 15:46:48 (8 years ago)
Author:
abeham
Message:

#2701, #2708: Made a new branch from ProblemRefactoring and removed ScopedBasicAlgorithm branch (which becomes MemPR branch)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/ProblemRefactoring/HeuristicLab.Selection/3.3/TournamentSelector.cs

    r12012 r14429  
    2727using HeuristicLab.Data;
    2828using HeuristicLab.Optimization;
     29using HeuristicLab.Optimization.Selection;
    2930using HeuristicLab.Parameters;
    3031using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    9091    }
    9192  }
     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  }
    92152}
Note: See TracChangeset for help on using the changeset viewer.