Esempio n. 1
0
    def test_predict(self):
        """Test _core.InterpolatedInferredDiscreteROM.predict()."""
        model = roi.InterpolatedInferredDiscreteROM("cAH")

        # Get data for fitting.
        n, m, k, r = 50, 10, 100, 5
        X1, _, U1 = _get_data(n, k, m)
        X2, U2 = X1+1, U1+1
        Xs = [X1, X2]
        Us = [U1, U2]
        ps = [1, 2]
        Vr = la.svd(np.hstack(Xs))[0][:,:r]

        # Parameters for predicting.
        x0 = np.random.random(n)
        niters = 5
        U = np.ones((m,niters))

        # Fit / predict with no inputs.
        model.fit(Vr, ps, Xs)
        model.predict(1, x0, niters)
        model.predict(1.5, x0, niters)

        # Fit / predict with inputs.
        model.modelform = "cAHB"
        model.fit(Vr, ps, Xs, Us)
        model.predict(1, x0, niters, U)
        model.predict(1.5, x0, niters, U)
Esempio n. 2
0
    def test_fit(self):
        """Test
        _core._interpolate._inferred.InterpolatedInferredDiscreteROM.fit().
        """
        model = roi.InterpolatedInferredDiscreteROM("cAH")

        # Get data for fitting.
        n, m, k, r = 50, 10, 100, 5
        X1, _, U1 = _get_data(n, k, m)
        X2, U2 = X1 + 1, U1 + 1
        Xs = [X1, X2]
        Us = [U1, U2]
        ps = [1, 2]
        Vr = la.svd(np.hstack(Xs))[0][:, :r]

        # Try with non-scalar parameters.
        with pytest.raises(ValueError) as ex:
            model.fit(Vr, [np.array([1, 1]), np.array([2, 2])], Xs)
        assert ex.value.args[0] == "only scalar parameter values are supported"

        # Try with bad number of Xs.
        with pytest.raises(ValueError) as ex:
            model.fit(Vr, ps, [X1, X2, X2 + 1])
        assert ex.value.args[0] == \
            "num parameter samples != num state snapshot sets (2 != 3)"

        # Try with varying input sizes.
        model.modelform = "cAHB"
        with pytest.raises(ValueError) as ex:
            model.fit(Vr, ps, Xs, [U1, U2[:-1]])
        assert ex.value.args[0] == "control inputs not aligned"

        # Fit correctly with no inputs.
        model.modelform = "cAH"
        model.fit(Vr, ps, Xs)
        for attr in ["models_", "fs_"
                     ] + [s[:-1] + "s_" for s in _LSTSQ_REPORTS]:
            assert hasattr(model, attr)
            assert len(getattr(model, attr)) == len(model.models_)

        # Fit correctly with inputs.
        model.modelform = "cAHGB"
        model.fit(Vr, ps, Xs, Us)

        assert len(model) == len(ps)

        # Test again with Vr = None and projected inputs.
        Xs_ = [Vr.T @ X for X in Xs]
        model.fit(None, ps, Xs_, Us)
        assert len(model) == len(ps)
        assert model.Vr is None
        assert model.n is None
Esempio n. 3
0
    def test_fit(self, n=20, m=4, k=500, r=3):
        """Test
        _core._interpolate._inferred.InterpolatedInferredDiscreteROM.fit().
        """
        model = roi.InterpolatedInferredDiscreteROM("cAH")

        # Get data for fitting.
        X1, _, U1 = _get_data(n, k, m)
        X2, U2 = X1 + 1, U1 + 1
        Xs = [X1, X2]
        Us = [U1, U2]
        ps = [1, 2]
        Vr = la.svd(np.hstack(Xs))[0][:, :r]

        # Try with non-scalar parameters.
        # with pytest.raises(ValueError) as ex:
        #     model.fit(Vr, [np.array([1,1]), np.array([2,2])], Xs)
        # assert ex.value.args[0] == "only scalar parameter values are supported"

        # Try with bad number of Xs.
        with pytest.raises(ValueError) as ex:
            model.fit(Vr, ps, [X1, X2, X2 + 1])
        assert ex.value.args[0] == "num parameter samples != num state " \
                                   "snapshot training sets (2 != 3)"

        # Fit correctly with no inputs.
        model.modelform = "cAH"
        model.fit(Vr, ps, Xs)
        for attr in ["models_", "fs_"]:
            assert hasattr(model, attr)
            assert len(getattr(model, attr)) == len(model.models_)

        # Fit correctly with inputs.
        model.modelform = "cAHGB"
        model.fit(Vr, ps, Xs, Us)

        assert len(model) == len(ps)

        # Test again with Vr = None and projected inputs.
        Xs_ = [Vr.T @ X for X in Xs]
        model.fit(None, ps, Xs_, Us)
        assert len(model) == len(ps)
        assert model.Vr is None
        assert model.n is None
    def test_fit(self):
        """Test _core.InterpolatedInferredDiscreteROM.fit()."""
        model = roi.InterpolatedInferredDiscreteROM("cAH")

        # Get data for fitting.
        n, m, k, r = 50, 10, 100, 5
        X1, _, U1 = _get_data(n, k, m)
        X2, U2 = X1+1, U1+1
        Xs = [X1, X2]
        Us = [U1, U2]
        ps = [1, 2]
        Vr = la.svd(np.hstack(Xs))[0][:,:r]

        # Try with non-scalar parameters.
        with pytest.raises(ValueError) as ex:
            model.fit(Vr, [np.array([1,1]), np.array([2,2])], Xs)
        assert ex.value.args[0] == "only scalar parameter values are supported"

        # Try with bad number of Xs.
        with pytest.raises(ValueError) as ex:
            model.fit(Vr, ps, [X1, X2, X2+1])
        assert ex.value.args[0] == \
            "num parameter samples != num state snapshot sets (2 != 3)"

        # Try with varying input sizes.
        model.modelform = "cAHB"
        with pytest.raises(ValueError) as ex:
            model.fit(Vr, ps, Xs, [U1, U2[:-1]])
        assert ex.value.args[0] == \
            "shape of 'U' inconsistent across samples"

        # Fit correctly with no inputs.
        model.modelform = "cAH"
        model.fit(Vr, ps, Xs)
        for attr in ["models_", "dataconds_", "residuals_", "fs_"]:
            assert hasattr(model, attr)
            assert len(getattr(model, attr)) == len(model.models_)

        # Fit correctly with inputs.
        model.modelform = "cAHB"
        model.fit(Vr, ps, Xs, Us)

        assert len(model) == len(ps)