-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgroundtruth_2dim.py
81 lines (72 loc) · 3.02 KB
/
groundtruth_2dim.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
from tqdm import tqdm
import numpy as np
from skopt.space import Space
from skopt.sampler import Halton
def classicInt(z,f1,f2,h):
## classical symplectic Euler scheme
dim = int(len(z)/2)
q=z[:dim]
p=z[dim:]
fstage = lambda stg: h * f1(np.block([q + stg, p]))
stageold=np.zeros(dim)
stage = fstage(stageold) +0.
Iter = 0
while (np.amax(abs(stage - stageold)) > 1e-10 and Iter<100):
stageold = stage+0.
stage = fstage(stage)+0.
Iter = Iter+1
q = q+stage
p = p + h*f2(np.block([q,p]))
return np.block([q,p])
def classicTrajectory(z,f1,f2,h,N=10,n_h=1):
## trajectory computed with classicInt
h_gen = h/n_h
z = z.reshape(1,-1)[0]
trj = np.zeros((len(z),N+1))
trj[:,0] = z.copy()
for i in range(0,N):
for j in range(0,int(n_h+1)):
trj[:,i+1] = classicInt(trj[:,i].copy(),f1,f2,h_gen)
return trj[:, :-1], trj[:, 1:]
def CreateTrainingDataTrajClassicInt(traj_len,ini_con,spacedim,h,f1,f2,n_h = 800):
space = Space(spacedim)
h_gen = h/n_h
halton = Halton()
startcon = np.array(halton.generate(space, ini_con)).transpose()
finalcon = startcon.copy()
# Compute flow map from Halton sequence to generate learning data
if ini_con==1: return classicTrajectory(startcon,f1,f2,h,N=traj_len)
else:
start, final= classicTrajectory(np.squeeze(startcon[:,0]),f1,f2,h,N=traj_len)
for k in range(ini_con-1):
new_start, new_final = classicTrajectory(np.squeeze(startcon[:,k+1]),f1,f2,h,N=traj_len)
start = np.hstack((start, new_start))
final = np.hstack((final, new_final))
return start,final
def CreateTrainingDataTrajClassicIntRandom(traj_len,ini_con,spacedim,h,f1,f2,seed,n_h = 800):
np.random.seed(seed = seed)
startcon = np.random.uniform(spacedim[0][0], spacedim[0][1], size = ini_con)
for i in range(len(spacedim)-1):
startcon = np.vstack((startcon, np.random.uniform(spacedim[i+1][0], spacedim[i+1][1], size = ini_con)))
h_gen = h/n_h
finalcon = startcon.copy()
# Compute flow map from Halton sequence to generate learning data
if ini_con==1: return classicTrajectory(startcon,f1,f2,h,N=traj_len)
else:
start, final= classicTrajectory(np.squeeze(startcon[:,0]),f1,f2,h,N=traj_len)
for k in range(ini_con-1):
new_start, new_final = classicTrajectory(np.squeeze(startcon[:,k+1]),f1,f2,h,N=traj_len)
start = np.hstack((start, new_start))
final = np.hstack((final, new_final))
return start,final
def get_within_array(trajectories, spacedim):
within_array = np.asarray([])
for i in range(len(trajectories)):
np.sum(np.square(np.asarray([spacedim[0][0], spacedim[0][1]]), np.asarray([spacedim[1][0], spacedim[1][1]])))
try:
v = np.amin(np.concatenate((np.where(trajectories[i][1][0]<spacedim[0][0])[0],np.where(trajectories[i][1][0]>spacedim[0][1])[0],
np.where(trajectories[i][1][1]<spacedim[1][0])[0], np.where(trajectories[i][1][1]>spacedim[1][1])[0])))
within_array = np.append(within_array, v)
except ValueError:
within_array = np.append(within_array, len(trajectories[i][1][1]))
return within_array