# Why this error happened when i run genetic algorithm?

2 views (last 30 days)
Firas Al-Kharabsheh on 11 Apr 2016
Commented: Brendan Hamm on 12 Apr 2016
when i run this function in genetic algorithm in optimization tool box this error occur
• Attempt to execute SCRIPT ga as a function:
function [ F ] = FF( A )
% A =[ 0 0 1 0 1 0 1 0 1 0 1 0 1 0 0 1
% 1 1 1 0 1 1 1 1 1 1 1 1 0 1 0 1
% 1 1 0 1 0 0 0 0 0 0 0 1 1 1 1 1
% 0 1 0 1 1 1 0 0 1 0 0 1 1 1 1 1
% 0 1 1 1 1 0 1 0 0 1 1 1 1 1 0 1
% 0 0 1 1 0 1 1 0 1 1 1 0 1 0 0 1
% 0 1 0 0 1 0 1 1 1 1 0 1 1 1 0 1
% 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1
% 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
% 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 1 ];
% input ' A = ' ;
[n,m]=size(A);
B=(m+repmat(1:m,n,1)-A.*cumsum(A,2)).*A;
for k=1:n
a=B(k,B(k,:)~=0);
[~,~,kk]=unique(a);
row1{k,1}=accumarray(kk,1);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %c= represent sum of rows
c1 = cellfun(@sum, row1);
Z1 = randi([0 1], n,m);
B1=(m+repmat(1:m,n,1)-Z1.*cumsum(Z1,2)).*Z1;
for k=1:n
a=B1(k,B1(k,:)~=0);
[~,~,kk]=unique(a);
row2{k,1}=accumarray(kk,1);
end
d1 = cellfun(@sum, row2);
x1= abs( c1-d1);
% F(X1)
X1 = sum(x1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%Find F(X2)
C=(n+repmat(1:n,m,1)-A'.*cumsum(A',2)).*A';
for k=1:m
a=C(k,C(k,:)~=0);
[~,~,kk]=unique(a);
col1{k,1}=accumarray(kk,1);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% x represnt sum of column
c2 = cellfun(@sum, col1);
% x = d.';
Z2 = randi([0 1], n,m);
C2=(n+repmat(1:n,m,1)-Z2'.*cumsum(Z2',2)).*Z2';
for k=1:m
a=C2(k,C2(k,:)~=0);
[~,~,kk]=unique(a);
col1{k,1}=accumarray(kk,1);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% x represnt sum of column
c3 = cellfun(@sum, col1);
c4 = abs (c3-c2);
X2 = sum(c4);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%Find F(X3)
idx = A~=1;
r = sum(idx,2);
Z3 = randi([0 1], n,m);
idx1 = Z3~=1;
r5 = sum(idx1,2);
r6 = abs (r-r5);
X3= sum(r6);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%Find F(X4)
idx = A~=1;
c = sum(idx,1);
Z4 = randi([0 1], n,m);
idx1 = Z4~=1;
c5 = sum(idx1,1);
c6 = abs(c-c5);
X4= sum(c6);
F = X1+X2+X3+X4;
end

Brendan Hamm on 11 Apr 2016
It sounds likely that you have a different file ga.m which is being found by MATLAB first.
If you type:
which ga
you will be returned the file which MATLAB is trying to execute. If this is the correct version from the Global Optimization toolbox, it would be found in matlabroot\toolbox\globaloptim\globaloptim\ga.m. You then would need to remane the file or place it lower on the MATLAB Search Path .
Firas Al-Kharabsheh on 11 Apr 2016
i see this C:\Users\Haitham Khlifat\Documents\MATLAB\ga.m
Brendan Hamm on 12 Apr 2016
Exactly, this is not the function which comes with the Toolbox. Go to that location and change the name of that file to something else.
Presumably if you type
which ga -all
you will find the location I mention beneath this on the list. This means that MATLAB will find your files in the Documents directory before it finds files provied with your toolboxes.
Type:
rmpath('C:\Users\Haitham Khlifat\Documents\MATLAB\ga.m')
and this will make it find your files last. Read that link I posted above so you can understand how MATLAB searches for files.