└── AUV_GA ├── coor.mat ├── InitPop.m ├── Fitness.m ├── Mutation.m ├── Nearest.m ├── README.txt ├── Select.m ├── RP_coordinate.m ├── DynamicProgram.m ├── NN.m ├── Crossover.m ├── main.m └── tsp_dp1.m /AUV_GA/coor.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MagRUAski/4th_year_project/HEAD/AUV_GA/coor.mat -------------------------------------------------------------------------------- /AUV_GA/InitPop.m: -------------------------------------------------------------------------------- 1 | %Generate 1st population 2 | function popparent = InitPop(Popsize,RPNUM,adjacency,Mixrate) 3 | popparent = zeros(Popsize,RPNUM); 4 | for i = 1:Popsize 5 | %if(i<=(Mixrate*Popsize)) 6 | %Normal 1st gen 7 | %popparent(i,:)=randperm(RPNUM); %1:RPNUM integers without repetition 8 | %else 9 | %Elite 1st gen 10 | popparent(i,:) = Nearest(RPNUM,adjacency); %Use Nearest Neighbor to get Elite 1st gen 11 | %end 12 | end 13 | 14 | -------------------------------------------------------------------------------- /AUV_GA/Fitness.m: -------------------------------------------------------------------------------- 1 | % Function of calculating fitness for population 2 | function fitness = Fitness(pop,adjacency) 3 | for i = 1:size(pop,1) 4 | 5 | dist=0; 6 | for k = 2:size(pop,2) 7 | dist = dist + adjacency(pop(i,k-1),pop(i,k)); %Add the distance between points 8 | end 9 | dist = dist + adjacency(pop(i,1),pop(i,size(pop,2)));%Add the distance from end point to start point 10 | fit(i).fitness=dist; 11 | 12 | end 13 | fitness = [fit.fitness]; -------------------------------------------------------------------------------- /AUV_GA/Mutation.m: -------------------------------------------------------------------------------- 1 | %Mutation by swaping genes and get Chindren population 2 | function Childpop = Mutation(crossover,Pmutation) 3 | Childpop = crossover; 4 | k=1; 5 | while(k<=size(Childpop,1)) 6 | %Russian roulette 7 | Pm = unifrnd(0,1); 8 | if(Pm 50 | end 51 | RELATION(a)=b; 52 | RELATION(b)=a; 53 | end 54 | i=i+1; 55 | end 56 | end 57 | j=1; 58 | %Replacement 59 | while(j<=size(Chrom1,2)) 60 | while(j>=SS&&j<=SE) 61 | j=j+1; 62 | end 63 | if(j>size(Chrom1,2)) 64 | break 65 | end 66 | if(RELATION(Chrom1(j))==0) 67 | j=j+1; 68 | else 69 | Chrom1(j)=RELATION(Chrom1(j)); 70 | j=j+1; 71 | end 72 | end 73 | j=j-1; 74 | while(j~=0) 75 | while(j>=SS&&j<=SE) 76 | j=j-1; 77 | end 78 | if(j==0) 79 | break; 80 | end 81 | if(RELATION(Chrom2(j))==0) 82 | j=j-1; 83 | else 84 | Chrom2(j)=RELATION(Chrom2(j)); 85 | j=j-1; 86 | end 87 | end 88 | 89 | crossover(k,:)=Chrom1; 90 | crossover(k+1,:)=Chrom2; 91 | 92 | end 93 | k=k+2; 94 | end 95 | 96 | 97 | 98 | 99 | -------------------------------------------------------------------------------- /AUV_GA/main.m: -------------------------------------------------------------------------------- 1 | %%%%%%%%%% AUV path planning using GA %%%%%%%%%% 2 | %%%%%Run 'RP_coordinate.m' before running main %%%% 3 | 4 | clear all; 5 | close all; 6 | tic;%Runtime timer 7 | %% global variables 8 | 9 | load ('coor.mat'); %Load data generated by RP_coordinate.m 10 | 11 | Popsize =50; %Population size, should be an even integer 12 | 13 | %Genetic parameters 14 | %MIXRATE = 0.3; 15 | ITERATION = 10000; %Number of iteration 16 | THRESHOLD = 100; 17 | Pcross = 0.7; %Crossover rate 18 | Pmutation = 0.3; %Mutation rate 19 | 20 | %Begin 21 | Parentpop=InitPop(Popsize,RPNUM,adjacency); 22 | Fitnesscurve=[]; 23 | Generation = 1; 24 | 25 | Fitconst=0; %Number of generations that fitness values remain constant 26 | %% Genetic algorithm 27 | while(Generation <= ITERATION) 28 | if (Fitconst<=THRESHOLD) %Stop iteration if fitness value is constant in threshold number of genreations 29 | fitness = Fitness(Parentpop,adjacency); %Calculate fitness of parents 30 | crossover = Crossover(Parentpop,Pcross); %Crossover 31 | Childpop = Mutation(crossover,Pmutation); %Mutate and get chindren 32 | combopop=[Parentpop;Childpop]; %Combine parents and chindren 33 | combofitness=Fitness(combopop,adjacency); %Calculate overall fitness 34 | nextpop=Select(combopop,combofitness); %Select the first half of best to get 2nd gen 35 | Parentpop=nextpop.pop; 36 | if(Generation ==1) 37 | Best_GApath=Parentpop(1,:); 38 | Best_Fitness=combofitness(nextpop.bestplan); 39 | else 40 | New_Best_Fitness=combofitness(nextpop.bestplan);%Evaluate best solution 41 | New_Best_GApath=Parentpop(1,:); 42 | 43 | if(New_Best_Fitness