-
Notifications
You must be signed in to change notification settings - Fork 0
/
TargetSpace.cs
98 lines (83 loc) · 3 KB
/
TargetSpace.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
using System;
using System.Collections.Generic;
using System.Linq;
using BayesOpt.Utils;
using MathNet.Numerics;
// TODO should optimizer be allowed to probe same point twice?
namespace BayesOpt
{
public class TargetSpace
{
#region Private properties
private (double min, double max) _bounds; // TODO review with nDim > 1
private HashSet<(double, double)> _cache;
private readonly List<double> _params;
private readonly List<double> _target;
private double[] _paramSpace;
private double[] _mean;
private double[] _covariance;
private double[] _acqValues;
private double _nextBest;
#endregion
public TargetSpace((double min, double max) paramBounds, int resolution)
{
_bounds = paramBounds;
_paramSpace = Generate.LinearSpaced(resolution, _bounds.min, _bounds.max);
_cache = new HashSet<(double, double)>();
_params = new List<double>();
_target = new List<double>();
this.resolution = resolution;
}
#region Public properties
public bool Contains(double x, double y)
{
return _cache.Contains((x, y));
}
public int Count { get { return _cache.Count; } }
public List<double> Params { get { return _params; } }
// TODO is this actually necessary
public List<double> Target { get { return _target; } }
public readonly int dim = 1; // TODO with nDim > 1
public (double, double) bounds { get { return _bounds; } }
public int resolution;
public double[] ParamSpace { get { return _paramSpace; } }
public double[] Mean { get { return _mean; } }
public double[] Covariance { get { return _covariance; } }
public double[] AcquisitionVals { get { return _acqValues; } }
public double NextBest { get { return _nextBest; } }
#endregion
public void register(double param, double target)
{
_params.Add(param);
_target.Add(target);
_cache.Add((param, target));
}
public double randomSample()
{
Random rng = new Random();
return _bounds.min + rng.NextDouble() * (_bounds.max - _bounds.min);
// TODO replace with U(min, max)?
}
public double max()
{
return _target.Max();
}
public (List<double> @params, List<double> target) res
{
get { return (_params, _target); }
}
public void setBounds(double min, double max)
{
_bounds.min = min;
_bounds.max = max;
}
public void logOptimisationData(double[] xs, double[] mean, double[] covariance, double[] acqVals, double nextBest)
{
_paramSpace = xs;
_mean = mean;
_covariance = covariance;
_acqValues = acqVals;
_nextBest = nextBest;
}
}
}