I am calling function in GA Program but getting error

[c,d]=crossover(a,b)
Data of crossover.m file is as under % Crossover operator
function [c,d]=crossover(a,b)
nn=length(a)-1;
% generating a random crossover point
cpoint=floor(nn*rand)+1;
c=[a(1:cpoint) b(cpoint+1:end)];
d=[b(1:cpoint) a(cpoint+1:end)];
end
% end for crossover
when i run the program i get this in return from command prompt "Error: File: GA.m Line: 169 Column: 2 This statement is not inside any function. (It follows the END that terminates the definition of the function "bintodec".)"
Please tell where the problem

5 Kommentare

KSSV
KSSV am 10 Okt. 2016
How you have named your file which has this function?
crossover.m
Dr. Silva i can send you complete code, in which i have called 3 functions, but its not working for me i am new in this group,should i email you code or attach it with comment
KSSV
KSSV am 10 Okt. 2016
When I run the above function, I am not getting any error.
i am calling three functions crossover,mutate,mutation but each time i run GA.m file i get different errors
i have attached all the files plz check

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

KSSV
KSSV am 10 Okt. 2016

1 Stimme

Bilal Khurshid you are calling the functions crossover and mutate, after the end of function GA.m. You cannot call a functions after ending a function. You can write the definition of the functions after end of main function, but you cannot call it. Change the location of those two functions; place them where you want to call/use them. In the attached code, I have changed their position, that error is not showing out now.

3 Kommentare

Thanks Dr Silva i have replaced my GA.m file with your modified file, but still i am getting this error
>> [costs,schedule]=GA([2 3 1 4],2,12) Undefined function or variable 'pm'.
Error in mutation (line 5) if pm>rand,
Error in GA (line 93) [kk,popnew]=mutation(popsize*rand);
KSSV
KSSV am 10 Okt. 2016
This is a error in your function mutation...you have used pm inside it and it is not sent/ defined inside the function.
I have added valve of pm and random operator is mutation function file, but still getting following error
Error in mutation (line 4) pm=0.05; % Mutation probability
Output argument "kk" (and maybe others) not assigned during call to "C:\Users\Bilal\Desktop\GA test\mutation.m>mutation".
Error in GA (line 93) [kk,popnew]=mutation(popsize*rand);

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu MATLAB finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by