def test_RotatingMasses(self): """ Get configured do-mpc modules: """ model = template_model() mpc = template_mpc(model) simulator = template_simulator(model) mhe = template_mhe(model) """ Set initial state """ np.random.seed(99) # Use different initial state for the true system (simulator) and for MHE / MPC x0_true = np.random.rand(model.n_x)-0.5 x0 = np.zeros(model.n_x) mpc.x0 = x0 simulator.x0 = x0_true mhe.x0 = x0 mhe.p_est0 = 1e-4 # Set initial guess for MHE/MPC based on initial state. mpc.set_initial_guess() mhe.set_initial_guess() """ Run some steps: """ for k in range(5): u0 = mpc.make_step(x0) y_next = simulator.make_step(u0) x0 = mhe.make_step(y_next) """ Store results (from reference run): """ #do_mpc.data.save_results([mpc, simulator, mhe], 'results_rotatingMasses', overwrite=True) """ Compare results to reference run: """ ref = do_mpc.data.load_results('./results/results_rotatingMasses.pkl') test = ['_x', '_u', '_time', '_z'] for test_i in test: # Check MPC check = np.allclose(mpc.data.__dict__[test_i], ref['mpc'].__dict__[test_i]) self.assertTrue(check) # Check Simulator check = np.allclose(simulator.data.__dict__[test_i], ref['simulator'].__dict__[test_i]) self.assertTrue(check) # Estimator check = np.allclose(mhe.data.__dict__[test_i], ref['estimator'].__dict__[test_i]) self.assertTrue(check)
def test_oscillating_masses_discrete(self): """ Get configured do-mpc modules: """ model = template_model() mpc = template_mpc(model) simulator = template_simulator(model) estimator = do_mpc.estimator.StateFeedback(model) """ Set initial state """ np.random.seed(99) x0 = np.random.rand(model.n_x) - 0.5 mpc.x0 = x0 simulator.x0 = x0 estimator.x0 = x0 # Use initial state to set the initial guess. mpc.set_initial_guess() # This is only meaningful for DAE systems. simulator.set_initial_guess() """ Run some steps: """ for k in range(5): u0 = mpc.make_step(x0) y_next = simulator.make_step(u0) x0 = estimator.make_step(y_next) """ Store results (from reference run): """ #do_mpc.data.save_results([mpc, simulator, estimator], 'results_oscillatingMasses_dae', overwrite=True) """ Compare results to reference run: """ ref = do_mpc.data.load_results( './results/results_oscillatingMasses_dae.pkl') test = ['_x', '_u', '_aux', '_time', '_z'] for test_i in test: # Check MPC check = np.allclose(mpc.data.__dict__[test_i], ref['mpc'].__dict__[test_i]) self.assertTrue(check) # Check Simulator check = np.allclose(simulator.data.__dict__[test_i], ref['simulator'].__dict__[test_i]) self.assertTrue(check) # Estimator check = np.allclose(estimator.data.__dict__[test_i], ref['estimator'].__dict__[test_i]) self.assertTrue(check)
def test_CSTR(self): """ Get configured do-mpc modules: """ model = template_model() mpc = template_mpc(model) simulator = template_simulator(model) estimator = do_mpc.estimator.StateFeedback(model) """ Set initial state """ # Set the initial state of mpc and simulator: C_a_0 = 0.8 # This is the initial concentration inside the tank [mol/l] C_b_0 = 0.5 # This is the controlled variable [mol/l] T_R_0 = 134.14 #[C] T_K_0 = 130.0 #[C] x0 = np.array([C_a_0, C_b_0, T_R_0, T_K_0]).reshape(-1, 1) mpc.x0 = x0 simulator.x0 = x0 mpc.set_initial_guess() """ Run some steps: """ for k in range(5): u0 = mpc.make_step(x0) y_next = simulator.make_step(u0) x0 = estimator.make_step(y_next) """ Store results (from reference run): """ #do_mpc.data.save_results([mpc, simulator, estimator], 'results_CSTR') """ Compare results to reference run: """ ref = do_mpc.data.load_results('./results/results_CSTR.pkl') test = ['_x', '_u', '_time', '_z'] for test_i in test: # Check MPC check = np.allclose(mpc.data.__dict__[test_i], ref['mpc'].__dict__[test_i]) self.assertTrue(check) # Check Simulator check = np.allclose(simulator.data.__dict__[test_i], ref['simulator'].__dict__[test_i]) self.assertTrue(check) # Estimator check = np.allclose(estimator.data.__dict__[test_i], ref['estimator'].__dict__[test_i]) self.assertTrue(check)
def test_batch_reactor(self): """ Get configured do-mpc modules: """ model = template_model() mpc = template_mpc(model) simulator = template_simulator(model) estimator = do_mpc.estimator.StateFeedback(model) """ Set initial state """ X_s_0 = 1.0 # This is the initial concentration inside the tank [mol/l] S_s_0 = 0.5 # This is the controlled variable [mol/l] P_s_0 = 0.0 #[C] V_s_0 = 120.0 #[C] x0 = np.array([X_s_0, S_s_0, P_s_0, V_s_0]) mpc.x0 = x0 simulator.x0 = x0 estimator.x0 = x0 mpc.set_initial_guess() """ Run some steps: """ for k in range(5): u0 = mpc.make_step(x0) y_next = simulator.make_step(u0) x0 = estimator.make_step(y_next) """ Compare results to reference run: """ ref = do_mpc.data.load_results('./results/results_batch_rector.pkl') test = ['_x', '_u', '_time', '_z'] for test_i in test: # Check MPC check = np.allclose(mpc.data.__dict__[test_i], ref['mpc'].__dict__[test_i]) self.assertTrue(check) # Check Simulator check = np.allclose(simulator.data.__dict__[test_i], ref['simulator'].__dict__[test_i]) self.assertTrue(check) # Estimator check = np.allclose(estimator.data.__dict__[test_i], ref['estimator'].__dict__[test_i]) self.assertTrue(check) """
import matplotlib.gridspec as gridspec import time from template_model import template_model from template_mpc import template_mpc from template_simulator import template_simulator """ User settings: """ show_animation = False store_results = False """ Get configured do-mpc modules: """ model = template_model() mpc = template_mpc(model) simulator = template_simulator(model) estimator = do_mpc.estimator.StateFeedback(model) """ Set initial state """ X_s_0 = 0.0 # This is the initial concentration inside the tank [mol/l] Y_s_0 = 0.0 # This is the controlled variable [mol/l] #V_s_0 = 0.0 #[C] T_s_0 = 0.0 #[C] x0 = np.array([X_s_0, Y_s_0, T_s_0]) mpc.x0 = x0 simulator.x0 = x0 estimator.x0 = x0
def test_industrialpoly(self): """ Get configured do-mpc modules: """ model = template_model() mpc = template_mpc(model) simulator = template_simulator(model) estimator = do_mpc.estimator.StateFeedback(model) """ Set initial state """ # Set the initial state of mpc and simulator: x0 = model._x(0) delH_R_real = 950.0 c_pR = 5.0 x0['m_W'] = 10000.0 x0['m_A'] = 853.0 x0['m_P'] = 26.5 x0['T_R'] = 90.0 + 273.15 x0['T_S'] = 90.0 + 273.15 x0['Tout_M'] = 90.0 + 273.15 x0['T_EK'] = 35.0 + 273.15 x0['Tout_AWT'] = 35.0 + 273.15 x0['accum_monom'] = 300.0 x0['T_adiab'] = x0['m_A'] * delH_R_real / ( (x0['m_W'] + x0['m_A'] + x0['m_P']) * c_pR) + x0['T_R'] mpc.set_initial_state(x0, reset_history=True) simulator.set_initial_state(x0, reset_history=True) """ Run some steps: """ for k in range(5): u0 = mpc.make_step(x0) y_next = simulator.make_step(u0) x0 = estimator.make_step(y_next) """ Store results (from reference run): """ #do_mpc.data.save_results([mpc, simulator, estimator], 'results_industrial_poly') """ Compare results to reference run: """ ref = do_mpc.data.load_results('./results/results_industrial_poly.pkl') test = ['_x', '_u', '_time', '_z'] for test_i in test: # Check MPC check = np.allclose(mpc.data.__dict__[test_i], ref['mpc'].__dict__[test_i]) self.assertTrue(check) # Check Simulator check = np.allclose(simulator.data.__dict__[test_i], ref['simulator'].__dict__[test_i]) self.assertTrue(check) # Estimator check = np.allclose(estimator.data.__dict__[test_i], ref['estimator'].__dict__[test_i]) self.assertTrue(check)
def test_industrialpoly(self): """ Get configured do-mpc modules: """ model = template_model() mpc = template_mpc(model) simulator = template_simulator(model) estimator = do_mpc.estimator.StateFeedback(model) """ Set initial state """ delH_R_real = 950.0 c_pR = 5.0 # x0 is a property of the simulator - we obtain it and set values. x0 = simulator.x0 x0['m_W'] = 10000.0 x0['m_A'] = 853.0 x0['m_P'] = 26.5 x0['T_R'] = 90.0 + 273.15 x0['T_S'] = 90.0 + 273.15 x0['Tout_M'] = 90.0 + 273.15 x0['T_EK'] = 35.0 + 273.15 x0['Tout_AWT'] = 35.0 + 273.15 x0['accum_monom'] = 300.0 x0['T_adiab'] = x0['m_A'] * delH_R_real / ( (x0['m_W'] + x0['m_A'] + x0['m_P']) * c_pR) + x0['T_R'] # Finally, the controller gets the same initial state. mpc.x0 = x0 # Which is used to set the initial guess: mpc.set_initial_guess() """ Run some steps: """ for k in range(5): u0 = mpc.make_step(x0) y_next = simulator.make_step(u0) x0 = estimator.make_step(y_next) """ Store results (from reference run): """ #do_mpc.data.save_results([mpc, simulator, estimator], 'results_industrial_poly', overwrite=True) """ Compare results to reference run: """ ref = do_mpc.data.load_results('./results/results_industrial_poly.pkl') test = ['_x', '_u', '_time', '_z'] for test_i in test: # Check MPC check = np.allclose(mpc.data.__dict__[test_i], ref['mpc'].__dict__[test_i]) self.assertTrue(check) # Check Simulator check = np.allclose(simulator.data.__dict__[test_i], ref['simulator'].__dict__[test_i]) self.assertTrue(check) # Estimator check = np.allclose(estimator.data.__dict__[test_i], ref['estimator'].__dict__[test_i]) self.assertTrue(check)
def test_dip(self): """ Get configured do-mpc modules: """ # Define obstacles to avoid (cicles) obstacles = [ { 'x': 0., 'y': 0.6, 'r': 0.3 }, ] model = template_model(obstacles) mpc = template_mpc(model) simulator = template_simulator(model) estimator = do_mpc.estimator.StateFeedback(model) """ Set initial state """ simulator.x0['theta'] = np.pi simulator.x0['pos'] = 0 x0 = simulator.x0.cat.full() mpc.x0 = x0 estimator.x0 = x0 mpc.set_initial_guess() """ Run some steps: """ for k in range(5): u0 = mpc.make_step(x0) y_next = simulator.make_step(u0) x0 = estimator.make_step(y_next) """ Store results (from reference run): """ #do_mpc.data.save_results([mpc, simulator, estimator], 'results_dip', overwrite=True) """ Compare results to reference run: """ ref = do_mpc.data.load_results('./results/results_dip.pkl') test = ['_x', '_u', '_time', '_z'] for test_i in test: # Check MPC check = np.allclose(mpc.data.__dict__[test_i], ref['mpc'].__dict__[test_i]) self.assertTrue(check) # Check Simulator check = np.allclose(simulator.data.__dict__[test_i], ref['simulator'].__dict__[test_i]) self.assertTrue(check) # Estimator check = np.allclose(estimator.data.__dict__[test_i], ref['estimator'].__dict__[test_i]) self.assertTrue(check)