how to call my code to GA
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
imola
el 15 de Ag. de 2014
Comentada: Geoff Hayes
el 27 de Ag. de 2014
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
0 comentarios
Respuesta aceptada
Geoff Hayes
el 15 de Ag. de 2014
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 comentarios
Geoff Hayes
el 25 de Ag. de 2014
Glad that I was able to help, Imola. I hope that the results were good!
Geoff Hayes
el 27 de Ag. de 2014
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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Solver Outputs and Iterative Display en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!