# Customized Optimization Algorithm

For now, MotionSolve only supports the SLSQP optimizer in SciPy. However, you might want to use your own algorithm when 1) A simpler and faster algorithm is enough for a simple problem; 2) Another algorithm fits your need better than SLSQP.

MotionSolve is designed so that you can use any gradient-based or gradient-free algorithm with little effort. The following example shows how you can use ‘L-BFGS-B’ in SciPy for optimization in just three steps.

## Step 1: Create model and add the optimizer.

>>> # Create model
>>> m = Model('test_L_BFGS_B')
...
>>> resp1 = RMS2(...)
>>> resp2 = RMS2(...)

>>> # Define the optimizer
>>> opt = Optimizer(
objective = [resp1, resp2],
weight    = [1.0, 1.0],
type      = 'STATIC',
dsa       = 'AUTO',
accuracy  = 1e-5
)

## Step 2: Get cost and sensitivity method from the optimizer.

We need to get cost and the sensitivity method from the optimizer. If you’re trying to use a gradient-free method, then only the cost method is needed.
>>> cost = opt.cost
>>> sens = opt.sensitivity

## Step 3: Call ‘L-BFGS-B’ method in scipy.minimize.

The last step is to call your own algorithm and run the optimization.
>>> result = scipy.minimize(
method = 'L-BFGS-B',
fun    = cost,
x0     = x0
)`

If you model is correct and well constrained, you should be able to get the optimal value. Notice that the output file will not be saved for the intermediate result if you use your own algorithm, making the debugging process intimidating. It is recommended to check your model by running a few iterations in SLSQP before switching to your own.