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;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
30 | using HeuristicLab.Optimization;
|
---|
31 | using HeuristicLab.Problems.DataAnalysis;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Algorithms.EGO {
|
---|
34 | internal static class EgoUtilities {
|
---|
35 | public static int ArgMax<T>(this IEnumerable<T> values, Func<T, double> func) {
|
---|
36 | var max = double.MinValue;
|
---|
37 | var maxIdx = 0;
|
---|
38 | var idx = 0;
|
---|
39 | foreach (var v in values) {
|
---|
40 | var d = func.Invoke(v);
|
---|
41 | if (d > max) {
|
---|
42 | max = d;
|
---|
43 | maxIdx = idx;
|
---|
44 | }
|
---|
45 | idx++;
|
---|
46 | }
|
---|
47 | return maxIdx;
|
---|
48 | }
|
---|
49 | public static int ArgMin<T>(this IEnumerable<T> values, Func<T, double> func) {
|
---|
50 | return ArgMax(values, x => -func.Invoke(x));
|
---|
51 | }
|
---|
52 |
|
---|
53 | public static ResultCollection SyncRunSubAlgorithm(IAlgorithm alg, int random) {
|
---|
54 |
|
---|
55 | if (alg.Parameters.ContainsKey("SetSeedRandomly") && alg.Parameters.ContainsKey("Seed")) {
|
---|
56 | var setSeed = alg.Parameters["SetSeedRandomly"].ActualValue as BoolValue;
|
---|
57 | var seed = alg.Parameters["Seed"].ActualValue as IntValue;
|
---|
58 | if (seed == null || setSeed == null) throw new ArgumentException("wrong SeedParametertypes");
|
---|
59 | setSeed.Value = false;
|
---|
60 | seed.Value = random;
|
---|
61 |
|
---|
62 | }
|
---|
63 |
|
---|
64 |
|
---|
65 | EventWaitHandle trigger = new AutoResetEvent(false);
|
---|
66 | Exception ex = null;
|
---|
67 | EventHandler<EventArgs<Exception>> exhandler = (sender, e) => ex = e.Value;
|
---|
68 | EventHandler stoppedHandler = (sender, e) => trigger.Set();
|
---|
69 | alg.ExceptionOccurred += exhandler;
|
---|
70 | alg.Stopped += stoppedHandler;
|
---|
71 | alg.Prepare();
|
---|
72 | alg.Start();
|
---|
73 | trigger.WaitOne();
|
---|
74 | alg.ExceptionOccurred -= exhandler;
|
---|
75 | alg.Stopped -= stoppedHandler;
|
---|
76 | if (ex != null) throw ex;
|
---|
77 | return alg.Results;
|
---|
78 | }
|
---|
79 | public static RealVector[] GetUniformRandomDesign(int points, int dim, DoubleMatrix bounds, IRandom random) {
|
---|
80 | var res = new RealVector[points];
|
---|
81 | for (var i = 0; i < points; i++) {
|
---|
82 | var r = new RealVector(dim);
|
---|
83 | res[i] = r;
|
---|
84 | for (var j = 0; j < dim; j++) {
|
---|
85 | var b = j % bounds.Rows;
|
---|
86 | r[j] = UniformRandom(bounds[b, 0], bounds[b, 1], random);
|
---|
87 | }
|
---|
88 | }
|
---|
89 | return res;
|
---|
90 | }
|
---|
91 | public static double UniformRandom(double min, double max, IRandom rand) {
|
---|
92 | return rand.NextDouble() * (max - min) + min;
|
---|
93 | }
|
---|
94 |
|
---|
95 | public static double GetEstimation(this IRegressionModel model, RealVector r) {
|
---|
96 | var dataset = GetDataSet(new[] { new Tuple<RealVector, double>(r, 0.0) });
|
---|
97 | return model.GetEstimatedValues(dataset, new[] { 0 }).First();
|
---|
98 | }
|
---|
99 | public static double GetVariance(this IConfidenceRegressionModel model, RealVector r) {
|
---|
100 | var dataset = GetDataSet(new[] { new Tuple<RealVector, double>(r, 0.0) });
|
---|
101 | return model.GetEstimatedVariances(dataset, new[] { 0 }).First();
|
---|
102 | }
|
---|
103 |
|
---|
104 | public static Dataset GetDataSet(IReadOnlyList<Tuple<RealVector, double>> samples) {
|
---|
105 | var n = samples[0].Item1.Length + 1;
|
---|
106 | var data = new double[samples.Count, n];
|
---|
107 | var names = new string[n - 1];
|
---|
108 | for (var i = 0; i < n; i++)
|
---|
109 | if (i < names.Length) {
|
---|
110 | names[i] = "input" + i;
|
---|
111 | for (var j = 0; j < samples.Count; j++) data[j, i] = samples[j].Item1[i];
|
---|
112 | } else
|
---|
113 | for (var j = 0; j < samples.Count; j++) data[j, n - 1] = samples[j].Item2;
|
---|
114 | return new Dataset(names.Concat(new[] { "output" }).ToArray(), data);
|
---|
115 | }
|
---|
116 | public static DoubleMatrix GetBoundingBox(IEnumerable<RealVector> vectors) {
|
---|
117 | DoubleMatrix res = null;
|
---|
118 | foreach (var vector in vectors)
|
---|
119 | if (res == null) {
|
---|
120 | res = new DoubleMatrix(vector.Length, 2);
|
---|
121 | for (var i = 0; i < vector.Length; i++)
|
---|
122 | res[i, 0] = res[i, 1] = vector[i];
|
---|
123 | } else
|
---|
124 | for (var i = 0; i < vector.Length; i++) {
|
---|
125 | res[i, 0] = Math.Min(vector[i], res[i, 0]);
|
---|
126 | res[i, 1] = Math.Max(vector[i], res[i, 1]);
|
---|
127 | }
|
---|
128 | return res;
|
---|
129 | }
|
---|
130 | }
|
---|
131 | }
|
---|