def test_zero_delay_derivative(self): m1 = Monomial(0, 1, [], [0], [], [2]) m = [m1] p = Polynomial(m) y = [[]] u = [[1]] params = [2] dy, du, dparams = p.derivatives(y, u, params) assert_array_almost_equal(du, [[[4]]]) assert_array_almost_equal(dy, np.reshape([], (1, 0, 1))) assert_array_almost_equal(dparams, [[1]])
def test_ar_model_predict_derivative(self): m1 = Monomial(1, 0, [1], [], [1], []) m2 = Monomial(1, 0, [1], [], [2], []) m = [m1, m2] mdl = Polynomial(m) y = [[0.500000000000000], [0.925000000000000], [0.256687500000000], [0.705956401171875], [0.768053255020420], [0.659145574149943], [0.831288939045395], [0.518916263804854], [0.923676047365561], [0.260844845488171], [0.713377804660565], [0.756538676169480], [0.681495258228080], [0.803120043590673], [0.585037484942277], [0.898243916772361], [0.338186596189094], [0.828120762684376], [0.526646030853067], [0.922372959447176]] u = np.reshape([], (20, 0)) params = [3.7, -3.7] jac = predict_derivatives(mdl, y[:-1], u[:-1], params) jac_numeric = predict_numeric_derivatives(mdl, y[:-1], u[:-1], params) assert_array_almost_equal(jac, jac_numeric)
def test_fir_model_predict_derivative(self): m1 = Monomial(0, 1, [], [1], [], [1]) m2 = Monomial(0, 1, [], [1], [], [2]) m = [m1, m2] mdl = Polynomial(m) y = [[0], [-1], [-6], [-15], [-28], [-45], [-66], [-91], [-120], [-153], [-190], [-231], [-276], [-325], [-378], [-435], [-496], [-561], [-630], [-703]] u = np.arange(1, 21).reshape((20, 1)) params = [1, -2] jac = predict_derivatives(mdl, y[:-1], u[:-1], params) jac_numeric = predict_numeric_derivatives(mdl, y[:-1], u[:-1], params) assert_array_almost_equal(jac, jac_numeric)
def test_ar_model_simulation_error_multiple_shoot(self): m1 = Monomial(1, 0, [1], [], [1], []) m2 = Monomial(1, 0, [1], [], [2], []) m = [m1, m2] mdl = Polynomial(m) y = [[0.500000000000000], [0.925000000000000 - 1.4], [0.256687500000000 - 1.4], [0.705956401171875 - 1.4], [0.768053255020420 - 1.4], [0.659145574149943 - 1.4], [0.831288939045395 - 1.4], [0.518916263804854 - 1.4], [0.923676047365561 - 1.4], [0.260844845488171 - 1.4], [0.713377804660565 - 1.4], [0.756538676169480 - 1.4], [0.681495258228080 - 1], [0.803120043590673 - 1], [0.585037484942277 - 1], [0.898243916772361 - 1], [0.338186596189094 - 1], [0.828120762684376 - 1], [0.526646030853067 - 1], [0.922372959447176 - 1]] u = np.reshape([], (20, 0)) params = [3.7, -3.7] extended_params = params + [0.5] + \ [0.659145574149943] + \ [0.713377804660565] + \ [0.898243916772361] error = SimulationError(mdl, y, u, 5) dparams = error.derivatives(extended_params) dparams_numeric \ = error_numeric_derivatives(error, extended_params) assert_array_almost_equal(dparams, dparams_numeric, decimal=3)
def test_fir_derivative(self): m1 = Monomial(0, 1, [], [1], [], [1]) m2 = Monomial(0, 1, [], [1], [], [2]) m = [m1, m2] p = Polynomial(m) y = [[]] u = [[1]] params = [1, -2] dy, du, dparams = p.derivatives(y, u, params) assert_array_almost_equal(du, [[[-3]]]) assert_array_almost_equal(dy, np.reshape([], (1, 0, 1))) assert_array_almost_equal(dparams, [[1, 1]])
def test_zero_delay_numeric_derivative(self): m1 = Monomial(0, 1, [], [0], [], [2]) m = [m1] p = Polynomial(m) y = [[]] u = [[1]] params = [2] dy, du, dparams = p.derivatives(y, u, params) dy_numeric, du_numeric, \ dparams_numeric = p._numeric_derivatives(y, u, params) assert_array_almost_equal(dy, dy_numeric) assert_array_almost_equal(du, du_numeric) assert_array_almost_equal(dparams, dparams_numeric)
def test_noinput_numeric_derivative(self): m1 = Monomial(1, 0, [1], [], [1], []) m2 = Monomial(1, 0, [1], [], [2], []) m = [m1, m2] p = Polynomial(m) y = [[1]] u = [[]] params = [1, -2] dy, du, dparams = p.derivatives(y, u, params) dy_numeric, du_numeric, \ dparams_numeric = p._numeric_derivatives(y, u, params) assert_array_almost_equal(dy, dy_numeric) assert_array_almost_equal(du, du_numeric) assert_array_almost_equal(dparams, dparams_numeric)
def test_siso_derivatives(TestCase): m1 = Monomial(2, 2, [1, 2], [1, 2], [1, 2], [1, 1]) m2 = Monomial(1, 0, [1], [], [1], []) m3 = Monomial(2, 1, [2, 3], [2], [1, 2], [3]) m = [m1, m2, m3] p = Polynomial(m) y = [[1], [2], [3]] u = [[3], [4]] params = [1, 2, 3] dy, du, dparams = p.derivatives(y, u, params) assert_array_almost_equal(dy, [[[50], [1776], [2304]]]) assert_array_almost_equal(du, [[[16], [2604]]]) assert_array_almost_equal(dparams, [[48, 1, 1152]])
def test_zero_delay_polynomial(self): m1 = Monomial(0, 1, [], [0], [], [2]) m = [m1] p = Polynomial(m) y = [[]] u = [[2]] params = [2] assert_equal(p(y, u, params), [8])
def test_fir_polynomial(self): m1 = Monomial(0, 1, [], [1], [], [1]) m2 = Monomial(0, 1, [], [1], [], [2]) m = [m1, m2] p = Polynomial(m) y = [[]] u = [[1]] params = [1, -2] assert_equal(p(y, u, params), [-1])
def test_noinput_polynomial(self): m1 = Monomial(1, 0, [1], [], [1], []) m2 = Monomial(1, 0, [1], [], [2], []) m = [m1, m2] p = Polynomial(m) y = [[1]] u = [[]] params = [1, -2] assert_equal(p(y, u, params), [-1])
def test_zero_delay_atribute_settings(self): m1 = Monomial(0, 1, [], [0], [], [2]) m = [m1] p = Polynomial(m) assert_equal(p.Nparams, 1) assert_equal(p.N, 0) assert_equal(p.M, 0) assert_equal(p.delay, 0) assert_equal(p.Mu, 1) assert_equal(p.Ny, 1) assert_equal(p.Nu, 1)
def test_fir_atribute_settings(self): m1 = Monomial(0, 1, [], [1], [], [1]) m2 = Monomial(0, 1, [], [1], [], [2]) m = [m1, m2] p = Polynomial(m) assert_equal(p.Nparams, 2) assert_equal(p.N, 0) assert_equal(p.M, 1) assert_equal(p.delay, 1) assert_equal(p.Mu, 1) assert_equal(p.Ny, 1) assert_equal(p.Nu, 1)
def test_noinput_atribute_settings(self): m1 = Monomial(1, 0, [1], [], [1], []) m2 = Monomial(1, 0, [1], [], [2], []) m = [m1, m2] p = Polynomial(m) assert_equal(p.Nparams, 2) assert_equal(p.N, 1) assert_equal(p.M, 0) assert_equal(p.delay, 0) assert_equal(p.Mu, 0) assert_equal(p.Ny, 1) assert_equal(p.Nu, 0)
def test_siso_numeric_derivatives(TestCase): m1 = Monomial(2, 2, [1, 2], [1, 2], [1, 2], [1, 1]) m2 = Monomial(1, 0, [1], [], [1], []) m3 = Monomial(2, 1, [2, 3], [2], [1, 2], [3]) m4 = Monomial(3, 1, [2, 3, 5], [3], [1, 2, 2], [2]) m5 = Monomial(0, 1, [], [3], [], [2]) m = [m1, m2, m3, m4, m5] p = Polynomial(m) y = [[12], [2.23], [4.51], [2.1], [3.24]] u = [[3.3], [4.45], [3.4]] params = [1.8, 2.2, 3.232, 32.3, 3.34] dy, du, dparams = p.derivatives(y, u, params) dy_numeric, du_numeric, \ dparams_numeric = p._numeric_derivatives(y, u, params) assert_array_almost_equal(dy, dy_numeric) assert_array_almost_equal(du, du_numeric) assert_array_almost_equal(dparams, dparams_numeric)
def test_mimo_polynomial(TestCase): m1 = Monomial(2, 2, [1, 2], [1, 2], [1, 2], [1, 1], [0, 1], [0, 1]) m2 = Monomial(1, 0, [1], [], [1], [], [1]) m3 = Monomial(2, 1, [2, 3], [2], [1, 2], [3], [0, 0], [1]) m = [[m1, m2], [m3]] p = Polynomial(m) y = [[1, 2], [2, 3], [3, 4]] u = [[3, 5], [4, 7]] params = [1, 2, 3] dy, du, dparams = p.derivatives(y, u, params) assert_array_almost_equal(dy, [[[189, 2], [0, 126], [0, 0]], [[0, 0], [9261, 0], [12348, 0]]]) assert_array_almost_equal(du, [[[63, 0], [0, 27]], [[0, 0], [0, 7938]]]) assert_array_almost_equal(dparams, [[189, 2, 0], [0, 0, 6174]])
def test_siso_atribute_settings(self): m1 = Monomial(3, 2, [1, 2, 4], [2, 3], [1, 2, 2], [1, 1]) m2 = Monomial(1, 0, [1], [], [1], []) m3 = Monomial(2, 1, [2, 3], [2], [1, 2], [3]) m = [m1, m2, m3] p = Polynomial(m) assert_equal(p.Nparams, 3) assert_equal(p.N, 4) assert_equal(p.M, 3) assert_equal(p.delay, 2) assert_equal(p.Ny, 1) assert_equal(p.Nu, 1)
def test_mimo_numeric_derivatives(TestCase): m1 = Monomial(2, 2, [1, 2], [1, 2], [1, 2], [1, 1], [1, 0]) m2 = Monomial(1, 0, [1], [], [1], [], [1]) m3 = Monomial(2, 1, [2, 3], [2], [1, 2], [3], [0, 0], [2]) m4 = Monomial(3, 1, [2, 3, 5], [3], [1, 2, 2], [2], [0, 1, 0], [0]) m5 = Monomial(0, 1, [], [3], [], [2], [], [2]) m = [[m1, m2, m3, m4, m5], [m1, m2]] p = Polynomial(m) y = [[12, 1.23], [2.23, 23.3], [4.51, 3.4], [2.1, 34.54], [3.24, 324.23]] u = [[3.3, 132, 1.2], [4.45, 32.32, 2.23], [3.4, 1.3214, 2.132]] params = [1.8, 2.2, 3.232, 32.3, 3.34, 3.3425, 13] dy, du, dparams = p.derivatives(y, u, params) dy_numeric, du_numeric, \ dparams_numeric = p._numeric_derivatives(y, u, params) assert_array_almost_equal(dy, dy_numeric) assert_array_almost_equal(du, du_numeric) assert_array_almost_equal(dparams, dparams_numeric)
def test_siso_polynomial(TestCase): m1 = Monomial(2, 2, [1, 2], [1, 2], [1, 2], [1, 1]) m2 = Monomial(1, 0, [1], [], [1], []) m3 = Monomial(2, 1, [2, 3], [2], [1, 2], [3]) m = [m1, m2, m3] p = Polynomial(m) y = [[1], [2], [3]] u = [[3], [4]] params = [1, 2, 3] assert_equal(p(y, u, params), [3506])
def test_fir_model_simulate(self): m1 = Monomial(0, 1, [], [1], [], [1]) m2 = Monomial(0, 1, [], [1], [], [2]) m = [m1, m2] mdl = Polynomial(m) y = [[0], [-1], [-6], [-15], [-28], [-45], [-66], [-91], [-120], [-153], [-190], [-231], [-276], [-325], [-378], [-435], [-496], [-561], [-630], [-703]] u = np.arange(1, 21).reshape((20, 1)) params = [1, -2] ys = simulate(mdl, np.reshape([], [0, 1]), u[:-1], params) assert_array_almost_equal(ys, y[1:])
def test_mimo_polynomial(TestCase): m1 = Monomial(2, 2, [1, 2], [1, 2], [1, 2], [1, 1], [0, 1], [0, 1]) m2 = Monomial(1, 0, [1], [], [1], [], [1]) m3 = Monomial(2, 1, [2, 3], [2], [1, 2], [3], [0, 0], [1]) m = [[m1, m2], [m3]] p = Polynomial(m) y = [[1, 2], [2, 3], [3, 4]] u = [[3, 5], [4, 7]] params = [1, 2, 3] assert_equal(p(y, u, params), [193, 18522])
def test_fir_model_simulate_derivatives(self): m1 = Monomial(0, 1, [], [1], [], [1]) m2 = Monomial(0, 1, [], [1], [], [2]) m = [m1, m2] mdl = Polynomial(m) u = np.arange(1, 21).reshape((20, 1)) params = [1, -2] y0 = np.reshape([], [0, 1]) jac_params, jac_y0 = simulate_derivatives(mdl, y0, u[:-1], params) jac_params_numeric \ = simulate_numeric_derivatives(mdl, y0, u[:-1], params, deriv_y0=False) assert_array_almost_equal(jac_params, jac_params_numeric, decimal=6) assert_array_almost_equal(jac_y0, np.reshape([], (19, 1, 0, 1)))