# Genetic algorithm gives incorrect output

3 views (last 30 days)
Wouter Tel on 4 Feb 2020
Answered: Alan Weiss on 5 Feb 2020
Hi,
I have a problem with a piece of code running a genetic algorithm. The algorithm optimizes input parameters to a model which has only time as an output. However when I put the parameters, that came out of the genetic algorithm optimization, into my model, the model gives out NaN. The optimum that the genetic algorithm is finding is not a viable solution for the model.
I hope someone can help me out, I checked everythin but I really can't seem to find the mistake.
Kind regards,
Wouter
%% Main file for simulation
clear all
close all
clc
%% Course options
% Distances = [2000 1000 17000;2000 3000 15000;10000 1000 9000;10000 3000 7000];
% Coursepar.distance = Distances(i,:);
% Coursepar.gradient = [0 atan(6/100) 0];
Coursepar.distance = [18000 2000];
%% Selecting Riders
Riderpar.Names = {'TT_specialist','TT_specialist','TT_specialist','TT_specialist','Climber','Climber','Climber','Climber'};
%% Strategy parameters
Optimpar = [450,450,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,0]/5;
% Optimpar = [450,450,450,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,0]/5;
Stratpar.min_finish = 4;
Stratpar.pref = 1;
Stratpar.sui_allow = 0;
%% Environment parameters
Environment.rho = 1.225;
Environment.g = 9.81;
Environment.Cr = 0.0027;
%% Running simulation
tic
fun = @(OptimPar)multisegrun(Riderpar,Coursepar,Stratpar,Environment,Optimpar);
[x,t,exitflag] = ga(fun,19,[],[],[],[],[70 70 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0],[160 160 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 4],[],[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19])
toc

Alan Weiss on 5 Feb 2020
I suggest that you give an initial population consisting of at least one feasible point, meaning a point that gives a non-NaN value of the fitness function. Set this population in the InitialPopulationMatrix option. If you cannot locate any such point, well, move on to the next suggestion.
What might be even better is to debug your multisegrun file so that it doesn't ever return NaN. This is more work, but fixes the underlying cause of the issue.
Alan Weiss
MATLAB mathematical toolbox documentation

R2018a

### Community Treasure Hunt

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

Start Hunting!

Translated by