-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
222 lines (196 loc) · 7.42 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using Newtonsoft.Json;
using MathNet.Numerics;
using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.Statistics;
namespace BayesOpt
{
using Kernels;
using Utils;
using Optimisers;
class Program
{
static List<double> nextBest;
static void Main(string[] args)
{
if (args.Length == 0)
{
System.Console.WriteLine("No inputs");
return;
}
else if (args.Length > 2)
{
System.Console.WriteLine("Too many args");
return;
}
else if (args.Length == 2)
{
var a = Matrix<double>.Build.Dense(3, 3, (i,j) => 3*i + j);
var b = Matrix<double>.Build.Dense(3,3, (i,j) => 3*j + i);
var V = Vector<double>.Build;
var c = V.DenseOfArray(new double[]{0, 1, 2, 3, 4});
var d = c;
var e = V.DenseOfArray(new double[] {10, 5, 1});
var white = new RBF();
var cov = white.Compute(c, d);
Console.WriteLine(a);
Console.WriteLine(e);
Console.WriteLine(a * e);
// testGridSearch(10000);
}
else
{
int runs;
if (!int.TryParse(args[0], out runs))
{
System.Console.WriteLine("Number of runs must be an integer");
return;
}
nextBest = new List<double>();
run(runs);
}
}
static void testGridSearch(int resolution)
{
Console.WriteLine("start");
GridSearch gs = new GridSearch(
v => 1 - Math.Pow(v[0],2) - Math.Pow(v[1],2) + 2*v[0] + 4*v[1],
new double[,]{{-4,4},{-4,4}},
resolution
);
var res = gs.maximise();
Console.WriteLine(string.Join(',', res.thetaOpt));
}
static void run(int runs)
{
Func<double, double> func = x => -x * Trig.Cos(-2 * x) * Math.Exp(-x / 3);
BayesianOptimisation optimizer = new BayesianOptimisation((0, 8), 800, func);
optimizer.probe(0, lazy: true);
optimizer.probe(8, lazy: true);
for (int i = 0; i < runs; i++)
{
optimizer.maximise(initPoints: 0, nIter: 1);
optimizer.suggest();
logRun(optimizer, i+1);
System.Console.WriteLine(i);
// System.Console.WriteLine(optimizer._gp.logMarginalLikelihoodValue);
}
}
static void logRun(BayesianOptimisation optimizer, int run)
{
int steps = optimizer.space.Count;
var res = optimizer.res;
double[] xObs = [email protected]();
double[] yObs = res.target.ToArray();
double[] x = optimizer.space.ParamSpace;
double[] mean = optimizer.space.Mean;
double[] covariance = optimizer.space.Covariance;
double[] acqVals = optimizer.space.AcquisitionVals;
double next = optimizer.space.NextBest;
Console.WriteLine(next);
nextBest.Add(next);
var estimationResults = new List<EstimationResult>();
var queryResults = new List<DataPoint>();
var aquisitionFunctionValues = new List<AquisitionFunctionValue>();
xObs.ForEach((i, q) => queryResults.Add(new DataPoint(q, yObs[i])));
x.ForEach((i, q) => estimationResults.Add(new EstimationResult(mean[i], covariance[i], q)));
x.ForEach((i, q) => aquisitionFunctionValues.Add(new AquisitionFunctionValue(q, acqVals[i])));
var er = estimationResults
.Select(q => new double[] { q.Mean, q.UpperBound, q.LowerBound, q.X })
.ToArray();
var qr = queryResults
.Select(q => new double[] { q.X, q.FX })
.ToArray();
var af = aquisitionFunctionValues
.Select(q => new double[] { q.X, q.FX })
.ToArray();
var json1 = JsonConvert.SerializeObject(er, Formatting.Indented);
string filename1 = "DataOutput/predicted_testCs" + run + ".json";
File.WriteAllText(filename1, json1);
var json2 = JsonConvert.SerializeObject(qr, Formatting.Indented);
string filename2 = "DataOutput/observed_testCs" + run + ".json";
File.WriteAllText(filename2, json2);
var json3 = JsonConvert.SerializeObject(af, Formatting.Indented);
string filename3 = "DataOutput/aquisition_testCs" + run + ".json";
File.WriteAllText(filename3, json3);
string filename = "DataOutput/nextbestCs.json";
var json4 = JsonConvert.SerializeObject(nextBest.ToArray());
File.WriteAllText(filename, json4);
}
static void writeJson(double[] vals, string name, int run)
{
var json = JsonConvert.SerializeObject(vals, Formatting.Indented);
string filename = $"DataOutput/{name}Cs" + run + ".json";
File.WriteAllText(filename, json);
}
static void Time(Action func)
{
var w = Stopwatch.StartNew();
func();
Console.WriteLine(func.ToString() + ": " + w.Elapsed);
}
static void testList()
{
Random rng = new Random();
List<double> xs = new List<double>();
for (int i = 0; i < 10000; i++)
{
xs.Add(rng.NextDouble());
}
double av = xs.Average();
}
static void testVector()
{
Random rng = new Random();
List<double> xs = new List<double>();
for (int i = 0; i < 10000; i++)
{
xs.Add(rng.NextDouble());
}
Vector<double> xsVector = Vector<double>.Build.DenseOfEnumerable(xs);
double av = xsVector.Mean();
}
}
public class DataPoint
{
public readonly double X;
public readonly double FX;
public DataPoint(double x, double fx)
{
X = x;
FX = fx;
}
}
public class EstimationResult
{
public readonly double Mean;
public readonly double LowerBound;
public readonly double UpperBound;
public readonly double X;
internal EstimationResult(double mean, double confidence, double x)
{
Mean = mean;
LowerBound = mean - confidence;
UpperBound = mean + confidence;
X = x;
}
}
public class AquisitionFunctionValue : IComparable<AquisitionFunctionValue>
{
public readonly double X;
public readonly double FX;
public AquisitionFunctionValue(double x, double fx)
{
X = x;
FX = fx;
}
public int CompareTo(AquisitionFunctionValue other)
{
return FX.CompareTo(other.FX);
}
}
}