Free cookie consent management tool by TermsFeed Policy Generator

source: branches/LearningClassifierSystems/HeuristicLab.Encodings.ConditionActionEncoding/3.3/Ensemble/ConditionActionEnsembleModel.cs @ 17189

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

#1980:

  • added multiple discretizer to GAssist
  • created ensembles for LCS problems and edited CrossValidation to use them
File size: 4.1 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.Encodings.ConditionActionEncoding {
29  [StorableClass]
30  [Item("ConditionActionEnsembleModel", "")]
31  public class ConditionActionEnsembleModel : NamedItem, IConditionActionEnsembleModel {
32
33    [Storable]
34    private List<IConditionActionModel> models;
35    public IEnumerable<IConditionActionModel> Models {
36      get { return new List<IConditionActionModel>(models); }
37    }
38
39    [Storable]
40    private IClassifierComparer comparer;
41    public IClassifierComparer ClassifierComparer {
42      get { return comparer; }
43      set { comparer = value; }
44    }
45
46    [StorableConstructor]
47    protected ConditionActionEnsembleModel(bool deserializing) : base(deserializing) { }
48    protected ConditionActionEnsembleModel(ConditionActionEnsembleModel original, Cloner cloner)
49      : base(original, cloner) {
50      this.models = original.Models.Select(m => cloner.Clone(m)).ToList();
51    }
52    public ConditionActionEnsembleModel() : this(Enumerable.Empty<IConditionActionModel>()) { }
53    public ConditionActionEnsembleModel(IEnumerable<IConditionActionModel> models)
54      : base() {
55      this.name = ItemName;
56      this.description = ItemDescription;
57      this.models = new List<IConditionActionModel>(models);
58    }
59    public override IDeepCloneable Clone(Cloner cloner) {
60      return new ConditionActionEnsembleModel(this, cloner);
61    }
62
63    public void Add(IConditionActionModel model) {
64      models.Add(model);
65    }
66    public void Remove(IConditionActionModel model) {
67      models.Remove(model);
68    }
69
70    public IEnumerable<IEnumerable<IAction>> GetEstimatedActionVectors(IEnumerable<IInput> input) {
71      var estimatedValuesEnumerators = (from model in models
72                                        select model.GetAction(input).GetEnumerator())
73                                       .ToList();
74
75      while (estimatedValuesEnumerators.All(en => en.MoveNext())) {
76        yield return from enumerator in estimatedValuesEnumerators
77                     select enumerator.Current;
78      }
79    }
80
81    public IEnumerable<IAction> GetAction(IEnumerable<IInput> input) {
82      foreach (var estimatedValuesVector in GetEstimatedActionVectors(input)) {
83        // return the class which is most often occuring
84        yield return
85          estimatedValuesVector
86          .GroupBy(x => x, comparer)
87          .OrderByDescending(g => g.Count())
88          .Select(g => g.Key)
89          .First();
90      }
91    }
92
93    public IAction GetAction(IInput classifier) {
94      var estimatedValuesVector = models.Select(x => x.GetAction(classifier));
95      return estimatedValuesVector
96           .GroupBy(x => x, comparer)
97           .OrderByDescending(g => g.Count())
98           .Select(g => g.Key)
99           .First();
100    }
101
102    public IConditionActionSolution CreateConditionActionSolution(IConditionActionProblemData problemData) {
103      return new ConditionActionEnsembleSolution(models, new ConditionActionEnsembleProblemData((IConditionActionProblemData)problemData.Clone()));
104    }
105
106    public int ClassifierCount {
107      get { return Models.Sum(x => x.ClassifierCount); }
108    }
109  }
110}
Note: See TracBrowser for help on using the repository browser.