-
Notifications
You must be signed in to change notification settings - Fork 0
/
otimizacao_estudoCaso1.py
215 lines (167 loc) · 6.22 KB
/
otimizacao_estudoCaso1.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#%%
# Otimização estrutural
## Pequena otimização estrutural usando elementos de treliça 3d e a biblioteca de algoritmos géneticos DEAP. A inteção aqui era minimizar a tensão maxima de von misses fixando um numero maximo de elementos de area com seção de 0.1 mm^2,simulando um experimento muito comum nas faculdades a ponte de macarrão.
#%%
from otimizacao_estudoCaso2 import AREA_REF
import random
import multiprocessing
from Treelicia import Fem3d
import numpy as np
import plotter3D as plott
from deap import algorithms
from deap import base
from deap import creator
from deap import tools
L = 1000
nodes = (
(0, 0, 0, 0 ),
(1, -L/2, 0, np.sqrt(3)*L/2 ),
(2, -L, 0, 0 ),
(3, -L/2, L, np.sqrt(3)*L/2 ),
)
elementos = (
(0, 1, 0),
(1, 1, 2),
(2, 0, 2),
(3, 0, 3),
(4, 1, 3),
(5, 2, 3),
)
## forcas
forcas = ( #(no, grau_liberdade, forca)
(3,2,-100.0),
)
## condicao de contorno
contorno = ( #(no, grau_liberdade, cc)
(0,0,0),
(0,1,0),
(0,2,0),
(1,0,0),
(1,1,0),
(1,2,0),
(2,0,0),
(2,1,0),
(2,2,0),
)
E2 = 3600#Mpa do macarrao
#rho = [2.76e-3, 2.86e-3, 2.96e-3, 2.66e-3, 2.56e-3, 2.46e-3]#2.76e-3
rho = 2.66e-3
CargaRuptura = 42.6 #N
MIN_ATRIB = 3
MAX_ATRIB = 10
AREA_REF = 0.2
def fitness(individual):
individual = np.array(individual)
#crossSection do macarrao 1.2 #mm^2
try:
A2 = AREA_REF*individual
model1 = Fem3d(nodes,elementos,forcas,contorno,E2,A2,rho)
Deslocamento, reacoes = model1.solve()
tensoes = model1.getStress(deslo= Deslocamento)
except:
return 10**12,
return max(np.abs(tensoes)),
def volume(individuo):
volu = 0
ltotal = 0
for index, elem in enumerate(elementos):
# index é o elemento
x = nodes[elem[1]][1] - nodes[elem[2]][1]
y = nodes[elem[1]][2] - nodes[elem[2]][2]
z = nodes[elem[1]][3] - nodes[elem[2]][3]
L = np.sqrt(x**2+y**2+z**2)
ltotal += L
volu += AREA_REF*individuo[index]*L
return volu
def checkGeometria(individuo):
# pacote de macarrao tem 500 fios
vol = volume(individuo)
#print(vol)
if (vol > 0. and vol <= 7000.):
return True
return False
creator.create("FitnessMax", base.Fitness, weights=(-1.0,))
creator.create("Individual", list, fitness=creator.FitnessMax)
toolbox = base.Toolbox()
toolbox.register("attr_bool", random.uniform, MIN_ATRIB, MAX_ATRIB)
toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_bool, n=6)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
toolbox.register("evaluate", fitness)
#toolbox.decorate("evaluate", tools.DeltaPenalty(checkGeometria, [10**10])) # funcao de penalidade da conflito com multithreads
toolbox.register("mate", tools.cxTwoPoint)
toolbox.register("mutate", tools.mutUniformInt, low = MIN_ATRIB, up = MAX_ATRIB, indpb = 0.08)
toolbox.register("select", tools.selTournament, tournsize=5)
def main():
random.seed(64)
pool = multiprocessing.Pool(processes=4)
toolbox.register("map", pool.map)
pop = toolbox.population(n=300)
hof = tools.HallOfFame(15)
stats = tools.Statistics(lambda ind: ind.fitness.values)
stats.register("avg", np.mean)
stats.register("std", np.std)
stats.register("min", np.min)
stats.register("max", np.max)
pop, logger = algorithms.eaSimple(pop, toolbox, cxpb=0.5, mutpb=0.2, ngen=100,stats=stats, halloffame=hof, verbose=True)
return [pop, logger, hof]
if __name__ == "__main__":
import matplotlib.pyplot as plt
import plotter3D as plott
import time
start = time.time()
[pop, logger, hof] = main()
finish = time.time()
print("tempo final: ",finish-start)
gen = logger.select("gen")
fit = logger.select("min")
avg = logger.select("avg")
plt.plot(gen,fit, '--r', label = "melhores individuos")
plt.plot(gen,avg, '--b', label = "media dos individuos")
plt.xlabel('fitness')
plt.ylabel('geração')
plt.legend()
plt.title('Pontuação dos melhores individuos')
plt.grid()
plt.show()
best = hof.items[0]
print()
print("Melhor Solucao = ", best, sum(best))
print("Fitness do melhor individuo = ", best.fitness.values[0])
A2 = [0.302, 0.729, 0.187, 0.302, 0.729, 0.187]
model1 = Fem3d(nodes,elementos,forcas,contorno,E2,A2,rho)
Deslocamento, reacoes = model1.solve()
tensoes = model1.getStress(deslo= Deslocamento)
pos = plott.Posprocess(model1)
pos.plotStress3D(tensoes, var='[MPa]')
# printando os resultados
print('=============================================')
print('\t\tDeslocamentos')
print('=============================================')
print(f'GL \t\t Desl. [mm] \t\t Reacoes [N]')
for index in range(len(Deslocamento)):
print('{0:1.2f} \t\t {1:4.4f} \t\t {2:4.4f}'.format(index, Deslocamento[index], reacoes[index]))
print('=============================================')
print('\t\tTensões')
print('=============================================')
print(f'Elemento \t\t Tensão [Pa] \t\t Area [mm]')
for index in range(len(tensoes)):
print('{0:1.2f} \t\t {1:4.4f} \t\t {2:4.4f} '.format(index, tensoes[index],A2[index]))
#-------- otimizacao
model2 = Fem3d(nodes,elementos,forcas,contorno,E2,AREA_REF*np.array(best),rho)
Deslocamento2, reacoes2 = model2.solve()
tensoes2 = model2.getStress(deslo= Deslocamento2)
pos = plott.Posprocess(model2)
pos.plotStress3D(tensoes2,var='[MPa]')
# printando os resultados
print('=============================================')
print('\t\tDeslocamentos')
print('=============================================')
print(f'GL \t\t Desl. [mm] \t\t Reacoes [N]')
for index in range(len(Deslocamento2)):
print('{0:1.2f} \t\t {1:4.4f} \t\t {2:4.4f}'.format(index, Deslocamento2[index], reacoes2[index]))
print('=============================================')
print('\t\tTensões')
print('=============================================')
print(f'Elemento \t\t Tensão [Pa] \t\t Area [mm] ')
for index in range(len(tensoes2)):
print('{0:1.2f} \t\t {1:4.4f} \t\t {2:4.4f} '.format(index, tensoes2[index], AREA_REF*best[index]))