File: exploration-vs-exploitation.rst

package info (click to toggle)
scikit-optimize 0.10.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,672 kB
  • sloc: python: 10,659; javascript: 438; makefile: 136; sh: 6
file content (740 lines) | stat: -rw-r--r-- 14,098 bytes parent folder | download
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740

.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples\exploration-vs-exploitation.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_auto_examples_exploration-vs-exploitation.py>`
        to download the full example code or to run this example in your browser via Binder

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_auto_examples_exploration-vs-exploitation.py:


===========================
Exploration vs exploitation
===========================

Sigurd Carlen, September 2019.
Reformatted by Holger Nahrstaedt 2020

.. currentmodule:: skopt


We can control how much the acqusition function favors exploration and
exploitation by tweaking the two parameters kappa and xi. Higher values
means more exploration and less exploitation and vice versa with low values.

kappa is only used if acq_func is set to "LCB". xi is used when acq_func is
"EI" or "PI". By default the acqusition function is set to "gp_hedge" which
chooses the best of these three. Therefore I recommend not using gp_hedge
when tweaking exploration/exploitation, but instead choosing "LCB",
"EI" or "PI".

The way to pass kappa and xi to the optimizer is to use the named argument
"acq_func_kwargs". This is a dict of extra arguments for the aqcuisition
function.

If you want opt.ask() to give a new acquisition value immediately after
tweaking kappa or xi call opt.update_next(). This ensures that the next
value is updated with the new acquisition parameters.

This example uses :class:`plots.plot_gaussian_process` which is available
since version 0.8.

.. GENERATED FROM PYTHON SOURCE LINES 33-42

.. code-block:: Python


    print(__doc__)

    import numpy as np

    np.random.seed(1234)
    from skopt import Optimizer
    from skopt.plots import plot_gaussian_process








.. GENERATED FROM PYTHON SOURCE LINES 43-49

Toy example
-----------
First we define our objective like in the ask-and-tell example notebook and
define a plotting function. We do however only use on initial random point.
All points after the first one is therefore chosen by the acquisition
function.

.. GENERATED FROM PYTHON SOURCE LINES 49-63

.. code-block:: Python


    noise_level = 0.1


    # Our 1D toy problem, this is the function we are trying to
    # minimize
    def objective(x, noise_level=noise_level):
        return np.sin(5 * x[0]) * (1 - np.tanh(x[0] ** 2)) + np.random.randn() * noise_level


    def objective_wo_noise(x):
        return objective(x, noise_level=0)









.. GENERATED FROM PYTHON SOURCE LINES 64-67

.. code-block:: Python



    opt = Optimizer([(-2.0, 2.0)], "GP", n_initial_points=3, acq_optimizer="sampling")







.. GENERATED FROM PYTHON SOURCE LINES 68-69

Plotting parameters

.. GENERATED FROM PYTHON SOURCE LINES 69-79

.. code-block:: Python


    plot_args = {
        "objective": objective_wo_noise,
        "noise_level": noise_level,
        "show_legend": True,
        "show_title": True,
        "show_next_point": False,
        "show_acq_func": True,
    }








.. GENERATED FROM PYTHON SOURCE LINES 80-81

We run a an optimization loop with standard settings

.. GENERATED FROM PYTHON SOURCE LINES 81-89

.. code-block:: Python


    for i in range(30):
        next_x = opt.ask()
        f_val = objective(next_x)
        opt.tell(next_x, f_val)
    # The same output could be created with opt.run(objective, n_iter=30)
    _ = plot_gaussian_process(opt.get_result(), **plot_args)




.. image-sg:: /auto_examples/images/sphx_glr_exploration-vs-exploitation_001.png
   :alt: x* = -0.2913, f(x*) = -1.0409
   :srcset: /auto_examples/images/sphx_glr_exploration-vs-exploitation_001.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 90-94

We see that some minima is found and "exploited"

Now lets try to set kappa and xi using'to other values and
pass it to the optimizer:

.. GENERATED FROM PYTHON SOURCE LINES 94-95

.. code-block:: Python

    acq_func_kwargs = {"xi": 10000, "kappa": 10000}







.. GENERATED FROM PYTHON SOURCE LINES 96-104

.. code-block:: Python


    opt = Optimizer(
        [(-2.0, 2.0)],
        "GP",
        n_initial_points=3,
        acq_optimizer="sampling",
        acq_func_kwargs=acq_func_kwargs,
    )







.. GENERATED FROM PYTHON SOURCE LINES 105-107

.. code-block:: Python

    opt.run(objective, n_iter=20)
    _ = plot_gaussian_process(opt.get_result(), **plot_args)



.. image-sg:: /auto_examples/images/sphx_glr_exploration-vs-exploitation_002.png
   :alt: x* = -0.3083, f(x*) = -0.7990
   :srcset: /auto_examples/images/sphx_glr_exploration-vs-exploitation_002.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 108-111

We see that the points are more random now.

This works both for kappa when using acq_func="LCB":

.. GENERATED FROM PYTHON SOURCE LINES 111-120

.. code-block:: Python


    opt = Optimizer(
        [(-2.0, 2.0)],
        "GP",
        n_initial_points=3,
        acq_func="LCB",
        acq_optimizer="sampling",
        acq_func_kwargs=acq_func_kwargs,
    )







.. GENERATED FROM PYTHON SOURCE LINES 121-123

.. code-block:: Python

    opt.run(objective, n_iter=20)
    _ = plot_gaussian_process(opt.get_result(), **plot_args)



.. image-sg:: /auto_examples/images/sphx_glr_exploration-vs-exploitation_003.png
   :alt: x* = -0.1829, f(x*) = -0.8271
   :srcset: /auto_examples/images/sphx_glr_exploration-vs-exploitation_003.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 124-125

And for xi when using acq_func="EI": or acq_func="PI":

.. GENERATED FROM PYTHON SOURCE LINES 125-134

.. code-block:: Python


    opt = Optimizer(
        [(-2.0, 2.0)],
        "GP",
        n_initial_points=3,
        acq_func="PI",
        acq_optimizer="sampling",
        acq_func_kwargs=acq_func_kwargs,
    )







.. GENERATED FROM PYTHON SOURCE LINES 135-137

.. code-block:: Python

    opt.run(objective, n_iter=20)
    _ = plot_gaussian_process(opt.get_result(), **plot_args)



.. image-sg:: /auto_examples/images/sphx_glr_exploration-vs-exploitation_004.png
   :alt: x* = -0.3877, f(x*) = -0.8487
   :srcset: /auto_examples/images/sphx_glr_exploration-vs-exploitation_004.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 138-139

Now lets try MES with 50 points:

.. GENERATED FROM PYTHON SOURCE LINES 140-141

.. code-block:: Python

    acq_func_kwargs = {"n_min_samples": 150}







.. GENERATED FROM PYTHON SOURCE LINES 142-151

.. code-block:: Python


    opt = Optimizer(
        [(-2.0, 2.0)],
        "GP",
        n_initial_points=3,
        acq_func="MES",
        acq_optimizer="sampling",
        acq_func_kwargs=acq_func_kwargs,
    )







.. GENERATED FROM PYTHON SOURCE LINES 152-154

.. code-block:: Python

    opt.run(objective, n_iter=20)
    _ = plot_gaussian_process(opt.get_result(), **plot_args)



.. image-sg:: /auto_examples/images/sphx_glr_exploration-vs-exploitation_005.png
   :alt: x* = -0.2441, f(x*) = -0.8469
   :srcset: /auto_examples/images/sphx_glr_exploration-vs-exploitation_005.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 155-156

We can also favor exploitaton:

.. GENERATED FROM PYTHON SOURCE LINES 156-157

.. code-block:: Python

    acq_func_kwargs = {"xi": 0.000001, "kappa": 0.001}







.. GENERATED FROM PYTHON SOURCE LINES 158-166

.. code-block:: Python

    opt = Optimizer(
        [(-2.0, 2.0)],
        "GP",
        n_initial_points=3,
        acq_func="LCB",
        acq_optimizer="sampling",
        acq_func_kwargs=acq_func_kwargs,
    )







.. GENERATED FROM PYTHON SOURCE LINES 167-169

.. code-block:: Python

    opt.run(objective, n_iter=20)
    _ = plot_gaussian_process(opt.get_result(), **plot_args)



.. image-sg:: /auto_examples/images/sphx_glr_exploration-vs-exploitation_006.png
   :alt: x* = 1.6319, f(x*) = -0.1482
   :srcset: /auto_examples/images/sphx_glr_exploration-vs-exploitation_006.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 170-178

.. code-block:: Python

    opt = Optimizer(
        [(-2.0, 2.0)],
        "GP",
        n_initial_points=3,
        acq_func="EI",
        acq_optimizer="sampling",
        acq_func_kwargs=acq_func_kwargs,
    )







.. GENERATED FROM PYTHON SOURCE LINES 179-181

.. code-block:: Python

    opt.run(objective, n_iter=20)
    _ = plot_gaussian_process(opt.get_result(), **plot_args)



.. image-sg:: /auto_examples/images/sphx_glr_exploration-vs-exploitation_007.png
   :alt: x* = -0.2705, f(x*) = -1.0266
   :srcset: /auto_examples/images/sphx_glr_exploration-vs-exploitation_007.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 182-190

.. code-block:: Python

    opt = Optimizer(
        [(-2.0, 2.0)],
        "GP",
        n_initial_points=3,
        acq_func="PI",
        acq_optimizer="sampling",
        acq_func_kwargs=acq_func_kwargs,
    )







.. GENERATED FROM PYTHON SOURCE LINES 191-194

.. code-block:: Python

    opt.run(objective, n_iter=20)
    _ = plot_gaussian_process(opt.get_result(), **plot_args)




.. image-sg:: /auto_examples/images/sphx_glr_exploration-vs-exploitation_008.png
   :alt: x* = -0.2961, f(x*) = -0.9580
   :srcset: /auto_examples/images/sphx_glr_exploration-vs-exploitation_008.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 195-197

Note that negative values does not work with the "PI"-acquisition function
but works with "EI":

.. GENERATED FROM PYTHON SOURCE LINES 197-198

.. code-block:: Python

    acq_func_kwargs = {"xi": -1000000000000}







.. GENERATED FROM PYTHON SOURCE LINES 199-208

.. code-block:: Python


    opt = Optimizer(
        [(-2.0, 2.0)],
        "GP",
        n_initial_points=3,
        acq_func="PI",
        acq_optimizer="sampling",
        acq_func_kwargs=acq_func_kwargs,
    )







.. GENERATED FROM PYTHON SOURCE LINES 209-211

.. code-block:: Python

    opt.run(objective, n_iter=20)
    _ = plot_gaussian_process(opt.get_result(), **plot_args)



.. image-sg:: /auto_examples/images/sphx_glr_exploration-vs-exploitation_009.png
   :alt: x* = -0.3491, f(x*) = -0.7981
   :srcset: /auto_examples/images/sphx_glr_exploration-vs-exploitation_009.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 212-220

.. code-block:: Python

    opt = Optimizer(
        [(-2.0, 2.0)],
        "GP",
        n_initial_points=3,
        acq_func="EI",
        acq_optimizer="sampling",
        acq_func_kwargs=acq_func_kwargs,
    )







.. GENERATED FROM PYTHON SOURCE LINES 221-223

.. code-block:: Python

    opt.run(objective, n_iter=20)
    _ = plot_gaussian_process(opt.get_result(), **plot_args)



.. image-sg:: /auto_examples/images/sphx_glr_exploration-vs-exploitation_010.png
   :alt: x* = -1.5268, f(x*) = -0.1786
   :srcset: /auto_examples/images/sphx_glr_exploration-vs-exploitation_010.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 224-230

Changing kappa and xi on the go
-------------------------------
If we want to change kappa or ki at any point during our optimization
process we just replace opt.acq_func_kwargs. Remember to call
`opt.update_next()` after the change, in order for next point to be
recalculated.

.. GENERATED FROM PYTHON SOURCE LINES 230-231

.. code-block:: Python

    acq_func_kwargs = {"kappa": 0}







.. GENERATED FROM PYTHON SOURCE LINES 232-240

.. code-block:: Python

    opt = Optimizer(
        [(-2.0, 2.0)],
        "GP",
        n_initial_points=3,
        acq_func="LCB",
        acq_optimizer="sampling",
        acq_func_kwargs=acq_func_kwargs,
    )







.. GENERATED FROM PYTHON SOURCE LINES 241-242

.. code-block:: Python

    opt.acq_func_kwargs




.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    {'kappa': 0}



.. GENERATED FROM PYTHON SOURCE LINES 243-245

.. code-block:: Python

    opt.run(objective, n_iter=20)
    _ = plot_gaussian_process(opt.get_result(), **plot_args)



.. image-sg:: /auto_examples/images/sphx_glr_exploration-vs-exploitation_011.png
   :alt: x* = -0.6144, f(x*) = -0.2081
   :srcset: /auto_examples/images/sphx_glr_exploration-vs-exploitation_011.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 246-247

.. code-block:: Python

    acq_func_kwargs = {"kappa": 100000}







.. GENERATED FROM PYTHON SOURCE LINES 248-250

.. code-block:: Python

    opt.acq_func_kwargs = acq_func_kwargs
    opt.update_next()







.. GENERATED FROM PYTHON SOURCE LINES 251-253

.. code-block:: Python

    opt.run(objective, n_iter=20)
    _ = plot_gaussian_process(opt.get_result(), **plot_args)



.. image-sg:: /auto_examples/images/sphx_glr_exploration-vs-exploitation_012.png
   :alt: x* = -0.1506, f(x*) = -0.6871
   :srcset: /auto_examples/images/sphx_glr_exploration-vs-exploitation_012.png
   :class: sphx-glr-single-img






.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 46.614 seconds)


.. _sphx_glr_download_auto_examples_exploration-vs-exploitation.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: binder-badge

      .. image:: images/binder_badge_logo.svg
        :target: https://mybinder.org/v2/gh/holgern/scikit-optimize/master?urlpath=lab/tree/notebooks/auto_examples/exploration-vs-exploitation.ipynb
        :alt: Launch binder
        :width: 150 px

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: exploration-vs-exploitation.ipynb <exploration-vs-exploitation.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: exploration-vs-exploitation.py <exploration-vs-exploitation.py>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_