-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstgp_elastica.py
More file actions
480 lines (377 loc) · 13.6 KB
/
stgp_elastica.py
File metadata and controls
480 lines (377 loc) · 13.6 KB
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
import numpy as np
import numpy.typing as npt
from typing import Callable, Tuple
import jax.numpy as jnp
from jax import grad, Array
from deap import gp
from dctkit.mesh.simplex import SimplicialComplex
from dctkit.mesh.util import generate_line_mesh, build_complex_from_mesh
from dctkit.dec import cochain as C
from dctkit import config
from dctkit.math.opt import optctrl as oc
import dctkit as dt
from flex.gp import regressor as gps
from flex.gp.util import load_config_data, compile_individuals
from flex.gp.primitives import add_primitives_to_pset_from_dict
import matplotlib.pyplot as plt
import math
import sys
import time
import ray
from functools import partial
import os
from util import *
residual_formulation = False
# choose precision and whether to use GPU or CPU
# needed for context of the plots at the end of the evolution
os.environ["JAX_PLATFORMS"] = "cpu"
config()
NUM_NODES = 11
LENGTH = 1.0
def is_valid_energy(
theta_in: npt.NDArray, theta: npt.NDArray, prb: oc.OptimizationProblem
) -> bool:
dim = len(theta_in)
noise = 0.0001 * np.ones(dim).astype(dt.float_dtype)
theta_in_noise = theta_in + noise
theta_noise = prb.solve(
x0=theta_in_noise, maxeval=500, ftol_abs=1e-12, ftol_rel=1e-12
)
is_valid = np.allclose(theta, theta_noise, rtol=1e-6, atol=1e-6)
return is_valid
class Objectives:
def __init__(self, S: SimplicialComplex) -> None:
self.S = S
def set_residual(self, func: Callable) -> None:
"""Set the energy function to be used for the computation of the objective
function."""
self.residual = func
def set_energy_func(self, func: Callable) -> None:
"""Set the energy function to be used for the computation of the objective
function."""
self.energy_func = func
# elastic energy including Dirichlet BC by elimination of the prescribed dofs
def total_energy(
self, theta_vec: npt.NDArray, FL2_EI0: float, theta_0: npt.NDArray
) -> Array:
# extend theta on the boundary w.r.t boundary conditions
theta_vec = jnp.insert(theta_vec, 0, theta_0)
theta = C.CochainD0(self.S, theta_vec)
FL2_EI0_coch = C.CochainD0(
self.S, FL2_EI0 * jnp.ones(self.S.num_nodes - 1, dtype=dt.float_dtype)
)
if residual_formulation:
residual = self.residual(theta, FL2_EI0_coch)
energy = jnp.linalg.norm(residual.coeffs[1:]) ** 2
else:
energy = self.energy_func(theta, FL2_EI0_coch)
return energy
# state function: stationarity conditions for the total energy
def total_energy_grad(self, x: npt.NDArray, theta_0: float) -> Array:
theta = x[:-1]
FL2_EI0 = x[-1]
if residual_formulation:
# FIXME: not sure why we are not applying grad to total_energy
theta_vec = jnp.insert(theta, 0, theta_0)
theta = C.CochainD0(self.S, theta_vec)
FL2_EI0_coch = C.CochainD0(
self.S, FL2_EI0 * jnp.ones(self.S.num_nodes - 1, dtype=dt.float_dtype)
)
return self.residual(theta, FL2_EI0_coch).coeffs[1:]
else:
return grad(self.total_energy)(theta, FL2_EI0, theta_0)
# objective function for the parameter EI0 identification problem
def MSE_theta(self, x: npt.NDArray, theta_true: npt.NDArray) -> Array:
theta = x[:-1]
theta = jnp.insert(theta, 0, theta_true[0])
return jnp.sum(jnp.square(theta - theta_true))
def tune_EI0(
func: Callable,
toolbox,
theta_true: npt.NDArray,
FL2: float,
EI0_guess: float,
theta_guess: npt.NDArray,
S: SimplicialComplex,
) -> float:
# number of unknowns angles
dim = S.num_nodes - 2
obj = Objectives(S=S)
if residual_formulation:
obj.set_residual(func)
else:
obj.set_energy_func(func)
# prescribed angle at x=0
theta_0 = theta_true[0]
# need to call config again before using JAX in energy evaluations to make sure that
# the current worker has initialized JAX
config()
constraint_args = {"theta_0": theta_0}
obj_args = {"theta_true": theta_true}
prb = oc.OptimalControlProblem(
objfun=obj.MSE_theta,
statefun=obj.total_energy_grad,
state_dim=dim,
nparams=S.num_nodes - 1,
constraint_args=constraint_args,
obj_args=obj_args,
)
def get_bounds():
lb = -100 * np.ones(dim + 1, dt.float_dtype)
ub = 100 * np.ones(dim + 1, dt.float_dtype)
lb[-1] = -100
ub[-1] = -1e-3
return (lb, ub)
prb.get_bounds = get_bounds
FL2_EI0 = FL2 / EI0_guess
x0 = np.append(theta_guess, FL2_EI0)
x = prb.solve(x0=x0, maxeval=500, ftol_abs=1e-12, ftol_rel=1e-12)
# theta = x[:-1]
FL2_EI0 = x[-1]
EI0 = FL2 / FL2_EI0
# compute violation norm
viol = prb.constr(x, **prb.constr_args)
viol = np.asarray(viol)
viol_norm = np.linalg.norm(viol, ord=2)
# define optimization success 1
success_case_1 = (
prb.last_opt_result == 1 or prb.last_opt_result == 3 or prb.last_opt_result == 4
)
success_case_2 = prb.last_opt_result == 5 and viol_norm <= 1e-12
if success_case_1 or success_case_2:
return EI0
# in this case optimization is failed -> return a bad value
return -1.0
def eval_MSE_sol(
func: Callable,
EI0: float,
thetas_true: npt.NDArray,
Fs: npt.NDArray,
theta_in_all: npt.NDArray,
S: SimplicialComplex,
) -> Tuple[float, npt.NDArray]:
# number of unknown angles
dim = S.num_nodes - 2
total_err = 0.0
obj = Objectives(S=S)
if residual_formulation:
obj.set_residual(func)
else:
obj.set_energy_func(func)
X_dim = thetas_true.shape[0]
best_theta = np.zeros((X_dim, S.num_nodes - 1), dtype=dt.float_dtype)
# need to call config again before using JAX in energy evaluations to make sure that
# the current worker has initialized JAX
config()
if EI0 < 0.0:
total_err = 40.0
else:
for i, theta_true in enumerate(thetas_true):
# extract prescribed value of theta at x = 0 from the dataset
theta_0 = theta_true[0]
theta_in = theta_in_all[i, :]
FL2 = Fs[i]
FL2_EI0 = FL2 / EI0
prb = oc.OptimizationProblem(
dim=dim, state_dim=dim, objfun=obj.total_energy
)
args = {"FL2_EI0": FL2_EI0, "theta_0": theta_0}
prb.set_obj_args(args)
theta = prb.solve(x0=theta_in, maxeval=500, ftol_abs=1e-12, ftol_rel=1e-12)
# check whether the energy is "admissible" (i.e. exclude constant energies
# and energies with minima that are too sensitive to the initial guess)
valid_energy = is_valid_energy(theta_in=theta_in, theta=theta, prb=prb)
if (
prb.last_opt_result == 1
or prb.last_opt_result == 3
or prb.last_opt_result == 4
) and valid_energy:
x = np.append(theta, FL2_EI0)
fval = float(obj.MSE_theta(x, theta_true))
else:
fval = math.nan
if math.isnan(fval):
total_err = 40.0
break
total_err += fval
# extend theta
theta = np.insert(theta, 0, theta_0)
# update best_theta
best_theta[i, :] = theta
total_err *= 1 / (X_dim)
# round total_err to 5 decimal digits
total_err = float("{:.5f}".format(total_err))
return 10.0 * total_err, best_theta
def fitness(
individuals_str: list[str],
toolbox,
X,
y,
theta_in_all: npt.NDArray,
S: SimplicialComplex,
penalty: dict,
) -> Tuple[float,]:
callables = compile_individuals(toolbox, individuals_str)
indlen = get_features_batch(individuals_str)
fitnesses = [None] * len(individuals_str)
for i, ind in enumerate(callables):
MSE, _ = eval_MSE_sol(
ind, individuals_str[i].EI0, X, y, theta_in_all["train"], S
)
# add penalty on length of the tree to promote simpler solutions
fitnesses[i] = (MSE + penalty["reg_param"] * indlen[i],)
return fitnesses
def predict(
individuals_str: list[str],
toolbox,
X,
y,
theta_in_all: npt.NDArray,
S: SimplicialComplex,
penalty: dict,
) -> list:
callables = compile_individuals(toolbox, individuals_str)
u = [None] * len(individuals_str)
for i, ind in enumerate(callables):
_, u[i] = eval_MSE_sol(
ind, individuals_str[i].EI0, X, y, theta_in_all["test"], S
)
return u
def score(
individuals_str: list[str],
toolbox,
X,
y,
theta_in_all: npt.NDArray,
S: SimplicialComplex,
penalty: dict,
) -> list:
callables = compile_individuals(toolbox, individuals_str)
MSE = [None] * len(individuals_str)
for i, ind in enumerate(callables):
MSE[i], _ = eval_MSE_sol(
ind, individuals_str[i].EI0, X, y, theta_in_all["val"], S
)
MSE[i] *= -1
return MSE
def print_EI0(best_individuals):
for ind in best_individuals:
print(f"The best individual's EI0 is: {ind.EI0}", flush=True)
def preprocess_callback_func(individuals, EI0s):
for ind, EI0 in zip(individuals, EI0s):
ind.EI0 = EI0
def evaluate_EI0s(
individuals_str,
toolbox,
theta_true,
FL2,
EI0_guess,
theta_guess,
S,
):
EI0s = [None] * len(individuals_str)
callables = compile_individuals(toolbox, individuals_str)
for i, ind in enumerate(callables):
EI0s[i] = tune_EI0(ind, toolbox, theta_true, FL2, EI0_guess, theta_guess, S)
return EI0s
def stgp_elastica(output_path=None):
global residual_formulation
regressor_params, config_file_data = load_config_data("elastica.yaml")
data_path = os.path.join(os.getcwd(), "data/elastica")
thetas_train, thetas_val, thetas_test, Fs_train, Fs_val, Fs_test = load_dataset(
data_path, "csv"
)
# TODO: how can we extract these numbers from the dataset (especially length)?
mesh, _ = generate_line_mesh(num_nodes=NUM_NODES, L=LENGTH)
S = build_complex_from_mesh(mesh)
S.get_hodge_star()
x_all, y_all = get_positions_from_angles((thetas_train, thetas_val, thetas_test))
theta_in_all = get_angles_initial_guesses(x_all, y_all)
residual_formulation = config_file_data["gp"]["residual_formulation"]
if residual_formulation:
print("Using residual formulation.")
pset = gp.PrimitiveSetTyped("MAIN", [C.CochainD0, C.CochainD0], C.CochainD0)
else:
pset = gp.PrimitiveSetTyped("MAIN", [C.CochainD0, C.CochainD0], float)
# add internal cochain as a terminal
internal_vec = np.ones(S.num_nodes, dtype=dt.float_dtype)
internal_vec[0] = 0.0
internal_vec[-1] = 0.0
internal_coch = C.CochainP0(complex=S, coeffs=internal_vec)
pset.addTerminal(internal_coch, C.CochainP0, name="int_coch")
# add constants
pset.addTerminal(0.5, float, name="1/2")
pset.addTerminal(-1.0, float, name="-1.")
pset.addTerminal(2.0, float, name="2.")
pset.renameArguments(ARG0="theta")
pset.renameArguments(ARG1="FL2_EI0")
pset = add_primitives_to_pset_from_dict(pset, config_file_data["gp"]["primitives"])
penalty = config_file_data["gp"]["penalty"]
common_params = {"S": S, "penalty": penalty, "theta_in_all": theta_in_all}
# define preprocess function to estimate EI0
preprocess_func_args = {
"theta_true": thetas_train[0, :],
"FL2": Fs_train[0],
"EI0_guess": 1.0,
"theta_guess": theta_in_all["train"][0, :],
"S": S,
}
preprocess_args = {
"func": evaluate_EI0s,
"func_args": preprocess_func_args,
"callback": preprocess_callback_func,
}
if config_file_data["gp"]["set_seed"]:
opt_string = (
"SubF(MulF(1/2, InnP0(CMulP0(int_coch, St_oneD1(cobD0(theta))), "
"CMulP0(int_coch, St_oneD1(cobD0(theta))))), InnD0(FL2_EI0, SinD0(theta)))"
)
seed = [opt_string]
else:
seed = None
gpsr = gps.GPSymbolicRegressor(
pset_config=pset,
fitness=fitness,
score_func=score,
predict_func=partial(predict, y=Fs_test),
print_log=True,
common_data=common_params,
seed_str=seed,
save_best_individual=True,
save_train_fit_history=True,
output_path=output_path,
preprocess_args=preprocess_args,
custom_logger=print_EI0,
num_cpus=1,
**regressor_params,
)
start = time.perf_counter()
gpsr.fit(X=thetas_train, y=Fs_train, X_val=thetas_val, y_val=Fs_val)
# -- PLOTS --
if config_file_data["gp"]["plot_best"]:
theta_pred_test = gpsr.predict(thetas_test)
# reconstruct (x,y) pred and true
x_pred, y_pred = get_positions_from_angles((theta_pred_test,))
x_true, y_true = get_positions_from_angles((thetas_test,))
dim = len(x_pred[0])
plt.figure(1, figsize=(12, 4))
_, axes = plt.subplots(1, dim, num=1)
for i in range(dim):
# plot the results
axes[i].scatter(x_pred[0][i], y_pred[0][i], c="r", label="Predicted")
axes[i].plot(x_true[0][i], y_true[0][i], c="b", label="True")
axes[i].grid()
axes[i].set_xlabel("x")
axes[i].set_ylabel("y")
axes[i].legend()
plt.savefig("elastica.png", dpi=300)
print(f"Elapsed time: {round(time.perf_counter() - start, 2)}")
ray.shutdown()
if __name__ == "__main__":
n_args = len(sys.argv)
# path for output data speficified
if n_args >= 2:
output_path = sys.argv[1]
else:
output_path = "."
stgp_elastica(output_path)