#region License Information
/* HeuristicLab
* Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Encodings.RealVectorEncoding;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.Problems.DataAnalysis;
// ReSharper disable once CheckNamespace
namespace HeuristicLab.Algorithms.EGO {
[StorableClass]
public abstract class ExpectedImprovementBase : InfillCriterionBase {
#region ParameterNames
private const string ExploitationWeightParameterName = "ExploitationWeight";
#endregion
#region ParameterProperties
public IFixedValueParameter ExploitationWeightParameter => Parameters[ExploitationWeightParameterName] as IFixedValueParameter;
#endregion
#region Properties
protected double ExploitationWeight => ExploitationWeightParameter.Value.Value;
[Storable]
protected double BestFitness;
#endregion
#region Constructors, Serialization and Cloning
[StorableConstructor]
protected ExpectedImprovementBase(bool deserializing) : base(deserializing) { }
[StorableHook(HookType.AfterDeserialization)]
private void AfterDeserialization() {
RegisterEventhandlers();
}
protected ExpectedImprovementBase(ExpectedImprovementBase original, Cloner cloner) : base(original, cloner) {
BestFitness = original.BestFitness;
RegisterEventhandlers();
}
protected ExpectedImprovementBase() {
Parameters.Add(new FixedValueParameter(ExploitationWeightParameterName, "A value between 0 and 1 indicating the focus on exploration (0) or exploitation (1). 0.5 equates to the original EI by Jones et al.", new DoubleValue(0.5)));
RegisterEventhandlers();
}
#endregion
public override void Initialize() {
var solution = RegressionSolution as IConfidenceRegressionSolution;
if (solution == null) throw new ArgumentException("can not calculate EI without a regression solution providing confidence values");
BestFitness = FindBestFitness(solution);
}
protected abstract double FindBestFitness(IConfidenceRegressionSolution solution);
public override double Evaluate(RealVector vector) {
var model = RegressionSolution.Model as IConfidenceRegressionModel;
var yhat = model.GetEstimation(vector);
var s = Math.Sqrt(model.GetVariance(vector));
return Evaluate(vector, yhat, s);
}
protected abstract double Evaluate(RealVector vector, double estimatedFitness, double estimatedStandardDeviation);
#region Eventhandling
private void RegisterEventhandlers() {
DeregisterEventhandlers();
ExploitationWeightParameter.Value.ValueChanged += OnExploitationWeightChanged;
}
private void DeregisterEventhandlers() {
ExploitationWeightParameter.Value.ValueChanged -= OnExploitationWeightChanged;
}
private void OnExploitationWeightChanged(object sender, EventArgs e) {
ExploitationWeightParameter.Value.ValueChanged -= OnExploitationWeightChanged;
ExploitationWeightParameter.Value.Value = Math.Max(0, Math.Min(ExploitationWeight, 1));
ExploitationWeightParameter.Value.ValueChanged += OnExploitationWeightChanged;
}
#endregion
#region Helpers
public static double GetEstimatedImprovement(double bestFitness, double estimatedFitness, double modelUncertainty, double weight, bool maximization) {
if (Math.Abs(modelUncertainty) < double.Epsilon) return 0;
var diff = maximization ? (estimatedFitness - bestFitness) : (bestFitness - estimatedFitness);
var val = diff / modelUncertainty;
var res = weight * diff * StandardNormalDistribution(val) + (1 - weight) * modelUncertainty * StandardNormalDensity(val);
return double.IsInfinity(res) || double.IsNaN(res) ? 0 : res;
}
private static double StandardNormalDensity(double x) {
if (Math.Abs(x) > 10) return 0;
return Math.Exp(-0.5 * x * x) / Math.Sqrt(2 * Math.PI);
}
//taken from https://www.johndcook.com/blog/2009/01/19/stand-alone-error-function-erf/
private static double StandardNormalDistribution(double x) {
if (x > 10) return 1;
if (x < -10) return 0;
const double a1 = 0.254829592;
const double a2 = -0.284496736;
const double a3 = 1.421413741;
const double a4 = -1.453152027;
const double a5 = 1.061405429;
const double p = 0.3275911;
var sign = x < 0 ? -1 : 1;
x = Math.Abs(x) / Math.Sqrt(2.0);
var t = 1.0 / (1.0 + p * x);
var y = 1.0 - ((((a5 * t + a4) * t + a3) * t + a2) * t + a1) * t * Math.Exp(-x * x);
return 0.5 * (1.0 + sign * y);
}
#endregion
}
}