/
geso.jl
353 lines (330 loc) · 9.19 KB
/
geso.jl
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
@params mutable struct GESOResult{T}
topology::AbstractVector{T}
objval::T
change::T
converged::Bool
fevals::Int
end
"""
The GESO algorithm, see [LiuYiLiShen2008](@cite).
"""
struct GESO <: TopOptAlgorithm
comp::Compliance
vol::Volume
vol_limit::Any
filter::Any
vars::AbstractVector
topology::AbstractVector
Pcmin::Any
Pcmax::Any
Pmmin::Any
Pmmax::Any
Pen::Any
string_length::Int
var_volumes::AbstractVector
cum_var_volumes::AbstractVector
order::AbstractVector{Int}
genotypes::BitArray{2}
children::BitArray{2}
var_black::BitVector
maxiter::Int
penalty::Any
sens::AbstractVector
old_sens::AbstractVector
obj_trace::MVector{10}
tol::Any
sens_tol::Any
result::GESOResult
end
Base.show(::IO, ::MIME{Symbol("text/plain")}, ::GESO) = println("TopOpt GESO algorithm")
function GESO(
comp::Compliance,
vol::Volume,
vol_limit,
filter;
maxiter=1000,
tol=0.001,
p=3.0,
Pcmin=0.6,
Pcmax=1.0,
Pmmin=0.5,
Pmmax=1.0,
Pen=3.0,
sens_tol=tol / 100,
string_length=4,
k=10,
)
penalty = comp.solver.penalty
setpenalty!(penalty, p)
solver = comp.solver
T = eltype(solver.vars)
nel = Ferrite.getncells(solver.problem.ch.dh.grid)
@unpack white, black = solver.problem
nvars = nel - sum(black) - sum(white)
vars = zeros(T, nvars)
topology = zeros(T, nel)
result = GESOResult(topology, T(NaN), T(NaN), false, 0)
sens = zeros(T, nvars)
old_sens = zeros(T, nvars)
obj_trace = zeros(MVector{k,T})
var_volumes = vol.cellvolumes[.!black .& .!white]
cum_var_volumes = zeros(T, nvars)
order = zeros(Int, nvars)
genotypes = trues(string_length, nvars)
children = trues(string_length, nvars)
var_black = trues(nvars)
return GESO(
comp,
vol,
vol_limit,
filter,
vars,
topology,
Pcmin,
Pcmax,
Pmmin,
Pmmax,
Pen,
string_length,
var_volumes,
cum_var_volumes,
order,
genotypes,
children,
var_black,
maxiter,
penalty,
sens,
old_sens,
obj_trace,
tol,
sens_tol,
result,
)
end
function Utilities.setpenalty!(b::GESO, p::Number)
b.penalty.p = p
return b
end
function get_progress(current_volume, total_volume, design_volume)
return clamp(
min(
(total_volume - current_volume) / (total_volume - design_volume),
current_volume / design_volume,
),
0,
1,
)
end
function get_probs(b::GESO, Prg)
return (
b.Pcmin + (b.Pcmax - b.Pcmin) * Prg^b.Pen, b.Pmmin + (b.Pmmax - b.Pmmin) * Prg^b.Pen
)
end
function crossover!(children, genotypes, i, j)
for k in 1:size(genotypes, 1)
r = rand()
if r < 0.5
children[k, i] = genotypes[k, i]
else
children[k, i] = genotypes[k, j]
end
end
return nothing
end
function update!(var_black, children, genotypes, Pc, Pm, high_class, mid_class, low_class)
topology_changed = false
while !topology_changed
for i in high_class
r = rand()
j = i
if length(high_class) > 1
if r < Pc
while i == j
j = rand(high_class)
end
elseif r < 0.5 + 0.5 * Pc
j = rand(mid_class)
else
j = rand(low_class)
end
else
if r < 0.5
j = rand(mid_class)
else
j = rand(low_class)
end
end
crossover!(children, genotypes, i, j)
end
for i in mid_class
r = rand()
j = i
if length(mid_class) > 1
if r < Pc
while i == j
j = rand(mid_class)
end
elseif r < 0.5 + 0.5 * Pc
j = rand(high_class)
else
j = rand(low_class)
end
else
if r < 0.5 + 0.5 * Pc
j = rand(high_class)
else
j = rand(low_class)
end
end
crossover!(children, genotypes, i, j)
end
for i in low_class
r = rand()
j = i
if length(low_class) > 1
if r < Pc
while i == j
j = rand(low_class)
end
elseif r < 0.5 + 0.5 * Pc
j = rand(mid_class)
else
j = rand(high_class)
end
else
if r < 0.5
j = rand(mid_class)
else
j = rand(high_class)
end
end
crossover!(children, genotypes, i, j)
end
genotypes .= children
for i in high_class
for j in 1:size(genotypes, 1)
r = rand()
if r < Pm && !genotypes[j, i]
genotypes[j, i] = !genotypes[j, i]
end
end
if any(@view genotypes[:, i]) != var_black[i]
var_black[i] = !var_black[i]
topology_changed = true
end
end
for i in mid_class
for j in 1:size(genotypes, 1)
r = rand()
if r < Pm && genotypes[j, i]
genotypes[j, i] = !genotypes[j, i]
end
end
if any(@view genotypes[:, i]) != var_black[i]
var_black[i] = !var_black[i]
topology_changed = true
end
end
for i in low_class
for j in 1:size(genotypes, 1)
r = rand()
if r < Pm && genotypes[j, i]
genotypes[j, i] = !genotypes[j, i]
end
end
if any(@view genotypes[:, i]) != var_black[i]
var_black[i] = !var_black[i]
topology_changed = true
end
end
end
return var_black
end
function (b::GESO)(x0=copy(b.comp.solver.vars); seed=NaN)
@unpack sens, old_sens, tol, maxiter = b
@unpack obj_trace, topology, sens_tol, vars = b
@unpack Pcmin, Pcmax, Pmmin, Pmmax, Pen = b
@unpack string_length, genotypes, children, var_black = b
@unpack cum_var_volumes, var_volumes, order = b
@unpack varind, black, white = b.comp.solver.problem
@unpack total_volume, cellvolumes, fixed_volume = b.vol
T = eltype(x0)
V = b.vol_limit
design_volume = V * total_volume
nel = length(x0)
nvars = length(vars)
# Set seed
isnan(seed) || Random.seed!(seed)
# Initialize the topology
for i in 1:length(topology)
if black[i]
topology[i] = 1
elseif white[i]
topology[i] = 0
else
topology[i] = round(x0[varind[i]])
vars[varind[i]] = topology[i]
end
end
check(x) = x > design_volume - fixed_volume
#rrmax = clamp(1 - design_volume/current_volume, 0, 1)
current_volume = dot(vars, var_volumes) + fixed_volume
vol = current_volume / total_volume
# Main loop
change = T(1)
iter = 0
f = x -> b.comp(b.filter(PseudoDensities(x)))
while (change > tol || vol > V) && iter < maxiter
iter += 1
if iter > 1
old_sens .= sens
end
for j in max(2, 10 - iter + 2):10
obj_trace[j - 1] = obj_trace[j]
end
obj_trace[10], pb = Zygote.pullback(f, vars)
sens = pb(1.0)[1]
rmul!(sens, -1)
if iter > 1
@. sens = (sens + old_sens) / 2
end
# Classify the cells by their sensitivities
sortperm!(order, sens; rev=true)
accumulate!(+, cum_var_volumes, view(var_volumes, order))
N1 = findfirst(check, cum_var_volumes) - 1
N2 = (nel - N1) ÷ 2
N3 = nvars - N1 - N2
high_class = @view order[1:N1]
mid_class = @view order[(N1 + 1):(N1 + N2)]
low_class = @view order[(N1 + N2 + 1):end]
# Crossover and mutation
Prg = get_progress(current_volume, total_volume, design_volume)
Pc, Pm = get_probs(b, Prg)
vars .= update!(
var_black, children, genotypes, Pc, Pm, high_class, mid_class, low_class
)
# Update crossover and mutation probabilities
current_volume = dot(vars, var_volumes) + fixed_volume
vol = current_volume / total_volume
if iter >= 10
l = sum(@view obj_trace[1:5])
h = sum(@view obj_trace[6:10])
change = abs(l - h) / h
end
end
for i in 1:length(topology)
if black[i]
topology[i] = 1
elseif white[i]
topology[i] = 0
else
topology[i] = vars[varind[i]]
end
end
b.result.objval = obj_trace[10]
b.result.change = change
b.result.converged = change <= tol
b.result.fevals = iter
return b.result
end