 ## Author: M. Turhan ÇOBAN
## Created: 2021-11-07
## Genetic optimisation using binary genes 0010011100101000111..
## genetic_opt

function [BestX BestY] =int_genetic_opt1(iPOPSIZE,iMAXGENS,iPXOVER,iPMUTATION,x,y)
# Maximizing a function
POPSIZE=iPOPSIZE;      ## population size
MAXGENS=iMAXGENS;      ## maximum number of generations;
PXOVER=iPXOVER;        ## probability of crossover;
PMUTATION=iPMUTATION;  ## proportion of mutated variables
Best=1;                ## The best genotype in the population
nBest=2;               ## next to  best genotype in the population
##============= INITIAL ==================
## set initial population
n=length(x);
total=0.0;
smallest=realmax('double');
biggest=-smallest;
max=biggest;
min=-biggest;
# set initial population, find Best and next to best
for i=1:POPSIZE
 int_genotype(i)= set_integer_random_genotype(x,y,i);
 int_genotype1(i)= int_genotype(i);
 x1=int_genotype(i).z;
 if x1<min
   min=x1;
   nBest=int_genotype(Best).no;
   Best=i;
   smallest=x1;
 endif
 if x1>max
   max=x1;
   biggest=max
 endif
 total=total+int_genotype(i).z;
endfor
# set normalised RFitness (0-1 ) each
# set cumulative normalised (0-1) total
for i=1:POPSIZE
 xx=int_genotype(i).z;
 int_genotype(i).RFitness= (xx-min)/(max-min);
endfor
## printf('original population \n');
## for i=1:POPSIZE
##  printf('%i %d %d %d \n',i,xx,int_genotype(i).z);
## endfor 
##================= GENERATIONS ================================
## initial population set is finished
## now iterate for MAXGENS maximum number of generations
for mng=1:MAXGENS
## create new generations
    ##  retain BEst and second best in the list
int_genotype1(1)=int_genotype(Best);
int_genotype1(2)=int_genotype(nBest);
for w=3:POPSIZE
    ## get parent # 1
    while 1
       n1=int8(1+POPSIZE*rand);
       xxx=int_genotype(n1).RFitness;
       if xxx<PXOVER
         break;
       endif
    endwhile
    ## get parent # 2
    while 1 
       n2=int8(1+POPSIZE*rand);
       xxx=int_genotype(n2).RFitness;
       if xxx<PXOVER
         break;
       endif
    endwhile
    ## now cross corrolate parents to create a child
      bb1=int_cross_over(int_genotype(n1).id,int_genotype(n2).id);
      ## There are some mutation possibility in here
      if rand<PMUTATION
        bb2=int_mutate(bb1);
      else
        bb2=bb1;
      end
      b3=bb2;
    int_genotype1(w).id=b3;
    int_genotype1(w).z=distance(b3);
endfor 
## new generation is created now replace old generation with the new ones
## smallest=realmax('double');
## biggest=-smallest;
##max=biggest;
## min=-biggest;
total=0.0;
# set population, find Best and next to best
for i=1:POPSIZE
 int_genotype(i).x= int_genotype1(i).x;
 int_genotype(i).y= int_genotype1(i).y;
 int_genotype(i).no= int_genotype1(i).no;
 int_genotype(i).z= int_genotype1(i).z;
 int_genotype(i).id= int_genotype1(i).id;
 x1=int_genotype(i).z;
 if x1<min
   min=x1;
   nBest=int_genotype(Best).no;
   Best=i;
   smallest=x1;
 endif
 if x1>max
   max=x1;
   biggest=max
 endif
 total=total+int_genotype(i).z;
endfor
# set normalised RFitness (0-1 ) each
for i=1:POPSIZE
 xx=int_genotype(i).z;
 int_genotype(i).RFitness= (xx-min)/(max-min);
endfor # for i=1:POPSIZE
## printf('%i generation %i population \n',mng);
##  for i=1:POPSIZE
##  printf('%i %d %d %d \n',i,xx ,genotype(i).y);
## endfor 
mng
BestX=int_genotype(Best).id
BestY=int_genotype(Best).z 
endfor # for mng=1:MAXGENS 
 
       function total=distance(id)
       total=0.0;
         for i=1:n-1
           x1=x(id(i+1))-x(id(i));
           y1=y(id(i+1))-y(id(i));
           total=total+sqrt(x1*x1+y1*y1);
         endfor
       endfunction 
endfunction
