-
Notifications
You must be signed in to change notification settings - Fork 0
/
variation.m
91 lines (84 loc) · 2.2 KB
/
variation.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
function newPopulation = variation(P,maskFitness)
%VARIATION 此处显示有关此函数的摘要
% 此处显示详细说明
[populationSize, numOfVariables] = size(P);
O = zeros(populationSize/2, numOfVariables);
i = 1;
while size(P,1) > 0
populationSize = size(P,1);
[m,n] = getTwoRandValue(populationSize);
p = P(m,:);
q = P(n,:);
if m < n
P(n,:)=[];
P(m,:)=[];
else
P(m,:)=[];
P(n,:)=[];
end
o = p;
% 交叉
if rand() < 0.5
goal = p & (1-q);
num = sum(goal);
if num >0
Y = find(goal==1);
X = randperm(num);
m = Y(X(1));
n = Y(X(end));
if maskFitness(m) > maskFitness(n)
o(m) = 0;
else
o(n) = 0;
end
end
else
goal = (1-p) & q;
num = sum(goal);
if num >0
Y = find(goal==1);
X = randperm(num);
m = Y(X(1));
n = Y(X(end));
if maskFitness(m) < maskFitness(n)
o(m) = 1;
else
o(n) = 1;
end
end
end
% 变异
if rand() < 0.5
goal = o;
num = sum(goal);
if num >0
Y = find(goal==1);
X = randperm(num);
m = Y(X(1));
n = Y(X(end));
if maskFitness(m) > maskFitness(n)
o(m) = 0;
else
o(n) = 0;
end
end
else
goal = 1 - o;
num = sum(goal);
if num >0
Y = find(goal==1);
X = randperm(num);
m = Y(X(1));
n = Y(X(end));
if maskFitness(m) < maskFitness(n)
o(m) = 1;
else
o(n) = 1;
end
end
end
O(i,:)=o;
i=i+1;
end
newPopulation = O;
end