- 易迪拓培训,专注于微波、射频、天线设计工程师的培养
HFSS15: 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:
The user has modified the MATLAB script to not terminate MATLAB after the optimization.
A syntax error or some other has occurred.
The user has added some other code which runs after the optimization has completed.
System Requirements
In order to use MATLAB to perform optimizations from your application:
A version of MATLAB must be installed on your system.
The computing platform (i.e. 32/64 bit) of MATLAB MUST match the platform of the ANSYS application you are using it with.
You must have the MATLAB Optimization Toolkit installed.
Specifying the MATLAB Location
The Tools>General Options:Miscellaneous tab 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 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 dropdown list. If you select MATLAB as the optimizer, the Setup Optimization dialog displays a Setup... button.
Select Setup.. to open the MATLAB Options dialog.
Thee 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:
Optimization algorithm: displays only the line of code invoking the actual optimization function. By changing this line, the user can use a different MATLAB function for optimization. By default we use fmincon() which is a derivative based constrained optimization. By modifying this line, the user could replace the fmincon() call with fminsearch() to use an unconstrained pattern searching optimizer or another optimization function. See the MATLAB documentation for details about available optimization functions.
Options: Each optimization function contains a multitude of options and parameters which are set in the MATLAB script prior to actually calling the optimization function. By modifying these options, the optimization can be customized as desired. For instance, options can be set for fmincon() to specify the algorithm that it uses internally. See the MATLAB documentation for details about options available for each optimization function.
Full script template: This choice displays the full optimization script that is passed to MATLAB.
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:
Change the optimization algorithm (e.g. call fminsearch instead of fmincon)
Change the parameters/options of the optimization algorithm (see the MATLAB documentation for details).
Specify a plot function to provide graphical output during optimization.
Specify a user defined output function to be called at completion or per iteration.
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.
$ANS_VARIABLE_LIST: | list of variables we are optimizing |
$ANS_STARTING_POINT: | vector of starting values of variables used in the optimization |
$ANS_MAXITERATIONS: | maximum number of iterations specified in optimization setup |
$ANS_MINVAL: | vector of minimum values from optimization setup |
$ANS_MAXVAL: | vector of maximum values from optimization setup |
$ANS_MINSTEP: | vector of minimum step sizes from optimization setup |
$ANS_MAXSTEP: | vector of maximum step sizes from optimization setup |
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_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