Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2745 migrated EGO from Connected Vehicles to its own branch, several modifications/simplifications along the way

File size: 5.6 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
45    {
46      get { return Parameters[ExploitationWeightParameterName] as IFixedValueParameter<DoubleValue>; }
47    }
48    #endregion
49
50    #region Properties
51    private double ExploitationWeight
52    {
53      get { return ExploitationWeightParameter.Value.Value; }
54    }
55
56    #endregion
57
58    #region HL-Constructors, Serialization and Cloning
59    [StorableConstructor]
60    private ExpectedImprovement(bool deserializing) : base(deserializing) { }
61    [StorableHook(HookType.AfterDeserialization)]
62    private void AfterDeserialization() {
63      RegisterEventhandlers();
64    }
65    private ExpectedImprovement(ExpectedImprovement original, Cloner cloner) : base(original, cloner) {
66      RegisterEventhandlers();
67    }
68    public ExpectedImprovement() {
69      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)));
70      RegisterEventhandlers();
71    }
72    public override IDeepCloneable Clone(Cloner cloner) {
73      return new ExpectedImprovement(this, cloner);
74    }
75    #endregion
76
77    public override double Evaluate(IRegressionSolution solution, RealVector vector, bool maximization) {
78      if (maximization) throw new NotImplementedException("Expected Improvement for maximization not yet implemented");
79      var model = solution.Model as IConfidenceRegressionModel;
80      if (model == null) throw new ArgumentException("can not calculate EI without confidence measure");
81      var yhat = model.GetEstimation(vector);
82      var min = solution.ProblemData.TargetVariableTrainingValues.Min();
83      var s = Math.Sqrt(model.GetVariance(vector));
84      return GetEstimatedImprovement(min, yhat, s, ExploitationWeight);
85    }
86
87    public override bool Maximization(bool expensiveProblemMaximization) {
88      return true;
89    }
90
91    #region Eventhandling
92    private void RegisterEventhandlers() {
93      DeregisterEventhandlers();
94      ExploitationWeightParameter.Value.ValueChanged += OnExploitationWeightChanged;
95    }
96    private void DeregisterEventhandlers() {
97      ExploitationWeightParameter.Value.ValueChanged -= OnExploitationWeightChanged;
98    }
99    private void OnExploitationWeightChanged(object sender, EventArgs e) {
100      ExploitationWeightParameter.Value.ValueChanged -= OnExploitationWeightChanged;
101      ExploitationWeightParameter.Value.Value = Math.Max(0, Math.Min(ExploitationWeight, 1));
102      ExploitationWeightParameter.Value.ValueChanged += OnExploitationWeightChanged;
103    }
104    #endregion
105
106    #region Helpers
107    private static double GetEstimatedImprovement(double ymin, double yhat, double s, double w) {
108      if (Math.Abs(s) < double.Epsilon) return 0;
109      var val = (ymin - yhat) / s;
110      var res = w * (ymin - yhat) * StandardNormalDistribution(val) + (1 - w) * s * StandardNormalDensity(val);
111      return double.IsInfinity(res) || double.IsNaN(res) ? 0 : res;
112    }
113
114    private static double StandardNormalDensity(double x) {
115      if (Math.Abs(x) > 10) return 0;
116      return Math.Exp(-0.5 * x * x) / Math.Sqrt(2 * Math.PI);
117    }
118
119    //taken from https://www.johndcook.com/blog/2009/01/19/stand-alone-error-function-erf/
120    private static double StandardNormalDistribution(double x) {
121      if (x > 10) return 1;
122      if (x < -10) return 0;
123      const double a1 = 0.254829592;
124      const double a2 = -0.284496736;
125      const double a3 = 1.421413741;
126      const double a4 = -1.453152027;
127      const double a5 = 1.061405429;
128      const double p = 0.3275911;
129      var sign = x < 0 ? -1 : 1;
130      x = Math.Abs(x) / Math.Sqrt(2.0);
131      var t = 1.0 / (1.0 + p * x);
132      var y = 1.0 - ((((a5 * t + a4) * t + a3) * t + a2) * t + a1) * t * Math.Exp(-x * x);
133      return 0.5 * (1.0 + sign * y);
134    }
135    #endregion
136  }
137}
Note: See TracBrowser for help on using the repository browser.