Free cookie consent management tool by TermsFeed Policy Generator

source: branches/LearningClassifierSystems/HeuristicLab.Encodings.DecisionList/3.3/Analyzer/BestTrainingDecisionListAnalyzer.cs @ 10377

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

#1980:

  • added DecisionListView
  • added event handlers in *ProblemData
  • renamed project Problems.XCS.Views to Problems.lCS.Views and Problems.Instances.ConditionActionClassification to Problems.Instances.LCS
  • integrated niching in GAssist and added NichingTournamentSelector
  • minor code improvements and property changes
File size: 4.9 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.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Operators;
27using HeuristicLab.Optimization;
28using HeuristicLab.Optimization.Operators.LCS;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Encodings.DecisionList {
33  [Item("BestTrainingDecisionListAnalyzer", "Description missing")]
34  [StorableClass]
35  public class BestTrainingDecisionListAnalyzer : SingleSuccessorOperator, IAnalyzer {
36    private const string BestTrainingSolutionName = "Best Training Solution";
37    private const string TrainingBestSolutionQualityParameterName = "Best training solution quality";
38
39    public bool EnabledByDefault {
40      get { return true; }
41    }
42
43    #region Parameter Properties
44    public ValueLookupParameter<ResultCollection> ResultsParameter {
45      get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
46    }
47    public ScopeTreeLookupParameter<IGAssistIndividual> IndividualParameter {
48      get { return (ScopeTreeLookupParameter<IGAssistIndividual>)Parameters["Individual"]; }
49    }
50    public LookupParameter<IDecisionListClassificationProblemData> ProblemDataParameter {
51      get { return (LookupParameter<IDecisionListClassificationProblemData>)Parameters["ProblemData"]; }
52    }
53    public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
54      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
55    }
56    public ILookupParameter<BoolValue> MaximizationParameter {
57      get { return (ILookupParameter<BoolValue>)Parameters["Maximization"]; }
58    }
59    #endregion
60
61    public ResultCollection Results { get { return ResultsParameter.ActualValue; } }
62
63    [StorableConstructor]
64    protected BestTrainingDecisionListAnalyzer(bool deserializing) : base(deserializing) { }
65    protected BestTrainingDecisionListAnalyzer(BestTrainingDecisionListAnalyzer original, Cloner cloner)
66      : base(original, cloner) {
67    }
68    public BestTrainingDecisionListAnalyzer()
69      : base() {
70      Parameters.Add(new ScopeTreeLookupParameter<IGAssistIndividual>("Individual", ""));
71      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the solution should be stored."));
72      Parameters.Add(new LookupParameter<IDecisionListClassificationProblemData>("ProblemData", ""));
73      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The qualities of the trees that should be analyzed."));
74      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "The direction of optimization."));
75    }
76    public override IDeepCloneable Clone(Cloner cloner) {
77      return new BestTrainingDecisionListAnalyzer(this, cloner);
78    }
79
80    public override IOperation Apply() {
81      ItemArray<IGAssistIndividual> individuals = IndividualParameter.ActualValue;
82      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
83      bool maximization = MaximizationParameter.ActualValue.Value;
84      IDecisionListClassificationProblemData problemData = ProblemDataParameter.ActualValue;
85
86      DoubleValue value = maximization ? qualities.MaxItems(x => x.Value).First() : qualities.MinItems(x => x.Value).First();
87
88      int index = qualities.IndexOf(value);
89
90      if (!Results.ContainsKey(BestTrainingSolutionName)) {
91        Results.Add(new Result(TrainingBestSolutionQualityParameterName, new DoubleValue(value.Value)));
92        Results.Add(new Result(BestTrainingSolutionName, new DecisionListSolution((DecisionList)individuals[index], problemData)));
93      } else {
94        double oldBestQuality = ((DoubleValue)Results[TrainingBestSolutionQualityParameterName].Value).Value;
95        if (maximization && oldBestQuality < value.Value || !maximization && oldBestQuality > value.Value) {
96          Results[BestTrainingSolutionName].Value = new DecisionListSolution((DecisionList)individuals[index], problemData);
97          Results[TrainingBestSolutionQualityParameterName].Value = value;
98        }
99      }
100
101      return base.Apply();
102    }
103  }
104}
Note: See TracBrowser for help on using the repository browser.