# Motion Optimizer

Adtile's **Motion Optimizer** is a web-application, written completely in JavaScript, to recursively play a set of motion cassettes and optimize new motion algorithms. It has been extensively used to develop, optimize, and test features in the MotionStack.

The optimization process is via a genetic algorithm. The user provides a model algorithm, set of parameters for the model, objective function, and training data. The Motion Optimizer finds the parameter values that give the best objective function value based on the model results for the given set of training data.

The model algorithm is designed to accept a set of parameters as input and return a set of results as output. Each motion cassette in the training data must have a corresponding set of expected results for the model algorithm. The optimizer will compare the actual model results to the expected model results and give a fitness score.

The **Motion Optimizer** relies on the **Motion VCR** to play motion cassettes.

Contact the **Adtile** team with a request for access to the **Motion Optimizer**.

## How to Run the Motion Optimizer

- Setup the Model.
- Select the Model.
- Run the file
`index.html`

in a web-browser. - Click one of the two available buttons.
- Click the
- The parameter values for the current best score.
`member<generationIndex>_s<score>.txt`

- A list of scores for each cassette in the training data.
`member<generationIndex>_s<score>_output.txt`

button to run the genetic algorithm until a perfect score or the generation threshold is satisfied. When a new best score is found, two files are output. - The parameter values for the current best score.
- Click the
- No parameters are set by the genetic algorithm. It is assumed that default parameter values are defined in the model.
- Each cassette output is saved in a file named
`Test_<cassettename>.txt`

in the directory`motion-vcr/cassettes/`

.

button to play each cassette once through the model and output each cassette's event data and score.

- Click the

## Setup the Model

A model is referred to as everything that is needed to run the particular use-case. All models are stored in the `./opimizer/models/`

directory. There is a `model-template`

folder in this directory that can be used to create a new model. Copy and paste `model-template`

in the `./opimizer/models/`

directory and give it a unique name for your model (e.g. `newModel`

). Each model has:

### Model Algorithm

Define the model algorithm in file `model.js`

. The model algorithm defines the actual method being tested or optimized, it's input parameters and output results, the output attributes to store for each event, and how to use the results to calculate the objective function.

- Define the model algorithm in the function
`constructModel`

. This is the actual method to be tested or optimized.

```
Model.prototype.constructModel = function(params) {
// Set design variables based on params.
return function(event) {
// calculate results.
return results;
};
}
```

- Process and save all model algorithm results in function
`receiveEvent`

.- Save result attributes by pushing into array
`this.events`

. This will store all output fro the model algorithm. - Do any other charting methods or desired processing (optional). Sometimes visual aids are needed to monitor the optimization process.

- Save result attributes by pushing into array
- Calculate the objective function in function
`calculateObjectiveFunction`

. This function runs once after each cassette. It compares the actual model results to the expected model results and gives a fitness score.

### Model Parameters

Define the model parameter to be optimized in file `parameters.js`

in the JavaScript object `designVariables`

. The object attributes are the parameter names. Each parameter has:

`lb`

**{Number}**: The lower bound of the search range.`ub`

**{Number}**: The upper bound of the search range.`real`

**{Boolean}**: True means the parameter is a real number, false means it is an integer.

```
Model.prototype.designVariables = {
"parameter1":{"lb":Number,"ub":Number,"real":Boolean},
"parameter2":{"lb":Number,"ub":Number,"real":Boolean},
"parameter2":{"lb":Number,"ub":Number,"real":Boolean}
};
```

Make sure that all parameter names in `designVariables`

match the attributes in `params`

used in function `modelAlgorithm`

.

### Model Training Data

Place all the model's motion cassettes in the subdirectory `trainingData`

. Then create the list of training data in file `trainingData.js`

as a JSON object.

- Hard-code each cassette as a JavaScript object with two attributes.
`name`

**{String}**– is the name of the cassette file without the .extension.`exactSolution`

**{Array}**– the exact expected output from the MotionStack feature the cassette was designed for.

- Group cassettes in JavaScript arrays to make it easier to find a particular type of cassette.
- For example, you can group cassettes by their exactSolution, or the design goal.

```
TrainingData.cassettesGroup1 = [
{ name: "cassetteFileName1", exactSolution: [<expectedResults>] },
{ name: "cassetteFileName2", exactSolution: [<expectedResults>] },
{ name: "cassetteFileName3", exactSolution: [<expectedResults>] },
{ name: "cassetteFileName4", exactSolution: [<expectedResults>] }
];
```

- Add the cassette groups in an array
`cassetteLists`

. The optimizer will loop over all cassette groups in this array.- Push each cassette group you want to use onto the
`cassetteLists`

array in the function`createTrainingDataLists`

.- Later to ignore a group, remove it from the
`cassetteLists`

array.

- Later to ignore a group, remove it from the

- Push each cassette group you want to use onto the

```
Model.prototype.cassetteLists = [
TrainingData.cassettesGroup1,
TrainingData.cassettesGroup2,
TrainingData.cassettesGroup3
];
```

### Genetic Algorithm Parameters

In the `model.js`

file, defines the genetic algorithm parameters.

`mutationRate`

**{Number}**: Mutation rate is the probability, between 0 and 1, that a chromosome will mutate. When a chromosome mutates, a random property of that chromosome changes value. (Default: 0.7)`elitism`

**{Number}**: Elitism rate is the fraction, between 0 and 1, of the strongest chromosomes that will survive to the next generation. (Default: 0.2)`populationSize`

**{Number}**: Population size of each generation. (Default: 100)`noImprovementThreshold`

**{Number}**: Improvement Threshold is the number of generations with no objective function score improvement. When the threshold is reached, then the genetic algorithm is terminated. (Default: 100)

```
Model.prototype.mutationRate = 0.7;
Model.prototype.elitism = 0.2;
Model.prototype.populationSize = 100;
Model.prototype.noImprovementThreshold = 100;
```

## Select the Model

In the `index.html`

change the src path for the new model.

```
<script src="./models/newModel/model.js"></script>
<script src="./models/newModel/trainingData.js"></script>
<script src="./models/newModel/parameters.js"></script>
```