-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrossoverPop.m
More file actions
45 lines (35 loc) · 808 Bytes
/
crossoverPop.m
File metadata and controls
45 lines (35 loc) · 808 Bytes
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
function [O] = crossoverPop(P, pc, alfa)
%%%%%%Input:
%P = parents
%pc = crossover probability
%%%%%%Output:
%O = offspring
O = P;
[nInd,~] = size(P);
perm = randperm(nInd);
if (mod(nInd,2) == 1) %daca ultimul individ nu are pereche
nInd = nInd-1;
end
for i = 1:2:nInd
r = unifrnd(0,1);
if (r <= pc)
px = P(perm(i),:);
py = P(perm(i+1),:);
cx = crossoverReal(px, py, alfa);
cy = crossoverReal(py, px, alfa);
O(perm(i), :) = cx;
O(perm(i+1), :) = cy;
end
end
end
function[z] = crossoverReal(x, y, alfa)
%x, y - parents
%z - child
% alfa = unifrnd(0,1);
global n;
z=zeros(1,n);
for i=1:n-1
z(i) = alfa*x(i) + (1-alfa)*y(i);
end
z = checkFez_decision(z,x);
end