Free cookie consent management tool by TermsFeed Policy Generator

source: branches/LearningClassifierSystems/HeuristicLab.Optimization.Operators.LCS/3.3/GAssist/Ensemble/GAssistEnsembleModel.cs @ 9411

Last change on this file since 9411 was 9411, checked in by sforsten, 11 years ago

#1980:

  • added multiple discretizer to GAssist
  • created ensembles for LCS problems and edited CrossValidation to use them
File size: 3.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Optimization.Operators.LCS {
29  [StorableClass]
30  [Item("GAssistEnsembleModel", "")]
31  public class GAssistEnsembleModel : NamedItem, IGAssistEnsembleModel {
32
33    [Storable]
34    private List<IGAssistModel> models;
35    public IEnumerable<IGAssistModel> Models {
36      get { return new List<IGAssistModel>(models); }
37    }
38
39    [StorableConstructor]
40    protected GAssistEnsembleModel(bool deserializing) : base(deserializing) { }
41    protected GAssistEnsembleModel(GAssistEnsembleModel original, Cloner cloner)
42      : base(original, cloner) {
43      this.models = original.Models.Select(m => cloner.Clone(m)).ToList();
44    }
45    public GAssistEnsembleModel() : this(Enumerable.Empty<IGAssistModel>()) { }
46    public GAssistEnsembleModel(IEnumerable<IGAssistModel> models)
47      : base() {
48      this.name = ItemName;
49      this.description = ItemDescription;
50      this.models = new List<IGAssistModel>(models);
51    }
52    public override IDeepCloneable Clone(Cloner cloner) {
53      return new GAssistEnsembleModel(this, cloner);
54    }
55
56    #region IGAssistEnsembleModel Members
57    public void Add(IGAssistModel model) {
58      models.Add(model);
59    }
60    public void Remove(IGAssistModel model) {
61      models.Remove(model);
62    }
63
64    public IEnumerable<IEnumerable<IGAssistNiche>> GetEstimatedNicheVectors(IEnumerable<IGAssistInput> input) {
65      var estimatedValuesEnumerators = (from model in models
66                                        select model.Evaluate(input).GetEnumerator())
67                                       .ToList();
68
69      while (estimatedValuesEnumerators.All(en => en.MoveNext())) {
70        yield return from enumerator in estimatedValuesEnumerators
71                     select enumerator.Current;
72      }
73    }
74    #endregion
75
76    #region IGAssistModel Members
77
78    public IEnumerable<IGAssistNiche> Evaluate(IEnumerable<IGAssistInput> input) {
79      foreach (var estimatedValuesVector in GetEstimatedNicheVectors(input)) {
80        // return the class which is most often occuring
81        yield return
82          estimatedValuesVector
83          .GroupBy(x => x, new GAssistNicheComparer())
84          .OrderByDescending(g => g.Count())
85          .Select(g => g.Key)
86          .First();
87      }
88    }
89
90    public IGAssistSolution CreateGAssistSolution(IGAssistProblemData problemData) {
91      return new GAssistEnsembleSolution(models, new GAssistEnsembleProblemData((IGAssistProblemData)problemData.Clone()));
92    }
93    #endregion
94
95    public IEnumerable<IGAssistNiche> Evaluate(IEnumerable<IGAssistInput> input, out ItemSet<IItem> aliveRules, out double theoryLength) {
96      aliveRules = new ItemSet<IItem>();
97      theoryLength = double.NaN;
98      return Evaluate(input);
99    }
100  }
101}
Note: See TracBrowser for help on using the repository browser.