Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2780_SAPBA/HeuristicLab.Algorithms.SAPBA/Strategies/InfillStrategy.cs @ 17847

Last change on this file since 17847 was 16108, checked in by bwerth, 6 years ago

#2780 renamed branch to include ticket number

File size: 5.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Linq;
24using HeuristicLab.Algorithms.EGO;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.RealVectorEncoding;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33namespace HeuristicLab.Algorithms.SAPBA {
34  [StorableClass]
35  public class InfillStrategy : StrategyBase {
36    #region Parameternames
37    public const string NoGenerationsParameterName = "Number of generations";
38    public const string NoIndividualsParameterName = "Number of individuals";
39    public const string InfillCriterionParameterName = "InfillCriterion";
40    #endregion
41    #region Paramterproperties
42    public IFixedValueParameter<IntValue> NoGenerationsParameter => Parameters[NoGenerationsParameterName] as IFixedValueParameter<IntValue>;
43    public IFixedValueParameter<IntValue> NoIndividualsParameter => Parameters[NoIndividualsParameterName] as IFixedValueParameter<IntValue>;
44    public IConstrainedValueParameter<IInfillCriterion> InfillCriterionParameter => Parameters[InfillCriterionParameterName] as IConstrainedValueParameter<IInfillCriterion>;
45    #endregion
46    #region Properties
47    public IntValue NoGenerations => NoGenerationsParameter.Value;
48    public IntValue NoIndividuals => NoIndividualsParameter.Value;
49    public IInfillCriterion InfillCriterion => InfillCriterionParameter.Value;
50    [Storable]
51    public int Generations;
52    #endregion
53
54    #region Constructors
55    [StorableConstructor]
56    protected InfillStrategy(bool deserializing) : base(deserializing) { }
57    [StorableHook(HookType.AfterDeserialization)]
58    private void AfterDeserialization() {
59      AttachListeners();
60    }
61    protected InfillStrategy(InfillStrategy original, Cloner cloner) : base(original, cloner) {
62      Generations = original.Generations;
63      AttachListeners();
64    }
65    public InfillStrategy() {
66      var critera = new ItemSet<IInfillCriterion> { new ExpectedImprovement(), new AugmentedExpectedImprovement(), new ExpectedQuality(), new ExpectedQuantileImprovement(), new MinimalQuantileCriterium(), new PluginExpectedImprovement() };
67      Parameters.Add(new FixedValueParameter<IntValue>(NoGenerationsParameterName, "The number of generations before a new model is constructed", new IntValue(3)));
68      Parameters.Add(new FixedValueParameter<IntValue>(NoIndividualsParameterName, "The number of individuals that are sampled each generation ", new IntValue(3)));
69      Parameters.Add(new ConstrainedValueParameter<IInfillCriterion>(InfillCriterionParameterName, "The infill criterion used to cheaply evaluate points.", critera, critera.First()));
70      AttachListeners();
71    }
72    public override IDeepCloneable Clone(Cloner cloner) {
73      return new InfillStrategy(this, cloner);
74    }
75    #endregion
76
77    protected override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, ResultCollection globalResults, IRandom random) { }
78    protected override void ProcessPopulation(Individual[] individuals, double[] qualities, IRandom random) {
79      if (RegressionSolution != null && Generations < NoGenerations.Value) Generations++;
80      else {
81        //Select NoIndividuals best samples
82        var samples = individuals
83          .Zip(qualities, (individual, d) => new Tuple<Individual, double>(individual, d))
84          .OrderBy(t => Problem.Maximization ? -t.Item2 : t.Item2)
85          .Take(NoIndividuals.Value)
86          .Select(t => t.Item1.RealVector());
87        foreach (var indi in samples) EvaluateSample(indi, random);
88        BuildRegressionSolution(random);
89        Generations = 0;
90      }
91    }
92    protected override void Initialize() {
93      Generations = 0;
94    }
95
96    #region events
97    private void AttachListeners() {
98      ModelChanged += OnModelChanged;
99    }
100    private void OnModelChanged(object sender, EventArgs e) {
101      InfillCriterion.Encoding = Problem?.Encoding as RealVectorEncoding;
102      InfillCriterion.RegressionSolution = RegressionSolution;
103      InfillCriterion.ExpensiveMaximization = Problem?.Maximization ?? false;
104      if (RegressionSolution != null && InfillCriterion.Encoding != null)
105        InfillCriterion.Initialize();
106    }
107    #endregion
108
109    protected override double Estimate(RealVector point, IRandom random) {
110      return InfillCriterion.Maximization() != Maximization() ? -InfillCriterion.Evaluate(point) : InfillCriterion.Evaluate(point);
111    }
112  }
113}
Note: See TracBrowser for help on using the repository browser.