Main Content

Performing a Multiobjective Optimization Using the Genetic Algorithm

This example shows how to perform a multiobjective optimization using multiobjective genetic algorithm functiongamultiobjin Global Optimization Toolbox.

Simple Multiobjective Optimization Problem

gamultiobjcan be used to solve multiobjective optimization problem in several variables. Here we want to minimize two objectives, each having one decision variable.

min F(x) = [objective1(x); objective2(x)] x
where, objective1(x) = (x+2)^2 - 10, and objective2(x) = (x-2)^2 + 20
% Plot two objective functions on the same axisx = -10:0.5:10; f1 = (x+2).^2 - 10; f2 = (x-2).^2 + 20; plot(x,f1); holdon; plot(x,f2,'r'); gridon; title('Plot of objectives ''(x+2)^2 - 10'' and ''(x-2)^2 + 20''');

The two objectives have their minima atx = -2andx = +2respectively. However, in a multiobjective problem,x = -2,x = 2, and any solution in the range-2 <= x <= 2is equally optimal. There is no single solution to this multiobjective problem. The goal of the multiobjective genetic algorithm is to find a set of solutions in that range (ideally with a good spread). The set of solutions is also known as a Pareto front. All solutions on the Pareto front are optimal.

Coding the Fitness Function

We create a MATLAB® file namedsimple_multiobjective.m:

function y = simple_multiobjective(x) y(1) = (x+2)^2 - 10; y(2) = (x-2)^2 + 20;

The Genetic Algorithm solver assumes the fitness function will take one inputx, wherexis a row vector with as many elements as the number of variables in the problem. The fitness function computes the value of each objective function and returns these values in a single vector outputy.

Minimizing Usinggamultiobj

To use thegamultiobjfunction, we need to provide at least two input arguments, a fitness function, and the number of variables in the problem. The first two output arguments returned bygamultiobjareX, the points on Pareto front, andFVAL, the objective function values at the valuesX. A third output argument,exitFlag, tells you the reason whygamultiobjstopped. A fourth argument,OUTPUT, contains information about the performance of the solver.gamultiobjcan also return a fifth argument,POPULATION, that contains the population whengamultiobjterminated and a sixth argument,SCORE, that contains the function values of all objectives forPOPULATIONwhengamultiobjterminated.

FitnessFunction = @simple_multiobjective; numberOfVariables = 1; [x,fval] = gamultiobj(FitnessFunction,numberOfVariables);
Optimization terminated: maximum number of generations exceeded.

TheXreturned by the solver is a matrix in which each row is the point on the Pareto front for the objective functions. TheFVALis a matrix in which each row contains the value of the objective functions evaluated at the corresponding point inX.

size(x) size(fval)
ans = 18 1 ans = 18 2

Constrained Multiobjective Optimization Problem

gamultiobjcan handle optimization problems with linear inequality, equality, and simple bound constraints. Here we want to add bound constraints on simple multiobjective problem solved previously.

min F(x) = [objective1(x); objective2(x)] x
subject to -1.5 <= x <= 0 (bound constraints)
where, objective1(x) = (x+2)^2 - 10, and objective2(x) = (x-2)^2 + 20

gamultiobjaccepts linear inequality constraints in the formA*x <= band linear equality constraints in the formAeq*x = beqand bound constraints in the formlb <= x <= ub. We passAandAeqas matrices andb,beq,lb, andubas vectors. Since we have no linear constraints in this example, we pass[]for those inputs.

A = []; b = []; Aeq = []; beq = []; lb = -1.5; ub = 0; x = gamultiobj(FitnessFunction,numberOfVariables,A,b,Aeq,beq,lb,ub);
Optimization terminated: maximum number of generations exceeded.

All solutions inX(each row) will satisfy all linear and bound constraints within the tolerance specified inoptions.ConstraintTolerance. However, if you use your own crossover or mutation function, ensure that the new individuals are feasible with respect to linear and simple bound constraints.

Adding Visualization

gamultiobjcan accept one or more plot functions through the options argument. This feature is useful for visualizing the performance of the solver at run time. Plot functions can be selected usingoptimoptions.

Here we useoptimoptionsto select two plot functions. The first plot function isgaplotpareto, which plots the Pareto front (limited to any three objectives) at every generation. The second plot function isgaplotscorediversity, which plots the score diversity for each objective. The options are passed as the last argument to the solver.

options = optimoptions(@gamultiobj,'PlotFcn',{@gaplotpareto,@gaplotscorediversity}); gamultiobj(FitnessFunction,numberOfVariables,[],[],[],[],lb,ub,options);
Optimization terminated: maximum number of generations exceeded.

Vectorizing Your Fitness Function

Consider the previous fitness functions again:

objective1(x) = (x+2)^2 - 10, and objective2(x) = (x-2)^2 + 20

By default, thegamultiobjsolver only passes in one point at a time to the fitness function. However, if the fitness function is vectorized to accept a set of points and returns a set of function values you can speed up your solution.

For example, if the solver needs to evaluate five points in one call to this fitness function, then it will call the function with a matrix of size 5-by-1, i.e., 5 rows and 1 column (recall that 1 is the number of variables).

Create a MATLAB file calledvectorized_multiobjective.m:

function scores = vectorized_multiobjective(pop) popSize = size(pop,1); % Population size numObj = 2; % Number of objectives % initialize scores scores = zeros(popSize, numObj); % Compute first objective scores(:,1) = (pop + 2).^2 - 10; % Compute second objective scores(:,2) = (pop - 2).^2 + 20;

This vectorized version of the fitness function takes a matrixpopwith an arbitrary number of points, the rows ofpop, and returns a matrix of sizepopulationSize-by-numberOfObjectives.

We need to specify that the fitness function is vectorized using the options created usingoptimoptions. The options are passed in as the ninth argument.

FitnessFunction = @(x) vectorized_multiobjective(x); options = optimoptions(@gamultiobj,'UseVectorized',true); gamultiobj(FitnessFunction,numberOfVariables,[],[],[],[],lb,ub,options);
Optimization terminated: average change in the spread of Pareto solutions less than options.FunctionTolerance.

Related Topics