Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProblemInstancesRegressionAndClassification/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SingleObjective/SymbolicClassificationSingleObjectiveProblem.cs @ 7750

Last change on this file since 7750 was 7750, checked in by sforsten, 13 years ago

#1784:

  • merged Problems.DataAnalysis r7273:7748 from trunk
  • prepared SymbolicClassificationSingleObjectiveProblem and SymbolicRegressionSingleObjectiveProblem to load and export problem instances
File size: 7.6 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
21using System;
22using System.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Problems.Instances;
29
30namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Classification {
31  [Item("Symbolic Classification Problem (single objective)", "Represents a single objective symbolic classfication problem.")]
32  [StorableClass]
33  [Creatable("Problems")]
34  public class SymbolicClassificationSingleObjectiveProblem : SymbolicDataAnalysisSingleObjectiveProblem<IClassificationProblemData, ISymbolicClassificationSingleObjectiveEvaluator, ISymbolicDataAnalysisSolutionCreator>, IClassificationProblem,
35    IProblemInstanceConsumer<ClassificationData>, IProblemInstanceExporter<ClassificationData> {
36    private const double PunishmentFactor = 10;
37    private const int InitialMaximumTreeDepth = 8;
38    private const int InitialMaximumTreeLength = 25;
39    private const string EstimationLimitsParameterName = "EstimationLimits";
40    private const string EstimationLimitsParameterDescription = "The lower and upper limit for the estimated value that can be returned by the symbolic classification model.";
41
42    #region parameter properties
43    public IFixedValueParameter<DoubleLimit> EstimationLimitsParameter {
44      get { return (IFixedValueParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
45    }
46    #endregion
47    #region properties
48    public DoubleLimit EstimationLimits {
49      get { return EstimationLimitsParameter.Value; }
50    }
51    #endregion
52    [StorableConstructor]
53    protected SymbolicClassificationSingleObjectiveProblem(bool deserializing) : base(deserializing) { }
54    protected SymbolicClassificationSingleObjectiveProblem(SymbolicClassificationSingleObjectiveProblem original, Cloner cloner) : base(original, cloner) { }
55    public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicClassificationSingleObjectiveProblem(this, cloner); }
56
57    public SymbolicClassificationSingleObjectiveProblem()
58      : base(new ClassificationProblemData(), new SymbolicClassificationSingleObjectiveMeanSquaredErrorEvaluator(), new SymbolicDataAnalysisExpressionTreeCreator()) {
59      Parameters.Add(new FixedValueParameter<DoubleLimit>(EstimationLimitsParameterName, EstimationLimitsParameterDescription));
60
61      EstimationLimitsParameter.Hidden = true;
62
63      MaximumSymbolicExpressionTreeDepth.Value = InitialMaximumTreeDepth;
64      MaximumSymbolicExpressionTreeLength.Value = InitialMaximumTreeLength;
65
66      SymbolicExpressionTreeGrammarParameter.ValueChanged += (o, e) => ConfigureGrammarSymbols();
67
68      ConfigureGrammarSymbols();
69      InitializeOperators();
70      UpdateEstimationLimits();
71    }
72
73    private void ConfigureGrammarSymbols() {
74      var grammar = SymbolicExpressionTreeGrammar as TypeCoherentExpressionGrammar;
75      if (grammar != null) grammar.ConfigureAsDefaultClassificationGrammar();
76    }
77
78    private void InitializeOperators() {
79      Operators.Add(new SymbolicClassificationSingleObjectiveTrainingBestSolutionAnalyzer());
80      Operators.Add(new SymbolicClassificationSingleObjectiveValidationBestSolutionAnalyzer());
81      Operators.Add(new SymbolicClassificationSingleObjectiveOverfittingAnalyzer());
82      Operators.Add(new SymbolicClassificationSingleObjectiveTrainingParetoBestSolutionAnalyzer());
83      Operators.Add(new SymbolicClassificationSingleObjectiveValidationParetoBestSolutionAnalyzer());
84      ParameterizeOperators();
85    }
86
87    private void UpdateEstimationLimits() {
88      if (ProblemData.TrainingIndizes.Any()) {
89        var targetValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndizes).ToList();
90        var mean = targetValues.Average();
91        var range = targetValues.Max() - targetValues.Min();
92        EstimationLimits.Upper = mean + PunishmentFactor * range;
93        EstimationLimits.Lower = mean - PunishmentFactor * range;
94      } else {
95        EstimationLimits.Upper = double.MaxValue;
96        EstimationLimits.Lower = double.MinValue;
97      }
98    }
99
100    protected override void OnProblemDataChanged() {
101      base.OnProblemDataChanged();
102      UpdateEstimationLimits();
103    }
104
105    protected override void ParameterizeOperators() {
106      base.ParameterizeOperators();
107      if (Parameters.ContainsKey(EstimationLimitsParameterName)) {
108        var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators);
109        foreach (var op in operators.OfType<ISymbolicDataAnalysisBoundedOperator>()) {
110          op.EstimationLimitsParameter.ActualName = EstimationLimitsParameter.Name;
111        }
112      }
113    }
114
115    public override void ImportProblemDataFromFile(string fileName) {
116      ClassificationProblemData problemData = ClassificationProblemData.ImportFromFile(fileName);
117      ProblemData = problemData;
118    }
119
120    public void Load(ClassificationData data) {
121      Name = data.Name;
122      Description = data.Description;
123      Dataset dataset = new Dataset(data.InputVariables, data.Values);
124      ProblemData = new ClassificationProblemData(dataset, data.AllowedInputVariables, data.TargetVariable);
125      ProblemData.TrainingPartition.Start = data.TrainingPartitionStart;
126      ProblemData.TrainingPartition.End = data.TrainingPartitionEnd;
127      ProblemData.TestPartition.Start = data.TestPartitionStart;
128      ProblemData.TestPartition.End = data.TestPartitionEnd;
129      OnReset();
130    }
131
132    public ClassificationData Export() {
133      if (!ProblemData.InputVariables.Count.Equals(ProblemData.Dataset.DoubleVariables.Count()))
134        throw new ArgumentException("Not all input variables are double variables! (Export only works with double variables)");
135
136      ClassificationData claData = new ClassificationData();
137      claData.Name = Name;
138      claData.Description = Description;
139      claData.TargetVariable = ProblemData.TargetVariable;
140      claData.InputVariables = ProblemData.InputVariables.Select(x => x.Value).ToArray();
141      claData.AllowedInputVariables = ProblemData.AllowedInputVariables.ToArray();
142      claData.TrainingPartitionStart = ProblemData.TrainingPartition.Start;
143      claData.TrainingPartitionEnd = ProblemData.TrainingPartition.End;
144      claData.TestPartitionStart = ProblemData.TestPartition.Start;
145      claData.TestPartitionEnd = ProblemData.TestPartition.End;
146
147      List<List<double>> data = new List<List<double>>();
148      foreach (var variable in ProblemData.Dataset.DoubleVariables) {
149        data.Add(ProblemData.Dataset.GetDoubleValues(variable).ToList());
150      }
151      claData.Values = Transformer.Transformation(data);
152
153      return claData;
154    }
155  }
156}
Note: See TracBrowser for help on using the repository browser.