## Linear Surrogate

Linear Surrogate is a linear approach to modeling the relationship between a scalar response or dependent variable and one or more explanatory variables. We will use Linear Surrogate to optimize following function:

$$$f(x) = sin(x) + log(x)$$$

.

First of all we have to import these two packages: Surrogates and Plots.

using Surrogates
using Plots
default()

### Sampling

We choose to sample f in 20 points between 0 and 10 using the sample function. The sampling points are chosen using a Sobol sequence, this can be done by passing SobolSample() to the sample function.

f(x) = sin(x) + log(x)
n_samples = 20
lower_bound = 5.2
upper_bound = 12.5
x = sample(n_samples, lower_bound, upper_bound, SobolSample())
y = f.(x)
scatter(x, y, label="Sampled points", xlims=(lower_bound, upper_bound))
plot!(f, label="True function", xlims=(lower_bound, upper_bound))

## Building a Surrogate

With our sampled points we can build the Linear Surrogate using the LinearSurrogate function.

We can simply calculate linear_surrogate for any value.

my_linear_surr_1D = LinearSurrogate(x, y, lower_bound, upper_bound)
add_point!(my_linear_surr_1D,4.0,7.2)
add_point!(my_linear_surr_1D,[5.0,6.0],[8.3,9.7])
val = my_linear_surr_1D(5.0)
1.4357676093321794

Now, we will simply plot linear_surrogate:

plot(x, y, seriestype=:scatter, label="Sampled points", xlims=(lower_bound, upper_bound))
plot!(f, label="True function",  xlims=(lower_bound, upper_bound))
plot!(my_linear_surr_1D, label="Surrogate function",  xlims=(lower_bound, upper_bound))

## Optimizing

Having built a surrogate, we can now use it to search for minimas in our original function f.

To optimize using our surrogate we call surrogate_optimize method. We choose to use Stochastic RBF as optimization technique and again Sobol sampling as sampling technique.

@show surrogate_optimize(f, SRBF(), lower_bound, upper_bound, my_linear_surr_1D, SobolSample())
scatter(x, y, label="Sampled points")
plot!(f, label="True function",  xlims=(lower_bound, upper_bound))
plot!(my_linear_surr_1D, label="Surrogate function",  xlims=(lower_bound, upper_bound))

## Linear Surrogate tutorial (ND)

First of all we will define the Egg Holder function we are going to build surrogate for. Notice, one how its argument is a vector of numbers, one for each coordinate, and its output is a scalar.

function egg(x)
x1=x[1]
x2=x[2]
term1 = -(x2+47) * sin(sqrt(abs(x2+x1/2+47)));
term2 = -x1 * sin(sqrt(abs(x1-(x2+47))));
y = term1 + term2;
end
egg (generic function with 1 method)

### Sampling

Let's define our bounds, this time we are working in two dimensions. In particular we want our first dimension x to have bounds -10, 5, and 0, 15 for the second dimension. We are taking 50 samples of the space using Sobol Sequences. We then evaluate our function on all of the sampling points.

n_samples = 50
lower_bound = [-10.0, 0.0]
upper_bound = [5.0, 15.0]

xys = sample(n_samples, lower_bound, upper_bound, SobolSample())
zs = egg.(xys);
50-element Vector{Float64}:
-16.24460616084012
-54.64643791993161
-29.810972564088008
-38.19438263308444
-24.366368385945748
-58.61970176694041
-44.2552059827217
-50.25659851885385
-13.825250317178524
-53.038761015471884
⋮
-56.66585173843803
-53.425487506680916
-57.961298503486795
-30.989172609953968
-61.86827229987339
-27.31432038816027
-34.7261459274722
-11.028330003734792
-51.57317116039064

### Building a surrogate

Using the sampled points we build the surrogate, the steps are analogous to the 1-dimensional case.

my_linear_ND = LinearSurrogate(xys, zs,  lower_bound, upper_bound)
(::LinearSurrogate{Vector{Tuple{Float64, Float64}}, Vector{Float64}, Vector{Float64}, Vector{Float64}, Vector{Float64}}) (generic function with 1 method)

### Optimizing

With our surrogate we can now search for the minimas of the function.

Notice how the new sampled points, which were created during the optimization process, are appended to the xys array. This is why its size changes.

size(xys)
(50,)
surrogate_optimize(egg, SRBF(), lower_bound, upper_bound, my_linear_ND, SobolSample(), maxiters=10)
((4.8828125, 14.8828125), -65.68822757708949)
size(xys)
(50,)