how to call my code to GA

Dear all,
I need to call my code to GA code, I have error message
Output argument "output" (and maybe others) not assigned during call to "F:\myheader.m>myheader".
Error in createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)
fcn_handle = @(x) fcn(x,FcnArgs{:});
Error in makeState (line 48)
firstMemberScore = FitnessFcn(state.Population(initScoreProvided+1,:));
Error in gaunc (line 41)
state = makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);
Error in ga (line 348)
[x,fval,exitFlag,output,population,scores] = gaunc(FitnessFcn,nvars, ...
Error in runsimple_fitnesss (line 16)
[x,fval] =ga(FitnessFunction,numberOfVariables,[],[],[],[],[],[],[],options)
Caused by:
Failure in initial user-supplied fitness function evaluation. GA cannot continue.
I don't know what it means or how to fixed my code. Could you explain to me what it means and help me to fix the error?
function [output]=myheader(i)
format long;format compact;fontSize = 13;
for i=1:4
p1=[25.8862 3.0912; 4.9390 44.5138; 25.3633 5.6858; 4.5108 48.9112];
p2=[11.5684 5.9243; 1.6695 41.9517; 24.5223 29.9721; 32.6509 23.0566];
d=sqrt((p1(i,1)-p1(i,2))^2+(p2(i,1)-p2(i,2))^2);
a=d/2; b=a/3; g=b/5; h=g/11; k=a-b;
results(i,1)=a;
results(i,2)=b;
results(i,3)=d;
results(i,4)=g;
results(i,5)=h;
results(i,6)=k;
end
R=results;
B=sortrows(R,-6);
BB= B(:,1:5);
for i=1:4
l=regexprep(cellstr(num2str(BB(i,:),'%011.6f')),'[.\l]','');
ll(i,1)=l;
end
ll
end
I want to call it to
FitnessFunction =@myheader
numberOfVariables =5;
options =gaoptimset('display','iter','generations',200,'populationsize',4,'FitnessScalingFcn'
{@fitscalingtop,0.4},'SelectionFcn',@selectiontournament,4},'crossoverfraction',0.65,'MutationFcn',{@mutationgaussian,0.25},'StallGenLimit',200)
[x,fval] =ga(FitnessFunction,numberOfVariables,[],[],[],[],[],[],[],options)
My individuals here is the matrix {ll} which contains 4 individuals, I should apply the ga functions then change the final result from string to 5 separate numbers {a, b, d, g, h}.
Could you help me please?
thanks in advance

 Accepted Answer

imola - there are a couple of things that aren't quite correct with your fitness function, myheader. Note the signature
function [output]=myheader(i)
The one input parameter i is, given that you have defined the GA to optimize your function 5 variables, a 1x5 vector of variables that are randomly created (you haven't defined the PopInitRange so I'm not sure what intervals the variables are initialized in). But note the name of this variable is i which is the same variable you are using to iterate in your for loop
for i=1:4
What this means is that you end up NOT using the input data at all!
Going back to the signature of this function, the return variable is named output, but nowhere in the code is the output variable being set. So this is why you are seeing the error message Output argument "output" (and maybe others) not assigned during call to.
So the above two issues need to be addressed, but more importantly, you need to decide what your fitness function should be doing. What is the purpose of myheader? As you are passing it as the fitness function to the Genetic Algorithm (GA), the GA is assuming that this is the function that you wish to minimize i.e. find an optimal (or near-optimal) solution for. The GA (in your case) creates a population of size 4 (why so small?) where each member of the population is randomly assigned values for each of the five variables. The GA will then score each of these members using the fitness function by passing in the vector of five variables and getting some sort of result/fitness/score for each. That which the better fitness/score (where better means smaller since we are minimizing the fitness function) is then more likely going to be considered/selected for reproduction in creating "children" for the next generations (whose data, i.e. vector of five variables) will be a combination of the data from the two parents (the crossover part of the GA).
I know that from a couple of your previous posts, you have written code that converts numeric data into integer "strings" so that you can do some sort of combination on them (like crossover) but you don't have to do that since you are using the GA from the Global Optimization Toolbox.
So what is the problem that you are trying to optimize (i.e. minimize) and how can you write your fitness function to accomplish this?

