how to plot optimization figure of genetic algorithm (ga) in y-axes number of iteration and in x-axes fitness value in matlab

To plot the optimization figure of a genetic algorithm (GA) in Matlab, with the y-axis representing the number of iterations and the x-axis representing the fitness value, you need to keep track of the iterations and fitness values during the execution of the GA.

Assuming you have a loop that iterates through the generations of the GA, you can store the fitness values for each iteration in a variable. Here's a step-by-step guide:

  1. Initialize an empty array variable to store the fitness values:
main.m
fitness_values = [];
21 chars
2 lines
  1. Within the loop of your GA, after evaluating the fitness of each individual, add the fitness value to the fitness_values array:
main.m
% ...
% Evaluate fitness and update population
% ...

fitness_values = [fitness_values, best_fitness]; % assuming best_fitness is the fitness value of the best individual in the current generation
197 chars
6 lines
  1. After the GA loop finishes, plot the optimization figure using the plot function:
main.m
iterations = 1:length(fitness_values);
plot(iterations, fitness_values)
xlabel('Number of Iterations')
ylabel('Fitness Value')
title('Optimization Figure of Genetic Algorithm')
177 chars
6 lines

This will create a plot where the x-axis represents the number of iterations and the y-axis represents the fitness values.

Note: Make sure you have the Genetic Algorithm and Direct Search Toolbox installed to use the ga function in Matlab.

gistlibby LogSnag