def test_optuna_regression_model(self, params):
        """Check that optuna works as expected with a regression model"""

        multi_models, ocl = params

        # define objective function
        def objective(trial):
            # select input and encoder usage
            target_lags = trial.suggest_int("lags", 1, 12)
            include_year = trial.suggest_categorical("year", [False, True])

            # optionally also add the (scaled) year value as a past covariate
            if include_year:
                encoders = {
                    "datetime_attribute": {"past": ["year"]},
                    "transformer": Scaler(),
                }
                past_lags = trial.suggest_int("lags_past_covariates", 1, 12)
            else:
                encoders = None
                past_lags = None

            # build the model
            model = LinearRegressionModel(
                lags=target_lags,
                lags_past_covariates=past_lags,
                output_chunk_length=ocl,
                multi_models=multi_models,
                add_encoders=encoders,
            )
            model.fit(
                series=self.train,
            )

            # Evaluate how good it is on the validation set, using sMAPE
            preds = model.predict(series=self.train, n=self.val_length)
            smapes = smape(self.val, preds)
            smape_val = np.mean(smapes)

            return smape_val if smape_val != np.nan else float("inf")

        # optimize hyperparameters by minimizing the sMAPE on the validation set
        study = optuna.create_study(direction="minimize")
        study.optimize(objective, n_trials=3)
Check that optuna works as expected with a regression model
darts/tests/optional_deps/test_optuna.py