-
Notifications
You must be signed in to change notification settings - Fork 1
/
gd.py
376 lines (288 loc) · 9.43 KB
/
gd.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
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
import bpy
from phaenotyp import basics, operators, geometry, calculation
import numpy as np
def create_indivdual(chromosome, frame):
"""
Creates an individual for bruteforce mode.
:param chromosome: The chromosome is a list of floats from 0 to 1.
"""
scene = bpy.context.scene
data = scene["<Phaenotyp>"]
obj = data["structure"]
shape_keys = obj.data.shape_keys.key_blocks
environment = data["environment"]
individuals = data["individuals"]
# apply shape keys
geometry.set_shape_keys(shape_keys, chromosome)
individual = {}
individual["name"] = str(frame) # individuals are identified by frame
individual["chromosome"] = chromosome
individual["fitness"] = {}
individuals[str(frame)] = individual
def generate_basis():
'''
Generate the basis individual to work with.
The fitness of all others are weighted with this first individual.
'''
scene = bpy.context.scene
phaenotyp = scene.phaenotyp
data = scene["<Phaenotyp>"]
obj = data["structure"]
shape_keys = obj.data.shape_keys.key_blocks
environment = data["environment"]
individuals = data["individuals"]
# update scene
frame = 0
bpy.context.scene.frame_start = frame
bpy.context.scene.frame_current = frame
bpy.context.view_layer.update()
# starting point is the current set of values
# in this way users can choose where to start from
chromosome = []
slope = []
for id, key in enumerate(shape_keys):
if id > 0:
v = key.value
chromosome.append(v)
slope.append(0)
# create indiviual
create_indivdual(chromosome, 0)
rounded_chromosome = [round(num, 3) for num in chromosome]
text = "Starting at: " + str(rounded_chromosome) + "\n"
basics.print_data(text)
# store in basics for later
basics.chromosome_current = chromosome
basics.slope = slope
def calculate_basis():
scene = bpy.context.scene
phaenotyp = scene.phaenotyp
if phaenotyp.optimization_pn != "none" or phaenotyp.optimization_fd != "none" or phaenotyp.optimization_quads != "none":
# calculate frames
calculation.calculate_frames(0, 1)
for i in range(phaenotyp.optimization_amount):
# optimize each frame
basics.jobs.append([calculation.sectional_optimization, 0])
# calculate frames again
calculation.calculate_frames(0, 1)
# without optimization
else:
# calculate frames
calculation.calculate_frames(0, 1)
# calculate fitness and set weight for basis
basics.jobs.append([calculation.calculate_fitness, 0])
basics.jobs.append([calculation.set_basis_fitness])
def make_step_st(frame):
'''
Make a step with the adapted chromosome.
:paramm chromosome: List of floats from 0 to 1.
:frame: Frame to save this individual to.
'''
scene = bpy.context.scene
data = scene["<Phaenotyp>"]
obj = data["structure"]
shape_keys = obj.data.shape_keys.key_blocks
phaenotyp = scene.phaenotyp
environment = data["environment"]
individuals = data["individuals"]
# get current chromosome
chromosome = basics.chromosome_current
# update frame
bpy.context.scene.frame_current = frame
bpy.context.view_layer.update()
# apply shape keys
geometry.set_shape_keys(shape_keys, chromosome)
# create indivual
individual = {}
individual["name"] = str(frame) # individuals are identified by frame
individual["chromosome"] = chromosome
individual["fitness"] = {}
individuals[str(frame)] = individual
def calculate_step_st(frame):
scene = bpy.context.scene
phaenotyp = scene.phaenotyp
if phaenotyp.optimization_pn != "none" or phaenotyp.optimization_fd != "none" or phaenotyp.optimization_quads != "none":
# calculate frames
calculation.calculate_frames(frame, frame+1)
for i in range(phaenotyp.optimization_amount):
# optimize each frame
basics.jobs.append([calculation.sectional_optimization, frame])
# calculate frames again
calculation.calculate_frames(frame, frame+1)
# without optimization
else:
# calculate frames
calculation.calculate_frames(frame, frame+1)
# calculate fitness
basics.jobs.append([calculation.calculate_fitness, frame])
def get_step_st(frame):
scene = bpy.context.scene
data = scene["<Phaenotyp>"]
obj = data["structure"]
shape_keys = obj.data.shape_keys.key_blocks
phaenotyp = scene.phaenotyp
environment = data["environment"]
individuals = data["individuals"]
# get data from individual
gd = individuals[str(frame)]
fitness = gd["fitness"]["weighted"]
text = "Step " + gd["name"] + " with fitness: " + str(round(fitness, 3))
basics.print_data(text)
basics.gd = gd
basics.fitness = fitness
def create_variations(frame):
scene = bpy.context.scene
data = scene["<Phaenotyp>"]
obj = data["structure"]
shape_keys = obj.data.shape_keys.key_blocks
phaenotyp = scene.phaenotyp
# copy current chromosome
chromosome = basics.chromosome_current.copy()
# delta
delta = basics.delta
for key_id in range(len(shape_keys)-1):
# update frame
frame += 1
# update chromosome
chromosome[key_id] += delta
# create individual
create_indivdual(chromosome, frame)
def make_step_mp(frames):
start, end = frames
scene = bpy.context.scene
phaenotyp = scene.phaenotyp
if phaenotyp.optimization_pn != "none" or phaenotyp.optimization_fd != "none" or phaenotyp.optimization_quads != "none":
# calculate frames
calculation.calculate_frames(start, end)
for i in range(phaenotyp.optimization_amount):
for frame in range(start, end):
# optimize each frame
basics.jobs.append([calculation.sectional_optimization, frame])
# calculate frames again
calculation.calculate_frames(start, end)
# without optimization
else:
# calculate frames
calculation.calculate_frames(start, end)
# calculate fitness
for frame in range(start, end):
basics.jobs.append([calculation.calculate_fitness, frame])
def get_next_step(frames):
scene = bpy.context.scene
data = scene["<Phaenotyp>"]
obj = data["structure"]
shape_keys = obj.data.shape_keys.key_blocks
phaenotyp = scene.phaenotyp
environment = data["environment"]
individuals = data["individuals"]
fitness_old = basics.fitness
slope = basics.slope
chromosome_current = basics.chromosome_current
delta = basics.delta
learning_rate = basics.learning_rate
iteration = basics.iteration
max_iteration = basics.max_iteration
abort = basics.abort
start, end = frames
for key_id, frame in enumerate(range(start, end)):
# get data from individual
gd = individuals[str(frame)]
fitness = gd["fitness"]["weighted"]
text = "Step " + gd["name"] + " with fitness: " + str(round(fitness, 3))
basics.print_data(text)
# calculate slope
# (in new loop with multiprocessing)
slope[key_id] = (fitness - fitness_old) / delta
text = "Slope of key " + str(key_id) + " = " + str(round(slope[key_id], 3))
basics.print_data(text)
# new direction
chromosome_current[key_id] = chromosome_current[key_id] - slope[key_id] * learning_rate
if chromosome_current[key_id] < 0:
chromosome_current[key_id] = 0
slope[key_id] = 0
if chromosome_current[key_id] > 1:
chromosome_current[key_id] = 1
slope[key_id] = 0
text = "Iteration: " + str(iteration) + "|"+ str(max_iteration)
basics.print_data(text)
rounded_chromosome = [round(num, 3) for num in chromosome_current]
text = "New step: " + str(rounded_chromosome)
basics.print_data(text)
vector = (np.linalg.norm(slope))*learning_rate
text = "Vector: " + str(round(vector, 3)) + "\n"
basics.print_data(text)
basics.iteration += 1
basics.fitness = fitness
basics.slope = slope
if vector < abort:
text = "Goal reached"
basics.print_data(text)
# delete jobs
basics.jobs = []
# and append finish
basics.jobs.append([finish])
bpy.context.scene.frame_end = frame
def finish():
# update view
basics.jobs.append([basics.view_vertex_colors])
# print done
basics.jobs.append([basics.print_data, "done"])
def start():
'''
Main function to run gradient descent.
'''
scene = bpy.context.scene
data = scene["<Phaenotyp>"]
obj = data["structure"]
shape_keys = obj.data.shape_keys.key_blocks
phaenotyp = scene.phaenotyp
environment = data["environment"]
individuals = data["individuals"]
# get data from gui
delta = phaenotyp.gd_delta
learning_rate = phaenotyp.gd_learning_rate
abort = phaenotyp.gd_abort
max_iteration = phaenotyp.gd_max_iteration
# set frame and iteration
frame = 0
iteration = 0
# create temp variables and dictionaries
basics.models = {}
basics.feas = {}
basics.delta = delta
basics.learning_rate = learning_rate
basics.iteration = iteration
basics.max_iteration = max_iteration
basics.abort = abort
# generate_basis for fitness
generate_basis()
calculate_basis()
size = len(shape_keys)-1
#while iteration < maxiteration:
for i in range(max_iteration):
# update frame
frame += 1
# be aware of list order from jobs!
# if a function is adding jobs,
# the jobs are added when the function is called
# therefore a function added afterwards, but added
# directly, will be executed before the jobs added
# from the functions
# make step
basics.jobs.append([make_step_st, frame])
calculate_step_st(frame)
basics.jobs.append([get_step_st, frame])
# create variations for next step
basics.jobs.append([create_variations, frame])
make_step_mp([frame+1, frame+size+1])
basics.jobs.append([get_next_step, [frame+1, frame+size+1]])
frame += size
frame += 1
# make last step
basics.jobs.append([make_step_st, frame])
calculate_step_st(frame)
basics.jobs.append([get_step_st, frame])
bpy.context.scene.frame_end = frame
# geometry post and viz
basics.jobs.append([finish])
# run jobs
bpy.ops.wm.phaenotyp_jobs()