Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2205_OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.SurrogateModeling/3.3/ExpectedImprovementHelpers.cs @ 16147

Last change on this file since 16147 was 15896, checked in by jkarder, 7 years ago

#2205: added surrogate modeling network

File size: 2.1 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using HeuristicLab.Common;
5using HeuristicLab.Encodings.RealVectorEncoding;
6using HeuristicLab.Problems.DataAnalysis;
7
8namespace 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}
Note: See TracBrowser for help on using the repository browser.