Compute Objective Functions

Objective (Fitness) Functions

To use Global Optimization Toolbox functions, first write a file (or an anonymous function) that computes the function you want to optimize. This is called an objective function for most solvers, or fitness function for ga. The function should accept a vector, whose length is the number of independent variables, and return a scalar. For gamultiobj, the function should return a row vector of objective function values. For vectorized solvers, the function should accept a matrix, where each row represents one input vector, and return a vector of objective function values. This section shows how to write the file.

Write a Function File

This example shows how to write a file for the function you want to optimize. Suppose that you want to minimize the function

f(x)=x122x1x2+6x1+4x223x2.

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.

  1. Select New > Script (Ctrl+N) from the MATLAB® File menu. A new file opens in the editor.

  2. 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);
  3. Save the file in a folder on the MATLAB path.

Check that the file returns the correct value.

my_fun([2 3])

ans =
   31

For gamultiobj, suppose you have three objectives. Your objective function returns a three-element vector consisting of the three objective function values:

function z = my_fun(x)
z = zeros(1,3); % allocate output
z(1) = x(1)^2 - 2*x(1)*x(2) + 6*x(1) + 4*x(2)^2 - 3*x(2);
z(2) = x(1)*x(2) + cos(3*x(2)/(2+x(1)));
z(3) = tanh(x(1) + x(2));

Write a Vectorized Function

The ga, gamultiobj, 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 arbitrary number of rows.

    • Return the vector of function values of each row.

    • For gamultiobj, return a matrix, where each row contains the objective function values of the corresponding input matrix 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 function evaluation > Evaluate objective/fitness and constraint functions to vectorized in the Optimization app. For patternsearch, also set CompletePoll to 'on'. Be sure to pass the options structure to the solver.

For example, to write the objective function of Write 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 gamultiobj,

function z = my_fun(x)
z = zeros(size(x,1),3); % allocate output
z(:,1) = x(:,1).^2 - 2*x(:,1).*x(:,2) + 6*x(:,1) + ...
   4*x(:,2).^2 - 3*x(:,2);
z(:,2) = x(:,1).*x(:,2) + cos(3*x(:,2)./(2+x(:,1)));
z(:,3) = tanh(x(:,1) + x(:,2));

To use my_fun as a vectorized objective function for gamultiobj:

options = gaoptimset('Vectorized','on');
[x fval] = gamultiobj(@my_fun,2,[],[],[],[],[],[],options);

For more information on writing vectorized functions for patternsearch, see Vectorize the Objective and Constraint Functions. For more information on writing vectorized functions for ga, see Vectorize the Fitness Function.

Gradients and Hessians

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 the Optimization Toolbox™ documentation. Use optimoptions to set options so that your solver uses the derivative information:

Local Solver = fmincon, fminunc

ConditionOption Setting
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.

Local Solver = lsqcurvefit, lsqnonlin

ConditionOption Setting
Objective function contains Jacobian'Jacobian' = 'on'

Maximizing vs. Minimizing

Global Optimization Toolbox optimization functions minimize the objective or fitness function. That is, they solve problems of the form

minxf(x).

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 of f(x) occurs.

For example, suppose you want to maximize the function

f(x)=x122x1x2+6x1+4x223x2.

Write your function file to compute

g(x)=f(x)=x12+2x1x26x14x22+3x2,

and minimize g(x).

Was this topic helpful?