a genetic algorithm to evolve vehicle route (TSP)
Posts: 6,843
Threads: 4
Joined: Mar 2015
genetic algorithm matlab code
This example shows how to create and minimize a fitness function using the Genetic Algorithm in the Global Optimization Toolbox.
A Simple Fitness Function
Here we want to minimize a simple function of two variables
min f(x) = 100 * (x(1)^2 - x(2)) ^2 + (1 - x(1))^2;
x
Coding the Fitness Function
We create a MATLAB file named simple_fitness.m with the following code in it:
function y = simple_fitness(x)
y = 100 * (x(1)^2 - x(2)) ^2 + (1 - x(1))^2;
The Genetic Algorithm solver assumes the fitness function will take one input x where x is a row vector with as many elements as number of variables in the problem. The fitness function computes the value of the function and returns that scalar value in its one return argument y.
Minimizing Using ga
To minimize our fitness function using the ga function, we need to pass in a function handle to the fitness function as well as specifying the number of variables in the problem.