forked from fslaborg/FSharp.Stats
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptimization.fsx
More file actions
371 lines (273 loc) · 9.78 KB
/
Optimization.fsx
File metadata and controls
371 lines (273 loc) · 9.78 KB
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
(**
---
title: Optimization
index: 23
category: Documentation
categoryindex: 0
---
*)
(*** hide ***)
(*** condition: prepare ***)
#r "nuget: FSharpAux.Core, 2.0.0"
#r "nuget: FSharpAux, 2.0.0"
#r "nuget: FSharpAux.IO, 2.0.0"
#r "nuget: OptimizedPriorityQueue, 5.1.0"
#r "nuget: FsMath, 0.0.2"
#I "../src/FSharp.Stats/bin/Release/.net8.0/"
#r "FSharp.Stats.dll"
#r "nuget: Plotly.NET, 4.0.0"
open FsMath
Plotly.NET.Defaults.DefaultDisplayOptions <-
Plotly.NET.DisplayOptions.init (PlotlyJSReference = Plotly.NET.PlotlyJSReference.NoReference)
(*** condition: ipynb ***)
#if IPYNB
#r "nuget: Plotly.NET, 4.0.0"
#r "nuget: Plotly.NET.Interactive, 4.0.0"
#r "nuget: FSharp.Stats"
open Plotly.NET
#endif // IPYNB
(**
# Optimization
[](https://mybinder.org/v2/gh/fslaborg/FSharp.Stats/gh-pages?urlpath=/tree/home/jovyan/Optimization.ipynb)
[]({{root}}{{fsdocs-source-basename}}.ipynb)
_Summary:_ This tutorial teaches how to use optimization methods within FSharp.Stats
## Nelder-Mead
The Nelder-Mead method (also downhill simplex method) can be used to find the minimum or maximum of an objective function.
Please check out Mathias' blog post about the [nelder mead algorithm](https://brandewinder.com/2022/03/31/breaking-down-Nelder-Mead/).
## Quadratic function
Task: Identify the minimum of the following function:
$$f(x)=x^2-0.32x-0.13$$
*)
open FSharp.Stats
open FSharp.Stats.Optimization
open System
open Plotly.NET
open Plotly.NET.TraceObjects
let myFunction (xs: Vector<float>) =
let x = xs.[0]
x**2. + 0.32*x + 0.13
// initial guess for the optimization
let x0 = vector [| 0.95|]
// default solver options
let nmc = NelderMead.NmConfig.defaultInit()
// optimization procedure
let optim =
//let stopCrit =
// { OptimizationStop.defaultStopCriteria with MinFunctionEpsilon = 1e-24 }
//NelderMead.minimizeWithStopCriteria nmc x0 myFunction stopCrit
NelderMead.minimize nmc x0 myFunction
// optimization results as x, y, and z coordinate
let xs,ys =
optim.Vectors.[0..40] |> Array.map (fun x -> x.[0],myFunction x)
|> Array.unzip
let optimizationPathchart =
[
Chart.Line(x=xs,y=ys,ShowMarkers=true,Name="Optimization path")
[-1. .. 0.005 .. 1.] |> List.map (fun x -> x,myFunction (vector [x])) |> Chart.Line |> Chart.withTraceInfo "myFunction"
Chart.Point([|optim.SolutionVector.[0],optim.Solution|],Name="Solution") |> Chart.withMarkerStyle(Size=20,Symbol=StyleParam.MarkerSymbol.ArrowUp)
]
|> Chart.combine
|> Chart.withTemplate ChartTemplates.lightMirrored
|> Chart.withXAxisStyle ("x",ShowGrid=false)
|> Chart.withYAxisStyle ("myFunction(x)",ShowGrid=false)
|> Chart.withSize (800.,800.)
(*** condition: ipynb ***)
#if IPYNB
optimizationPathchart
#endif // IPYNB
(***hide***)
optimizationPathchart |> GenericChart.toChartHTML
(***include-it-raw***)
(**
### Rosenbrock
In the following chapter the minium of the three-dimensional rosenbrock function is identified.
$$f(x,y) = (\alpha - x)^2 + \beta(y-x^2)^2$$
When $\alpha = 1$ and $\beta = 100$ the minimum is at $\alpha^2=1$.
Lets define the function, and a starting coordinate for the optimization task.
*)
// Rosenbrock's valley or Rosenbrock's banana function
let rosenbrock (xs: Vector<float>) =
let x, y = xs.[0], xs.[1]
pown (1.0 - x) 2 + 100.0 * pown (y - pown x 2) 2
// initial guess for the optimization
let x0_rb = vector [| 1.85; -1.65|]
// rosenbrock visualization
let rosenBrockChart =
let range = [-2. .. 0.05 .. 2.]
range
|> List.map (fun y ->
range
|> List.map (fun x ->
rosenbrock (vector [x;y])
)
)
|> fun z ->
Chart.Surface(zData=z,X=range,Y=range,Opacity = 0.5, Contours = Contours.initXyz (Show = true))
let startConditionsChart =
[
Chart.Point3D([x0_rb.[0],x0_rb.[1],rosenbrock x0_rb])
rosenBrockChart
]
|> Chart.combine
|> Chart.withTemplate ChartTemplates.lightMirrored
|> Chart.withXAxisStyle ("x", Id = StyleParam.SubPlotId.Scene 1,ShowGrid=false)
|> Chart.withYAxisStyle ("y", Id = StyleParam.SubPlotId.Scene 1,ShowGrid=false)
|> Chart.withZAxisStyle ("rosenbrock(x,y)",ShowGrid=false)
|> Chart.withSize (800.,800.)
(*** condition: ipynb ***)
#if IPYNB
startConditionsChart
#endif // IPYNB
(***hide***)
startConditionsChart |> GenericChart.toChartHTML
(***include-it-raw***)
(**
Now the functions minimum should be identified using the Nelder-Mead method. Default solver options are used for optimizations.
*)
// default solver options
let nmc_rb = NelderMead.NmConfig.defaultInit()
// optimization procedure
let optim_rb = NelderMead.minimize nmc_rb x0_rb rosenbrock
// minimum x and y value
optim_rb.SolutionVector //vector [|0.9999978246; 1.000002057|]
// minimum z value
optim_rb.Solution //4.110573695e-09
// all z values during optimization steps
optim_rb.Values
// all x and y values during optimization steps
optim_rb.Vectors
(**
#### Plotting of the optimization path
The minimum was correctly identified to be located at $(1,1)$. Lets investigate the path the Nelder-Mead method took to converge to this result.
*)
// optimization results as x, y, and z coordinate
let x3d,y3d,z3d =
optim_rb.Vectors.[0..60] |> Array.map (fun x -> x.[0],x.[1],rosenbrock x)
|> Array.unzip3
let optimizationPathChart =
[
Chart.Line3D(x=x3d,y=y3d,z=z3d,ShowMarkers=true)
rosenBrockChart
]
|> Chart.combine
|> Chart.withTemplate ChartTemplates.lightMirrored
|> Chart.withXAxisStyle ("x", Id = StyleParam.SubPlotId.Scene 1,ShowGrid=false)
|> Chart.withYAxisStyle ("y", Id = StyleParam.SubPlotId.Scene 1,ShowGrid=false)
|> Chart.withZAxisStyle ("rosenbrock(x,y)",ShowGrid=false)
|> Chart.withSize (800.,800.)
(*** condition: ipynb ***)
#if IPYNB
optimizationPathChart
#endif // IPYNB
(***hide***)
optimizationPathChart |> GenericChart.toChartHTML
(***include-it-raw***)
(**
### Auckley function
The Auckley function has many valleys, with one center and global minimum at $(0,0)$.
*)
// Auckley function
let auckley (xs: Vector<float>) =
let x, y = xs.[0], xs.[1]
-20.*exp(-0.2*sqrt(0.5*(x**2. + y**2))) -
exp(0.5*(cos(2. * Math.PI * x) + cos(2. * Math.PI * y))) +
Math.E + 20.
// auckley visualization
let auckleyChart =
let range = [-2. .. 0.05 .. 2.]
range
|> List.map (fun y ->
range
|> List.map (fun x ->
auckley (vector [x;y])
)
)
|> fun z ->
Chart.Surface(zData=z,X=range,Y=range,Opacity = 0.5)
// initial guess for the optimization
let x0_auckley = vector [| 1.2; -1.25 |]
// default solver options
let nmc_auckley = NelderMead.NmConfig.defaultInit()
// optimization procedure
let optim_auckley = NelderMead.minimize nmc_auckley x0_auckley auckley
// optimization results as x, y, and z coordinate
let x3d_auc,y3d_auc,z3d_auc =
optim_auckley.Vectors.[0..44] |> Array.map (fun x -> x.[0],x.[1],auckley x)
|> Array.unzip3
let optimizationPathChart_auckley =
[
Chart.Line3D(x=x3d_auc,y=y3d_auc,z=z3d_auc,ShowMarkers=true)
auckleyChart
]
|> Chart.combine
|> Chart.withTemplate ChartTemplates.lightMirrored
|> Chart.withXAxisStyle ("x", Id = StyleParam.SubPlotId.Scene 1,ShowGrid=false)
|> Chart.withYAxisStyle ("y", Id = StyleParam.SubPlotId.Scene 1,ShowGrid=false)
|> Chart.withZAxisStyle ("auckley(x,y)",ShowGrid=false)
|> Chart.withSize (800.,800.)
(*** condition: ipynb ***)
#if IPYNB
optimizationPathChart_auckley
#endif // IPYNB
(***hide***)
optimizationPathChart_auckley |> GenericChart.toChartHTML
(***include-it-raw***)
(**
The Nelder-Mead method is able to identiy a local minimum, but misses the global minimum
### Beale function
*)
// Beale function function
let beale (xs: Vector<float>) =
let x, y = xs.[0], xs.[1]
(1.5 - x + x*y)**2. +
(2.25 - x + x*y**2)**2. +
(2.625 - x + x*y**3)**2.
// Beale visualization
let bealeChart =
let range = [-4. .. 0.01 .. 4.]
range
|> List.map (fun y ->
range
|> List.map (fun x ->
log10 (beale (vector [x;y]))
)
)
|> fun z ->
Chart.Surface(zData=z,X=range,Y=range,Opacity = 0.5)
// initial guess for the optimization
let x0_beale_1 = vector [| -3.5; -3.5 |]
let x0_beale_2 = vector [| 3. ; 2.5 |]
// default solver options
let nmc_beale = NelderMead.NmConfig.defaultInit()
// optimization procedure
let optim_beale_1 = NelderMead.minimize nmc_beale x0_beale_1 beale
let optim_beale_2 = NelderMead.minimize nmc_beale x0_beale_2 beale
// optimization results as x, y, and z coordinate
let x3d_bea_1,y3d_bea_1,z3d_bea_1 =
optim_beale_1.Vectors.[0..34] |> Array.map (fun x -> x.[0],x.[1],beale x |> log10)
|> Array.unzip3
let x3d_bea_2,y3d_bea_2,z3d_bea_2 =
optim_beale_2.Vectors.[0..34] |> Array.map (fun x -> x.[0],x.[1],beale x |> log10)
|> Array.unzip3
let optimizationPathChart_beale =
[
bealeChart
Chart.Line3D(x=x3d_bea_1,y=y3d_bea_1,z=z3d_bea_1,ShowMarkers=true,Name="local minimum")
Chart.Line3D(x=x3d_bea_2,y=y3d_bea_2,z=z3d_bea_2,ShowMarkers=true,Name="global minimum")
]
|> Chart.combine
|> Chart.withTemplate ChartTemplates.lightMirrored
|> Chart.withXAxisStyle ("x", Id = StyleParam.SubPlotId.Scene 1,ShowGrid=false)
|> Chart.withYAxisStyle ("y", Id = StyleParam.SubPlotId.Scene 1,ShowGrid=false)
|> Chart.withZAxisStyle ("log10(beale(x,y))",ShowGrid=false)
|> Chart.withSize (800.,800.)
(*** condition: ipynb ***)
#if IPYNB
optimizationPathChart_beale
#endif // IPYNB
(***hide***)
optimizationPathChart_beale |> GenericChart.toChartHTML
(***include-it-raw***)
(**
Depending on the start conditions, the method yield a wrong, and a correct path to identify the global minimum.
*)