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