# Stepwise Bayesian Optimization with mlrMBO

**mlr-org**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

With the release of the new version of mlrMBO we added some minor fixes and added a practical feature called *Human-in-the-loop MBO*.
It enables you to sequentially

- visualize the state of the surrogate model,
- obtain the suggested parameter configuration for the next iteration and
- update the surrogate model with arbitrary evaluations.

In the following we will demonstrate this feature on a simple example.

First we need an objective function we want to optimize.
For this post a simple function will suffice but note that this function could also be an external process as in this mode **mlrMBO** does not need to access the objective function as you will only have to pass the results of the function to **mlrMBO**.

However we still need to define the our search space. In this case we look for a real valued value between -3 and 3. For more hints about how to define ParamSets you can look here or in the help of ParamHelpers.

We also need some initial evaluations to start the optimization.
The design has to be passed as a `data.frame`

with one column for each dimension of the search space and one column `y`

for the outcomes of the objective function.

With these values we can initialize our sequential MBO object.

The `opt.state`

now contains all necessary information for the optimization.
We can even plot it to see how the Gaussian process models the objective function.

In the first panel the *expected improvement* ($EI = E(y_{min}-\hat{y})$) (see Jones et.al.) is plotted over the search space.
The maximum of the *EI* indicates the point that we should evaluate next.
The second panel shows the mean prediction of the surrogate model, which is the Gaussian regression model aka *Kriging* in this example.
The third panel shows the uncertainty prediction of the surrogate.
We can see, that the *EI* is high at points, where the mean prediction is low and/or the uncertainty is high.

To obtain the specific configuration suggested by mlrMBO for the next evaluation of the objective we can run:

We will execute our objective function with the suggested value for `x`

and feed it back to mlrMBO:

The nice thing about the *human-in-the-loop* mode is, that you don’t have to stick to the suggestion.
In other words we can feed the model with values without receiving a proposal.
Let’s assume we have an expert who tells us to evaluate the values $x=-1$ and $x=1$ we can easily do so:

We can also automate the process easily:

*Note:* We suggest to use the normal mlrMBO if you are only doing this as mlrMBO has more advanced logging, termination and handling of errors etc.

Let’s see how the surrogate models the true objective function after having seen seven configurations:

You can convert the `opt.state`

object from this run to a normal mlrMBO result object like this:

*Note:* You can always run the *human-in-the-loop MBO* on `res$final.opt.state`

.

For the curious, let’s see how our original function actually looks like and which points we evaluated during our optimization:

We can see, that we got pretty close to the global optimum and that the surrogate in the previous plot models the objective quite accurate.

For more in-depth information look at the Vignette for Human-in-the-loop MBO and check out the other topics of our mlrMBO page.

**leave a comment**for the author, please follow the link and comment on their blog:

**mlr-org**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.