# Genetic algorithm plot diagram display

20 views (last 30 days)

Show older comments

joshua Abam
on 21 Feb 2020

Commented: joshua Abam
on 23 Feb 2020

Dear all,

I am having a few challenges with regards to GA displaying the plots from the plot function and saving the output results from the output function, while using the m file, however when put in optimization toolbox, the plot are displayed and the output functions are saved respectively. That draws me to the questions, what do I have to do to make the plots displays and save the out put functions using an m file as displayed below.

Thanks

Kind regards

Joshua Abam

function [x,fval,existflag,output,population,scores] = scr_gafunction(x)

options = optimoptions('ga'); %Define Optimization the solver

options = optimoptions('ga','PopulationSize',50,'Generations',50,'OutputFcn',@gascroutputdata); %Define the Population Size

%Define the generation Size %Define Ouput Function

options.SelectionFcn = ('selectionstochunif'); %Define the selection pattern

options.MutationFcn = ('mutationgaussian'); %Define the mutation methods

options.CrossoverFcn = ('crossoverheuristic'); %Define the crossover methods

options = optimoptions('ga', 'Display','diagnose','MaxStallGenerations',60); %Define Display function

%Define at what Generation is GA stall

%Define the Plot Function

scr_gafunction.options.PlotFcns = optimoptions('ga','PlotFcn',{@gaplotbestf,@gaplotrange,@gaplotscores,...

@gaplotselection,@gaplotdistance,@gaplotexpectation,@gaplotstopping})

options = optimoptions('ga','UseParallel', true, 'UseVectorized', false);

ObjectiveFunction = @weight; %fitness function

nvars = 3; % Number of variables

A = [0 0 -1]; %Linear Inequality Constraint matrix

b = [-0.0254]; %Linear inequality constraint vector (t_1+t_corr+t_fab)

Aeq = [1 -0.03 0]; %Linear equality Constraint matrix

beq = [77]; %Linear equality constraint vector

lb = [167, 3000, 0.0127]; %Vector of Lower bounds

ub = [173, 3200, 0.0286]; %Vector of Upper bounds

% Initial design point could be determined in 2 ways: One is to leave it to

% GA automatically generate; or it can be specified by the users.

%x0 = []; %Initial Feasible Point

nonlcon = @nonlinearconst; %Nonlinear constraints

Intcon = []; %Vector indicating variables that take integer values

%Excuate GA with output

[x,fval,exitflag,output,population,scores] = ga(ObjectiveFunction,nvars,A,b,Aeq,beq,lb,ub,nonlcon,Intcon,options)

Final_time = toc(startTime);

fprintf('GA optimization takes %g seconds.\n',Final_time)

end

##### 4 Comments

### Accepted Answer

Walter Roberson
on 23 Feb 2020

### More Answers (0)

### See Also

### Categories

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!