#region License Information /* HeuristicLab * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System.Collections.Generic; using System.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Problems.DataAnalysis { /// /// Represents regression solutions that contain an ensemble of multiple regression models /// [StorableClass] [Item("RegressionEnsembleModel", "A regression model that contains an ensemble of multiple regression models")] public class RegressionEnsembleModel : NamedItem, IRegressionEnsembleModel { private List models; public IEnumerable Models { get { return new List(models); } } [Storable(Name = "Models")] private IEnumerable StorableModels { get { return models; } set { models = value.ToList(); } } #region backwards compatiblity 3.3.5 [Storable(Name = "models", AllowOneWay = true)] private List OldStorableModels { set { models = value; } } #endregion [StorableConstructor] protected RegressionEnsembleModel(bool deserializing) : base(deserializing) { } protected RegressionEnsembleModel(RegressionEnsembleModel original, Cloner cloner) : base(original, cloner) { this.models = original.Models.Select(m => cloner.Clone(m)).ToList(); } public RegressionEnsembleModel() : this(Enumerable.Empty()) { } public RegressionEnsembleModel(IEnumerable models) : base() { this.name = ItemName; this.description = ItemDescription; this.models = new List(models); } public override IDeepCloneable Clone(Cloner cloner) { return new RegressionEnsembleModel(this, cloner); } #region IRegressionEnsembleModel Members public void Add(IRegressionModel model) { models.Add(model); } public void Remove(IRegressionModel model) { models.Remove(model); } public IEnumerable> GetEstimatedValueVectors(Dataset dataset, IEnumerable rows) { var estimatedValuesEnumerators = (from model in models select model.GetEstimatedValues(dataset, rows).GetEnumerator()) .ToList(); while (estimatedValuesEnumerators.All(en => en.MoveNext())) { yield return from enumerator in estimatedValuesEnumerators select enumerator.Current; } } #endregion #region IRegressionModel Members public IEnumerable GetEstimatedValues(Dataset dataset, IEnumerable rows) { foreach (var estimatedValuesVector in GetEstimatedValueVectors(dataset, rows)) { yield return estimatedValuesVector.Average(); } } public RegressionEnsembleSolution CreateRegressionSolution(IRegressionProblemData problemData) { return new RegressionEnsembleSolution(this.Models, new RegressionEnsembleProblemData(problemData)); } IRegressionSolution IRegressionModel.CreateRegressionSolution(IRegressionProblemData problemData) { return CreateRegressionSolution(problemData); } #endregion } }