├── K_out_n_GA.m ├── Manoj_CostFun.m ├── PermutationCrossover.m ├── PermutationMutate.m ├── README.md └── RouletteWheelSelection.m /K_out_n_GA.m: -------------------------------------------------------------------------------- 1 | 2 | clc 3 | clear 4 | close all 5 | 6 | %% Problem Definition 7 | 8 | 9 | CostFunction = @ Manoj_CostFun; % Objective Function 10 | K = 15; 11 | nVar = 4; % Number of Decision Variables 12 | VarSize = [1 nVar]; % Decision Variables Matrix Size 13 | 14 | 15 | %% GA Parameters 16 | 17 | MaxIt=1500; % Maximum Number of Iterations 18 | 19 | nPop=1500; % Population Size 20 | 21 | pc=0.4; % Crossover Percentage 22 | nc=2*round(pc*nPop/2); % Number of Offsprings (Parents) 23 | 24 | pm=0.8; % Mutation Percentage 25 | nm=round(pm*nPop); % Number of Mutants 26 | 27 | beta=5; % Selection Pressure 28 | 29 | %% Initialization 30 | 31 | % Create Empty Structure 32 | empty_individual.Position=[]; 33 | empty_individual.Cost=[]; 34 | empty_individual.Sol=[]; 35 | 36 | 37 | % Create Population Matrix (Array) 38 | pop=repmat(empty_individual,nPop,1); 39 | 40 | % Initialize Population 41 | for i=1:nPop 42 | 43 | % Initialize Position 44 | pop(i).Position=randperm(K,nVar); 45 | 46 | % Evaluation 47 | pop(i).Cost=CostFunction(pop(i).Position); 48 | 49 | end 50 | 51 | % Sort Population 52 | Costs=[pop.Cost]; 53 | [Costs, SortOrder]=sort(Costs); 54 | pop=pop(SortOrder); 55 | 56 | % Update Best Solution Ever Found 57 | BestSol=pop(1); 58 | 59 | % Update Worst Cost 60 | WorstCost=max(Costs); 61 | 62 | % Array to Hold Best Cost Values 63 | BestCost=zeros(MaxIt,1); 64 | 65 | 66 | %% GA Main Loop 67 | 68 | for it=1:MaxIt 69 | 70 | % Calculate Selection Probabilities 71 | P=exp(-beta*Costs/WorstCost); 72 | P=P/sum(P); 73 | 74 | % Crossover 75 | popc=repmat(empty_individual,nc/2,2); 76 | for k=1:nc/2 77 | 78 | % Select Parents 79 | i1=RouletteWheelSelection(P); 80 | i2=RouletteWheelSelection(P); 81 | p1=pop(i1); 82 | p2=pop(i2); 83 | 84 | % Apply Crossover 85 | [popc(k,1).Position, popc(k,2).Position]=PermutationCrossover(p1.Position,p2.Position); 86 | 87 | % Evaluate Offsprings 88 | popc(k,1).Cost=CostFunction(popc(k,1).Position); 89 | popc(k,2).Cost=CostFunction(popc(k,2).Position); 90 | 91 | end 92 | popc=popc(:); 93 | 94 | % Mutation 95 | popm=repmat(empty_individual,nm,1); 96 | for k=1:nm 97 | 98 | % Select Parent Index 99 | i=randi([1 nPop]); 100 | 101 | % Select Parent 102 | p=pop(i); 103 | 104 | % Apply Mutation 105 | popm(k).Position=PermutationMutate(p.Position); 106 | 107 | % Evaluate Mutant 108 | popm(k).Cost=CostFunction(popm(k).Position); 109 | 110 | end 111 | 112 | % Merge Population 113 | pop=[pop 114 | popc 115 | popm]; %#ok 116 | 117 | % Sort Population 118 | Costs=[pop.Cost]; 119 | [Costs, SortOrder]=sort(Costs); 120 | pop=pop(SortOrder); 121 | 122 | % Truancate Extra Memebrs 123 | pop=pop(1:nPop); 124 | Costs=Costs(1:nPop); 125 | 126 | % Update Best Solution Ever Found 127 | BestSol=pop(1); 128 | 129 | % Update Worst Cost 130 | WorstCost=max(WorstCost,max(Costs)); 131 | 132 | % Update Best Cost Ever Found 133 | BestCost(it)=BestSol.Cost; 134 | 135 | % Show Iteration Information 136 | disp(['Iteration ' num2str(it) ': Best Cost = ' num2str(BestCost(it))]); 137 | 138 | end 139 | 140 | %% Results 141 | 142 | figure; 143 | plot(BestCost,'LineWidth',2); 144 | xlabel('Iteration'); 145 | ylabel('Best Cost'); 146 | 147 | -------------------------------------------------------------------------------- /Manoj_CostFun.m: -------------------------------------------------------------------------------- 1 | function Z = Manoj_CostFun(P) 2 | 3 | L =[0.52 0.56 0.58 0.6 0.72 0.74 0.78 0.8 0.82 0.84 0.85 0.86 0.88 0.9 0.92]; 4 | t = 10; 5 | 6 | R = exp(-L*t); 7 | Rstar = 5e-7; 8 | C = [80000 82000 78000 75000 85000 64000 72000 69000 84000 87000 92000 85000 74000 95000 89000]; 9 | 10 | Cf = 332000; 11 | 12 | C_P = C(P); 13 | R_P = R(P); 14 | R1 = R_P(1); R2 = R_P(2); R3 = R_P(3); R4 = R_P(4); 15 | 16 | 17 | Rs = R1*R2+R1*R3+R1*R4+R2*R3+R2*R4+R3*R4-R1*R2*R3-R1*R3*R4-R2*R3*R4+R1*R2*R3*R4; 18 | 19 | if Rs>Rstar 20 | Z = vpa(sum(C_P)+(1-Rs)*Cf); 21 | else 22 | Z = 1e+20; 23 | 24 | end 25 | -------------------------------------------------------------------------------- /PermutationCrossover.m: -------------------------------------------------------------------------------- 1 | % Permutation Crossover 2 | 3 | function [y1, y2]=PermutationCrossover(x1,x2) 4 | 5 | nVar=numel(x1); 6 | 7 | c=randi([1 nVar-1]); 8 | 9 | x11=x1(1:c); 10 | x12=x1(c+1:end); 11 | 12 | x21=x2(1:c); 13 | x22=x2(c+1:end); 14 | 15 | r1=intersect(x11,x22); 16 | r2=intersect(x21,x12); 17 | % x11(ismember(x11,r1)) 18 | % x11(ismember(x11,r1))=r2 19 | % x21(ismember(x21,r2))=r1 20 | 21 | if numel(x11(ismember(x11,r1)))==r2 & numel(x21(ismember(x21,r2)))==r1 22 | x11(ismember(x11,r1))=r2; 23 | x21(ismember(x21,r2))=r1; 24 | y1=[x11 x22]; 25 | y2=[x21 x12]; 26 | else 27 | y1=x1; 28 | y2 = x2; 29 | end 30 | 31 | 32 | 33 | 34 | 35 | end -------------------------------------------------------------------------------- /PermutationMutate.m: -------------------------------------------------------------------------------- 1 | % Permutation Mutation 2 | 3 | function y=PermutationMutate(x) 4 | 5 | M=randi([1 3]); 6 | 7 | switch M 8 | case 1 9 | % Swap 10 | y=DoSwap(x); 11 | 12 | case 2 13 | % Reversion 14 | y=DoReversion(x); 15 | 16 | case 3 17 | % Insertion 18 | y=DoInsertion(x); 19 | 20 | end 21 | 22 | end 23 | 24 | function y=DoSwap(x) 25 | 26 | n=numel(x); 27 | 28 | i=randsample(n,2); 29 | i1=i(1); 30 | i2=i(2); 31 | 32 | y=x; 33 | y([i1 i2])=x([i2 i1]); 34 | 35 | end 36 | 37 | function y=DoReversion(x) 38 | 39 | n=numel(x); 40 | 41 | i=randsample(n,2); 42 | i1=min(i(1),i(2)); 43 | i2=max(i(1),i(2)); 44 | 45 | y=x; 46 | y(i1:i2)=x(i2:-1:i1); 47 | 48 | end 49 | 50 | function y=DoInsertion(x) 51 | 52 | n=numel(x); 53 | 54 | i=randsample(n,2); 55 | i1=i(1); 56 | i2=i(2); 57 | 58 | if i1