def test_MPC_solve_u_opt_2d(self): '''test of MPC.solve_u_opt() method for 2d system''' dt = self.dt dyn2 = self.dyn2 n_sys = 2 assert n_sys == dyn2.nx n_hor = int(2.5/dt) assert n_hor == 12 u_max = 1.2 #kW c2 = dmpc.MPC(dyn2, n_hor, 0, u_max, 1, 100) # input predictions. needs to be flattened T_ext_hor = 2+np.zeros((n_hor, n_sys)) # °C T_ext_hor = T_ext_hor.reshape((n_hor * n_sys, 1)) Ts_hor = 18 + np.zeros((n_hor, n_sys)) # °C Ts_hor[n_hor//2:] = 22 # °C Ts_hor = Ts_hor.reshape((n_hor * n_sys, 1)) T0 = np.array([20,10]).reshape((n_sys,1)) c2.set_xyp(T0, Ts_hor, T_ext_hor) u_opt = c2.solve_u_opt() assert_equal(u_opt.shape, (n_hor * n_sys, 1)) u_opt = c2.solve_u_opt(reshape_2d=True) assert_equal(u_opt.shape, (n_hor, n_sys)) u_expected = np.array([ [ 0.39993771, 1.19999994], [ 0.79998731, 1.19999975], [ 0.79998749, 1.19994822], [ 0.7999875 , 0.83837907], [ 0.79998827, 0.80037647], [ 1.1215989 , 1.12105229], [ 1.19999977, 1.19999963], [ 1.19999867, 1.19999681], [ 1.0593051 , 1.05942779], [ 0.99998763, 0.99998742], # steady state at 1 kW [ 0.99998749, 0.9999875 ], [ 0.99973719, 0.99973719]]) print(repr(u_opt)) assert_allclose6(u_opt, u_expected)
def test_pred_output(self): '''test MPC.pred_output method for 2d system''' dt = self.dt dyn2 = self.dyn2 n_sys = 2 assert n_sys == dyn2.nx n_hor = int(2.5/dt) assert n_hor == 12 u_max = 1.2 #kW c2 = dmpc.MPC(dyn2, n_hor, 0, u_max, 1, 100) # input predictions. needs to be flattened T_ext_hor = 2+np.zeros((n_hor, n_sys)) # °C T_ext_hor = T_ext_hor.reshape((-1, 1)) u_hor = np.zeros((n_hor, n_sys)) u_hor[n_hor//2:] = 1 u_hor = u_hor.reshape((-1, 1)) T0 = np.array([20,10]).reshape((n_sys,1)) T_pred = c2.pred_output(T0, u_hor, T_ext_hor) assert_equal(T_pred.shape, (n_sys*n_hor, 1)) T_pred = c2.pred_output(T0, u_hor, T_ext_hor, reshape_2d=True) assert_equal(T_pred.shape, (n_hor, n_sys)) T_expected = np.array([ [ 16.4 , 8.4 ], [ 13.52 , 7.12 ], [ 11.216 , 6.096 ], [ 9.3728 , 5.2768 ], [ 7.89824 , 4.62144 ], [ 6.718592 , 4.097152 ], [ 9.7748736 , 7.6777216 ], [ 12.21989888, 10.54217728], [ 14.1759191 , 12.83374182], [ 15.74073528, 14.66699346], [ 16.99258823, 16.13359477], [ 17.99407058, 17.30687581]]) print(T_pred) assert_allclose9(T_pred, T_expected)
def test_MPC_solve_u_opt(self): '''test of MPC.solve_u_opt() method (calls cvxopt)''' dt = self.dt dyn = self.dyn1 n_hor = int(2.5/dt) assert n_hor == 12 u_max = 1.2 #kW ctrl = dmpc.MPC(dyn, n_hor, 0, u_max, 1, 100) zn = np.zeros(n_hor)[:,None] T_ext_hor = 2 + zn # °C Ts_hor = 18 + zn # °C Ts_hor[5:] = 22 # °C () T0 = 20 # °C ctrl.set_xyp(T0, Ts_hor, T_ext_hor) u_opt = ctrl.solve_u_opt() assert_equal(u_opt.shape, (n_hor,1)) u_expected = np.array([ [ 0.39993755], [ 0.79998746], # steady state at 0.8 kW [ 0.7999875 ], [ 0.79998751], [ 1.12160221], [ 1.19999986], # peak at u_max [ 1.19999099], [ 1.05931038], [ 0.9999868 ], # steady state at 1 kW [ 0.99998794], [ 0.99998734], [ 0.99973748]]) print((u_opt - u_expected)) assert_allclose6(u_opt, u_expected)
def test_MPC_closed_loop_sim_2d(self): '''test MPC.closed_loop_sim() method on a 2d system''' dt = self.dt dyn2 = self.dyn2 n_sys = 2 assert n_sys == dyn2.nx n_hor = int(1.5/dt) assert n_hor == 7 n_sim = int(3/dt) assert n_sim == 15 u_max = 1.2 #kW c2 = dmpc.MPC(dyn2, n_hor, 0, u_max, 1, 100) Ts_fcast_arr = 18 + np.zeros((n_sim+n_hor, n_sys)) # °C Ts_fcast_arr[n_sim//2:] = 22 # °C Ts_fcast = dmpc.Oracle(Ts_fcast_arr) T_ext_fcast = dmpc.ConstOracle([2, 2]) # °C T0 = np.array([20,10]).reshape((n_sys,1)) # °C c2.set_oracles(Ts_fcast, T_ext_fcast) u_sim, p, x, T_sim, Ts = c2.closed_loop_sim(T0, n_sim) # check consistency of T set point with Oracle input assert_equal(Ts.shape, (n_sim, n_sys)) assert_allclose9(Ts, Ts_fcast_arr[0:n_sim, :]) # Check temperature output assert_equal(T_sim.shape, (n_sim, n_sys)) T_expected = np.array([ [ 20. , 10. ], # Ts is 18° [ 17.99975046, 13.19999987], [ 17.99974999, 15.75999979], [ 17.99974999, 17.80799464], [ 17.99974966, 17.99974909], [ 17.99975053, 17.99975053], [ 19.28619851, 19.28619851], [ 20.62895623, 20.62895623], # Ts becomes 22° [ 21.70300215, 21.70300215], [ 21.99974997, 21.99974997], [ 21.99974998, 21.99974998], [ 21.99974998, 21.99974998], [ 21.99974998, 21.99974998], [ 21.99974998, 21.99974998], [ 21.99974998, 21.99974998]]) print((T_sim - T_expected)) print(np.hstack((T_sim, T_expected))) assert_allclose6(T_sim, T_expected) # Check MPC control sequence assert_equal(u_sim.shape, (n_sim, n_sys)) u_expected = np.array([ [ 0.39993761, 1.19999997], [ 0.7999874 , 1.19999997], # u1 steady state at 0.8 kW, u2=u_max [ 0.7999875 , 1.1999987 ], [ 0.79998742, 0.83833834], [ 0.7999877 , 0.79998781], # steady state at 0.8 kW [ 1.12159952, 1.12159952], [ 1.19999936, 1.19999936], # peak at u_max [ 1.19995929, 1.19995929], [ 1.05933706, 1.05933706], [ 0.9999875 , 0.9999875 ], # steady state at 1 kW [ 0.9999875 , 0.9999875 ], [ 0.9999875 , 0.9999875 ], [ 0.9999875 , 0.9999875 ], [ 0.9999875 , 0.9999875 ], [ 0.9999875 , 0.9999875 ]]) print((u_sim - u_expected)) assert_allclose6(u_sim, u_expected)
def test_MPC_closed_loop_sim(self): '''test MPC.closed_loop_sim() method on a short simulation''' dt = self.dt dyn = self.dyn1 n_hor = int(1.5/dt) assert n_hor == 7 n_sim = int(3/dt) assert n_sim == 15 u_max = 1.2 #kW ctrl = dmpc.MPC(dyn, n_hor, 0, u_max, 1, 100) Ts_fcast_arr = 18 + np.zeros(n_sim+n_hor) # °C Ts_fcast_arr[n_sim//2:] = 22 # °C Ts_fcast = dmpc.Oracle(Ts_fcast_arr) T_ext_fcast = dmpc.ConstOracle(2) # °C T0 = np.atleast_2d(20) # °C ctrl.set_oracles(Ts_fcast, T_ext_fcast) u_sim, p, x, T_sim, Ts = ctrl.closed_loop_sim(T0, n_sim) # check consistency of T set point with Oracle input assert_equal(Ts.shape, (n_sim,1)) assert_allclose9(Ts, Ts_fcast_arr[0:n_sim, None]) # Check temperature output assert_equal(T_sim.shape, (n_sim,1)) T_expected = np.array([ [ 20. ], # Ts is 18° [ 17.99975035], [ 17.99974999], [ 17.99974999], [ 17.99974966], [ 17.99975053], [ 19.28619851], [ 20.62895623], # Ts becomes 22° [ 21.70300215], [ 21.99974997], [ 21.99974998], [ 21.99974998], [ 21.99974998], [ 21.99974998], [ 21.99974998]]) print((T_sim - T_expected)) print(np.hstack((T_sim, T_expected))) assert_allclose6(T_sim, T_expected) # Check MPC control sequence assert_equal(u_sim.shape, (n_sim,1)) u_expected = np.array([ [ 0.39993759], [ 0.79998743], # steady state at 0.8 kW [ 0.7999875 ], [ 0.79998742], [ 0.7999877 ], [ 1.12159952], # peak at u_max [ 1.19999936], [ 1.19995929], [ 1.05933706], [ 0.9999875 ], # steady state at 1 kW [ 0.9999875 ], [ 0.9999875 ], [ 0.9999875 ], [ 0.9999875 ], [ 0.9999875 ]]) print((u_sim - u_expected)) assert_allclose6(u_sim, u_expected)
# Create the dynamics from therm_data import dt, C, R, P_max print('Timestep dt: {} h'.format(dt)) print('Capacity C: {} kWh/K'.format(C)) print('Resistance R: {} °C/kW'.format(R)) print('P_max: {} kWh'.format(P_max)) dyn = dmpc.dynamics.from_thermal(R, C, dt, 'single room') # Create the MPC controller th = 2 # hours nh = int(th / dt) ctrl = dmpc.MPC(dyn, nh, u_min=0, u_max=P_max, u_cost=1, track_weight=100) # Create the simulation context, e.g. forecast of perturbation and set points from therm_data import T0, T_out, occupancy, T_abs, T_pres print('Init temp: {} °C'.format(T0)) print('Outside temp: {} °C'.format(T_out)) print('Temp setpoints: {} °C (absent), {} °C (present)'.format(T_abs, T_pres)) n_sim = int(24 / dt) t_fcast = np.arange(n_sim + nh) * dt occ = occupancy(t_fcast) T_set = np.zeros(n_sim + nh) + T_abs # °C T_set[occ] = T_pres