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_singleterm_mimo_monomial_derivatives(self): m = Monomial(0, 1, [], [2], [], [3], [], [3]) y = [] u = [[1, 2, 3, 7], [4, 5, 6, 8]] dy, du = m.derivatives(y, u, 1, True, True) assert_array_almost_equal(du, [[0, 0, 0, 0], [0, 0, 0, 3*8**2]])
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_singleterm_siso_monomial_derivatives(self): m = Monomial(1, 0, [2], [], [2], []) y = [[1.12], [2.12]] u = [] dy, du = m.derivatives(y, u, 1, True, True) assert_array_almost_equal(dy, [[0], [2.12*2]])
def test_exception_raised_tipeerror(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], [1], [1, 2], [3]) m = [m1, m2, m3, 4] assert_raises(TypeError, Polynomial, m)
def test_singleterm_mimo_numeric_derivatives(self): m = Monomial(0, 1, [], [2], [], [4], [], [3]) y = [] u = [[1, 2.12, 3, 7], [4, 5, 6, 8]] du = m.derivatives(y, u, 1, deriv_y=False) du_numeric = monomial_numeric_derivatives(m, y, u, 1, deriv_y=False) assert_array_almost_equal(du, du_numeric)
def test_singleterm_siso_numeric_derivatives(self): m = Monomial(1, 0, [3], [], [4], []) y = [[1.12], [123.123], [1.2]] u = [] dy = m.derivatives(y, u, 1, deriv_u=False) dy_numeric = monomial_numeric_derivatives(m, y, u, 1, deriv_u=False) assert_array_almost_equal(dy, dy_numeric)
def test_siso_numeric_derivatives(self): m = Monomial(2, 3, [1, 2], [4, 5, 6]) y = [[1.12], [2.12]] u = [[21.1], [2.1], [3.4]] dy, du = m.derivatives(y, u, 4) dy_numeric, du_numeric = monomial_numeric_derivatives(m, y, u, 4) assert_array_almost_equal(dy, dy_numeric) assert_array_almost_equal(du, du_numeric)
def test_exception_raised_unknown_output(self): m1 = Monomial(3, 2, [1, 2, 4], [2, 3], [1, 2, 2], [1, 1], [0, 1, 1], [0, 1]) m2 = Monomial(1, 0, [1], [], [1], []) m3 = Monomial(2, 1, [2, 3], [2], [1, 2], [3], [1, 1], [1]) m = [m1, m2, m3] assert_raises(ValueError, Polynomial, m)
def test_mimo_numeric_derivatives(self): m = Monomial(3, 2, [1, 2, 3], [4, 5], [1, 2, 1], [2, 2], [0, 2, 3], [0, 1]) y = [[1.12, 3, 4, 5], [2.12, 7, 8, 9], [3, 10, 11, 12]] u = [[2, 1], [2, 3.2]] dy, du = m.derivatives(y, u, 4) dy_numeric, du_numeric = monomial_numeric_derivatives(m, y, u, 4) assert_array_almost_equal(dy, dy_numeric) assert_array_almost_equal(du, du_numeric)
def test_siso_monomial_derivatives(self): m = Monomial(3, 2, [1, 2, 3], [4, 5], [1, 1, 1], [3, 3]) y = [[1.12], [2.12], [3]] u = [[2], [2]] dy, du = m.derivatives(y, u, 4) assert_array_almost_equal(dy, [[2.12*3*(2**3)*(2**3)], [1.12*3*(2**3)*(2**3)], [1.12*2.12*(2**3)*(2**3)]]) assert_array_almost_equal(du, [[1.12*2.12*3*(3*2**2)*(2**3)], [1.12*2.12*3*(2**3)*(3*2**2)]])
def test_siso_monomial_evaluation(self): m = Monomial(3, 2, [1, 2, 3], [4, 5], [1, 1, 1], [2, 2]) y = [[1.12], [2.12], [3]] u = [[2], [2]] assert_array_almost_equal(m(y, u, 4), 1.12*2.12*3*(2**2)*(2**2)) m = Monomial(3, 2, [1, 2, 3], [4, 5]) y = [[1.12], [2.12], [3]] u = [[2], [2]] assert_array_almost_equal(m(y, u, 4), 1.12*2.12*3*2*2)
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_mimo_monomial_derivatives(self): m = Monomial(3, 2, [1, 2, 3], [4, 5], [1, 1, 1], [2, 2], [1, 2, 3], [0, 1]) y = [[1.12, 3, 4, 5], [2.12, 7, 8, 9], [3, 10, 11, 12]] u = [[2, 1], [2, 3.2]] dy, du = m.derivatives(y, u, 4) assert_array_almost_equal(dy, [[0, 8*12*(2**2)*(3.2**2), 0, 0], [0, 0, 3*12*(2**2)*(3.2**2), 0], [0, 0, 0, 3*8*(2**2)*(3.2**2)]]) assert_array_almost_equal(du, [[3*8*12*(2*2)*(3.2**2), 0], [0, 3*8*12*(2**2)*(3.2*2)]])
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_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_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_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_mimo_monomial_evaluation(self): m = Monomial(3, 2, [1, 2, 3], [4, 5], [1, 1, 1], [2, 2], [1, 2, 3], [0, 1]) y = [[1.12, 3, 4, 5], [2.12, 7, 8, 9], [3, 10, 11, 12]] u = [[2, 1], [2, 3.2]] assert_array_almost_equal(m(y, u, 4), 3*8*12*(2**2)*(3.2**2))
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_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(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_singleterm_mimo_monomial_evaluation(self): m = Monomial(0, 1, [], [2], [], [2], [], [3]) y = [] u = [[1, 2, 3, 7], [4, 5, 6, 8]] assert_array_almost_equal(m(y, u), 8**2)
def test_siso_atribute_setting(self): m = Monomial(3, 2, [1, 2, 4], [2, 3], [1, 2, 2], [1, 1]) assert_equal(m.N, 4) assert_equal(m.M, 3) assert_equal(m.minlag_u, 2) assert_equal(m.Ny, 1) assert_equal(m.Nu, 1) assert_equal(m.Nl, 7)
def test_singleterm_siso_atribute_setting(self): m = Monomial(1, 0, [1], [], [1], []) assert_equal(m.N, 1) assert_equal(m.M, 0) assert_equal(m.minlag_u, np.Inf) assert_equal(m.Ny, 1) assert_equal(m.Nu, 0) assert_equal(m.Nl, 1)
def test_singleterm_mimo_atribute_setting(self): m = Monomial(0, 1, [], [2], [], [2], [], [3]) assert_equal(m.N, 0) assert_equal(m.M, 2) assert_equal(m.minlag_u, 2) assert_equal(m.Ny, 0) assert_equal(m.Nu, 4) assert_equal(m.Nl, 2)
def test_fir_numeric_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) 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_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)))