-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluation.py
178 lines (133 loc) · 5.27 KB
/
evaluation.py
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# -----------------------------------------------------------
# This module provides evaluation functions for each parameter.
#
# Each evaluation function:
# (1) Runs genetic algorithm on defined parameters
# (2) Calculates running time
# (3) Calculates success rate (where optimum solution is found)
# (4) Creates a matplotlib figure (pyplot)
# (5) Plots running time and success rate against the parameter
#
#
# (C) 2020 Muhammad Bilal Akmal, 17K-3669
# -----------------------------------------------------------
import time
import matplotlib.pyplot as plt
import numpy as np
from genetic_algorithm import GeneticAlgorithm
from parameters import Parameters
def _run_trials(parameters: Parameters, trials: int):
results = np.empty(shape=(trials,), dtype=bool)
ga = GeneticAlgorithm(parameters)
start_time = time.time()
for i in range(trials):
results[i] = ga.run()
finish_time = time.time()
average_time = (finish_time-start_time) / trials
success_rate = np.count_nonzero(results) / trials
return (average_time, success_rate)
def evaluate_chromosome_length(
start, stop, step, pop_size, max_gens, trials):
# calculate number of iterations [performance]
iterations = (lambda x,y,z: (y-x)//z + ((y-x)%z>0))(start, stop, step)
chr_lnth = np.empty((iterations,), dtype=int)
completion_times = np.empty((iterations,), dtype=float)
success_rates = np.empty((iterations,), dtype=float)
for idx, value in enumerate(range(start, stop, step)):
parameters = Parameters(value, pop_size, max_gens)
chr_lnth[idx] = value
results = _run_trials(parameters, trials)
completion_times[idx] = results[0]
success_rates[idx] = results[1]
# convert rates to percentages
success_rates *= 100
# plot Completion Times
plt.subplot(211)
plt.title('Effect Of Chromosome Length On Performance.',
style='italic', fontsize=14) # title
plt.plot(chr_lnth, completion_times, 'b.-', label='completion time (s)')
plt.legend()
# plot Success Rates
plt.subplot(212)
plt.xlabel('Chromosome length [bits]')
plt.plot(chr_lnth, success_rates, 'r.-', label='success rate (%)')
plt.legend()
#Show parameters
plt.suptitle(
f'Range({start},{stop},{step}) '
f'| Population: {pop_size} '
f'| Maximum Generations: {max_gens} '
f'| Trials: {trials}',
fontsize=10, fontweight='light', color='brown'
)
plt.show(block=False)
def evaluate_population_size(
start, stop, step, max_gens, chr_lnth, trials):
# calculate number of iterations [performance]
iterations = (lambda x,y,z: (y-x)//z + ((y-x)%z>0))(start, stop, step)
pop_size = np.empty((iterations,), dtype=int)
completion_times = np.empty((iterations,), dtype=float)
success_rates = np.empty((iterations,), dtype=float)
for idx, value in enumerate(range(start, stop, step)):
parameters = Parameters(chr_lnth, value, max_gens)
pop_size[idx] = value
results = _run_trials(parameters, trials)
completion_times[idx] = results[0]
success_rates[idx] = results[1]
# convert rates to percentages
success_rates *= 100
# plot Completion Times
plt.subplot(211)
plt.title('Effect Of Population Size On Peformance.',
style='italic', fontsize=14) # title
plt.plot(pop_size, completion_times, 'b.-', label='completion time (s)')
plt.legend()
# plot Success Rates
plt.subplot(212)
plt.xlabel('Population size [units]')
plt.plot(pop_size, success_rates, 'r.-', label='success rate (%)')
plt.legend()
#Show parameters
plt.suptitle(
f'Range({start},{stop},{step}) '
f'| Chromosome Length: {chr_lnth} '
f'| Maximum Generations: {max_gens} '
f'| Trials: {trials}',
fontsize=10, fontweight='light', color='brown'
)
plt.show(block=False)
def evaluate_maximum_generations(
start, stop, step, chr_lnth, pop_size, trials):
# calculate number of iterations [performance]
iterations = (lambda x,y,z: (y-x)//z + ((y-x)%z>0))(start, stop, step)
max_gens = np.empty((iterations,), dtype=int)
completion_times = np.empty((iterations,), dtype=float)
success_rates = np.empty((iterations,), dtype=float)
for idx, value in enumerate(range(start, stop, step)):
parameters = Parameters(chr_lnth, pop_size, value)
max_gens[idx] = value
results = _run_trials(parameters, trials)
completion_times[idx] = results[0]
success_rates[idx] = results[1]
# convert rates to percentages
success_rates *= 100
# plot Completion Times
plt.subplot(211)
plt.title('Effect Of Maximum Generations On Peformance.',
style='italic', fontsize=14) # title
plt.plot(max_gens, completion_times, 'b.-', label='completion time (s)')
plt.legend()
# plot Success Rates
plt.subplot(212)
plt.xlabel('Maximum generations [units]')
plt.plot(max_gens, success_rates, 'r.-', label='success rate (%)')
plt.legend()
#Show parameters
plt.suptitle(
f'Range({start},{stop},{step}) '
f'| Chromosome Length: {chr_lnth} '
f'| Population: {pop_size} '
f'| Trials: {trials}',
fontsize=10, fontweight='light', color='brown'
)
plt.show(block=False)