def parameters(self, system='Pendulum-v0'): self.system = system self.env = gym.make(self.system) self.env.reset() # to reset the environment state self.x0 = self.env.state self.nx = self.x0.shape[0] self.action_sample = self.env.action_space.sample() self.nu = np.asarray([self.action_sample]).shape[0] # default simulation setup self.U = np.zeros([(self.nsim - 1), self.nu]) self.U[int(self.nsim / 8)] = 0.1 # randomIndList =[] # for i in range(0, 100): # # any random numbers from 0 to 1000 # ind = random.randint(0, self.nsim-1) # self.U[ind] = random.uniform(-1, 1) self.U = Steps(nx=1, nsim=self.nsim, values=None, randsteps=int(np.ceil(self.nsim / 40)), xmax=0.5, xmin=-0.5) # self.U = SplineSignal(nsim=self.nsim, values=None, xmin=-2.0, xmax=2.0) # print(self.U.shape) if type(self.action_sample) == int: self.U = self.U.astype(int)
def parameters(self): self.N = 10000 # population # initial number of infected and recovered individuals self.e_0 = 1 / self.N self.i_0 = 0.00 self.r_0 = 0.00 self.s_0 = 1 - self.e_0 - self.i_0 - self.r_0 self.x0 = np.asarray([self.s_0, self.e_0, self.i_0, self.r_0]) self.nx = 4 self.nu = 1 self.t_incubation = 5.1 self.t_infective = 3.3 self.R0 = 2.4 self.alpha = 1 / self.t_incubation self.gamma = 1 / self.t_infective self.beta = self.R0 * self.gamma # default simulation setup self.U = Steps(nx=self.nu, nsim=self.nsim, values=None, randsteps=int(np.ceil(self.nsim / 100)), xmax=1, xmin=0)
def parameters(self, system='Pendulum-v0'): self.system = system self.env = gym.make(self.system) self.env.reset() # to reset the environment state self.x0 = self.env.state self.nx = self.x0.shape[0] self.action_sample = self.env.action_space.sample() self.nu = np.asarray([self.action_sample]).shape[0] # default simulation setup self.U = np.zeros([(self.nsim - 1), self.nu]) self.U[int(self.nsim / 8)] = 0.1 self.U = Steps(nx=1, nsim=self.nsim, values=None, randsteps=int(np.ceil(self.nsim / 40)), xmax=0.5, xmin=-0.5) if type(self.action_sample) == int: self.U = self.U.astype(int)
def parameters(self): self.rho = 1000.0 # water density (kg/m^3) self.A = 1.0 # tank area (m^2) # Initial Conditions for the States self.x0 = 0 # default imputs c = Steps(nx=1, nsim=self.nsim, values=None, randsteps=int(np.ceil(self.nsim / 400)), xmax=55, xmin=45) valve = Steps(nx=1, nsim=self.nsim, values=None, randsteps=int(np.ceil(self.nsim / 400)), xmax=100, xmin=0) self.U = np.vstack([c.T, valve.T]).T self.nu = 2 self.nx = 1
def parameters(self): super().parameters() self.ts = 1 self.c1 = 0.08 # inlet valve coefficient self.c2 = 0.04 # tank outlet coefficient # Initial Conditions for the States self.x0 = np.asarray([0, 0]) # default simulation setup pump = Steps(nx=1, nsim=self.nsim, values=None, randsteps=int(np.ceil(self.nsim / 400)), xmax=0.5, xmin=0) valve = Steps(nx=1, nsim=self.nsim, values=None, randsteps=int(np.ceil(self.nsim / 400)), xmax=0.4, xmin=0) self.U = np.vstack([pump.T, valve.T]).T self.nu = 2 self.nx = 2
def parameters(self): # Volumetric Flowrate (m^3/sec) self.q = 100 # Volume of CSTR (m^3) self.V = 100 # Density of A-B Mixture (kg/m^3) self.rho = 1000 # Heat capacity of A-B Mixture (J/kg-K) self.Cp = 0.239 # Heat of reaction for A->B (J/mol) self.mdelH = 5e4 # E - Activation energy in the Arrhenius Equation (J/mol) # R - Universal Gas Constant = 8.31451 J/mol-K self.EoverR = 8750 # Pre-exponential factor (1/sec) self.k0 = 7.2e10 # U - Overall Heat Transfer Coefficient (W/m^2-K) # A - Area - this value is specific for the U calculation (m^2) self.UA = 5e4 # Steady State Initial Conditions for the States self.Ca_ss = 0.87725294608097 self.T_ss = 324.475443431599 self.x0 = np.empty(2) self.x0[0] = self.Ca_ss self.x0[1] = self.T_ss # Steady State Initial Condition for the Uncontrolled Inputs self.u_ss = 300.0 # cooling jacket Temperature (K) self.Tf = 350 # Feed Temperature (K) self.Caf = 1 # Feed Concentration (mol/m^3) # dimensions self.nx = 2 self.nu = 1 self.nd = 2 # default simulation setup self.U = self.u_ss + Steps(nx=1, nsim=self.nsim, values=None, randsteps=int(np.ceil(self.nsim / 100)), xmax=6, xmin=-6)
class GymWrapper(EmulatorBase): """ wrapper for OpenAI gym environments https://gym.openai.com/read-only.html https://github.com/openai/gym # TODO: include visualization option for the trajectories or render of OpenAI gym """ def __init__(self, nsim=1000, ninit=0, system='Pendulum-v0', seed=59): super().__init__(nsim=nsim, ninit=ninit) self.system = system def parameters(self, system='Pendulum-v0'): self.system = system self.env = gym.make(self.system) self.env.reset() # to reset the environment state self.x0 = self.env.state self.nx = self.x0.shape[0] self.action_sample = self.env.action_space.sample() self.nu = np.asarray([self.action_sample]).shape[0] # default simulation setup self.U = np.zeros([(self.nsim - 1), self.nu]) self.U[int(self.nsim / 8)] = 0.1 self.U = Steps(nx=1, nsim=self.nsim, values=None, randsteps=int(np.ceil(self.nsim / 40)), xmax=0.5, xmin=-0.5) if type(self.action_sample) == int: self.U = self.U.astype(int) def equations(self, x, u): if type(self.action_sample) == int: u = u.item() self.env.state = x x, reward, done, info = self.env.step(u) return x, reward def simulate(self, nsim=None, U=None, x0=None, **kwargs): """ :param nsim: (int) Number of steps for open loop response :param U: (ndarray, shape=(self.nu)) control actions :param x0: (ndarray, shape=(self.nx)) Initial state. If not give will use internal state. :return: The response trajectories, X """ if nsim is None: nsim = self.nsim if U is None: U = self.U if x0 is None: x = self.x0 else: assert x0.shape[0] == self.nx, "Mismatch in x0 size" x = x0 X, Reward = [], [] N = 0 for u in U: x, reward = self.equations(x, u) X.append(x) # updated states trajectories Reward.append(reward) # updated states trajectories N += 1 if N == nsim: break Xout = np.asarray(X) Yout = np.asarray(Reward).reshape(-1, 1) Uout = np.asarray(U) return {'X': Xout, 'Y': Yout, 'U': Uout}