MATLAB Optimizer

The MATLAB optimizer option lets you pass a script to MATLAB to perform the optimization. When the optimization is analyzed, MATLAB is launched and a script is passed in to MATLAB to perform the optimization. During the optimization, MATLAB will call back into our application to perform the solve and compute the cost. The cost will be reported back to MATLAB, and MATLAB's optimization will determine the next step in the optimization.

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

Running the Optimization

The MATLAB optimization is launched just like any other optimization. The Message Window will display status messages when MATLAB is being launched, and status messages will be generated for each solve that is being performed.

In most cases, MATLAB will terminate when the optimization has been completed. Some reasons why MATLAB would not terminate are:

System Requirements

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

To see if the optimization toolbox is installed, users can type the "ver" command at the command prompt of a running MATLAB instance. For example:

>> ver

-------------------------------------------------------------------------------------------------------

MATLAB Version: 8.1.0.604 (R2023a)

MATLAB License Number: 162684

Operating System: Microsoft Windows 10

Java Version: Java 1.6.0_17-b04 with Sun Microsystems Inc. Java HotSpot(TM) 64-Bit Server VM mixed mode

-------------------------------------------------------------------------------------------------------

MATLAB Version 8.1 (R2023a)

Simulink Version 8.1 (R2023a)

Optimization Toolbox Version 6.3 (R2023a)

To see if the optimization toolbox is licensed, you can use the "license('test','optimization_toolbox')" command at the MATLAB command prompt:

>> license('test','optimization_toolbox')

ans =

1

The answer will be 1 if the MATLAB Optimization Toolbox is licensed or 0 otherwise.

Specifying the MATLAB Location

The Tools> General Options: Miscellaneous group contains a setting for the MATLAB location. This setting must to point to the version of MATLAB to be used for performing the optimization. The platform (32/64 bit or Linux) 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 menu. If you select MATLAB as the optimizer, the Setup Optimization dialog box displays a Setup... button.

Goals tab, Optimizer set to MATLAB.

Select Setup... to open the MATLAB Options dialog box.

MATLAB Options window. Script Section set to Optimization Algorithm.

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

This screen allows you to modify the script that is passed to MATLAB to perform the optimization. The complete script contains all the instructions necessary for MATLAB to connect to our application and perform the optimization, and a lot of that code is unimportant to users. We have addressed this issue by displaying a dropdown to let you view only the portion of code they are interested in 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, users 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, please ensure 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