MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today**New to MATLAB?**

Asked by imola
on 15 Aug 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

Answer by Geoff Hayes
on 15 Aug 2014

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?

Show 12 older comments

Geoff Hayes
on 22 Aug 2014

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.

Geoff Hayes
on 25 Aug 2014

Glad that I was able to help, Imola. I hope that the results were good!

Geoff Hayes
on 27 Aug 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.

## 0 Comments