# TOMLAB Multi Layer Optimization

This page is part of the TOMLAB Manual. See TOMLAB Manual. |

TOMLAB supports optimization with any level of recursion assuming that a MEX interface is not permanently allocated in memory. For example, SNOPT cannot use SNOPT as a sub-solver however, it is possible with a solver using a QP MEX solver internally as the MEX solver finishes on each run. The sub optimization problems can be defined as constraints or objectives.

In order to use a sub-solver, a special driver routine *tomSolve *is needed. The universal driver routine *tomRun *goes through several steps before initializing the solution process, so only a pre-check on the sub problem should be done.

The following steps should be followed when setting up a multi layer optimization problem.

- Create a TOMLAB problem using the appropriate assign routine. For example,
*Prob = conAssign(...)*

- Create the sub problem in the same manner,
*Prob2 = conAssign(...)*. Then check that the problem is correctly setup by executing, Prob = ProbCheck(Prob, Solver, solvType, probType);

- The subproblem should be included in the main problem in the 'user' field,
*Prob.user.Prob2 = Prob2*.

- Call the universal driver routine,
*tomRun*.*Result = tomRun('snopt', Prob, 1).*

- In the routine that executes the sub-optimization extract the subproblem,
*Prob2 = Prob.user.Prob2*and supply it to*tomSolve*

- Before calling
*tomSolve*, parameters depending on the decision variables,*x*, from the outer problem should be set in*Prob2*. For example lower and upper bounds as well as user parameters could be modified.

When doing multi layer optimization the user can still define the derivatives for known parts of the problem. *Prob.CheckNaN *should be set to obtain derivatives for subproblems.