[15896] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using HeuristicLab.Common;
|
---|
| 5 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
| 6 | using HeuristicLab.Problems.DataAnalysis;
|
---|
| 7 |
|
---|
| 8 | namespace HeuristicLab.Networks.IntegratedOptimization.SurrogateModeling {
|
---|
| 9 | public static class ExpectedImprovementHelpers {
|
---|
| 10 | public static IEnumerable<double> Evaluate(IEnumerable<RealVector> points, IRegressionSolution solution, bool calculateExpectedImprovement = true) {
|
---|
| 11 | var model = solution.Model;
|
---|
| 12 | var problemData = solution.ProblemData;
|
---|
| 13 | var dataset = problemData.Dataset;
|
---|
| 14 |
|
---|
| 15 | var modifiableDataset = new ModifiableDataset(problemData.AllowedInputVariables, problemData.AllowedInputVariables.Select(x => new List<double>()));
|
---|
| 16 | foreach (var point in points)
|
---|
| 17 | modifiableDataset.AddRow(point.Select(x => (object)x));
|
---|
| 18 |
|
---|
| 19 | var targets = model.GetEstimatedValues(modifiableDataset, Enumerable.Range(0, modifiableDataset.Rows))
|
---|
| 20 | .ToArray();
|
---|
| 21 |
|
---|
| 22 | if (calculateExpectedImprovement) {
|
---|
| 23 | var confModel = model as IConfidenceRegressionModel;
|
---|
| 24 | if (confModel != null) {
|
---|
| 25 | var minTarget = dataset.GetDoubleValues(problemData.TargetVariable).ToList().Min();
|
---|
| 26 | var uncertainties = confModel.GetEstimatedVariances(modifiableDataset, Enumerable.Range(0, modifiableDataset.Rows))
|
---|
| 27 | .Select(Math.Sqrt)
|
---|
| 28 | .ToArray();
|
---|
| 29 | for (int i = 0; i < modifiableDataset.Rows; i++)
|
---|
| 30 | targets[i] = CalculateExpectedImprovement(minTarget, targets[i], uncertainties[i]);
|
---|
| 31 | }
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | return targets;
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | private static double CalculateExpectedImprovement(double bestTarget, double estimatedTarget, double modelUncertainty) {
|
---|
| 38 | if (modelUncertainty.IsAlmost(0.0)) return 0.0;
|
---|
| 39 |
|
---|
| 40 | var delta = bestTarget - estimatedTarget;
|
---|
| 41 | var x = delta / modelUncertainty;
|
---|
| 42 | var expImp = delta * alglib.normaldistribution(x) + modelUncertainty * Math.Exp(-0.5 * x * x) / Math.Sqrt(2 * Math.PI);
|
---|
| 43 |
|
---|
| 44 | return double.IsNaN(expImp) || double.IsInfinity(expImp) ? 0.0 : expImp;
|
---|
| 45 | }
|
---|
| 46 | }
|
---|
| 47 | }
|
---|