Free cookie consent management tool by TermsFeed Policy Generator

source: branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/InfillCriteria/ExpectedImprovement.cs @ 14818

Last change on this file since 14818 was 14818, checked in by bwerth, 7 years ago

#2745 added several new InfillCriteria and moved Parameters from the InfillProblem to the Criteria themselves; added Sanitiy checks for GaussianProcessRegression

File size: 5.7 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.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.RealVectorEncoding;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Problems.DataAnalysis;
31
32// ReSharper disable once CheckNamespace
33namespace HeuristicLab.Algorithms.EGO {
34
35  [StorableClass]
36  [Item("ExpectedImprovementMeassure", "Extension of the Expected Improvement to a weighted version by ANDRAS SÓBESTER , STEPHEN J. LEARY and ANDY J. KEANE   in \n On the Design of Optimization Strategies Based on Global Response Surface Approximation Models")]
37  public class ExpectedImprovement : InfillCriterionBase {
38
39    #region ParameterNames
40    private const string ExploitationWeightParameterName = "ExploitationWeight";
41    #endregion
42
43    #region ParameterProperties
44    public IFixedValueParameter<DoubleValue> ExploitationWeightParameter => Parameters[ExploitationWeightParameterName] as IFixedValueParameter<DoubleValue>;
45
46    #endregion
47
48    #region Properties
49    protected double ExploitationWeight => ExploitationWeightParameter.Value.Value;
50
51    [Storable]
52    protected double YMin;
53    #endregion
54
55    #region HL-Constructors, Serialization and Cloning
56    [StorableConstructor]
57    protected ExpectedImprovement(bool deserializing) : base(deserializing) { }
58    [StorableHook(HookType.AfterDeserialization)]
59    private void AfterDeserialization() {
60      RegisterEventhandlers();
61    }
62    protected ExpectedImprovement(ExpectedImprovement original, Cloner cloner) : base(original, cloner) {
63      RegisterEventhandlers();
64    }
65    public ExpectedImprovement() {
66      Parameters.Add(new FixedValueParameter<DoubleValue>(ExploitationWeightParameterName, "A value between 0 and 1 indicating the focus on exploration (0) or exploitation (1)", new DoubleValue(0.5)));
67      RegisterEventhandlers();
68    }
69    public override IDeepCloneable Clone(Cloner cloner) {
70      return new ExpectedImprovement(this, cloner);
71    }
72    #endregion
73
74    public override double Evaluate(RealVector vector) {
75      var model = RegressionSolution.Model as IConfidenceRegressionModel;
76      var yhat = model.GetEstimation(vector);
77      var s = Math.Sqrt(model.GetVariance(vector));
78      return GetEstimatedImprovement(YMin, yhat, s, ExploitationWeight);
79    }
80
81    public override bool Maximization() {
82      return true;
83    }
84
85    protected override void Initialize() {
86      if (ExpensiveMaximization) throw new NotImplementedException("Expected Improvement for maximization not yet implemented");
87      var model = RegressionSolution.Model as IConfidenceRegressionModel;
88      if (model == null) throw new ArgumentException("can not calculate EI without confidence measure");
89      YMin = RegressionSolution.ProblemData.TargetVariableTrainingValues.Min();
90    }
91
92    #region Eventhandling
93    private void RegisterEventhandlers() {
94      DeregisterEventhandlers();
95      ExploitationWeightParameter.Value.ValueChanged += OnExploitationWeightChanged;
96    }
97    private void DeregisterEventhandlers() {
98      ExploitationWeightParameter.Value.ValueChanged -= OnExploitationWeightChanged;
99    }
100    private void OnExploitationWeightChanged(object sender, EventArgs e) {
101      ExploitationWeightParameter.Value.ValueChanged -= OnExploitationWeightChanged;
102      ExploitationWeightParameter.Value.Value = Math.Max(0, Math.Min(ExploitationWeight, 1));
103      ExploitationWeightParameter.Value.ValueChanged += OnExploitationWeightChanged;
104    }
105    #endregion
106
107    #region Helpers
108    protected static double GetEstimatedImprovement(double ymin, double yhat, double s, double w) {
109      if (Math.Abs(s) < double.Epsilon) return 0;
110      var val = (ymin - yhat) / s;
111      var res = w * (ymin - yhat) * StandardNormalDistribution(val) + (1 - w) * s * StandardNormalDensity(val);
112      return double.IsInfinity(res) || double.IsNaN(res) ? 0 : res;
113    }
114
115    private static double StandardNormalDensity(double x) {
116      if (Math.Abs(x) > 10) return 0;
117      return Math.Exp(-0.5 * x * x) / Math.Sqrt(2 * Math.PI);
118    }
119
120    //taken from https://www.johndcook.com/blog/2009/01/19/stand-alone-error-function-erf/
121    private static double StandardNormalDistribution(double x) {
122      if (x > 10) return 1;
123      if (x < -10) return 0;
124      const double a1 = 0.254829592;
125      const double a2 = -0.284496736;
126      const double a3 = 1.421413741;
127      const double a4 = -1.453152027;
128      const double a5 = 1.061405429;
129      const double p = 0.3275911;
130      var sign = x < 0 ? -1 : 1;
131      x = Math.Abs(x) / Math.Sqrt(2.0);
132      var t = 1.0 / (1.0 + p * x);
133      var y = 1.0 - ((((a5 * t + a4) * t + a3) * t + a2) * t + a1) * t * Math.Exp(-x * x);
134      return 0.5 * (1.0 + sign * y);
135    }
136    #endregion
137  }
138}
Note: See TracBrowser for help on using the repository browser.