import numpy as np
from math import inf
from spotPython.fun.objectivefunctions import analytical
from spotPython.spot import spot
from scipy.optimize import shgo
from scipy.optimize import direct
from scipy.optimize import differential_evolution
import matplotlib.pyplot as plt
4 Using sklearn
Surrogates in spotPython
This notebook explains how different surrogate models from scikit-learn
can be used as surrogates in spotPython
optimization runs.
4.1 Example: Branin Function with spotPython
’s Internal Kriging Surrogate
4.1.1 The Objective Function Branin
The
spotPython
package provides several classes of objective functions.We will use an analytical objective function, i.e., a function that can be described by a (closed) formula.
Here we will use the Branin function:
y = a * (x2 - b * x1**2 + c * x1 - r) ** 2 + s * (1 - t) * np.cos(x1) + s, where values of a, b, c, r, s and t are: a = 1, b = 5.1 / (4*pi**2), c = 5 / pi, r = 6, s = 10 and t = 1 / (8*pi).
It has three global minima:
f(x) = 0.397887 at (-pi, 12.275), (pi, 2.275), and (9.42478, 2.475).
from spotPython.fun.objectivefunctions import analytical
= np.array([-5,-0])
lower = np.array([10,15]) upper
= analytical().fun_branin fun
4.1.2 Running the surrogate model based optimizer Spot
:
= spot.Spot(fun=fun,
spot_2 = lower,
lower = upper,
upper = 20,
fun_evals = inf,
max_time =123,
seed={"init_size": 10}) design_control
spot_2.run()
<spotPython.spot.spot.Spot at 0x11085c610>
4.1.3 Print the Results
spot_2.print_results()
min y: 0.3982295132785083
x0: 3.135528626303215
x1: 2.2926027772585886
[['x0', 3.135528626303215], ['x1', 2.2926027772585886]]
4.1.4 Show the Progress and the Surrogate
=True) spot_2.plot_progress(log_y
spot_2.surrogate.plot()
4.2 Example: Using Surrogates From scikit-learn
- Default is the
spotPython
(i.e., the internal)kriging
surrogate. - It can be called explicitely and passed to
Spot
.
from spotPython.build.kriging import Kriging
= Kriging(name='kriging', seed=123) S_0
- Alternatively, models from
scikit-learn
can be selected, e.g., Gaussian Process, RBFs, Regression Trees, etc.
# Needed for the sklearn surrogates:
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import RBF
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn import linear_model
from sklearn import tree
import pandas as pd
- Here are some additional models that might be useful later:
= DecisionTreeRegressor(random_state=0)
S_Tree = linear_model.LinearRegression()
S_LM = linear_model.Ridge()
S_Ridge = RandomForestRegressor(max_depth=2, random_state=0) S_RF
4.2.1 GaussianProcessRegressor as a Surrogate
- To use a Gaussian Process model from
sklearn
, that is similar tospotPython
’sKriging
, we can proceed as follows:
= 1 * RBF(length_scale=1.0, length_scale_bounds=(1e-2, 1e2))
kernel = GaussianProcessRegressor(kernel=kernel, n_restarts_optimizer=9) S_GP
The scikit-learn GP model
S_GP
is selected forSpot
as follows:surrogate = S_GP
We can check the kind of surogate model with the command
isinstance
:
isinstance(S_GP, GaussianProcessRegressor)
True
isinstance(S_0, Kriging)
True
- Similar to the
Spot
run with the internalKriging
model, we can call the run with thescikit-learn
surrogate:
= analytical(seed=123).fun_branin
fun = spot.Spot(fun=fun,
spot_2_GP = lower,
lower = upper,
upper = 20,
fun_evals =123,
seed={"init_size": 10},
design_control= S_GP)
surrogate spot_2_GP.run()
<spotPython.spot.spot.Spot at 0x297ca0bb0>
spot_2_GP.plot_progress()
spot_2_GP.print_results()
min y: 0.3982104287322219
x0: 3.149118141965294
x1: 2.276292107790458
[['x0', 3.149118141965294], ['x1', 2.276292107790458]]
4.3 Example: One-dimensional Sphere Function With spotPython
’s Kriging
- In this example, we will use an one-dimensional function, which allows us to visualize the optimization process.
show_models= True
is added to the argument list.
from spotPython.fun.objectivefunctions import analytical
= np.array([-1])
lower = np.array([1])
upper = analytical(seed=123).fun_sphere fun
= spot.Spot(fun=fun,
spot_1 = lower,
lower = upper,
upper = 10,
fun_evals = inf,
max_time =123,
seed= True,
show_models= np.sqrt(np.spacing(1)),
tolerance_x ={"init_size": 3},)
design_control spot_1.run()
<spotPython.spot.spot.Spot at 0x297ca0910>
4.3.1 Results
spot_1.print_results()
min y: 4.41925228274096e-08
x0: -0.00021022017702259125
[['x0', -0.00021022017702259125]]
=True) spot_1.plot_progress(log_y
- The method
plot_model
plots the final surrogate:
spot_1.plot_model()
4.4 Example: Sklearn
Model GaussianProcess
- This example visualizes the search process on the
GaussianProcessRegression
surrogate fromsklearn
. - Therefore
surrogate = S_GP
is added to the argument list.
= analytical(seed=123).fun_sphere
fun = spot.Spot(fun=fun,
spot_1_GP = lower,
lower = upper,
upper = 10,
fun_evals = inf,
max_time =123,
seed= True,
show_models={"init_size": 3},
design_control= S_GP)
surrogate spot_1_GP.run()
<spotPython.spot.spot.Spot at 0x2979be6b0>
spot_1_GP.print_results()
min y: 1.1973025673035437e-09
x0: 3.460206015981626e-05
[['x0', 3.460206015981626e-05]]
=True) spot_1_GP.plot_progress(log_y
spot_1_GP.plot_model()
4.5 Exercises
4.5.1 DecisionTreeRegressor
- Describe the surrogate model.
- Use the surrogate as the model for optimization.
4.5.2 RandomForestRegressor
- Describe the surrogate model.
- Use the surrogate as the model for optimization.
4.5.3 linear_model.LinearRegression
- Describe the surrogate model.
- Use the surrogate as the model for optimization.
4.5.4 linear_model.Ridge
- Describe the surrogate model.
- Use the surrogate as the model for optimization.
4.6 Exercise 2
Compare the performance of the five different surrogates on both objective functions:
spotPython
’s internal KrigingDecisionTreeRegressor
RandomForestRegressor
linear_model.LinearRegression
linear_model.Ridge