15 Comments

Imola - since you have decided to do the ga functionality from the Global Optimization Toolbox, then you don't have to worry about coding up the software to do the crossover or the mutation. The toolbox will do it for you. You don't have to convert the real data of
p1=[25.8862 3.0912; 4.9390 44.5138; 25.3633 5.6858; 4.5108 48.9112];
p2=[11.5684 5.9243; 1.6695 41.9517; 24.5223 29.9721; 32.6509 23.0566];
to string equivalents so that crossover is performed on these two parents (if that is what they are?). In fact, your fitness function doesn't know about parents or children or crossover or mutation. The sole purpose of the fitness function is to accept an input vector and return a fitness or score for that input. Since the GA is used to minimize the (fitness) function, then that input vector that leads to a smaller output is considered stronger (more fit) and better suited for reproduction.
From your comment I want to call the matrix of strings {ll}and apply crossover and mutation on it such that the string of 50 numbers represent 5 parameters{a,b,d,g,h} every parameter contains 10 numbers, you don't have to worry about this. The ga function will perform its own crossover (there are a number of methods that you can choose - one-point, two-point, heuristic, etc.) on two members of the population.
I think that the first step is to define your fitness function. You have stated that there are five variables. What do these represent? If I give you these five variables, what equation would you pass them into, or how would you evaluate all five together? What equation are you trying to minimize?
If the fitness function looked like the following
function [score] = fitnessFunction(varData)
% what gets filled in here so that score is set given the
% 1x5 vector of variable data, varData
score = ????;
end
What would the body of the above function be?
Dear Geoff,
I'm sorry for confuse you with me, but this code is mimic my original code so {a, b, d, g, h} are represent parameters for a certain shape and because the code is very large I summarized for simple functions by wrote {b, d, g, h} in this way, because of that the shape is not clear and {k} also not clear.
When I wrote the function,
function [score] = fitnessFunction(varData)
I wanted to generate 10 strings represent 10 shapes by using 10 points {p1,p2} that I want to apply the GA on it to minimize {k}, and the 5 parameters represent the parameters of the shape that I want . if I write varData = 5 will be fault because I wrote this function to generate even 1000 shape not just 10 and minimize {k},
when I apply the GA, I found that it takes the whole matrix {ll} as individual and even not apply the crossover on it, so how I call {ll} to the GA as initial population and {k} as fitness function which I wrote it in the middle of the code while I wrote [score] to generate the shapes.
As I told you before when I put k in
function [k] = fitnessFunction(varData)
I got result but its absolutely mistake.
thank you for everything, and forgive me again for confuse you, I will wait for your suggestions, you should know that this code is follow the original one step by step, so what I get here is exactly the same their.
Imola - could you outline what your fitness function is supposed to do? It sounds like there are five parameters of a shape, and that these five parameters are the input to your fitness function. How does your fitness function evaluate these five inputs? What are you trying to minimize?
-------------------------------------------------------------------------
Here is a very simple example. Suppose we want to minimize the following function
(x-1)^2 + (y-1)^2
The minimum for this function is zero, and if all of our variables are positive, then the optimal solution should be when (x,y)==(1,1).
If we wish to use a genetic algorithm, then we would create our fitness function as
function [score] = fitnessFunction(varData)
% get the variable data
x = varData(1);
y = varData(2);
score = (x-1)^2 + (y-1)^2;
end
Note how a 1x2 row vector varData is passed in to the function, and how we evaluate the two variables in the equation that produces the score. The genetic algorithm will use this function to determine the score for each of its randomly created members of the initial population. Those with the better score (where better implies smaller since we are minimizing our function) are more likely to be selected to create children for the next generation/iteration. Once the parents are selected, the crossover and mutation occurs to create the children. The cycle then repeats for each iteration.
-------------------------------------------------------------------------
Your fitness function must behave in a similar way. It accepts the 1x5 row vector, accesses each variable, and evaluates them all to produce a score for a single member of the population. Your population size may be 4, 10, 100, or even 1000. The fitness function doesn't care or even know about the population - it just needs to know how to handle the 1x5 row vector that represents (I think) in your case 5 shape parameters/variables.
imola's "Answer" moved here since it's not an answer to her original question but a reply to Geoff:
Dear Geoff
Thanks for replying, I think you got the idea, the two set of points {p1,p2} I used to find the shapes every two points give one shape, {d} is the distance between the points p1 and p2, {a} is the major axis and {b} is the minor, {g} are the centre and {h} is the angle of the shape to rotate, and {k} is the area { k = a*b*h} I’m sorry because I wrote it as {k = a-b} for short and I use {k} to sort the strings to see which area is the best notes I’m in 2 dim. As you see I put the fife parameters in string so I can apply the crossover on it such that I can change the position of the centres and the length of the axis between the shapes like {h a g b d} , so that I can get the best shape with minimum area which is the fitness function. the code
Function [ ] = myheader(n)
*** area
end
Is to generate the shapes, in every iteration the function draw one shape and compute the area for it. Because here I have four points that mean I will get four shapes, the {***} is the code of how I find all the parameters and the area which is the fitness function, my question, how I’m going to call the fitness function and the population, it suppose the matrix of the strings are my individual so how I’m going to call it. When I wrote {k} in
Function [k] = myheader(n)
and call, it was fault as I told you before. The GA code take default points for the population not my individuals and in the command window I got the matrix of the strings {ll} in every generation fixed not change
I hope you get now clear idea, every string represent a shape, in the end I want to get a best final shape with minimum area. I’m keeping search about GA examples and read, I may find some help, I’m really don’t know how to handle it. It is not straightway and easy.
Dear Geoff, if you don’t know how to handle, how I’m going to handle and I'm new in GA area and using MATLAB, could you please try to help me, I will appreciate that a lot. I will wait for your answer
regards
geoff's reply moved here:
Imola - from your comment As you see I put the fife parameters in string so I can apply the crossover on it such that I can change the position of the centres and the length of the axis between the shapes like {h a g b d} , so that I can get the best shape with minimum area which is the fitness function.
If you are going to use ga functionality from the Global Optimization Toolbox, then you don't have to worry (for the time being) about doing the crossover yourself. The ga code will do this for you, and give you the opportunity to choose different crossover algorithms and (I think) use your own if needed.
Going back to your code,
for i=1:10
p1=[25.8862 3.0912; 4.9390 44.5138; 25.3633 5.6858; 4.5108 48.9112];
p2=[11.5684 5.9243; 1.6695 41.9517; 24.5223 29.9721; 32.6509 23.0566];
d=sqrt((p1(i,1)-p1(i,2))^2+(p2(i,1)-p2(i,2))^2);
% other stuff
end
The p1 and p2 matrices are 4x2, so you will get an Index exceeds matrix dimensions message once i reaches 5. Given the above, and what you have said in the previous comment, I think that I'm getting a better idea of how your fitness function should be organized.
On each iteration of the above loop, you use four variables - two from p1 and two from p2 - to compute some intermediate values and the k which you are trying to minimize. To me, this means that your fitness function should process a 1x4 vector of data as follows
function [score] = myFitnessFunc(data)
p11 = data(1);
p12 = data(2);
p21 = data(3);
p22 = data(4);
d=sqrt((p11-p12)^2+(p21-p22)^2);
a=d/2;
b=a/3;
g=b/5;
h=g/11;
k=a*b*h;
score = k;
end
In the above, we use the four variables (found in the input parameter/vector data) and calculate the parameters that you need to determine k.
The GA will create a random population of members and apply this function to each, so that all members are scored. It will then apply a selection algorithm to determine which members of the population should be parents and with whom. The crossover will follow (I recommend using the heuristic or other real-valued crossover functions), along with some mutation. The cycle will repeat itself for the next generation until the number of iterations has been reached.
With this, you don't have to be concerned with converting data to strings and performing the crossover yourself.
Imola - I don't see how your above myFitnessFunc can work. What is G? What is p11, p12, p21 and p22? These last four are the variables that I described in my previous comment as functions of the input vector data, but you have not initialized them in any way, and you are not making use of data. So the above fitness function will fail with errors.
I also don't understand what you mean by about my initial population because I still need to regard the matrix of the strings as my population. Why must the matrix of strings be your initial population? Why are you insistent on using these strings?
Do you want your strings matrix to be the initial population for the GA? If so, then you have to convert these strings to a matrix of numbers where the rows represent each member in the population, and the columns represent each variable. If this new matrix is initPop then you would use add it to the options object as
options = gaoptimset(options,'InitialPopulation',initPop);
But you still have to decide what a member of the population looks like. Does it have just four variables to define the shape? From your question way up top, the p1 and p2 could be used to create an initial population as
initPop = [25.8862 3.0912 11.5684 5.9243; ...
4.939 44.5138 1.6695 41.9517; ...
25.3633 5.6858 24.5223 29.9721; ...
4.5108 48.9112 32.6509 23.0566];
Here you have four members, each with four variables that when using the fitness function I described in my previous comment would be able to score each one according to the equation that you have defined.
Imola, unless there is some requirement to use strings, I would ignore that for the time being and just try to work with numbers. (Or just let the GA work these numbers...)
Imola's answer pasted here
Dear Geoff, I'm really sorry because I wrote the code incorrect way, it should be like this
function [k] = myheader() %for example this is one string for short
ll = [ '00282348210009411607005646964200018823210000171120'
'00227125850007570862004542517000015141720000137652'
'00117416770003913892002348335300007827780000071162'
'00102091180003403039002041823500006806080000061873'];
for i=1:4
m=reshape(ll(i,:),10,[])';
v=[str2num(m(:,1:4))+str2num(m(:,6:end))/1e6]'
%%%d=sqrt()p11-p12)^2+(p21-p22)^2);
%%%a=d/2; b=a/3; g=b/5; h=g/11;
% I don't need to find {a, b, d, g, h} now because
% I have the parameters within the strings that I
% split so I will just compute {k} from the strings
% so I wrote
a=v(:,1);b=v(:,2); h=v(:,5);
k=v(:,1)*v(:,2)*v(:,5);
fitnessfunction = k;
end
end
with respect to the strings matrix, are you sure that the GA doesn't accept it as strings ?
I wanted like this so the functions mutation and crossover will applied directly, because when I spilt it, it gave a little bit different values. if so, then I called {k} to the GA, and the initial population I did exactly like you I copy the strings matrix after split it in this file above and pasted in GA file and wrote it as you
options = gaoptimset(options,'InitialPopulation',initPop);
but it gave me error, too many input, I wrote it as before see
FitnessFunction =@myheader
numberOfVariables =5; % the string has 5 parameters
initpop=[ 28.0348 9.0116 56.0696 1.0823 0.0711
22.0126 7.0709 45.0252 1.0142 0.0377
11.0417 3.0139 23.0834 0.0828 0.0712
10.0091 3.0030 20.0182 0.0806 0.0619];
options = gaoptimset('display','iter','InitialPopulation',initPop, ...
'generations',2,'populationsize',4,...
'FitnessScalingFcn', ...
{@fitscalingtop,0.4},'SelectionFcn', ...
@selectiontournament,4}, ...
'crossoverfraction',0.65,'MutationFcn', ...
{@mutationgaussian,0.25},'StallGenLimit',200);
[x,fval] =ga(FitnessFunction,numberOfVariables,[],[],[],[],[],[],[],options);
I think I get contradiction if I use {v} to compute the fitness function, and I use the {p1,p2} as initial pop in GA, so I used the same matrix as pop. but I got error
Too many input arguments.
Error in createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)
fcn_handle = @(x) fcn(x,FcnArgs{:});
Error in makeState (line 48)
firstMemberScore = FitnessFcn(state.Population(initScoreProvided+1,:));
Error in gaunc (line 41)
state =
makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);
Error in ga (line 348)
[x,fval,exitFlag,output,population,scores] = gaunc(FitnessFcn,nvars, ...
Error in runfitcustomplot (line 18)
[x,fval,output,exitflag,population,scores]=ga(FitnessFunction,numberOfVariables,[],[],[],[],[],[],[],options)
Caused by:
Failure in initial user-supplied fitness function evaluation. GA cannot continue.
do you see what I did correct if it will work I will need to draw the result to see how it is good
Thank you so much
Imola - as we have already discussed, the fitness function must accept a row vector as its input. Your function
function [k] = myheader()
doesn't do that so you will continually get the error message too many input arguments.
As well, you have now changed the number of variables from four to five.
Before going any further, you have to come up with a fitness function that conforms to what the ga is expecting.
Imola's comment moved here
Dear Geoff, my fitness function use the vector {v} as inputs
k=v(:,1)*v(:,2)*v(:,5);
so should I write
function [k] = myheader(v)
I got this result
Best Mean Stall
Generation f-count f(x) f(x) Generations
1 8 1.86 1.86 0
2 12 1.86 1.86 1
3 16 1.86 1.86 2
4 20 1.86 1.86 3
5 24 1.86 1.86 4
6 28 1.86 1.86 5
7 32 1.86 1.86 6
8 36 1.86 1.86 7
9 40 1.86 1.86 8
10 44 1.86 1.86 9
11 48 1.86 1.86 10
12 52 1.86 1.86 11
13 56 1.86 1.86 12
14 60 1.86 1.86 13
15 64 1.86 1.86 14
16 68 1.86 1.86 15
17 72 1.86 1.86 16
18 76 1.86 1.86 17
19 80 1.86 1.86 18
20 84 1.86 1.86 19
21 88 1.86 1.86 20
22 92 1.86 1.86 21
23 96 1.86 1.86 22
24 100 1.86 1.86 23
25 104 1.86 1.86 24
26 108 1.86 1.86 25
27 112 1.86 1.86 26
28 116 1.86 1.86 27
29 120 1.86 1.86 28
30 124 1.86 1.86 29
Best Mean Stall
Generation f-count f(x) f(x) Generations
31 128 1.86 1.86 30
32 132 1.86 1.86 31
33 136 1.86 1.86 32
34 140 1.86 1.86 33
35 144 1.86 1.86 34
36 148 1.86 1.86 35
37 152 1.86 1.86 36
38 156 1.86 1.86 37
39 160 1.86 1.86 38
40 164 1.86 1.86 39
Optimization terminated: maximum number of generations exceeded.
x =
28.0348 9.0116 56.0696 1.0823 0.0711
fval =
1.8598
output =
0
exitflag =
problemtype: 'unconstrained'
rngstate: [1x1 struct]
generations: 40
funccount: 164
message: 'Optimization terminated: maximum number of generations exceeded.'
population =
28.0348 9.0116 56.0696 1.0823 0.0711
22.0126 7.0709 45.0252 1.0142 0.0377
28.0348 9.0116 45.0252 1.0823 0.0711
22.0126 7.0709 45.0252 1.0142 0.0377
scores =
1.8598
1.8598
1.8598
1.8598
>>
this for 40 generation, as you see nothing happen
regards
Imola - please continue the conversation here rather than creating answers that aren't answers to your original question. It will be less confusing for others who view this posting.
As for your above comment, I'm not sure what to say since you have changed the fitness function again and I do not know what it is. Is it simply
function [k] = myheader(v)
k=v(:,1)*v(:,2)*v(:,5);
which doesn't make sense given that all scores are 1.8598 and none of the population members produce that kind of score.
As for "nothing happening" in 40 generations, yes, this does make sense since your population size is only 4! You will not get much (genetic) variability in the population with only four members. Also, many of the genes/variables for each member is the same so I suspect that you are using a two-point crossover method which will not vary the genes - it will just replace them. You should be using the crossoverheuristic with a ratio (say) of 1.2.
I would like you to do the following - rather than pasting code in comments or answers, please just attach the following to a comment:
- the fitness function that you are using (this can be in a file) myheader.m but you have to stop making changes to it
- the code that you are using to initialize the GA - what is the initial population, what are the options, etc.
Use the paperclip button to attach your code.
imola's "Answer" moved here since it's not an answer to the original question but a reply to Geoff:
Dear Geoff,
I'm really sorry because I wrote the code incorrect way, it should be like this
function [k] = myheader() %for example this is one string for short
ll = [ '00282348210009411607005646964200018823210000171120'
'00227125850007570862004542517000015141720000137652'
'00117416770003913892002348335300007827780000071162'
'00102091180003403039002041823500006806080000061873'];
for i=1:4
m=reshape(ll(i,:),10,[])';
v=[str2num(m(:,1:4))+str2num(m(:,6:end))/1e6]'
%%%d=sqrt()p11-p12)^2+(p21-p22)^2);
%%%a=d/2; b=a/3; g=b/5; h=g/11;
% I don't need to find {a, b, d, g, h} now because I have the parameters within the strings that I split so I will just compute {k} from the strings so I wrote
a=v(:,1);b=v(:,2); h=v(:,5);
k=v(:,1)*v(:,2)*v(:,5);
fitnessfunction = k;
end
end
with respect to the strings matrix, are you sure that the GA doesn't accept it as strings ?
I wanted like this so the functions mutation and crossover will applied directly, because when I spilt it, it gave a little bit different values.
if so, then I called {k} to the GA, and the initial population I did exactly like you I copy the strings matrix after split it in this file above and pasted in GA file and wrote it as you
options = gaoptimset(options,'InitialPopulation',initPop);
but it gave me error, too many input, I wrote it as before see
FitnessFunction =@myheader
numberOfVariables =5; % the string has 5 parameters
initpop=[ 28.0348 9.0116 56.0696 1.0823 0.0711
22.0126 7.0709 45.0252 1.0142 0.0377
11.0417 3.0139 23.0834 0.0828 0.0712
10.0091 3.0030 20.0182 0.0806 0.0619];
options =gaoptimset('display','iter','InitialPopulation',initPop,'generations',2,'populationsize',4,'FitnessScalingFcn', {@fitscalingtop,0.4},'SelectionFcn',@selectiontournament,4},'crossoverfraction',0.65,'MutationFcn',{@mutationgaussian,0.25},'StallGenLimit',200);
[x,fval] =ga(FitnessFunction,numberOfVariables,[],[],[],[],[],[],[],options);
I think I get contradiction if I use {v} to compute the fitness function, and I use the {p1,p2} as initial pop in GA, so I used the same matrix as pop, but I got error
Too many input arguments.
Error in createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)
fcn_handle = @(x) fcn(x,FcnArgs{:});
Error in makeState (line 48)
firstMemberScore = FitnessFcn(state.Population(initScoreProvided+1,:));
Error in gaunc (line 41)
state = makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);
Error in ga (line 348)
[x,fval,exitFlag,output,population,scores] = gaunc(FitnessFcn,nvars, ...
Error in runfitcustomplot (line 18)
[x,fval,output,exitflag,population,scores]=ga(FitnessFunction,numberOfVariables,[],[],[],[],[],[],[],options)
Caused by:
Failure in initial user-supplied fitness function evaluation. GA cannot continue.
Do you see what I did correct? If it will work I will need to draw the result to see how it is good
Thank you so much
imola's second response moved here:
Dear Geoff,
My fitness function use the vector {v} as inputs
k=v(:,1)*v(:,2)*v(:,5);
so should I write
function [k] = myheader(v)
I got this result
Best Mean Stall
Generation f-count f(x) f(x) Generations
1 8 1.86 1.86 0
2 12 1.86 1.86 1
3 16 1.86 1.86 2
4 20 1.86 1.86 3
5 24 1.86 1.86 4
6 28 1.86 1.86 5
7 32 1.86 1.86 6
8 36 1.86 1.86 7
9 40 1.86 1.86 8
10 44 1.86 1.86 9
11 48 1.86 1.86 10
12 52 1.86 1.86 11
13 56 1.86 1.86 12
14 60 1.86 1.86 13
15 64 1.86 1.86 14
16 68 1.86 1.86 15
17 72 1.86 1.86 16
18 76 1.86 1.86 17
19 80 1.86 1.86 18
20 84 1.86 1.86 19
21 88 1.86 1.86 20
22 92 1.86 1.86 21
23 96 1.86 1.86 22
24 100 1.86 1.86 23
25 104 1.86 1.86 24
26 108 1.86 1.86 25
27 112 1.86 1.86 26
28 116 1.86 1.86 27
29 120 1.86 1.86 28
30 124 1.86 1.86 29
Best Mean Stall
Generation f-count f(x) f(x) Generations
31 128 1.86 1.86 30
32 132 1.86 1.86 31
33 136 1.86 1.86 32
34 140 1.86 1.86 33
35 144 1.86 1.86 34
36 148 1.86 1.86 35
37 152 1.86 1.86 36
38 156 1.86 1.86 37
39 160 1.86 1.86 38
40 164 1.86 1.86 39
Optimization terminated: maximum number of generations exceeded.
x =
28.0348 9.0116 56.0696 1.0823 0.0711
fval =
1.8598
output =
0
exitflag =
problemtype: 'unconstrained'
rngstate: [1x1 struct]
generations: 40
funccount: 164
message: 'Optimization terminated: maximum number of generations exceeded.'
population =
28.0348 9.0116 56.0696 1.0823 0.0711
22.0126 7.0709 45.0252 1.0142 0.0377
28.0348 9.0116 45.0252 1.0823 0.0711
22.0126 7.0709 45.0252 1.0142 0.0377
scores =
1.8598
1.8598
1.8598
1.8598
>> this for 40 generation, as you see nothing happen
regards
Imola - please continue the conversation here rather than creating answers that aren't answers to your original question. It will be less confusing for others who view this posting.
As for your above comment, I'm not sure what to say since you have changed the fitness function again and I do not know what it is. Is it simply
function [k] = myheader(v)
k=v(:,1)*v(:,2)*v(:,5);
which doesn't make sense given that all scores are 1.8598 and none of the population members produce that kind of score.
As for "nothing happening" in 40 generations, yes, this does make sense since your population size is only 4! You will not get much (genetic) variability in the population with only four members. Also, many of the genes/variables for each member is the same so I suspect that you are using a two-point crossover method which will not vary the genes - it will just replace them. You should be using the crossoverheuristic with a ratio (say) of 1.2.
I would like you to do the following - rather than pasting code in comments or answers, please just attach the following to a comment:
- the fitness function that you are using (this can be in a file) myheader.m but you have to stop making changes to it
- the code that you are using to initialize the GA - what is the initial population, what are the options, etc.
Use the paperclip button to attach your code.
Glad that I was able to help, Imola. I hope that the results were good!
Imola - please post this as a new question as it doesn't follow from your original one. Also, format the code portions of your message by using the {}Code button so that it is readable and distinct from your question/comments. Right now, you have formatted everything in your comment as code.

Sign in to comment.

More Answers (0)

Tags

Asked:

on 15 Aug 2014

Commented:

on 27 Aug 2014

Community Treasure Hunt

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

Start Hunting!