-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_model.py
executable file
·120 lines (105 loc) · 4.42 KB
/
file_model.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
import os
import json
import csv
import numpy as np
from matplotlib import pyplot as plt
class FileModel:
'''
This class for opening or saving model of population or properties from genetic AI
'''
def __init__(self, model_name):
self.dict = os.getcwd()
self.model_name = model_name
self.name_population = self.model_name + '_population.csv'
self.name_properties = self.model_name + '_properties.json'
os.system("mkdir -p genetic_data/"+self.model_name)
os.system("mkdir -p genetic_data/"+self.model_name+"/graph")
os.system("mkdir -p genetic_data/"+self.model_name+"/step")
def save_population_to_model(self, population):
temp_population = []
for i in range(len(population)):
temp_population.append(i)
temp_population[i] = [
population[i].kp,
population[i].ki,
population[i].kd,
population[i].fitness,
population[i].risetime,
population[i].overshoot,
population[i].settling_time,
population[i].peak,
population[i].steadystate,
population[i].creator,
population[i].saved
]
with open(self.dict + "/genetic_data/" + self.model_name + "/" + self.name_population, 'w') as population_file:
population_writer= csv.writer(population_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
for i in range(len(population)):
population_writer.writerow(temp_population[i])
def save_properties_to_model(self, properties):
with open(self.dict + "/genetic_data/" + self.model_name + "/" + self.name_properties, 'w') as fp:
json.dump(properties, fp)
def save_individu_to_model(self, individu):
temp_population = [
individu.kp,
individu.ki,
individu.kd,
individu.fitness,
individu.risetime,
individu.overshoot,
individu.settling_time,
individu.peak,
individu.steadystate,
individu.creator,
individu.saved
]
os.system("mkdir -p genetic_data/"+self.model_name)
with open(self.dict + "/genetic_data/" + self.model_name + "/" + self.name_population, 'a') as population_file:
population_writer= csv.writer(population_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
population_writer.writerow(temp_population)
def save_individu_to_graph(self, x_list, y_list, setpoint, name):
sp_list = np.full(len(x_list), setpoint)
plt.figure(name)
plt.plot(x_list, y_list, label="control data")
plt.plot(x_list, sp_list, label="setpoint")
plt.savefig(self.dict + "/genetic_data/" + self.model_name + "/graph/" + str(name) + ".png")
plt.close(name)
temp_step= []
for i in range(len(x_list)):
temp_step.append(i)
temp_step[i] = [
x_list[i],
y_list[i]
]
with open(self.dict + "/genetic_data/" + self.model_name + "/step/" + str(name) + ".csv", 'w') as step_file:
step_writer = csv.writer(step_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
for i in range(len(x_list)):
step_writer.writerow(temp_step[i])
def open_population_from_model(self):
population_temp = []
fol = self.dict + "/genetic_data/" + self.model_name + "/" + self.name_population
with open(fol) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
line_count = 0
for row in csv_reader:
population_temp.append(1)
population_temp[line_count] = [
row[0],
row[1],
row[2],
row[3],
row[4],
row[5],
row[6],
row[7],
row[8],
row[9],
row[10]
]
line_count += 1
return population_temp
def open_properties_from_model(self):
fol = self.dict + "/genetic_data/" + self.model_name + "/" + self.name_properties
with open(fol) as json_file:
pooldata = json.load(json_file)
return pooldata