# Add Modeling Algorithm

Make sure you are familiar with OO Programming in Matlab before you attempt any of this.

## Model vs Model Builder

It is important to first make the distinction between the model type and the model builder (= modeling algorithm or hyperparameter optimization algorithm). Please first read the Add Model Type page first.

The toolbox comes with a number of modeling algorithms but others can be easily added.

## Available Model Builders

A list of the available model builders can be found in the `src/matlab/modelbuilders`

subdirectory. The most important are:

**OptimizerModelBuilder**: uses any algorithm that is implemented as part of the Optimizer hierarcy. A list of the optimizer configurations used in the default.xml can be found here.**GeneticModelBuilder**: a genetic algorithm is used to search the parameter space. Given an initial population the algorithm uses crossover and mutation operators to search through the parameter space. (requires the Matlab Direct Search Toolbox (GADS) toolbox). Note that the GeneticModelBuilder has two possible population types: custom and doubleVector. Selecting doubleVector will pass the hyperparameters directly to the GADS toolbox. This allows you to use the mutation and crossover operators built-in the GADS toolbox. Using the custom option will pas the models itself to the GADS toolbox. This requires custom crossover and mutation operators for the model type you are using, which should be defined in the ModelFactory class for this model.**ModelBuilder**: this is the base class, this will build a single model each iteration according to the configuration set in the xml file. Thus there is no optimization, but it allows for full manual control**ParetoModelBuilder**: uses the multi-objective NSGA-II algorithm to generate models multi-objectively**EGOModelBuilder**: uses an algorithm based on the Efficient Global Optimization algorithm to search for good model parameters. Basically this uses a nested kriging model to predict where to find the most promising models**RandomModelBuilder**: this is solely useful as a baseline comparison, it simply generates models with random model parameters (within user specified bounds)

## Adding a new model builder

If you would like to add a new optimization algorithm for optimizing the model parameters, you usually will have to do nothing more than add a new optimizer class. Then you can simply plug that optimizer into the OptimizerModelBuilder. Use one of the existing optimizers as an example to guide you.

Alternatively, if you prefer to write your own algorithm from scratch you just have to derive from ModelBuilder and provide the following methods:

- a constructor that reads out the xml configuration and configures the modeling algorithm (MyModelBuilder.m)
- runLoop.m : this is the main loop of your algorithm, it gets the current available samples and builds new models according to your optimization algorithm
- the optimized models are passed to the rest of Toolbox via the
*DefaultFitnessFunction*, which has to be called to in runLoop.m

Once your algorithm is implemented you can then write the necessary factories for the model types you wish to use. See the Add Model Type page for more information. Make sure you think hard about the interfacing with the different model factories. Try to keep the number of methods that model factories need to implement as small as possible.