-
Notifications
You must be signed in to change notification settings - Fork 1
/
fitnessfunctions.rb
95 lines (91 loc) · 2.38 KB
/
fitnessfunctions.rb
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
#Fitness A: high overall SC
def f_high(chrom, act)
# genotype -> phenotype transfer
activity = Array.new
while (a=chrom.take(act)) != [] do
activity << a
chrom = chrom.drop(act)
end
# SC calculation
sc = Array.new
activity.each do |output_vec|
activity.each do |activity_vec|
sc<<ochiai(activity_vec, output_vec)
end
end
# fitness: sum up sc values
fitness = sc.inject{|sum,x| sum + x}
return fitness
end
# Fitness B: discriminable SC
def f_discrim(chrom, act, diff=:high)
# genotype -> phenotype transfer
# same as f_high()
...
# SC calculation
# same as f_high()
...
# fitness: discriminiable SC
highest_sc = (sc.sort!)[-1]
pivot = sc.find_index(highest_sc)
low_sc = sc[0..pivot-1]
top_sc = sc[pivot..-1]
sum_top = top_sc.inject {|sum,x| sum+x}
sum_low = low_sc.inject {|sum,x| sum+x}
return sum_top - sum_low if diff==:high
return sum_low - sum_top if diff==:low
end
# Fitness C: random intermittency
def f_randinterm(chrom, activ, diff=:high)
# genotype -> phenotype transfer
# same as f_high()
...
# SC calculation
sc = Array.new
activity.each do |output_vec|
output_vec.remove_all_ones_except_rand(3)
activity.each do |activity_vec|
sc<<ochiai(activity_vec,output_vec)
end
end
# fitness: discriminable
# same as f_discrim()
...
end
# Fitness D: constant intermittency
def f_constinterm(chrom, activ, diff=:high)
# genotype -> phenotype transfer
# same as f_high()
...
# SC calculation with const. output vector
output_vec = [0,0,0,1,0,0,0,0,0,1,0,0,0,...]
activity.each do |activity_vec|
sc<<ochiai(activity_vec,output_vec)
end
# fitness: discriminable
# same as f_discrim()
...
end
# Fitness E: discr. SC
# with output vector
def f_discrout(chrom, act, diff=:low)
# genotype -> phenotype transfer
# same as f_discrim()
...
# SC calculation
# ouput -> last comp act vector
output = activity_matrix[-1]
activity_matrix.delete_at(-1)
sc = Array.new
activity_matrix.each do |activ|
sc << ochiai(activ, output)
end
# fitness: discriminable SC
top_sc = (sc.sort!)[-1]
top_cnt = sc.count(top_sc)
low_sc = sc[0..-2]
sum_low = low_sc.inject {|sum,x| sum+x}
return (top_sc - sum_low) / top_cnt if diff==:low
return (sum_low - top_sc) / top_cnt if diff==:high
#return (sum_low - top_sc) / (top_cnt + output.count(1)) # favor. low num. of failures
end