-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plot.py
124 lines (88 loc) · 2.77 KB
/
Plot.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
import matplotlib.pyplot as plt
import numpy as np
import os
import argparse
outputPath = "./Graficos/"
def readfile(filename):
buffer = None
with open(filename, 'r') as f:
buffer = f.read()
buffer = buffer.split('\n')
buffer.pop(-1)
return buffer
def parseFile(filename: str):
file = readfile(filename)
generations = []
fit = []
mean = []
for element in file:
element = element.split(',')
generations.append(int(element[0]))
fit.append(float(element[1]))
mean.append(float(element[2]))
return generations, fit, mean
def plotSimplesComMedia(g, f, m, name):
fig, ax = plt.subplots()
fig.set_figheight(8)
fig.set_figwidth(20)
ax.plot(g, f, color='b', label="Fitness")
ax.plot(g, m, color='orange', label="Mean")
ax.legend()
ax.set_title(name + " with mean")
ax.set_xlabel("Generations")
ax.set_ylabel("Fitness")
plt.savefig(outputPath + name + '_WithMean.png')
def plotSimplesSemMedia(g, f, name):
fig, ax = plt.subplots()
fig.set_figheight(8)
fig.set_figwidth(20)
ax.plot(g, f, color='b', label="Fitness")
ax.legend()
ax.set_title(name)
ax.set_xlabel("Generations")
ax.set_ylabel("Fitness")
plt.savefig(outputPath + name + '_Original.png')
def plotConjuntoTotal(g, f, names, save_name):
fig, ax = plt.subplots()
fig.set_figheight(8)
fig.set_figwidth(20)
for gen, fit, name in zip(g, f, names):
ax.plot(gen, fit, label=name)
ax.legend()
ax.set_xlabel("Generations")
ax.set_ylabel("Fitness")
plt.savefig(outputPath + save_name + '.png')
def main(content: list, fpath: str):
content = [e for e in content if not e.endswith(".png")]
allGen = []
allFit = []
allName = []
eli_Gen = []
eli_Fit = []
eli_names = []
tor_Gen = []
tor_Fit = []
tor_names = []
for filename in content:
generations, fit, mean = parseFile(fpath + filename)
name = filename.replace(".txt", "")
if(name.startswith("eli")):
eli_Gen.append(generations)
eli_Fit.append(fit)
eli_names.append(name)
else:
tor_Gen.append(generations)
tor_Fit.append(fit)
tor_names.append(name)
allGen.append(generations)
allFit.append(fit)
allName.append(filename.replace(".txt", ""))
plotSimplesComMedia(generations, fit, mean, name)
plotSimplesSemMedia(generations, fit, name)
plotConjuntoTotal(allGen, allFit, allName, "Todas")
plotConjuntoTotal(eli_Gen, eli_Fit, eli_names, "SoElitismo")
plotConjuntoTotal(tor_Gen, tor_Fit, tor_names, "soTorneio")
if '__main__' == __name__:
path = "./data/"
main(os.listdir(path), path)
pass