Free cookie consent management tool by TermsFeed Policy Generator

source: branches/SAPBA/HeuristicLab.Algorithms.SAPBA/Strategies/IndividualStrategy.cs @ 16099

Last change on this file since 16099 was 14894, checked in by bwerth, 8 years ago

#2780 worked on SAPBA; added IndividualStrategy

File size: 1.9 KB
Line 
1using System;
2using System.Linq;
3using HeuristicLab.Common;
4using HeuristicLab.Core;
5using HeuristicLab.Data;
6using HeuristicLab.Encodings.RealVectorEncoding;
7using HeuristicLab.Optimization;
8using HeuristicLab.Parameters;
9
10namespace HeuristicLab.Algorithms.SAPBA.Strategies {
11  public class IndividualStrategy : StrategyBase {
12    public const string SampledIndividualsParameterName = "SampledIndividals";
13    public IFixedValueParameter<IntValue> SampledIndividualsParameter => Parameters[SampledIndividualsParameterName] as IFixedValueParameter<IntValue>;
14    public IntValue SampledIndividuals => SampledIndividualsParameter.Value;
15
16    #region Constructors
17    private IndividualStrategy(IndividualStrategy original, Cloner cloner) : base(original, cloner) {
18    }
19    public IndividualStrategy() {
20      Parameters.Add(new FixedValueParameter<IntValue>(SampledIndividualsParameterName, "The number of individuals that are sampled each generation", new IntValue(3)));
21    }
22    public override IDeepCloneable Clone(Cloner cloner) {
23      return new IndividualStrategy(this, cloner);
24    }
25    #endregion
26
27    public override double Evaluate(RealVector r, IRandom random) {
28      return EstimateSample(r, random).Item2;
29    }
30
31    protected override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, ResultCollection globalResults, IRandom random) {
32    }
33
34    protected override void ProcessPopulation(Individual[] individuals, double[] qualities, IRandom random) {
35      var samples = individuals.Zip(qualities, (individual, d) => new Tuple<Individual, double>(individual, d)).OrderBy(t => Algorithm.Problem.Maximization ? -t.Item2 : t.Item2).Take(SampledIndividuals.Value).Select(t => t.Item1.RealVector());
36      foreach (var indi in samples) EvaluateSample(indi, random);
37      BuildRegressionSolution(random);
38    }
39
40    protected override void Initialize() {
41    }
42  }
43}
Note: See TracBrowser for help on using the repository browser.