def test_mimo_simulation_derivative(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]] 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 ] jac_params, jac_y0 = simulate_derivatives(mdl, y[:3], u[:-1], params) jac_params_numeric, jac_y0_numeric \ = simulate_numeric_derivatives(mdl, y[:3], u[:-1], params) assert_array_almost_equal(jac_params, jac_params_numeric, decimal=6) assert_array_almost_equal(jac_y0, jac_y0_numeric, decimal=6)
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_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_check_parameters_size(self): mdl = Linear(N=2, M=2, Ny=2, Nu=2) y0 = [[1, 2], [2, 3]] u = [[2, 3], [3, 4], [5, 6]] params = [1, 2, 0, 0, 0, 0, 5, 6, 3, 4, 0, 0] assert_raises(ValueError, simulate_derivatives, mdl, y0, u, params)
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_prediction_error(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 ] expected_error = [ 7.5, 15, 7.5, 15, 7.5, 15, 6.7, 13.4, 5.7, 11.4, 4.5, 9, 4.5, 9, 4.5, 9, 4.5, 9, 4.5, 9, 4.5, 9, 4.5, 9, 4.5, 9, 7.3, 14.6, 10.8, 21.6, 15, 30, 15, 30 ] error = PredictionError(mdl, y, u)(params) assert_array_almost_equal(error, expected_error)
def test_mimo_estimate_parameters(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 = PredictionError(mdl, y, u) error = PredictionError(mdl, y, u) params, info = error.lsq_estimate_parameters(initial_guess) assert_array_almost_equal(params, true_params)
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_check_wrong_u_size(self): mdl = Linear(N=2, M=2, Ny=2, Nu=2) y0 = [[1, 2], [2, 3]] u = [[2, 3, 3], [3, 4, 2], [5, 6, 4]] params = [1, 2, 0, 0, 0, 0, 5, 6, 3, 4, 0, 0, 0, 0, 1, 2] assert_raises(ValueError, simulate, mdl, y0, u, params)
def test_check_too_small_Nd(self): mdl = Linear(N=2, M=2, Ny=2, Nu=2) y = [[1, 2], [2, 3]] u = [[2, 3]] params = [1, 2, 0, 0, 0, 0, 5, 6, 3, 4, 0, 0, 0, 0, 1, 2] assert_raises(ValueError, simulate, mdl, y, u, params)
def test_siso_ynext_derivatives(self): mdl = Linear(N=2, M=2) dy, du, dparams = mdl.derivatives(y=[1, 2], u=[2, 3], params=[10, 20, 30, 40]) assert_array_almost_equal(dy, [[[10], [20]]]) assert_array_almost_equal(du, [[[30], [40]]]) assert_array_almost_equal(dparams, [[1, 2, 2, 3]])
def test_ynext_tito(self): mdl = Linear(N=2, M=2, Ny=2, Nu=2) y = [[1, 2], [2, 3]] u = [[2, 3], [3, 4]] ynext = mdl(y=y, u=u, params=[1, 2, 0, 0, 0, 0, 5, 6, 3, 4, 0, 0, 0, 0, 7, 8]) assert_array_almost_equal(ynext, [23, 81])
def test_check_parameters_size(self): mdl = Linear(N=2, M=2, Ny=2, Nu=2) y = [[1, 2], [2, 3], [3, 4]] u = [[2, 3], [3, 4], [5, 6]] params = [1, 2, 0, 0, 0, 0, 5, 6, 3, 4, 0, 0] assert_raises(ValueError, predict, mdl, y, u, params)
def test_check_diferent_Nd(self): mdl = Linear(N=2, M=2, Ny=2, Nu=2) y = [[1, 2], [2, 3], [3, 4]] u = [[2, 3], [3, 3], [4, 2], [5, 6]] params = [1, 2, 0, 0, 0, 0, 5, 6, 3, 4, 0, 0, 0, 0, 1, 2] assert_raises(ValueError, predict, mdl, y, u, params)
def test_check_wrong_u_size(self): mdl = Linear(N=2, M=2, Ny=2, Nu=2) y = [[1, 2], [2, 3], [3, 4]] u = [[2, 3, 3], [3, 4, 2], [5, 6, 4]] params = [1, 2, 0, 0, 0, 0, 5, 6, 3, 4, 0, 0, 0, 0, 1, 2] assert_raises(ValueError, predict_derivatives, mdl, y, u, params)
def test_ar_model_derivatives(self): mdl = Linear(N=3, M=0, Ny=1, Nu=0) y = [1, 2, 3] u = [] params = [1, 2, 3] dy, du, dparams = mdl.derivatives(y, u, params) assert_array_almost_equal(dy, [[[1], [2], [3]]]) assert_array_almost_equal(du, np.reshape([], (1, 0, 0))) assert_array_almost_equal(dparams, [[1, 2, 3]])
def test_fir_model_derivatives(self): mdl = Linear(N=0, M=2, Ny=1, Nu=1) y = [] u = [2, 1] params = [1, 2] dy, du, dparams = mdl.derivatives(y, u, params) assert_array_almost_equal(du, [[[1], [2]]]) assert_array_almost_equal(dy, np.reshape([], (1, 0, 1))) assert_array_almost_equal(dparams, [[2, 1]])
def test_tito_ynext_derivatives(self): mdl = Linear(N=2, M=2, Ny=2, Nu=2) y = [[1, 2], [2, 3]] u = [[2, 3], [3, 4]] dy, du, dparams = mdl.derivatives( y=y, u=u, params=[10, 20, 0, 0, 0, 0, 50, 60, 30, 40, 0, 0, 0, 0, 70, 80]) assert_array_almost_equal(dy, [[[10, 20], [0, 0]], [[0, 0], [50, 60]]]) assert_array_almost_equal(du, [[[30, 40], [0, 0]], [[0, 0], [70, 80]]]) assert_array_almost_equal( dparams, [[1, 2, 2, 3, 0, 0, 0, 0, 2, 3, 3, 4, 0, 0, 0, 0], [0, 0, 0, 0, 1, 2, 2, 3, 0, 0, 0, 0, 2, 3, 3, 4]])
def test_siso_simulation(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]] params = [1, -0.8, 0.2, 4, 5, 6] ys = simulate(mdl, y[:3], u[:-1], params) assert_array_almost_equal(ys, y[3:])
def test_fir_model_numerical_derivatives(self): mdl = Linear(N=0, M=2, Ny=1, Nu=1) y = [] u = [2, 1] params = [1, 2] dy, du, dparams = mdl.derivatives(y, u, params) dy_numeric, du_numeric, \ dparams_numeric = mdl._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_predict_numerical_derivativesative(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]] params = [1, -0.8, 0.2, 4, 5, 6] 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_siso_estimate_parameters(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 = PredictionError(mdl, y, u) params, info = error.lsq_estimate_parameters(initial_guess) assert_array_almost_equal(params, true_params)
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)
def test_siso_prediction_error_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] error = PredictionError(mdl, y, u) jac = error.derivatives(params) jac_numeric \ = error_numeric_derivatives(error, params) assert_array_almost_equal(jac, jac_numeric)
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_prediction_error(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] expected_error = [ 7.5, 7.5, 7.5, 6.7, 5.7, 4.5, 4.5, 4.5, 4.5, 4.5, 4.5, 4.5, 4.5, 7.3, 10.8, 15, 15 ] e = PredictionError(mdl, y, u) error = e(params) assert_array_almost_equal(error, expected_error)
def test_mimo_numerical_approx_twoinputs_threeoutputs(self): # Define model N = 3 M = 2 Ny = 2 Nu = 3 mdl = Linear(N, M, Ny, Nu) Nparams = mdl.Nparams # Define ynext and derivatives y = np.array([[12, 2.41], [3, 1.23], [2.41, 5]]) u = np.array([[4.5, 6.4, 10], [2.23, 2.41, 521]]) params = np.arange(Nparams) dy, du, dparams = mdl.derivatives(y, u, params) # Compute the same derivatives numerically dy_numeric, du_numeric, \ dparams_numeric = mdl._numeric_derivatives(y, u, params) # Check assert_array_almost_equal(dy, dy_numeric) assert_array_almost_equal(du, du_numeric) assert_array_almost_equal(dparams, dparams_numeric)
def test_siso_numerical_approx(self): # Define model N = 2 M = 2 Ny = 1 Nu = 1 mdl = Linear(N, M) Nparams = mdl.Nparams # Define ynext and derivatives y = np.array([1.23, 2.41]) u = np.array([2.54, 23]) params = [1, 5.46, 3.23, 4] dy, du, dparams = mdl.derivatives(y, u, params) # Compute the same derivatives numerically dy_numeric, du_numeric, \ dparams_numeric = mdl._numeric_derivatives(y, u, params) # Check assert_array_almost_equal(dy, dy_numeric) assert_array_almost_equal(du, du_numeric) assert_array_almost_equal(dparams, dparams_numeric)
def test_check_diferent_Nd(self): mdl = Linear(N=2, M=2, Ny=2, Nu=2) y = [[1, 2], [2, 3], [3, 4]] u = [[2, 3], [3, 3], [4, 2], [5, 6]] assert_raises(ValueError, PredictionError, mdl, y, u)
def test_check_wrong_u_size(self): mdl = Linear(N=2, M=2, Ny=2, Nu=2) y = [[1, 2], [2, 3], [3, 4]] u = [[2, 3, 3], [3, 4, 2], [5, 6, 4]] assert_raises(ValueError, PredictionError, mdl, y, u)