-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.py
65 lines (48 loc) · 2.02 KB
/
graph.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
# graph.py
import matplotlib.pyplot as plt
import os
import config
from algorithm import main as run
# Graph files directory
dir = 'temp'
# Tests properties
parameter_name = 'crossover_type'
parameter_values = ["single_point_crossover", "two_point_crossover", "uniform_crossover"]
# Function to change config
def update_config(new_config):
config.population_size = new_config.get('population_size', config.population_size)
config.generations_size = new_config.get('generations_size', config.generations_size)
config.mutation_rate = new_config.get('mutation_rate', config.mutation_rate)
config.elitism_rate = new_config.get('elitism_rate', config.elitism_rate)
config.future_generation = new_config.get('future_generation', config.future_generation)
config.selection_type = new_config.get('selection_type', config.selection_type)
config.crossover_type = new_config.get('crossover_type', config.crossover_type)
# Length, height and rocks as in config.py
# Function to collect data
def test(parameter_name, parameter_values):
all_fitnesses = []
for value in parameter_values:
new_config = {parameter_name: value}
update_config(new_config)
fitnesses, fitness = run(need_print=False)
all_fitnesses.append((value, fitnesses))
return all_fitnesses
# Function to make graph
def plot_fitnesses(all_fitnesses, parameter_name):
plt.figure()
for value, fitnesses in all_fitnesses:
generations = range(1, len(fitnesses) + 1)
plt.plot(generations, fitnesses, label=f"{parameter_name} = {value}")
plt.xlabel('Generation')
plt.ylabel('Fitness')
plt.title(f'Fitness evolution depends on {parameter_name}')
plt.legend()
plt.grid(True)
if not os.path.exists(dir):
os.makedirs(dir)
filename = os.path.join(dir, f'fitness_evolution_{parameter_name}.png')
plt.savefig(filename)
plt.show()
if __name__ == "__main__":
all_fitnesses = test(parameter_name, parameter_values)
plot_fitnesses(all_fitnesses, parameter_name)