File: exploration-vs-exploitation.py

package info (click to toggle)
scikit-optimize 0.10.2-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,684 kB
  • sloc: python: 10,659; javascript: 438; makefile: 136; sh: 6
file content (252 lines) | stat: -rw-r--r-- 8,639 bytes parent folder | download | duplicates (4)
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
"""
===========================
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.
"""

print(__doc__)

import numpy as np

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

#############################################################################
# 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.

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)


#############################################################################


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

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

#############################################################################
# We run a an optimization loop with standard settings

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)

#############################################################################
# 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:
acq_func_kwargs = {"xi": 10000, "kappa": 10000}
#############################################################################

opt = Optimizer(
    [(-2.0, 2.0)],
    "GP",
    n_initial_points=3,
    acq_optimizer="sampling",
    acq_func_kwargs=acq_func_kwargs,
)
#############################################################################
opt.run(objective, n_iter=20)
_ = plot_gaussian_process(opt.get_result(), **plot_args)
#############################################################################
# We see that the points are more random now.
#
# This works both for kappa when using acq_func="LCB":

opt = Optimizer(
    [(-2.0, 2.0)],
    "GP",
    n_initial_points=3,
    acq_func="LCB",
    acq_optimizer="sampling",
    acq_func_kwargs=acq_func_kwargs,
)
#############################################################################
opt.run(objective, n_iter=20)
_ = plot_gaussian_process(opt.get_result(), **plot_args)
#############################################################################
# And for xi when using acq_func="EI": or acq_func="PI":

opt = Optimizer(
    [(-2.0, 2.0)],
    "GP",
    n_initial_points=3,
    acq_func="PI",
    acq_optimizer="sampling",
    acq_func_kwargs=acq_func_kwargs,
)
#############################################################################
opt.run(objective, n_iter=20)
_ = plot_gaussian_process(opt.get_result(), **plot_args)
#############################################################################
#
# Now lets try MES with 50 points:
acq_func_kwargs = {"n_min_samples": 150}
#############################################################################

opt = Optimizer(
    [(-2.0, 2.0)],
    "GP",
    n_initial_points=3,
    acq_func="MES",
    acq_optimizer="sampling",
    acq_func_kwargs=acq_func_kwargs,
)
#############################################################################
opt.run(objective, n_iter=20)
_ = plot_gaussian_process(opt.get_result(), **plot_args)
#############################################################################
# We can also favor exploitaton:
acq_func_kwargs = {"xi": 0.000001, "kappa": 0.001}
#############################################################################
opt = Optimizer(
    [(-2.0, 2.0)],
    "GP",
    n_initial_points=3,
    acq_func="LCB",
    acq_optimizer="sampling",
    acq_func_kwargs=acq_func_kwargs,
)
#############################################################################
opt.run(objective, n_iter=20)
_ = plot_gaussian_process(opt.get_result(), **plot_args)
#############################################################################
opt = Optimizer(
    [(-2.0, 2.0)],
    "GP",
    n_initial_points=3,
    acq_func="EI",
    acq_optimizer="sampling",
    acq_func_kwargs=acq_func_kwargs,
)
#############################################################################
opt.run(objective, n_iter=20)
_ = plot_gaussian_process(opt.get_result(), **plot_args)
#############################################################################
opt = Optimizer(
    [(-2.0, 2.0)],
    "GP",
    n_initial_points=3,
    acq_func="PI",
    acq_optimizer="sampling",
    acq_func_kwargs=acq_func_kwargs,
)
#############################################################################
opt.run(objective, n_iter=20)
_ = plot_gaussian_process(opt.get_result(), **plot_args)

#############################################################################
# Note that negative values does not work with the "PI"-acquisition function
# but works with "EI":
acq_func_kwargs = {"xi": -1000000000000}
#############################################################################

opt = Optimizer(
    [(-2.0, 2.0)],
    "GP",
    n_initial_points=3,
    acq_func="PI",
    acq_optimizer="sampling",
    acq_func_kwargs=acq_func_kwargs,
)
#############################################################################
opt.run(objective, n_iter=20)
_ = plot_gaussian_process(opt.get_result(), **plot_args)
#############################################################################
opt = Optimizer(
    [(-2.0, 2.0)],
    "GP",
    n_initial_points=3,
    acq_func="EI",
    acq_optimizer="sampling",
    acq_func_kwargs=acq_func_kwargs,
)
#############################################################################
opt.run(objective, n_iter=20)
_ = plot_gaussian_process(opt.get_result(), **plot_args)
#############################################################################
# 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.
acq_func_kwargs = {"kappa": 0}
#############################################################################
opt = Optimizer(
    [(-2.0, 2.0)],
    "GP",
    n_initial_points=3,
    acq_func="LCB",
    acq_optimizer="sampling",
    acq_func_kwargs=acq_func_kwargs,
)
#############################################################################
opt.acq_func_kwargs
#############################################################################
opt.run(objective, n_iter=20)
_ = plot_gaussian_process(opt.get_result(), **plot_args)
#############################################################################
acq_func_kwargs = {"kappa": 100000}
#############################################################################
opt.acq_func_kwargs = acq_func_kwargs
opt.update_next()
#############################################################################
opt.run(objective, n_iter=20)
_ = plot_gaussian_process(opt.get_result(), **plot_args)