Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/15/16 15:07:59 (8 years ago)
Author:
mkommend
Message:

#2590: Added model weights for ensembles.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/RegressionEnsembleModel.cs

    r13701 r13704  
    4646    }
    4747
     48    private List<double> modelWeights;
     49    public IEnumerable<double> ModelWeights {
     50      get { return modelWeights; }
     51    }
     52
     53    [Storable(Name = "ModelWeights")]
     54    private IEnumerable<double> StorableModelWeights {
     55      get { return modelWeights; }
     56      set { modelWeights = value.ToList(); }
     57    }
     58
    4859    [Storable]
    4960    private bool averageModelEstimates = true;
     
    5364        if (averageModelEstimates != value) {
    5465          averageModelEstimates = value;
    55           OnAverageModelEstimatesChanged();
     66          OnChanged();
    5667        }
    5768      }
     
    6475    }
    6576    #endregion
     77
     78    [StorableHook(HookType.AfterDeserialization)]
     79    private void AfterDeserialization() {
     80      // BackwardsCompatibility 3.3.14
     81      #region Backwards compatible code, remove with 3.4
     82      if (modelWeights == null || !modelWeights.Any())
     83        modelWeights = new List<double>(models.Select(m => 1.0));
     84      #endregion
     85    }
    6686
    6787    [StorableConstructor]
     
    7090      : base(original, cloner) {
    7191      this.models = original.Models.Select(cloner.Clone).ToList();
     92      this.modelWeights = new List<double>(original.ModelWeights);
    7293      this.averageModelEstimates = original.averageModelEstimates;
    7394    }
     
    7798
    7899    public RegressionEnsembleModel() : this(Enumerable.Empty<IRegressionModel>()) { }
    79     public RegressionEnsembleModel(IEnumerable<IRegressionModel> models)
     100    public RegressionEnsembleModel(IEnumerable<IRegressionModel> models) : this(models, models.Select(m => 1.0)) { }
     101    public RegressionEnsembleModel(IEnumerable<IRegressionModel> models, IEnumerable<double> modelWeights)
    80102      : base() {
    81103      this.name = ItemName;
    82104      this.description = ItemDescription;
     105
     106
    83107      this.models = new List<IRegressionModel>(models);
     108      this.modelWeights = new List<double>(modelWeights);
    84109    }
    85110
    86111    #region IRegressionEnsembleModel Members
    87112    public void Add(IRegressionModel model) {
     113      Add(model, 1.0);
     114    }
     115    public void Add(IRegressionModel model, double weight) {
    88116      models.Add(model);
     117      modelWeights.Add(weight);
     118      OnChanged();
     119    }
     120
     121    public void AddRange(IEnumerable<IRegressionModel> models) {
     122      AddRange(models, models.Select(m => 1.0));
     123    }
     124    public void AddRange(IEnumerable<IRegressionModel> models, IEnumerable<double> weights) {
     125      this.models.AddRange(models);
     126      modelWeights.AddRange(weights);
     127      OnChanged();
    89128    }
    90129
    91130    public void Remove(IRegressionModel model) {
    92       models.Remove(model);
     131      var index = models.IndexOf(model);
     132      models.RemoveAt(index);
     133      modelWeights.RemoveAt(index);
     134      OnChanged();
     135    }
     136    public void RemoveRange(IEnumerable<IRegressionModel> models) {
     137      foreach (var model in models) {
     138        var index = this.models.IndexOf(model);
     139        this.models.RemoveAt(index);
     140        modelWeights.RemoveAt(index);
     141      }
     142      OnChanged();
     143    }
     144
     145    public double GetModelWeight(IRegressionModel model) {
     146      var index = models.IndexOf(model);
     147      return modelWeights[index];
     148    }
     149    public void SetModelWeight(IRegressionModel model, double weight) {
     150      var index = models.IndexOf(model);
     151      modelWeights[index] = weight;
     152      OnChanged();
    93153    }
    94154
     
    127187    }
    128188
    129     public event EventHandler AverageModelEstimatesChanged;
    130     private void OnAverageModelEstimatesChanged() {
    131       var handler = AverageModelEstimatesChanged;
     189    public event EventHandler Changed;
     190    private void OnChanged() {
     191      var handler = Changed;
    132192      if (handler != null)
    133193        handler(this, EventArgs.Empty);
Note: See TracChangeset for help on using the changeset viewer.