[14741] | 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.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Threading;
|
---|
[15064] | 26 | using HeuristicLab.Core;
|
---|
[14741] | 27 | using HeuristicLab.Data;
|
---|
| 28 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
[15343] | 29 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
[14741] | 30 | using HeuristicLab.Optimization;
|
---|
| 31 | using HeuristicLab.Problems.DataAnalysis;
|
---|
| 32 |
|
---|
| 33 | namespace HeuristicLab.Algorithms.EGO {
|
---|
| 34 | internal static class EgoUtilities {
|
---|
[15064] | 35 | //Extention methods for convenience
|
---|
[14741] | 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 | }
|
---|
[15064] | 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 | }
|
---|
[15343] | 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 | }
|
---|
[15064] | 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 | }
|
---|
[15343] | 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 | }
|
---|
[15064] | 69 | public static double GetDoubleValue(this IDataset dataset, int i, int j) {
|
---|
| 70 | return dataset.GetDoubleValue("input" + j, i);
|
---|
| 71 | }
|
---|
[15343] | 72 | public static RealVector ToRealVector(this IntegerVector vector) {
|
---|
| 73 | return new RealVector(vector.Select(x => (double)x).ToArray());
|
---|
| 74 | }
|
---|
[14741] | 75 |
|
---|
[15343] | 76 |
|
---|
[15064] | 77 | //Sub-ALgorithms
|
---|
[15338] | 78 | public static ResultCollection SyncRunSubAlgorithm(IAlgorithm alg, int random, CancellationToken cancellation) {
|
---|
[14768] | 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 | }
|
---|
[15064] | 86 | if (alg.ExecutionState != ExecutionState.Paused) alg.Prepare();
|
---|
[15338] | 87 | alg.Start(cancellation);
|
---|
[14741] | 88 | return alg.Results;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
[15064] | 91 | //RegressionModel extensions
|
---|
[15338] | 92 | private const double DuplicateResolution = 0.0001;
|
---|
[14818] | 93 | public static Dataset GetDataSet(IReadOnlyList<Tuple<RealVector, double>> samples, bool removeDuplicates) {
|
---|
[15064] | 94 | if (removeDuplicates) samples = RemoveDuplicates(samples); //TODO duplicate removal leads to incorrect uncertainty values in models
|
---|
[14818] | 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 | }
|
---|
[14741] | 103 | return new Dataset(names.Concat(new[] { "output" }).ToArray(), data);
|
---|
| 104 | }
|
---|
[15343] | 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 | }
|
---|
[14818] | 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);
|
---|
[15064] | 126 | if (d > DuplicateResolution)
|
---|
[14818] | 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 | }
|
---|
[14741] | 139 | }
|
---|
| 140 | }
|
---|