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

Learn moreOpportunities for recent engineering grads.

Apply Today
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 17 older comments

imola on 27 Aug 2014 at 9:32

That was really good,

thank you very much,

but now I want to see the generation before the final, could you please show me a simple example to follow because I don't know how to write the options and the state, it looks I stuck all the time I already write it as a question

Dear All, I'm trying to use the next function to find some information about previous generation and I found difficulty with writing the state and the options options = gaoptimset('OutputFcn', @outputfun); [state,options,optchanged] = outputfun(options,state,flag) I have this code which is work, function [x,fval,exitflag,output,population,score] = optimGA(nvars,lb,PopulationSize_Data,Generations_Data,TolFun_Data,InitialPopulation_Data) %% This is an auto generated MATLAB file from Optimization Tool. %% Start with the default options options = gaoptimset; %% Modify options setting options = gaoptimset(options,'PopulationSize', PopulationSize_Data); options = gaoptimset(options,'Generations', Generations_Data); options = gaoptimset(options,'TolFun', TolFun_Data); options = gaoptimset(options,'InitialPopulation', InitialPopulation_Data); options = gaoptimset(options,'FitnessScalingFcn', @fitscalingprop); options = gaoptimset(options,'SelectionFcn', { @selectiontournament [] }); options = gaoptimset(options,'Display', 'diagnose'); options = gaoptimset(options,'PlotFcns', { @gaplotbestf @gaplotbestindiv });

[x,fval,exitflag,output,population,score] = ... ga(@fitnessFct,nvars,[],[],[],[],lb,[],[],[],options);

% % I CHOOSE THE OPTIONS FROM GA TOOLBOX THEN FROM FILE WE GENERAT THIS CODE FOR OUR OPTIONS I run this code from another file [syntax , ResMatrix]=myheader() [z,fval,exitflag,output,population,score] = optimGA(5,0,150,100,0,ResMatrix) 5 is number of variable, 0 Ib,150 size pop, 100 generation, 0 tolerance, ResMatrix is my initial population matrix, everything is good, but I need now to use 'OutputFcn' so I decided to write new file for the outputfun and call it as custom fun from GA toolbox and then generate MATLAB file from Optimization Tool as above, but I don't know how to set state and options, could you please explain to me how to write it. or you may have better idea Thank you in advance

Geoff Hayes on 27 Aug 2014 at 13:01

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