%function of creating first genertion
function GAStart ()
%number of chroms in every generation 
global cn;
%number of chroms in every generation is 11
cn=11;
%put the last best result of n time GA execution in result variable
result(1,2)=0;
%evaluation function for calculating best results of n time GA execution
func=0;
%value for n times GA execution
n=10;
%n times excution of GA
for i=1:n
    %first generation chroms creating
    chrom(cn,2)=0;
    %creating every chrom of first generation
    for j=1:cn
        %random creating for x between 0 and 180 degree
        r=180*rand();
        %changing r from float to int
        r=floor(r);
        %puting random number in x
        chrom(j,1)=r;
        %random creating for y between 0 and sqrt(180) degree
        r=sqrt(180)*rand();
        %changing r from float to int
        r=floor(r);
        %puting random number in y
        chrom(j,2)=r;
    %end of for
    end
    %saving best result of every time of GA execution in tempresult
    tempresult=GA(chrom);
    %calculating evaluation function for every tempresult
    funct=sin(degtorad(tempresult(1,1)+(tempresult(1,2)^2)));
    %if funct is better than last best func so...
    if(funct>func)
        %result is first row of tempresult that is the best result
        result=tempresult(1,:);
        %put funct in func
        func=funct;
    %end of if
    end
%end of for
end
%display best result of n times execution of GA
disp(result);
%end of GAstart function
end

%main function of GA gets first generation and sets last generation
function ngc=GA(chrom)
%making cn global in GA function as well as GAstart function
global cn;
%ittration of GA execution
for ittration=1:10
    %creating func array for evaluation function
    func(cn)=0;
    %value of sum of func s
    sum=0;
    %calculating evaluation function for each chrom
    for i=1:cn
        func(i)=sin(degtorad(chrom(i,1)+(chrom(i,2)^2)));
        %if sin is less than 0.5 it is not suitable so...
        if(func(i)<0.5)
            %func(i) is zero so it can not be in roulette circle
            func(i)=0;
        end
        %calculating sum of func s
        sum=sum+func(i);
    %end of for
    end
    %calculating and comparing ratio of func s
    for i=1:cn
        if(func(i)<(sum/cn))
            %if func(i) is less than sum/cn so is not suitable and is zero
            func(i)=0;
        %end of if
        end
    %end of for    
    end
    %sum of percents (between zero and one)
    percentsum(cn)=0;
    %first value of percentsum
    percentsum(1)=func(1)/sum;
    %set to percentsum s
    for i=2:cn
        percentsum(i)=func(i)/sum+percentsum(i-1);
    %end of for
    end
    %next generation chrom
    ngc(cn,2)=0;
    %transfering best chrom directly to the new generation
    temps=func(1);
    for i=1:cn
        if(temps<func(i))
            ngc(1,:)=chrom(i,:);
            temps=func(i);
        %end of if
        end
    %end of for
    end
    %roulette circle
    for j=2:cn
        r=rand();
        for i=1:cn
            if (r<percentsum(i))
                ngc(j,:)=chrom(i,:);
                break;
            end
        end
    end
    %crossover with the propebility of 80 percent
    for i=2:2:cn
    r=rand();
        if(r<0.8)
            [ngc(i,:),ngc(i+1,:)]=crossover(ngc(i,:),ngc(i+1,:));
        end
    end
    %mutation with the propebility of 3 percent
    r=rand();
    for i=1:cn
        if(r<0.03)
            mutation(ngc(i,:));
        end
    end
    %chrom = ngc for next ittration
    chrom=ngc;
end
end

%crossover function
function [ngc1,ngc2]=crossover(chrom1,chrom2)
%changing all gens to binary
binstr11=dec2bin(chrom1(1),7);
binstr12=dec2bin(chrom1(2),7);
binstr21=dec2bin(chrom2(1),7);
binstr22=dec2bin(chrom2(2),7);
%get a random from 1 to 15 because of number of bits
r=rand()*14+1;
%append two gens to make a chrom
com1=strcat(binstr11,binstr12);
com2=strcat(binstr21,binstr22);
%taking apart for crossover
temp11=com1(1:floor(r));
temp12=com1(floor(r)+1:14);
temp21=com2(1:floor(r));
temp22=com2(floor(r)+1:14);
%appending
tempr1=strcat(temp11,temp22);
tempr2=strcat(temp21,temp12);
%making gens
temp11=tempr1(1:7);
temp12=tempr1(8:14);
temp21=tempr2(1:7);
temp22=tempr2(8:14);
%changing gens from binary to decimal
ngc1(1)=bin2dec(temp11);
ngc1(2)=bin2dec(temp12);
ngc2(1)=bin2dec(temp21);
ngc2(2)=bin2dec(temp22);
%end of crossover function
end

function ngc=mutation(chrom)
%changing all gens to binary
binstr1=dec2bin(chrom(1),7);
binstr2=dec2bin(chrom(2),7);
%chrom in binary
com=strcat(binstr1,binstr2);
for i=1:3
    %get a random from 1 to 15 because of number of bits
    r=rand()*14+1;
    %if is 0 change to 1 and if is 1 change to 0
    if(com(floor(r))=='1')
        com(floor(r))='0';
    else
        com(floor(r))='1';
    end
end
%taking apart chrom to gens
temp1=com(1:7);
temp2=com(8:14);
%changing binary to decimal in gens of result chrom
ngc(1)=bin2dec(temp1);
ngc(2)=bin2dec(temp2);
%end of mutation function
end