def dataset_load(args, device): if systems[args.system] == 'emulator': dataset = EmulatorDataset(system=args.system, nsim=args.nsim, norm=args.norm, nsteps=args.nsteps, device=device, savedir=args.savedir, name='closedloop') else: dataset = FileDataset(system=args.system, nsim=args.nsim, norm=args.norm, nsteps=args.nsteps, device=device, savedir=args.savedir, name='closedloop') ny = args.ny if not ny == dataset.data['Y'].shape[1]: new_sequences = {'Y': dataset.data['Y'][:, :1]} dataset.add_data(new_sequences, overwrite=True) dataset.min_max_norms['Ymin'] = dataset.min_max_norms['Ymin'][0] dataset.min_max_norms['Ymax'] = dataset.min_max_norms['Ymax'][0] nsim = dataset.data['Y'].shape[0] nu = dataset.data['U'].shape[1] # new_sequences = {'Y_max': 0.8 * np.ones([nsim, 1]), 'Y_min': 0.2 * np.ones([nsim, 1]), new_sequences = {'Y_max': psl.Periodic(nx=1, nsim=nsim, numPeriods=30, xmax=0.9, xmin=0.6), 'Y_min': psl.Periodic(nx=1, nsim=nsim, numPeriods=25, xmax=0.4, xmin=0.1), 'U_max': np.ones([nsim, nu]), 'U_min': np.zeros([nsim, nu]), # 'R': psl.Steps(nx=1, nsim=nsim, randsteps=60, xmax=0.7, xmin=0.3), 'R': psl.Periodic(nx=1, nsim=nsim, numPeriods=20, xmax=0.8, xmin=0.2) # 'Y_ctrl_': psl.RandomWalk(nx=ny, nsim=nsim, xmax=[1.0] * ny, xmin=[0.0] * ny, sigma=0.05)} #'Y_ctrl_': psl.WhiteNoise(nx=ny, nsim=nsim, xmax=[1.0] * ny, xmin=[0.0] * ny)} } dataset.add_data(new_sequences) # dataset.dims['Rf'] = (9000, 1) return dataset
def add_reference_features(args, dataset, dynamics_model): """ """ ny = dynamics_model.fy.out_features if ny != dataset.data["Y"].shape[1]: new_sequences = {"Y": dataset.data["Y"][:, :1]} dataset.add_data(new_sequences, overwrite=True) dataset.min_max_norms["Ymin"] = dataset.min_max_norms["Ymin"][0] dataset.min_max_norms["Ymax"] = dataset.min_max_norms["Ymax"][0] # nsim = dataset.data["Y"].shape[0] nsim = dataset.dims['nsim'] nu = dataset.data["U"].shape[1] ny = len(args.controlled_outputs) dataset.add_data({ # "Y_max": psl.Periodic(nx=ny, nsim=nsim, numPeriods=30, xmax=1.0, xmin=0.9)[:nsim, :], # "Y_min": psl.Periodic(nx=ny, nsim=nsim, numPeriods=24, xmax=0.8, xmin=0.7)[:nsim, :], "Y_max": np.ones([nsim, ny]), "Y_min": 0.7 * np.ones([nsim, ny]), "U_max": np.ones([nsim, nu]), "U_min": np.zeros([nsim, nu]), "R": psl.Periodic(nx=ny, nsim=nsim, numPeriods=20, xmax=0.9, xmin=0.8)[:nsim, :] # 'Y_ctrl_': psl.WhiteNoise(nx=ny, nsim=nsim, xmax=[1.0] * ny, xmin=[0.0] * ny) }) # indices of controlled states, e.g. [0, 1, 3] out of 5 outputs dataset.ctrl_outputs = args.controlled_outputs return dataset
def __init__(self, nsteps, nx, xmax=(0.5, 0.1), xmin=0.0, min_period=5, max_period=30, name='period', device='cpu'): super().__init__(nsteps, nx, xmax, xmin, name=name, device=device) self.min_period = min_period self.max_period = max_period self.white_noise_generator = lambda nsim, xmin, xmax: psl.WhiteNoise(nx=self.nx, nsim=nsim, xmax=xmax, xmin=xmin)[:nsim] self.period_generator = lambda nsim, xmin, xmax: psl.Periodic(nx=self.nx, nsim=nsim, numPeriods=random.randint(self.min_period, self.max_period), xmax=xmax, xmin=xmin)[:nsim] # self.sequence_generator = lambda nsim, xmin, xmax: self.period_generator(nsim, xmin, xmax) + self.white_noise_generator(nsim, -0.1*xmax, 0.1*xmax) self.sequence_generator = lambda nsim, xmin, xmax: self.period_generator(nsim, xmin, xmax) + self.white_noise_generator(nsim, xmin, 1.0 - xmax)
def __init__(self, nsteps, nx, xmax, xmin, min_period=5, max_period=30, name='period'): super().__init__(nsteps, nx, xmax, xmin, name=name) self.sequence_generator = lambda nsim: psl.Periodic( nx=self.nx, nsim=nsim, numPeriods=random.randint(min_period, max_period), xmax=self.xmax, xmin=self.xmin)
def forward(self, data): with torch.no_grad(): key = list(data.keys())[0] # nsim = data[key].shape[1]*data[key].shape[0] + self.nsteps current_nbatch = data[key].shape[1] if self.nbatch is None: self.nbatch = current_nbatch state_estimator_input = dict() for input_signal, dim in self.data_dims_in.items(): numPeriods = random.randint(self.nsteps // 4, self.nsteps // 2) if numPeriods > self.nsteps: numPeriods = self.nsteps sample = psl.Periodic(nx=dim, nsim=self.nsteps, numPeriods=numPeriods, xmax=self.xmax, xmin=self.xmin) state_estimator_input[input_signal] = torch.tensor( sample).view(self.nsteps, 1, dim).float() estimator_output = self.estimator(state_estimator_input) ysignals = [] dynamics_input = dict() x0 = estimator_output[f'x0_{self.estimator.name}'] # TODO: this is a hack!!! FIX in FY21 for i in range(self.nbatch + 5): for dim, name in zip([self.ny, self.nu, self.nd], self.dynamics_input_keys): if dim > 0: dynamics_input[name] = torch.tensor( psl.Periodic(nx=dim, nsim=self.nsteps, numPeriods=random.randint( self.nsteps // 4, self.nsteps // 2), xmax=self.xmax, xmin=self.xmin)).view( self.nsteps, 1, dim).float() dynamics_input[f'x0_{self.estimator.name}'] = x0 dynamics_output = self.dynamics({ **estimator_output, **dynamics_input }) x0 = dynamics_output[f'X_pred_{self.dynamics.name}'][-1] ysignals.append( dynamics_output[f'Y_pred_{self.dynamics.name}']) if self.nbatch == current_nbatch: # TODO: this is a hack!!! FIX in FY21 Yp = torch.stack(ysignals[:-5], dim=1).view(self.nsteps, self.nbatch, self.ny) Yf = torch.stack(ysignals[1:-4], dim=1).view(self.nsteps, self.nbatch, self.ny) else: # TODO: this is a hack!!! FIX in FY21 # print(ysignals[0].shape) # print(len(ysignals)) Yp = torch.cat(ysignals[:-1]).squeeze(1) Yf = torch.cat(ysignals[1:]).squeeze(1) # print(Yp.shape) # print(Yf.shape) end_step_Yp = Yp.shape[0] - self.nsteps end_step_Yf = Yf.shape[0] - self.nsteps # print(end_step_Yp) # print(end_step_Yf) Yp = torch.stack([ Yp[k:k + self.nsteps, :] for k in range(0, end_step_Yp) ]).transpose(1, 0) # nsteps X nsamples X nfeatures Yf = torch.stack([ Yf[k:k + self.nsteps, :] for k in range(0, end_step_Yf) ]).transpose(1, 0) # nsteps X nsamples X nfeatures Yp = Yp[:, :2993, :] Yf = Yf[:, :2993, :] return {self.name + 'p': Yp, self.name + 'f': Yf}
pickle_module=dill, map_location=torch.device('cpu')) estimator = load_model.components[0] dynamics = load_model.components[1] nsteps = 32 ny = load_model.components[1].fy.out_features dataset = FileDataset(system='flexy_air', nsim=10000, norm=['U', 'D', 'Y'], nsteps=nsteps, device='cpu') dataset.min_max_norms['Ymin'] = dataset.min_max_norms['Ymin'][0] dataset.min_max_norms['Ymax'] = dataset.min_max_norms['Ymax'][0] nsim = dataset.data['Y'].shape[0] nu = dataset.data['U'].shape[1] new_sequences = { 'Y_max': 0.8 * np.ones([nsim, 1]), 'Y_min': 0.2 * np.ones([nsim, 1]), 'U_max': np.ones([nsim, nu]), 'U_min': np.zeros([nsim, nu]), 'R': psl.Periodic(nx=1, nsim=nsim, numPeriods=20, xmax=0.7, xmin=0.3) } dataset.add_data(new_sequences) dynamics_generator = SignalGeneratorDynamics(dynamics, estimator, nsteps, xmax=1.0, xmin=0.0, name='Y_ctrl_') out = dynamics_generator(dataset.train_data) print(out['Y_ctrl_p'].shape)
def __init__(self, nsteps, nx, xmax, xmin, min_period=5, max_period=30, name='period', device='cpu'): super().__init__(nsteps, nx, xmax, xmin, name=name, device=device) self.min_period = min_period self.max_period = max_period self.sequence_generator = lambda nsim, xmin, xmax: psl.Periodic(nx=self.nx, nsim=nsim, numPeriods=random.randint(self.min_period, self.max_period), xmax=xmax, xmin=xmin)
# dataset nsim = 3000 dataset = FileDataset(system='flexy_air', nsim=nsim, norm=['U', 'D', 'Y'], nsteps=estimator.nsteps) ny = 1 nu = 1 nsteps = policy.nsteps R = { 'steps': psl.Steps(nx=1, nsim=nsim, randsteps=30, xmax=0.7, xmin=0.3), 'periodic': psl.Periodic(nx=1, nsim=nsim, numPeriods=20, xmax=0.7, xmin=0.3) }[args.ref_type] if args.dynamic_constraints: bounds_reference = { 'Y_max': psl.Periodic(nx=ny, nsim=nsim, numPeriods=30, xmax=0.9, xmin=0.6), 'Y_min': psl.Periodic(nx=ny, nsim=nsim, numPeriods=25, xmax=0.4, xmin=0.1), 'U_max': np.ones([nsim, nu]), 'U_min': np.zeros([nsim, nu]), 'R': R }
# dataset.data['D'] = np.mean(dataset.data['D']) * np.ones((dataset.data['D']).shape) dataset.data['D'] = disturb_static * np.ones((dataset.data['D']).shape) # list(np.random.random(10)) R = { 'static': 0.5 * np.ones([nsim, 1]), # 'spline': psl.SplineSignal(nsim=nsim, values=[0.3, 0.7, 0.6, 0.8, 0.5, 0.7, 0.4, 0.75, 0.3]).reshape(nsim, 1), 'spline': psl.SplineSignal(nsim=nsim, values=[0.5, 0.6, 0.4, 0.8, 0.5, 0.6, 0.5, 0.6, 0.4]).reshape(nsim, 1), 'steps': psl.Steps(nx=1, nsim=nsim, randsteps=15, xmax=0.7, xmin=0.3), 'periodic': psl.Periodic(nx=1, nsim=nsim, numPeriods=15, xmax=0.7, xmin=0.3), 'ramps': psl.sawtooth(nx=1, nsim=nsim, numPeriods=15, xmax=0.7, xmin=0.3) }[args.ref_type] if args.dynamic_constraints: bounds_reference = { 'Y_max': psl.Periodic(nx=ny, nsim=nsim, numPeriods=30, xmax=0.8, xmin=0.8), 'Y_min': psl.Periodic(nx=ny, nsim=nsim, numPeriods=25, xmax=0.2, xmin=0.2), 'U_max': np.ones([nsim, nu]), 'U_min': np.zeros([nsim, nu]), 'R':