def get_test_data(self,seed=None): u = [] for i in range(1000): if i%7==0: u.append(self.random.normal(scale=1)+1) else: u.append(u[-1]) exp = System_data(u=u) return self.apply_experiment(exp)
def apply_experiment(self, experiment): '''Return the u,(x),y combination todo: move this function to System''' u = experiment.u # N_transient = experiment.N_transient x = None from scipy import signal y = signal.lfilter(self.b, self.a, u) return System_data(u=u, x=x, y=y, system_dict=self.settings, experiment_dict=experiment.settings)
def plot_and_fit(sys): global id exp = System_data(u=np.random.normal(scale=0.1, size=1000)) sys = sys() sys_data = sys.apply_experiment(exp) SYS = deepSI.fit_systems.System_IO_fit_linear sys_fit, score, kwargs, _ = deepSI.fit_systems.grid_search( SYS, sys_data, dict(na=range(0, 10), nb=range(1, 10))) na, nb = kwargs['na'], kwargs['nb'] sys_data_predict = sys_fit.apply_experiment(sys_data) plt.subplot(2, 3, id) sys_data.plot() sys_data_predict.plot() plt.title(f'{sys.name} BFR {sys_data_predict.NRMS(sys_data):.2%}') plt.legend(['real', f'predict na={na},nb={nb}']) # plt.title(sys.name) id += 1
dx2 = 0 return [dx1, dx2] if __name__ == '__main__': # sys = Double_bath() # sys_data = sys.get_train_data() # SYS = fit_systems.System_IO_fit_linear # score, sys_fit, kwargs, _ = fit_systems.grid_search(SYS, sys_data, dict(na=range(0,3),nb=range(1,20))) # sys_data_predict = sys_fit.apply_experiment(sys_data) # sys_data.plot() # sys_data_predict.plot(show=True) sys = Cascaded_tanks_continuous(dt=2) data = System_data(u=np.ones(shape=(300))*5) data = sys.apply_experiment(data,save_state=True) data.plot(show=True) from matplotlib import pyplot as plt plt.plot(data.x) plt.show() print(np.max(data.x,axis=0)) #longest timescale = 25 #max x1 = 9. and x2 = 35 #overflow at 20 x2 and #u max = 8 #dt = 2
def f(self, x, u): u = np.array(u) if u.ndim == 0: u = u[None] return np.dot(self.Adis, x) + np.dot(self.Bdis, u) if __name__ == '__main__': np.random.seed(42) A = np.array([[0.89, 0.], [0., 0.45]]) #(2,2) x<-x B = np.array([[0.3], [2.5]]) #(2,1) x<-u C = np.array([[0.7, 1.]]) #(1,2) y<-u D = np.array([[0.0]]) #(1,1) sys = SS_linear(A=A, B=B, C=C, D=D) exp = System_data(u=np.random.normal(size=1000)[:, None]) sys_data = sys.apply_experiment(exp) # sys_data.plot(show=True) sys_fit = SS_linear(nx=4) sys_fit.fit(sys_data, SS_f=10) sys_data_predict = sys_fit.apply_experiment(sys_data) print(sys_data_predict.NRMS(sys_data)) #0017893805399810095 # sys_data_fitted = sys_fit.simulation(sys_data) # print(sys_fit.A,sys_fit.B,sys_fit.C,sys_fit.D) # sys_data.plot(show=False) # sys_data_fitted.plot() # print(sys_data_fitted.NMSE(sys_data)) # sys = deepSI.systems.Nonlin_io_normals() # train_data = sys.sample_system()
def get_test_data(self): exp = System_data( u=self.random.uniform(low=-2.5, high=2.5, size=(2000, ))) return self.apply_experiment(exp)
def get_test_data(self): exp = System_data(u=self.random.uniform(-1, 1, size=10**4)) return self.apply_experiment(exp)
x' = v Fmax < a Fmin > -a """ def __init__(self, Fmax=0.25, image_height=25, image_width=25): self.image_height, self.image_width = image_height, image_width super(Ball_in_box_video, self).__init__(Fmax=Fmax) self.observation_space = Box(0., 1., shape=(self.image_height, self.image_width)) def h(self, x, u): # A = np.zeros((self.image_width,self.image_height)) Y = np.linspace(0, 1, num=self.image_height) X = np.linspace(0, 1, num=self.image_width) X, Y = np.meshgrid(X, Y) # self.X = X[y,x] r = 0.22 A = np.clip((r**2 - (X - x[0])**2 - (Y - x[1])**2) / r**2, 0, 1) return A #return position if __name__ == '__main__': sys = Ball_in_box_video() exp = System_data(u=[sys.action_space.sample() for i in range(1000)]) print(sys.action_space.low) sys_data = sys.apply_experiment(exp) sys_data.to_video(file_name='test')
def get_test_data(self): exp = System_data(u=np.random.normal(size=1000)) return self.apply_experiment(exp)
def io_step(self, uy): return self.reg.predict( [uy])[0] if uy.ndim == 1 else self.reg.predict(uy) from sklearn import linear_model class Sklearn_io_linear(Sklearn_io): def __init__(self, na, nb): super(Sklearn_io_linear, self).__init__(na, nb, linear_model.LinearRegression()) if __name__ == '__main__': import numpy as np from matplotlib import pyplot as plt sys = deepSI.systems.Wiener_sysid_book() sys_data = sys.apply_experiment( System_data(u=np.random.normal(scale=0.1, size=400))) sys = Sklearn_io_linear(na=2, nb=1) sys.fit(sys_data) # sys, score, kwargs = deepSI.fit_systems.fit_system_tuner(Sklearn_io_linear,sys_data,dict(na=range(1,10),nb=range(1,10))) # print(score,kwargs) # sys.apply_experiment(sys_data).plot() # sys_data.plot(show=True) print(f'len(sys_data)={len(sys_data)}') plt.plot(sys.n_step_error(sys_data)) plt.show() sys.one_step_ahead(sys_data).plot(show=True)
import deepSI from deepSI.systems.system import System, System_deriv, System_data import numpy as np class Van_der_pol_oscillator(System_deriv): """docstring for Van_der_pol_oscillator""" def __init__(self, dt=0.2,mu=2.5): self.mu = mu super(Van_der_pol_oscillator, self).__init__(nx=2,dt=dt) def reset(self): x = np.random.uniform(low=[-3,-3],high=[3,3],size=(2,)) for i in range(500): x = self.f(x,0) self.x = x return self.h(self.x) def h(self,x): return x[0] def deriv(self,state,u): #will be converted by Deriv system x, y = state #unpack xp = self.mu*(x-x**3/3-y) yp = 1/self.mu*(x-u) return np.array([xp,yp]) if __name__ == '__main__': sys = Van_der_pol_oscillator() sys_data = sys.apply_experiment(System_data(u=0*np.sin(np.linspace(0,2*np.pi*20,num=10000)))) sys_data[:1000].plot(show=True)
from deepSI.systems.system import System, System_deriv, System_data import numpy as np class Van_der_pol_oscillator(System_deriv): """docstring for Van_der_pol_oscillator""" def __init__(self, dt=0.2, mu=2.5): self.mu = mu super(Van_der_pol_oscillator, self).__init__(nx=2, dt=dt) def reset_state(self): x = np.random.uniform(low=[-3, -3], high=[3, 3], size=(2, )) for i in range(500): x = self.f(x, 0) self.x = x def h(self, x, u): return x[0] def deriv(self, state, u): #will be converted by Deriv system x, y = state #unpack xp = self.mu * (x - x**3 / 3 - y) yp = 1 / self.mu * (x - u) return np.array([xp, yp]) if __name__ == '__main__': sys = Van_der_pol_oscillator() sys_data = sys.apply_experiment( System_data(u=0 * np.sin(np.linspace(0, 2 * np.pi * 20, num=10000)))) sys_data[:1000].plot(show=True)