# Difference between revisions of "Add Modeling Algorithm"

(→FixedModelBuilder) |
|||

Line 1: | Line 1: | ||

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

− | == Model vs |
+ | == Model vs Model Builder == |

− | It is |
+ | 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. |
||

− | The model type is the kind of model. For example: a polynomial, a radial basis function, a reduced order model for a particular application, etc. Each model type is fully characterized by a number of parameters. For example: a polynomial is determined by its degree n and the n+1 coefficients; a neural network is determined by the number of neurons in each layer and the connection weights between them. So different model types have different numbers of parameters that need to be set. This is where the difficulty usually lies. Setting correct values requires finding the right balance between the bias/variance tradeoff and in many cases this is more of an art than a science. |
||

+ | == Available Model Builders == |
||

− | Finding the best model parameters given the data can be seen as a (constrained) optimization problem over the parameter space. This is where the modeling algorithm, comes in. The modeling algorithm is a particular optimization/search algorithm over the model parameter space. The toolbox comes with a number of modeling algorithms but others can be easily added. Adding support for a new model type therefore boils down to implementing the model type itself and providing an implementation of an optimization algorithm. How this is done in code is discussed below. |
||

+ | A list of the available model builders can be found in the <code>src/matlab/modelbuilders</code> subdirectory. The most important are: |
||

− | == Adding a new modeling algorithm == |
||

− | Some of the modeling algorithms (= hyperparameter optimization algorithms) available in the SUMO-Toolbox are: |
||

+ | * '''OptimizerModelBuilder''': uses any algorithm that is implemented as part of the [[Optimizer]] hierarcy. |
||

− | * '''SequentialModelBuilder''': a kind of hillclimber, it keeps a sliding window of the past n models and builds new models, one by one based on this history window. |
||

+ | * '''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 [http://www.mathworks.com/products/gads/ Matlab Direct Search Toolbox (GADS) toolbox]). Note that the GeneticModelBuilder has two possible population types: custom and doubleVector. |
||

− | * '''BatchModelBuilder''': here you start with an initial batch of n models and each iteration that batch is replaced by a new batch. |
||

+ | * '''AdaptiveModelBuilder''': 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 |
||

− | * '''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) |
||

+ | * '''ParetoModelBuilder''': uses the [[Multi-Objective Modeling| multi-objective NSGA-II algorithm]] to generate models multi-objectively |
||

− | * '''PatternSearchModelBuilder''': uses a pattern search algorithm to optimize the model parameters (requires the Matlab Direct Search Toolbox (GADS) toolbox) |
||

+ | * '''EGOModelBuilder''': uses an algorithm based on the [http://portal.acm.org/citation.cfm?id=596218 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 |
||

− | * '''OptimToolboxModelBuilder''': Uses the algorithms from the Matlab Optimization toolbox |
||

+ | * '''RandomModelBuilder''': this is solely useful as a baseline comparison, it simply generates models with random model parameters (within user specified bounds) |
||

− | * '''PSOModelBuilde''': uses particle swarm optimization |
||

− | * '''OptimizationModelBuilder''': can work with any algorithm implemented as part of the [[Optimizer]] hierarcy. |
||

− | * '''SimAnnealingModelBuilder''': Uses the simulated annealing algorithms from the Matlab Direct Search Toolbox (GADS) toolbox |
||

− | * '''RandomModelBuilder''': this is solely useful as a baseline comparison search algorithm. |
||

− | * '''FixedModelBuilder''': this will build whatever model you tell it to, it uses the same fixed set of model parameters for every model |
||

+ | == Adding a new model builder == |
||

− | The algorithms themselves are implemented in the src/matlab/modelbuilder directory (always check this directory for the most up to date list). If you would like to add a new optimization algorithm for optimizing the model parameters, you need to create a new ModelBuilder class in src/matlab/modelbuilders (alternatively you could just [[add a new optimizer]] and use the OptimizerModelBuilder). This new class should derive from AdaptiveModelBuilder and provide the following methods: |
||

+ | |||

+ | 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 AdaptiveModelBuilder and provide the following methods: |
||

* a constructor that reads out the xml configuration and configures the modeling algorithm (MyModelBuilder.m) |
* 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 |
* 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 |
||

− | Make sure you think hard about the interfacing with the different model |
+ | 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. |

− | |||

− | Once your algorithm is implemented you can then write the necessary interfaces for the model types you wish to use. For the gradient descent example, say you wanted to use RBF functions, you would implement a RBFGradientDescentInterface class. See below for more information. |
||

− | |||

− | == Adding a modeling algorithm implementation == |
||

− | In order to implement one or more of the modeling algorithms for your specific model type you need to provide an MyModelSomeAlgorithmInterface class (eg: MyModelPSOInterface). In this subclass you will need to provide the methods that allow the modeling algorithms to interact with your type. However, in most cases this is just one simple method and there is no need to create a new class. You can just add the method to your MyModelInterface class. |
||

− | |||

− | The cases for which this is not the case (i.e., it is best to create a new class instead of adding a method to the interface base class) are (See the existing implementations for details): |
||

− | |||

− | === GeneticModelBuilder === |
||

− | * a constructor (eg: MyModelGeneticInterface) |
||

− | * a population creation function |
||

− | * a mutation operator function |
||

− | * a crossover operator function |
||

− | |||

− | NB: you also have to derive from GeneticInterface. |
||

− | |||

− | === SequentialModelBuilder === |
||

− | * a constructor: that reads in the configuration (eg. MyModelSequentialInterface.m) |
||

− | * create.m: based on a history of the past n models, return the next model in the search |
||

− | |||

− | NB: this modelbuilder type is deprecated, try to avoid using it |
||

− | |||

− | === BatchModelBuilder === |
||

− | * a constructor |
||

− | * createBatch: given a batch of models, return a new, improved batch |
||

− | |||

− | NB: this modelbuilder is deprecated, try to avoid using it |
||

− | |||

− | |||

− | |||

− | For the following model builders it is not really necessary to create a new class (though nothing stops you from doing so). You simply have to provide the specific methods in your model interface baseclass (MyModelInterface). In that case your base class must also have the following methods: |
||

− | |||

− | * a createModelFromIndividual method: given a parameter vector it should return the corresponding model object |
||

− | * a createInitialModels method: return an one or more models (or parameter vectors) to use as the starting point(s) of the hyperparameter optimization |
||

− | * a getBounds method: specifies the bounds on the parameter values |
||

− | |||

− | === PatternSearchModelBuilder === |
||

− | * No further methods needed |
||

− | |||

− | === OptimToolboxModelBuilder === |
||

− | * No further methods needed |
||

− | |||

− | === SimAnnealingModelBuilder === |
||

− | * No further methods needed |
||

− | |||

− | === RandomModelBuilder === |
||

− | * a createRandomModel method that takes no argument and returns a model with random values for its model parameters. |
||

− | |||

− | This is useful to see how much better your optimization algorithm is compared to a random search. |
||

− | |||

− | === FixedModelBuilder === |
||

− | * a createFixedModel method that takes no argument and returns a model with a fixed set of parameters. This allows you to set the model parameters manually. |
||

− | |||

− | == Using your model builder == |
||

− | Having written the implementations you then have to update your xml configuration file to use you new model type, run 'go'. |

## Revision as of 18:13, 8 February 2009

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.**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.**AdaptiveModelBuilder**: 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 AdaptiveModelBuilder 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

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.