-
Notifications
You must be signed in to change notification settings - Fork 0
/
GA_Main.m
116 lines (75 loc) · 6.8 KB
/
GA_Main.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
%start Genetic algorithm
warning('off')
clear
popSize = 150; %population size
MaxEpochs = 25; %max epochs
mutRate = 0.9 ; %mutation Rate
CrossOverRate=0.9; %CrossOver Rate
%---User Input Data ! at least 6 points needed--------
x_blue = [5,5,10,9,3,11,15,3,6,8,1,8,5,12,22,14,18,5,14,12,0,1,10,18,15,12,7,15,19,34,25,28,35,44,56,61,2,3,66,57,71,73,77,57,69,56,81,82,84,86,87,40,41,58,40,77,80,90,92,94];
y_blue = [11,2,1,5,4,25,13,7,15,17,5,4,15,27,5,6,13,8,12,15,12,3,8,12,5,16,19,22,24,25,2,4,7,23,25,26,55,57,69,56,77,74,71,66,57,71,35,44,56,61,54,60,55,40,62,78,26,32,34,36];
x_red = [2,22,3,8,12,1,35,19,1,21,24,26,31,35,42,47,51,53,67,68,82,85,89,91,93,95,81,82,86,87,89,65,75,78,85,80,77,82,85,87,9,7,8,10,5,40,50,60,70,20,30,0];
y_red = [2,12,16,0,1,20,8,17,2,14,51,53,67,68,24,26,31,35,42,47,71,73,75,76,78,79,32,42,52,60,50,80,81,83,79,5,7,8,10,9,80,77,82,85,87,9,10,5,12,40,39,30];
chrom_len = length (x_blue)+2; % 1 gen for break point(BR) and Real length (RL) is added
%population initialize
Population= initPopulation(x_blue,y_blue,x_red,y_red,chrom_len, popSize);
for epoch = 1 : MaxEpochs
epoch
%CrossedPop = crossOver(Population ,CrossOverRate,x_blue); % crossover on main pop | output : new childPop matrix | smaller than main pop size
mutatedPop = Mutation (Population,x_blue,mutRate); % mutation on main pop | output : mutated pop matrix
for i=1:20
BB = InjectChrom(x_blue,y_blue,chrom_len); % Inject random chromosome
mutatedPop = cat(1,mutatedPop,BB); % append injected chromosome to mutated pop
end
%[FinalPop,sortetFit,AverageGeneration(epoch,1),BestIndex(epoch,1),BestSolution(epoch,:),BestFitness(epoch,1)] = SurvivalSelection (mutatedPop ,CrossedPop,Population ,x_blue,y_blue,x_red,y_red); % return survivals | size = main pop size
[FinalPop,sortetFit,AverageGeneration(epoch,1),BestIndex(epoch,1),BestSolution(epoch,:),BestFitness(epoch,1)] = SurvivalSelection (mutatedPop ,Population ,x_blue,y_blue,x_red,y_red); % return survivals | size = main pop size
Population = FinalPop; % hold final pop in next generation
% BB=BestSolution(epoch,:);
end
[BestofBestFit, BestSolGeneration] = maxk(BestFitness,1); % find the best Fitness of the best
BestSolRealLength = BestSolution(BestSolGeneration,end); % best solution Real Length
BestSolBrPoint = BestSolution(BestSolGeneration,end-1); % best Solution break point
BestSol = BestSolution(BestSolGeneration,1:BestSolRealLength);
[BestSolIdx n] = size(BestSolution);
for i=10:BestSolIdx
BestSolRealLength = BestSolution(i,end); % best solution Real Length
BestSolBrPoint = BestSolution(i,end-1);
BestSol = BestSolution(i,1:BestSolRealLength);
PlotChromPolygon(BestSol,BestSolBrPoint,x_blue,y_blue,x_red,y_red); % plot best Solution
figure
hold on
end
% PlotChromPolygon(BestSol,BestSolBrPoint,x_blue,y_blue,x_red,y_red); % plot best Solution
% figure
% hold on
plot(AverageGeneration)
plot(BestFitness)
% ----------------------------------- Other pre-defined points -----------------------------------
% x_blue = [1,1,10,10,5,5,9,3,2];
% y_blue = [1,10,1,10,8,0,5,4,3];
% x_blue = [1,1,10,5,9,3,11,15,3,6,8];
% y_blue = [1,10,1,8,5,4,15,12,7,15,17];
% x_red = [2,12,4,3,8,12,4,5,6];
% y_red = [2,12,5,16,0,1,4,6,7];
% --------------------------------------------
% x_blue=[-17.2811059907834;14.0552995391705;62.9032258064516;61.0599078341014;54.1474654377880;26.9585253456221;-14.5161290322581;-25.5760368663594;14.0552995391705;28.8018433179724;9.44700460829493;-5.76036866359446;-4.37788018433180;15.4377880184332;31.1059907834102;44.4700460829493;38.0184331797235;15.8986175115208;-14.9769585253456;-37.0967741935484;-43.5483870967742;-28.3410138248848;-22.8110599078341;-29.2626728110599;-40.3225806451613;-48.6175115207373;-47.6958525345622;-25.5760368663594;12.2119815668203;55.5299539170507;65.6682027649770 ];
% y_blue=[62.9737609329446;68.2215743440233;51.3119533527697;7.58017492711369;-22.1574344023324;-41.9825072886297;-38.4839650145773;-17.4927113702624;1.74927113702623;16.3265306122449;30.9037900874636;24.4897959183674;1.16618075801749;-23.9067055393586;-12.8279883381924;15.7434402332362;32.6530612244898;38.4839650145773;34.9854227405248;27.4052478134111;5.83090379008746;-13.9941690962099;5.24781341107871;5.24781341107871;-17.4927113702624;-24.4897959183673;10.4956268221574;-57.1428571428571;-71.1370262390671;-74.6355685131195;-60.0583090379009];
% x_red=[2.07373271889402;19.1244239631337;6.68202764976957;-32.9493087557604;-24.6543778801843;-12.6728110599078;-10.3686635944700;5.29953917050692;38.9400921658987;44.4700460829493;33.4101382488479;32.0276497695853;48.6175115207373;68.4331797235023;89.1705069124424;90.5529953917051;92.8571428571429;87.3271889400922;41.7050691244240;-33.4101382488479;-69.8156682027650;-43.5483870967742;-17.7419354838710;10.8294930875576;30.1843317972350;46.7741935483871;37.0967741935484 ] ;
% y_red=[ 25.6559766763848;30.3206997084548;-5.83090379008746;2.91545189504375;26.2390670553936;13.9941690962099;-5.83090379008746;-38.4839650145773;-54.8104956268222;-11.6618075801749;31.4868804664723;54.2274052478134;82.2157434402332;89.7959183673470;62.9737609329446;24.4897959183674;-19.2419825072886;-45.4810495626822;-68.8046647230321;-74.6355685131195;-71.1370262390671;80.4664723032070;64.1399416909621;46.6472303206997;22.7405247813411;-2.91545189504373;0.583090379008738];
% x_blue = [1,1,10,5,9,3,11,15,3,6,8,1,8,5];
% y_blue = [1,10,1,8,5,4,15,12,7,15,17,5,4,15];
% x_red = [2,12,4,3,8,12,4,5,6,5,4,15];
% y_red = [2,12,5,16,0,1,4,6,7,10,5,9];
%-----------------------------------------------------
% x_blue = [5,5,10,9,3,11,15,3,6,8,1,8,5,12,22,14,18,5,14,12,0,1,10,18,15,12,7,15,19,34];
% y_blue = [11,2,1,5,4,25,13,7,15,17,5,4,15,27,5,6,13,8,12,15,12,3,8,12,5,16,19,22,24,25];
% x_red = [2,22,3,8,12,1,35,19,1,21];
% y_red = [2,12,16,0,1,20,8,17,2,1];
% x_blue = [5,5,10,9,3,11,15,3,6,8,1,8,5,12,22,14,18,5,14,12,0,1,10,18,15,12,7,15,19,34,25,28,35,44,56,61];
% y_blue = [11,2,1,5,4,25,13,7,15,17,5,4,15,27,5,6,13,8,12,15,12,3,8,12,5,16,19,22,24,25,2,4,7,23,25,26];
% x_red = [2,22,3,8,12,1,35,19,1,21];
% y_red = [2,12,16,0,1,20,8,17,2,1];
% x_blue = [5,5,10,9,3,11,15,3,6,8,1,8,5,12,22,14,18,5,14,12,0,1,10,18,15,12,7,15,19,34,25,28,35,44,56,61,2,3,66,57,71,73,77,57,69,56,81,82,84,86,87];
% y_blue = [11,2,1,5,4,25,13,7,15,17,5,4,15,27,5,6,13,8,12,15,12,3,8,12,5,16,19,22,24,25,2,4,7,23,25,26,55,57,69,56,77,74,71,66,57,71,35,44,56,61,54];
% x_red = [2,22,3,8,12,1,35,19,1,21,24,26,31,35,42,47,51,53,67,68,82,85,89,91,93,95,81,82,86,87,89,65,75,78,85,80,77,82,85,87,9,7,8,10,5];
% y_red = [2,12,16,0,1,20,8,17,2,14,51,53,67,68,24,26,31,35,42,47,71,73,75,76,78,79,32,42,52,60,50,80,81,83,79,5,7,8,10,9,80,77,82,85,87];