Free cookie consent management tool by TermsFeed Policy Generator

source: branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/EgoUtilities.cs @ 15956

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

#2745 added discretized EGO-version for use with IntegerVectors

File size: 6.3 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.Collections.Generic;
24using System.Linq;
25using System.Threading;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.RealVectorEncoding;
29using HeuristicLab.Encodings.IntegerVectorEncoding;
30using HeuristicLab.Optimization;
31using HeuristicLab.Problems.DataAnalysis;
32
33namespace HeuristicLab.Algorithms.EGO {
34  internal static class EgoUtilities {
35    //Extention methods for convenience
36    public static int ArgMax<T>(this IEnumerable<T> values, Func<T, double> func) {
37      var max = double.MinValue;
38      var maxIdx = 0;
39      var idx = 0;
40      foreach (var v in values) {
41        var d = func.Invoke(v);
42        if (d > max) {
43          max = d;
44          maxIdx = idx;
45        }
46        idx++;
47      }
48      return maxIdx;
49    }
50    public static int ArgMin<T>(this IEnumerable<T> values, Func<T, double> func) {
51      return ArgMax(values, x => -func.Invoke(x));
52    }
53    public static double GetEstimation(this IRegressionModel model, RealVector r) {
54      var dataset = GetDataSet(new[] { new Tuple<RealVector, double>(r, 0.0) }, false);
55      return model.GetEstimatedValues(dataset, new[] { 0 }).First();
56    }
57    public static double GetEstimation(this IRegressionModel model, IntegerVector r) {
58      var dataset = GetDataSet(new[] { new Tuple<IntegerVector, double>(r, 0.0) });
59      return model.GetEstimatedValues(dataset, new[] { 0 }).First();
60    }
61    public static double GetVariance(this IConfidenceRegressionModel model, RealVector r) {
62      var dataset = GetDataSet(new[] { new Tuple<RealVector, double>(r, 0.0) }, false);
63      return model.GetEstimatedVariances(dataset, new[] { 0 }).First();
64    }
65    public static double GetVariance(this IConfidenceRegressionModel model, IntegerVector r) {
66      var dataset = GetDataSet(new[] { new Tuple<IntegerVector, double>(r, 0.0) });
67      return model.GetEstimatedVariances(dataset, new[] { 0 }).First();
68    }
69    public static double GetDoubleValue(this IDataset dataset, int i, int j) {
70      return dataset.GetDoubleValue("input" + j, i);
71    }
72    public static RealVector ToRealVector(this IntegerVector vector) {
73      return new RealVector(vector.Select(x => (double)x).ToArray());
74    }
75
76
77    //Sub-ALgorithms
78    public static ResultCollection SyncRunSubAlgorithm(IAlgorithm alg, int random, CancellationToken cancellation) {
79      if (alg.Parameters.ContainsKey("SetSeedRandomly") && alg.Parameters.ContainsKey("Seed")) {
80        var setSeed = alg.Parameters["SetSeedRandomly"].ActualValue as BoolValue;
81        var seed = alg.Parameters["Seed"].ActualValue as IntValue;
82        if (seed == null || setSeed == null) throw new ArgumentException("wrong SeedParametertypes");
83        setSeed.Value = false;
84        seed.Value = random;
85      }
86      if (alg.ExecutionState != ExecutionState.Paused) alg.Prepare();
87      alg.Start(cancellation);
88      return alg.Results;
89    }
90
91    //RegressionModel extensions
92    private const double DuplicateResolution = 0.0001;
93    public static Dataset GetDataSet(IReadOnlyList<Tuple<RealVector, double>> samples, bool removeDuplicates) {
94      if (removeDuplicates) samples = RemoveDuplicates(samples); //TODO duplicate removal leads to incorrect uncertainty values in models
95      var dimensions = samples[0].Item1.Length + 1;
96      var data = new double[samples.Count, dimensions];
97      var names = new string[dimensions - 1];
98      for (var i = 0; i < names.Length; i++) names[i] = "input" + i;
99      for (var j = 0; j < samples.Count; j++) {
100        for (var i = 0; i < names.Length; i++) data[j, i] = samples[j].Item1[i];
101        data[j, dimensions - 1] = samples[j].Item2;
102      }
103      return new Dataset(names.Concat(new[] { "output" }).ToArray(), data);
104    }
105    //overload for IntegerVectors does not support duplicate removal
106    public static Dataset GetDataSet(IReadOnlyList<Tuple<IntegerVector, double>> samples) {
107      var dimensions = samples[0].Item1.Length + 1;
108      var data = new double[samples.Count, dimensions];
109      var names = new string[dimensions - 1];
110      for (var i = 0; i < names.Length; i++) names[i] = "input" + i;
111      for (var j = 0; j < samples.Count; j++) {
112        for (var i = 0; i < names.Length; i++) data[j, i] = samples[j].Item1[i];
113        data[j, dimensions - 1] = samples[j].Item2;
114      }
115      return new Dataset(names.Concat(new[] { "output" }).ToArray(), data);
116    }
117    private static IReadOnlyList<Tuple<RealVector, double>> RemoveDuplicates(IReadOnlyList<Tuple<RealVector, double>> samples) {
118      var res = new List<Tuple<RealVector, double, int>>();
119      foreach (var sample in samples) {
120        if (res.Count == 0) {
121          res.Add(new Tuple<RealVector, double, int>(sample.Item1, sample.Item2, 1));
122          continue;
123        }
124        var index = res.ArgMin(x => Euclidian(sample.Item1, x.Item1));
125        var d = Euclidian(res[index].Item1, sample.Item1);
126        if (d > DuplicateResolution)
127          res.Add(new Tuple<RealVector, double, int>(sample.Item1, sample.Item2, 1));
128        else {
129          var t = res[index];
130          res.RemoveAt(index);
131          res.Add(new Tuple<RealVector, double, int>(t.Item1, t.Item2 + sample.Item2, t.Item3 + 1));
132        }
133      }
134      return res.Select(x => new Tuple<RealVector, double>(x.Item1, x.Item2 / x.Item3)).ToArray();
135    }
136    private static double Euclidian(IEnumerable<double> a, IEnumerable<double> b) {
137      return Math.Sqrt(a.Zip(b, (d, d1) => d - d1).Sum(d => d * d));
138    }
139  }
140}
Note: See TracBrowser for help on using the repository browser.