Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2780_SAPBA/HeuristicLab.Algorithms.SAPBA/Strategies/GenerationalStrategy.cs @ 16386

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

#2780 worked on SAPBA; added IndividualStrategy

File size: 2.0 KB
Line 
1using HeuristicLab.Common;
2using HeuristicLab.Core;
3using HeuristicLab.Data;
4using HeuristicLab.Encodings.RealVectorEncoding;
5using HeuristicLab.Optimization;
6using HeuristicLab.Parameters;
7using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
8
9namespace HeuristicLab.Algorithms.SAPBA.Strategies {
10  public class GenerationalStrategy : StrategyBase {
11    public const string NoGenerationsParameterName = "Number of generations";
12    public IFixedValueParameter<IntValue> NoGenerationsParameter => Parameters[NoGenerationsParameterName] as IFixedValueParameter<IntValue>;
13    public IntValue NoGenerations => NoGenerationsParameter.Value;
14    [Storable]
15    public int Generations;
16
17    #region Constructors
18    private GenerationalStrategy(GenerationalStrategy original, Cloner cloner) : base(original, cloner) {
19      Generations = original.Generations;
20    }
21    public GenerationalStrategy() {
22      Parameters.Add(new FixedValueParameter<IntValue>(NoGenerationsParameterName, "The number of generations before a new model is constructed", new IntValue(3)));
23    }
24    public override IDeepCloneable Clone(Cloner cloner) {
25      return new GenerationalStrategy(this, cloner);
26    }
27    #endregion
28
29    public override double Evaluate(RealVector r, IRandom random) {
30      return EstimateSample(r, random).Item2;
31    }
32
33    protected override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, ResultCollection globalResults, IRandom random) {
34    }
35
36    protected override void ProcessPopulation(Individual[] individuals, double[] qualities, IRandom random) {
37      if (RegressionSolution != null && Generations < NoGenerations.Value) Generations++;
38      else {
39        foreach (var indi in individuals) EvaluateSample(indi.RealVector(), random);
40        BuildRegressionSolution(random);
41        Generations = 0;
42      }
43    }
44
45    protected override void Initialize() {
46      Generations = 0;
47    }
48  }
49}
Note: See TracBrowser for help on using the repository browser.