#region License Information /* HeuristicLab * Copyright (C) 2002-2012 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.Encodings.ConditionActionEncoding { [StorableClass] [Item("ConditionActionEnsembleModel", "")] public class ConditionActionEnsembleModel : NamedItem, IConditionActionEnsembleModel { [Storable] private List models; public IEnumerable Models { get { return new List(models); } } [Storable] private IClassifierComparer comparer; public IClassifierComparer ClassifierComparer { get { return comparer; } set { comparer = value; } } [StorableConstructor] protected ConditionActionEnsembleModel(bool deserializing) : base(deserializing) { } protected ConditionActionEnsembleModel(ConditionActionEnsembleModel original, Cloner cloner) : base(original, cloner) { this.models = original.Models.Select(m => cloner.Clone(m)).ToList(); } public ConditionActionEnsembleModel() : this(Enumerable.Empty()) { } public ConditionActionEnsembleModel(IEnumerable models) : base() { this.name = ItemName; this.description = ItemDescription; this.models = new List(models); } public override IDeepCloneable Clone(Cloner cloner) { return new ConditionActionEnsembleModel(this, cloner); } public void Add(IConditionActionModel model) { models.Add(model); } public void Remove(IConditionActionModel model) { models.Remove(model); } public IEnumerable> GetEstimatedActionVectors(IEnumerable input) { var estimatedValuesEnumerators = (from model in models select model.GetAction(input).GetEnumerator()) .ToList(); while (estimatedValuesEnumerators.All(en => en.MoveNext())) { yield return from enumerator in estimatedValuesEnumerators select enumerator.Current; } } public IEnumerable GetAction(IEnumerable input) { foreach (var estimatedValuesVector in GetEstimatedActionVectors(input)) { // return the class which is most often occuring yield return estimatedValuesVector .GroupBy(x => x, comparer) .OrderByDescending(g => g.Count()) .Select(g => g.Key) .First(); } } public IAction GetAction(IInput classifier) { var estimatedValuesVector = models.Select(x => x.GetAction(classifier)); return estimatedValuesVector .GroupBy(x => x, comparer) .OrderByDescending(g => g.Count()) .Select(g => g.Key) .First(); } public IConditionActionSolution CreateConditionActionSolution(IConditionActionProblemData problemData) { return new ConditionActionEnsembleSolution(models, new ConditionActionEnsembleProblemData((IConditionActionProblemData)problemData.Clone())); } public int ClassifierCount { get { return Models.Sum(x => x.ClassifierCount); } } } }