computing objective functions __ writing files for optimization functions (global optimization...
TRANSCRIPT
-
7/31/2019 Computing Objective Functions __ Writing Files for Optimization Functions (Global Optimization Toolbox)
1/2
Comp u t ing Ob jec t i ve Funct ions
On th is page
Objective (Fitness) Functions
Example: Writing a Function File
Example: Writing a Vectorized Function
Gradients and Hessians
Maximizing vs. Minimizing
Objec t ive (F i tness) Func t ions
To use Global Optimization Toolbox functions, you must first write a file (or an anonymous function) that computes the function you want to optimize. This function iscalled an objective function for most solvers or a fitness function for ga. The function should accept a vector whose length is the number of independent variables, and
should return a scalar. For vectorized solvers, the function should accept a matrix (where each row represents one input vector), and return a vector of objectivefunction values. This section shows how to write the fi le.
Examp le : Wr i t ing a Func t ion F i le
The following example shows how to write a file for the function you want to optimize. Suppose that you want to minimize the function
The file that computes this function must accept a vector x of length 2, corresponding to the variables x1 and x2, and return a scalar equal to the value of the function
at x. To write the file, do the following steps:
Select N ew > S cr i p t (Ct r l + N) from the MATLAB Fi le menu. This opens a new file in the editor.1.
In the file, enter the following two lines of code:
function z = my_fun(x)
z = x(1)^2 - 2*x(1)*x(2) + 6*x(1) + 4*x(2)^2 - 3*x(2);
2.
Save the file in a folder on the MATLAB path.3.
To check that the file returns the correct value, enter
my_fun([2 3])
ans =
31
Examp le : Wr i t ing a Vec to r ized Func t ion
The ga and patternsearch solvers optionally compute the objective functions of a collection of vectors in one function call. This method can take less time than
computing the objective functions of the vectors serially. This method is called a vectorized function call.
To compute in vectorized fashion:
Write your objective function to:
Accept a matrix with an arbi trary number of rows
Return the vector of function values of each row
If you have a nonlinear constraint, be sure to write the constraint in a vectorized fashion. For details, see Vectorized Constraints.Set the Vectorized option to 'on' with gaoptimset or psoptimset, or set User func t ion eva lua t ion > Eva lua te ob jec t i ve / f i t ness and cons t ra in t
f u n c t i o n s to vectorized in the Optimization Tool. For patternsearch, also set CompletePoll to 'on'. Be sure to pass the options structure to the solver.
For example, to write the objective function ofExample: Writing a Function File in a vectorized fashion,
function z = my_fun(x)
z = x(:,1).^2 - 2*x(:,1).*x(:,2) + 6*x(:,1) + ...
4*x(:,2).^2 - 3*x(:,2);
To use my_fun as a vectorized objective function for patternsearch:
options = psoptimset('CompletePoll','on','Vectorized','on');
[x fval] = patternsearch(@my_fun,[1 1],[],[],[],[],[],[],...
[],options);
To use my_fun as a vectorized objective function for ga:
options = gaoptimset('Vectorized','on');
[x fval] = ga(@my_fun,2,[],[],[],[],[],[],[],options);
For more information on writing vectorized functions for patternsearch, see Vectorizing the Objective and Constraint Functions. For more information on writing
vectorized functions for ga, see Vectorizing the Fitness Function.
Grad ien ts and Hess ians
If you use GlobalSearch or MultiStart, your objective function can return derivatives (gradient, Jacobian, or Hessian). For details on how to include this syntax in
your objective function, see Writing Objective Functions in Optimization Toolbox documentation. Use optimset to set options so that your solver uses the derivative
information:
Loca l So lve r = fm incon , fm inunc
Co n d i t io n Op t i o n Set t i ng
Objective function contains gradient 'GradObj' = 'on'
Objective function contains Hessian 'Hessian' = 'on'
Constraint function contains gradient 'GradConstr' = 'on'
Calculate Hessians of Lagrangian in an extra function 'Hessian' = 'on', 'HessFcn' = function handle
For more information about Hessians for fmincon, see Hessian.
Loca l So lve r = l sqcurve f i t , l sqnon l in
Co n d i t io n Op t io n Set t in g
Objective function contains Jacobian 'Jacobian' = 'on'
mputing Objective Functions :: Writing Files for Optimization Functi... http://www.mathworks.com/help/toolbox/gads/brdvu8r.html
2 7/25/2012 3:07 PM
-
7/31/2019 Computing Objective Functions __ Writing Files for Optimization Functions (Global Optimization Toolbox)
2/2
Free Op t im iza t ion
I n te rac t i ve Ki t
Learn how to use optimization tosolve systems of equations, fitmodels to data, or optimizesystem performance.
Get free kit
Tr ia ls Ava i lab le
Try the latest version ofoptimization products.
Get trial software
Maxim iz ing vs . M in im iz ing
Global Optimization Toolbox optimization functions minimize the objective or fitness function. That is, they solve problems of the form
If you want to maximize f(x), minimize f(x), because the point at which the minimum of f(x) occurs is the same as the point at which the maximum off(x) occurs.
For example, suppose you want to maximize the function
Write your function file to compute
and minimize g(x).
1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS
mputing Objective Functions :: Writing Files for Optimization Functi... http://www.mathworks.com/help/toolbox/gads/brdvu8r.html
2 7/25/2012 3:07 PM