def test_siso_estimate_parameters_multiple_shoot(self): mdl = Linear(N=3, M=3) y = [[0], [4], [13], [24.8], [30.2], [27.96], [11.76], [-16.568], [-50.384], [-64.7776], [-57.784], [-26.03872], [22.23296], [76.507136], [98.513024], [86.7539072], [96.2449152], [154.54439424], [244.89924352], [290.512711168]] u = [[1], [1], [1], [1], [1], [-2], [-2], [-2], [-2], [-2], [3], [3], [3], [3], [3], [10], [10], [10], [10], [10]] true_params = [1, -0.8, 0.2, 4, 5, 6] initial_guess = [0, 0, 0, 0, 0, 0] error = SimulationError(mdl, y, u, maxlength=5) params, info = error.lsq_estimate_parameters(initial_guess) multiple_shoots = info["multiple_shoots"] initial_conditions = info["initial_conditions"] assert_array_almost_equal(params, true_params, decimal=3) assert_array_almost_equal(multiple_shoots, [(0, 8), (5, 13), (10, 18), (15, 20)], decimal=3) assert_array_almost_equal( initial_conditions, [[[0], [4], [13]], [[27.96], [11.76], [-16.568]], [[-57.784], [-26.03872], [22.23296]], [[86.7539072], [96.2449152], [154.54439424]]], decimal=3)
def test_mimo_estimate_parameters_single_shoot(self): mdl = Linear(N=3, M=3, Ny=2, Nu=2) y = [[0, 0], [4, 8], [13, 26], [24.8, 49.6], [30.2, 60.4], [27.96, 63.92], [11.76, 61.52], [-16.568, 55.464], [-50.384, 34.032], [-64.7776, 8.9648], [-57.784, -10.1680], [-26.03872, -5.53344], [22.23296, 34.39392], [76.507136, 112.787072], [98.513024, 180.165248], [86.7539072, 216.8143744], [96.2449152, 243.2395904], [154.54439424, 260.82114048], [244.89924352, 271.59234304], [290.512711168, 231.583348736]] u = [[1, 1], [1, 1], [1, 1], [1, 1], [1, 3], [-2, 3], [-2, 3], [-2, 3], [-2, 1], [-2, 1], [3, 1], [3, 1], [3, 5], [3, 5], [3, 5], [10, 5], [10, -2], [10, -2], [10, -2], [10, -2]] true_params = [ 1, 0, -0.8, 0, 0.2, 0, 0, 1, 0, -0.8, 0, 0.2, 4, 0, 5, 0, 6, 0, 4, 4, 5, 5, 6, 6 ] initial_guess = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] error = SimulationError(mdl, y, u, maxlength=20) params, info = error.lsq_estimate_parameters(initial_guess, use_sparse=False) multiple_shoots = info["multiple_shoots"] initial_conditions = info["initial_conditions"] assert_array_almost_equal(params, true_params) assert_array_almost_equal(multiple_shoots, [(0, 20)], decimal=3) assert_array_almost_equal(initial_conditions, [[[0, 0], [4, 8], [13, 26]]], decimal=3)
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_mimo_simulation_error_one_shoot_derivatives(self): mdl = Linear(N=3, M=3, Ny=2, Nu=2) y = [[0, 0], [4, 8], [13, 26], [24.8, 49.6], [30.2, 60.4], [27.96, 63.92], [11.76, 61.52], [-16.568, 55.464], [-50.384, 34.032], [-64.7776, 8.9648], [-57.784, -10.1680], [-26.03872, -5.53344], [22.23296, 34.39392], [76.507136, 112.787072], [98.513024, 180.165248], [86.7539072, 216.8143744], [96.2449152, 243.2395904], [154.54439424, 260.82114048], [244.89924352, 271.59234304], [290.512711168, 231.583348736]] u = [[1 + 0.5, 1 + 0.5], [1 + 0.5, 1 + 0.5], [1 + 0.5, 1 + 0.5], [1 + 0.5, 1 + 0.5], [1 + 0.5, 3 + 0.5], [-2 + 0.3, 3 + 0.3], [-2 + 0.3, 3 + 0.3], [-2 + 0.3, 3 + 0.3], [-2 + 0.3, 1 + 0.3], [-2 + 0.3, 1 + 0.3], [3 + 0.3, 1 + 0.3], [3 + 0.3, 1 + 0.3], [3 + 0.3, 5 + 0.3], [3 + 0.3, 5 + 0.3], [3 + 0.3, 5 + 0.3], [10 + 1, 5 + 1], [10 + 1, -2 + 1], [10 + 1, -2 + 1], [10 + 1, -2 + 1], [10 + 1, -2 + 1]] params = [ 1, 0, -0.8, 0, 0.2, 0, 0, 1, 0, -0.8, 0, 0.2, 4, 0, 5, 0, 6, 0, 4, 4, 5, 5, 6, 6 ] extended_params = params + [0, 0, 4, 8, 13, 26] error = SimulationError(mdl, y, u) dparams = error.derivatives(extended_params) dparams_numeric \ = error_numeric_derivatives(error, extended_params) assert_array_almost_equal(dparams, dparams_numeric)
def test_mimo_simulation_error_one_shoot(self): mdl = Linear(N=3, M=3, Ny=2, Nu=2) y = [[0, 0], [4, 8], [13, 26], [24.8 - 1.23, 49.6 - 2], [30.2 - 1.23, 60.4 - 2], [27.96 - 1.23, 63.92 - 2], [11.76 - 1.23, 61.52 - 2], [-16.568 - 1.23, 55.464 - 2], [-50.384 - 1.23, 34.032 - 2], [-64.7776 - 1.23, 8.9648 - 2], [-57.784 - 1.23, -10.1680 - 2], [-26.03872 - 1.23, -5.53344 - 2], [22.23296 - 12, 34.39392 - 2], [76.507136 - 12, 112.787072 - 2.45], [98.513024 - 12, 180.165248 - 2.45], [86.7539072 - 12, 216.8143744 - 2.45], [96.2449152 - 12, 243.2395904 - 2.45], [154.54439424 - 12, 260.82114048 - 2.45], [244.89924352 - 12, 271.59234304 - 2.45], [290.512711168 - 12, 231.583348736 - 2.45]] u = [[1, 1], [1, 1], [1, 1], [1, 1], [1, 3], [-2, 3], [-2, 3], [-2, 3], [-2, 1], [-2, 1], [3, 1], [3, 1], [3, 5], [3, 5], [3, 5], [10, 5], [10, -2], [10, -2], [10, -2], [10, -2]] params = [ 1, 0, -0.8, 0, 0.2, 0, 0, 1, 0, -0.8, 0, 0.2, 4, 0, 5, 0, 6, 0, 4, 4, 5, 5, 6, 6 ] expected_error = [ 1.23, 2, 1.23, 2, 1.23, 2, 1.23, 2, 1.23, 2, 1.23, 2, 1.23, 2, 1.23, 2, 1.23, 2, 12, 2, 12, 2.45, 12, 2.45, 12, 2.45, 12, 2.45, 12, 2.45, 12, 2.45, 12, 2.45 ] error = SimulationError(mdl, y, u)(params + [0, 0, 4, 8, 13, 26]) assert_array_almost_equal(error, expected_error)
def test_siso_simulation_error_one_shoot_derivatives(self): mdl = Linear(N=3, M=3) y = [[0], [4], [13], [24.8], [30.2], [27.96], [11.76], [-16.568], [-50.384], [-64.7776], [-57.784], [-26.03872], [22.23296], [76.507136], [98.513024], [86.7539072], [96.2449152], [154.54439424], [244.89924352], [290.512711168]] u = [[1 + 0.5], [1 + 0.5], [1 + 0.5], [1 + 0.5], [1 + 0.5], [-2 + 0.3], [-2 + 0.3], [-2 + 0.3], [-2 + 0.3], [-2 + 0.3], [3 + 0.3], [3 + 0.3], [3 + 0.3], [3 + 0.3], [3 + 0.3], [10 + 1], [10 + 1], [10 + 1], [10 + 1], [10 + 1]] params = [1, -0.8, 0.2, 4, 5, 6] extended_params = params + [0, 4, 13] error = SimulationError(mdl, y, u) dparams = error.derivatives(extended_params) dparams_numeric \ = error_numeric_derivatives(error, extended_params) assert_array_almost_equal(dparams, dparams_numeric)
def test_siso_simulation_error_multiple_shoot(self): mdl = Linear(N=3, M=3) y = [[0], [4], [13], [24.8 - 1.34], [30.2 - 1.34], [27.96 - 1.34], [11.76 - 1.34], [-16.568 - 1.34], [-50.384 - 1.34], [-64.7776 - 1.34], [-57.784 - 1.34], [-26.03872 - 5.34], [22.23296 - 5.34], [76.507136 - 5.34], [98.513024 - 5.34], [86.7539072 - 5.34], [96.2449152 - 5.34], [154.54439424 - 5.34], [244.89924352 - 5.34], [290.512711168 - 5.34]] u = [[1], [1], [1], [1], [1], [-2], [-2], [-2], [-2], [-2], [3], [3], [3], [3], [3], [10], [10], [10], [10], [10]] params = [1, -0.8, 0.2, 4, 5, 6] expected_error = [ 1.34, 1.34, 1.34, 1.34, 1.34, 0, # overlap error 0, # overlap error 0, # overlap error 1.34, 1.34, 1.34, 5.34, 5.34, 0, # overlap error 0, # overlap error 0, # overlap error 5.34, 5.34, 5.34, 5.34, 5.34, 0, # overlap error 0, # overlap error 0, # overlap error 5.34, 5.34 ] e = SimulationError(mdl, y, u, 5) error = e(params + [0, 4, 13] + [27.96, 11.76, -16.568] + [-57.784, -26.03872, 22.23296] + [86.7539072, 96.2449152, 154.54439424]) assert_array_almost_equal(error, expected_error)