-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathModel.fs
92 lines (71 loc) · 4.75 KB
/
Model.fs
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
module Model
open System
open NUnit.Framework
open MathNet.Numerics
open Optimization.Model
open Optimization.Domain
[<Test>]
let ``Maximize Sin Between -π and π`` () =
let gaussianProcess : GaussianProcess =
createProcessWithSquaredExponentialKernel { LengthScale = 1.; Variance = 1. }
let objectiveFunction : ObjectiveFunction = QueryContinuousFunction Trig.Sin
let model : GaussianModel = createModel gaussianProcess objectiveFunction ExpectedImprovement -Math.PI Math.PI 20_000
let optima : OptimaResult = findOptima { Model = model; Goal = Goal.Max; Iterations = 10 }
Assert.AreEqual(optima.Optima.X, Math.PI / 2., 0.001)
[<Test>]
let ``Minimize Sin Between -π and π`` () =
let gaussianProcess : GaussianProcess =
createProcessWithSquaredExponentialKernel { LengthScale = 1.; Variance = 1. }
let objectiveFunction : ObjectiveFunction = QueryContinuousFunction Trig.Sin
let model : GaussianModel = createModel gaussianProcess objectiveFunction ExpectedImprovement -Math.PI Math.PI 20_000
let optima : OptimaResult = findOptima { Model = model; Goal = Goal.Min; Iterations = 10 }
Assert.AreEqual(optima.Optima.X, 0., 0.001)
[<Test>]
let ``Maximize Cos Between -π and π`` () =
let gaussianProcess : GaussianProcess =
createProcessWithSquaredExponentialKernel { LengthScale = 1.; Variance = 1. }
let objectiveFunction : ObjectiveFunction = QueryContinuousFunction Trig.Cos
let model : GaussianModel = createModel gaussianProcess objectiveFunction ExpectedImprovement -Math.PI Math.PI 20_000
let optima : OptimaResult = findOptima { Model = model; Goal = Goal.Max; Iterations = 10 }
Assert.AreEqual(optima.Optima.X, 0., 0.001)
[<Test>]
let ``Minimize Cos Between -π and π`` () =
let gaussianProcess : GaussianProcess =
createProcessWithSquaredExponentialKernel { LengthScale = 1.; Variance = 1. }
let objectiveFunction : ObjectiveFunction = QueryContinuousFunction Trig.Cos
let model : GaussianModel = createModel gaussianProcess objectiveFunction ExpectedImprovement -Math.PI Math.PI 20_000
let optima : OptimaResult = findOptima { Model = model; Goal = Goal.Min; Iterations = 10 }
// Cos(-Pi) = -1, the minima.
Assert.AreEqual(optima.Optima.X, -Math.PI, 0.001)
[<Test>]
let ``Minimize $$ x^2 - x $$ between 50 and 100`` () =
let gaussianProcess : GaussianProcess =
createProcessWithSquaredExponentialKernel { LengthScale = 0.1; Variance = 1. }
let objectiveFunction : ObjectiveFunction = QueryContinuousFunction (fun input -> (input * input) - input)
let model : GaussianModel = createModel gaussianProcess objectiveFunction ExpectedImprovement 50 100 300
let optima : OptimaResult = findOptima { Model = model; Goal = Goal.Min; Iterations = 30 }
Assert.AreEqual(optima.Optima.X, 50, 0.001)
[<Test>]
let ``Maximize $$ x^2 - x $$ between 50 and 100`` () =
let gaussianProcess : GaussianProcess =
createProcessWithSquaredExponentialKernel { LengthScale = 0.1; Variance = 1. }
let objectiveFunction : ObjectiveFunction = QueryContinuousFunction (fun input -> (input * input) - input)
let model : GaussianModel = createModel gaussianProcess objectiveFunction ExpectedImprovement 50 100 300
let optima : OptimaResult = findOptima { Model = model; Goal = Goal.Max; Iterations = 30 }
Assert.AreEqual(optima.Optima.X, 100, 0.01)
[<Test>]
let ``Minimize $$ x^2 - 98x + 4 $$ between 0 and 100`` () =
let gaussianProcess : GaussianProcess =
createProcessWithSquaredExponentialKernel { LengthScale = 0.1; Variance = 1. }
let objectiveFunction : ObjectiveFunction = QueryContinuousFunction (fun input -> (input * input) - (98. * input) + 4.)
let model : GaussianModel = createModel gaussianProcess objectiveFunction ExpectedImprovement 0 100 300
let optima : OptimaResult = findOptima { Model = model; Goal = Goal.Min; Iterations = 30 }
Assert.AreEqual(optima.Optima.X, 22.4, 0.01)
[<Test>]
let ``Maximize $$ x^2 - 98x + 4 $$ between 0 and 100`` () =
let gaussianProcess : GaussianProcess =
createProcessWithSquaredExponentialKernel { LengthScale = 0.1; Variance = 1. }
let objectiveFunction : ObjectiveFunction = QueryContinuousFunction (fun input -> (input * input) - (98. * input) + 4.)
let model : GaussianModel = createModel gaussianProcess objectiveFunction ExpectedImprovement 0 100 300
let optima : OptimaResult = findOptima { Model = model; Goal = Goal.Max; Iterations = 30 }
Assert.AreEqual(optima.Optima.X, 100, 0.01)