def test_uguess_outside_bounds(self): """Test initial guess for u outsidethe feasible region (should be adjusted)""" # Bounds ubounds = [(0., 4000.)] xbounds = [(21., 23.), (0., 50.), (0., 50.)] # Initial u uguess = np.full((self.inp.index.size - 1, 1), 9999.) # Initial state x0 = [21., 21., 21.] # Instantiate multiple shooting optc = mshoot.MShoot(cfun=cfun) # Optimize udf, xdf = optc.optimize(model=self.model, inp=self.inp, free=['qhvac'], ubounds=ubounds, xbounds=xbounds, x0=x0, uguess=uguess, ynominal=[20., 20., 20., 4000.], join=1, maxiter=2) # Heating power should not exceed 4000 W self.assertTrue((udf['qhvac'] < 4000.).all())
def test_normalized_uy(self): """Test the right solution is achieved with nominal u and y""" # Bounds ubounds = [(0., 4000.)] xbounds = [(21., 23.), (0., 50.), (0., 50.)] # Initial state x0 = [21., 21., 21.] # Instantiate multiple shooting optc = mshoot.MShoot(cfun=cfun) # Optimize udf, xdf = optc.optimize(model=self.model, inp=self.inp, free=['qhvac'], ubounds=ubounds, xbounds=xbounds, x0=x0, uguess=None, unominal=[4000.], ynominal=[20., 20., 20., 4000.], join=3) # Final room temperature should be equal to lower bound self.assertLess(np.abs(xdf.iloc[-1][0] - xbounds[0][0]), 1e-4, "Final state different than lower bound") # Heating should be provided self.assertGreater(udf['qhvac'].sum(), 0, "qhvac should be positive")
def test_infeasible_x0(self): """Test if good solution is found even with infeasible x0""" # Bounds ubounds = [(0., 4000.)] xbounds = [(21., 23.), (0., 50.), (0., 50.)] # Initial state x0 = [15., 21., 21.] # <- infeasible x0 # Instantiate multiple shooting optc = mshoot.MShoot(cfun=cfun) # Optimize udf, xdf = optc.optimize(model=self.model, inp=self.inp, free=['qhvac'], ubounds=ubounds, xbounds=xbounds, ynominal=[20., 20., 20., 4000.], x0=x0, uguess=None, join=3) # Final room temperature should be equal to lower bound self.assertLess(np.abs(xdf.iloc[-1][0] - xbounds[0][0]), 1e-4, "Final state different than lower bound") # Heating should be provided self.assertGreater(udf['qhvac'].sum(), 0, "qhvac should be positive")
def test_optimize_step_bounds(self): """Test preheating (step increase in state lower bound)""" # Bounds Tlo = np.full(self.inp.index.size, 16.) Tlo[-2:] = 19. plt.plot(Tlo) ubounds = [(0., 5000.)] xbounds = [(Tlo, 30.), (0., 50.), (0., 50.)] # Initial state x0 = [16., 16., 16.] # Instantiate multiple shooting optc = mshoot.MShoot(cfun=cfun) # Optimize udf, xdf = optc.optimize(model=self.model, inp=self.inp, free=['qhvac'], ubounds=ubounds, xbounds=xbounds, ynominal=[20., 20., 20., 4000.], x0=x0, uguess=None, join=1) # Final room temperature should be equal to lower bound self.assertLess(np.abs(xdf.iloc[-1][0] - xbounds[0][0][-1]), 1e-4, "Final state different than lower bound") # Room temperature should be always higher or equal to the lower bound self.assertTrue((xdf['x0'].values >= Tlo).all(), "Temperature bound violated") # Heating should be provided self.assertGreater(udf['qhvac'].sum(), 0, "qhvac should be positive")
def test_optimal_control(self): print('** Test FMU optimal control **') # Inputs t = np.arange(0, 3600 * 10, 3600) inp = pd.DataFrame(index=pd.Index(t, name='time'), columns=['q', 'Tout']) inp['q'] = np.full(t.size, 0) inp['Tout'] = np.full(t.size, 273.15) # Bounds ubounds = [(0., 4000.)] xbounds = [(293.15, 296.15)] # Initial state x0 = [293.65] # Optimization mpc = mshoot.MShoot(cfun=cfun) udf, xdf = mpc.optimize( model=self.model, inp=inp, free=['q'], ubounds=ubounds, xbounds=xbounds, x0=x0, uguess=None, unominal=[4000.], ynominal=[4000., 293.15], ) # ax = udf.plot(title='udf') # ax.set_ylim(0, 4000) # ax = xdf.plot(title='xdf') # ax.set_ylim(293.15, 296.15) # plt.show() self.assertLess(abs(xdf['x0'].iloc[-1] - 293.15), 0.01) self.assertLess(abs(udf['q'].iloc[-1] - 2000.), 0.01)
# Inputs t = np.arange(0, 3600 * 10, 3600) inp = pd.DataFrame(index=pd.Index(t, name='time'), columns=['q', 'Tout']) inp['q'] = np.full(t.size, 0.) inp['Tout'] = np.full(t.size, 273.15) # Bounds ubounds = [(0., 4000.)] xbounds = [(293.15, 296.15), (293.15, 296.15)] # Initial state x0 = [293.65, 293.35] # Optimization mpc = mshoot.MShoot(cfun=cfun) udf, xdf = mpc.optimize( model=get_model(), inp=inp, free=['q'], ubounds=ubounds, xbounds=xbounds, x0=x0, uguess=None, ynominal=[4000, 295.], join=1, maxiter=30 ) # Show results print(udf)