Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/SciML/Surrogates.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
ludoro committed Jun 29, 2020
2 parents c133ae2 + c1557f9 commit c36b78a
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ GLM = "1.3"
GaussianMixtures = "0.3"
IterativeSolvers = "0.8"
LatinHypercubeSampling = "1.2"
PolyChaos = "0.2"
Requires = "0.5, 1.0"
Sobol = "1.3"
Stheno = "0.6"
XGBoost = "0.4"
XGBoost = "0.4, 1.1"
julia = "1"

[extras]
Expand Down
1 change: 1 addition & 0 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ makedocs(
"Basics" => "tutorials.md",
"RandomForestSurrogate" => "randomforest.md",
"Radials" => "radials.md",
"LinearSurrogate" => "LinearSurrogate.md",
"Kriging" => "kriging.md"
]
"Contributing" => "contributing.md"
Expand Down
67 changes: 67 additions & 0 deletions docs/src/LinearSurrogate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
## 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 **Eggholder Function**.

$f(x) = - x_{1} \sin\left(\sqrt{\lvert{x_{1} - x_{2} -47}\rvert}\right) - \left(x_{2} + 47\right) \sin\left(\sqrt{\left|{\frac{1}{2} x_{1} + x_{2} + 47}\right|}\right)$.

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

```@example linear_surrogate1D
using Surrogates
using Plots
default()
```

### Sampling

We choose to sample f in 6 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.

```@example linear_surrogate1D
# http://infinity77.net/global_optimization/test_functions.html#test-functions-index.
x_1 = 1
x_2 = 2
term1 = -(x_2+47) * sin(sqrt(abs(x_2+x_1/2+47)))
term2 = -x_1 * sin(sqrt(abs(x_1-(x_2+47))))
f(x) = term1 + term2
n_samples = 6
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.

```@example linear_surrogate1D
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)
```

Now, we will simply plot `linear_surrogate`:

```@example linear_surrogate1D
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.

```@example linear_surrogate1D
@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))
```
4 changes: 2 additions & 2 deletions src/Radials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ end)
Constructor for RadialBasis surrogate.
"""
function RadialBasis(x, y, lb::Number, ub::Number; rad::RadialFunction=linearRadial, scale_factor::Real=1.0, sparse = false)
q = rad.q
q = rad.q + 1
phi = rad.phi
coeff = _calc_coeffs(x, y, lb, ub, phi, q,scale_factor, sparse)
return RadialBasis(phi, q, x, y, lb, ub, coeff,scale_factor,sparse)
Expand All @@ -44,7 +44,7 @@ RadialBasis(x,y,lb,ub,rad::RadialFunction, scale_factor::Float = 1.0)
Constructor for RadialBasis surrogate
"""
function RadialBasis(x, y, lb, ub; rad::RadialFunction = linearRadial, scale_factor::Real=1.0, sparse = false)
q = rad.q
q = rad.q + 1
phi = rad.phi
coeff = _calc_coeffs(x, y, lb, ub, phi, q, scale_factor, sparse)
return RadialBasis(phi, q, x, y, lb, ub, coeff,scale_factor, sparse)
Expand Down

0 comments on commit c36b78a

Please sign in to comment.