MATLAB optimizer

The MATLAB optimizer option lets you pass a script to MATLAB to perform the optimization. When optimization starts, MATLAB launches and a script passes in to MATLAB to perform the optimization. During optimization, MATLAB calls back into Twin Builder to perform the solve and compute the cost. The cost is reported back to MATLAB, and MATLAB's optimization then determines the next step in the optimization.

The optimization script is specified as part of the optimization setup. By modifying the optimization script, you can change optimization parameters and optimization method as well as use the full power of MATLAB.

Running the Optimization

A MATLAB optimization launches like any other optimization. The Message Manager pane displays status messages when MATLAB launches, and status messages generate for each solve being performed.

In most cases, MATLAB terminates when the optimization completes. Some reasons why MATLAB may fail to terminate include:

System Requirements

In order to use MATLAB to perform optimizations from your application:

Specifying the MATLAB Location

The Miscellaneous tab at Tools > Options > General Options contains a setting for the MATLAB application location. This setting must point to the version of MATLAB that will perform the optimization. The platform (64-bit) of the specified version of MATLAB must match the platform of this application.

MATLAB Optimization Setup

MATLAB optimization starts by creating an optimization and selecting MATLAB from the optimizer drop-down list. If you select MATLAB as the optimizer, the Setup Optimization dialog box displays a Setup button.

Click Setup to open the MATLAB Options dialog box.

The upper text panel is informative. The Script Section drop-down list lets you select a lower panel display for Optimization algorithm, Options, or the Full script template.

This panel lets you modify the script passed to MATLAB to perform the optimization. The complete script contains all the instructions necessary for MATLAB to connect to Twin Builder and perform the optimization. The drop-down selection lets you view only the portion of code of interest without having to view the full script. The choices are:

The initial Script Section display for the Optimization algorithm shows the following:

% invoke optimization

[x,fval,exitflag,output] = fmincon(wrapperfunc, startingpoint, [], [], [], [], $ANS_MINVAL, $ANS_MAXVAL, nlcon, options)

The initial Script Section Options display shows the following:

% customers can add their own options below

options = optimset(options, 'display', 'iter')

options = optimset(options, 'Algorithm', 'interior-point')

% options = optimset(options, 'PlotFcns', @optimplotfval)

You can modify the script to extend and customize the optimization to your needs. You must ensure that the script follows MATLAB syntax. For instance, by modifying the optimization script you can:

Symbols

When modifying the MATLAB code, you can use symbols to represent values from the optimization setup. The symbols and their definitions are listed below.



Note:

The linear constraints as generated for MATLAB have the form [A][x] <= [B], where [A] is the coefficient matrix, [x] is the variable list matrix (column vector), and [B] is the bounds matrix (column vector).

Note:

While modifying the script, make sure that the script follows MATLAB syntax.

MATLAB Optimization Script Template

The script template shown in the Script Section is as follows:

% make sure platform matches

if strcmp(computer, '$ANS_EXPECTED_PLATFORM') ~= 1

h = msgbox('32/64 platform does not match calling application, exiting')

uiwait(h)

exit

end

% add installation dir to search path so .mex file can be found

originalpath = addpath('$ANS_EXEDIR')

% connect back to opticomengine

callbackinterface = optimex('connect', '$ANS_CONNECTIONSTRING')

% set up optimization

% variables are: $ANS_VARIABLELIST

startingpoint = $ANS_STARTINGPOINT

options = optimset('MaxIter', $ANS_MAXITERATIONS)

iterationCallbackWrapper = @(x, optimValues, state) optimex('notifyiterationcomplete', callbackinterface, x, optimValues.fval, state)

options = optimset(options, 'OutputFcn', iterationCallbackWrapper)

% halt execution so debugger can be attached

% h = msgbox('attach debugger if desired')

% uiwait(h)

% attributes that user can pass to optimization algorithm

% variables are: $ANS_VARIABLELIST

% this is the objective function which returns cost

wrapperfunc = @(x)optimex('eval', callbackinterface, x)

% this is our non linear constraint function, returns no constraints

returnempty = @(x)[];

nlcon = @(x) deal(returnempty(x), returnempty(x));

% DO NOT EDIT THIS LINE - START OPTIONS SECTION

% customers can add their own options below

options = optimset(options, 'display', 'iter')

options = optimset(options, 'Algorithm', 'interior-point')

% options = optimset(options, 'PlotFcns', @optimplotfval)

% DO NOT EDIT THIS LINE - END OPTIONS SECTION

% DO NOT EDIT THIS LINE - START OPTIMIZATION ALGO SECTION

% invoke optimization

[x,fval,exitflag,output] = fmincon(wrapperfunc, startingpoint, $ANS_A_MATRIX, $ANS_B_MATRIX, [], [], $ANS_MINVAL, $ANS_MAXVAL, nlcon, options)

% DO NOT EDIT THIS LINE - END OPTIMIZATION ALGO SECTION

% write exit message to Ansoft message window (warning=0,error=1,info=2)

optimex('postansoftmessage', callbackinterface, 2, output.message)

% notify opticomengine that optimization is finished

optimex('optimizationfinished', callbackinterface, exitflag)

% restore original path

path = originalpath

% note: comment below line if you want MATLAB to remain

% running after optimization

exit

Related Topics

Optimization Setup for the MATLAB Optimizer

General Options: Miscellaneous