Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.FeatureSelection/3.3/FeatureSelectionProblem.cs @ 4118

Last change on this file since 4118 was 4118, checked in by abeham, 14 years ago

#1090

  • Fixed problem plugins reloading their operators on deserialization in following problems (forgot on them in the first commit)
    • SupportVectorRegressionProblem
    • SymbolicTimeSeriesPrognosisProblem
  • Fixed a bug in the FeatureSelectionProblem introduced in r4098
  • Fixed the issues mentioned in the code review of mkommend
File size: 8.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
29using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Analyzers;
30using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;
31using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Interfaces;
32using HeuristicLab.Optimization;
33using HeuristicLab.Parameters;
34using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
35using HeuristicLab.PluginInfrastructure;
36using HeuristicLab.Encodings.BinaryVectorEncoding;
37
38namespace HeuristicLab.Problems.DataAnalysis.FeatureSelection {
39  [Item("Feature Selection Problem", "Represents a feature selection problem.")]
40  [Creatable("Problems")]
41  [StorableClass]
42  public class FeatureSelectionProblem : DataAnalysisProblem, IMultiObjectiveProblem {
43
44    #region Parameter Properties
45    public ValueParameter<BoolArray> MaximizationParameter {
46      get { return (ValueParameter<BoolArray>)Parameters["Maximization"]; }
47    }
48    IParameter IMultiObjectiveProblem.MaximizationParameter {
49      get { return MaximizationParameter; }
50    }
51    public new ValueParameter<IBinaryVectorCreator> SolutionCreatorParameter {
52      get { return (ValueParameter<IBinaryVectorCreator>)Parameters["SolutionCreator"]; }
53    }
54    IParameter IProblem.SolutionCreatorParameter {
55      get { return SolutionCreatorParameter; }
56    }
57    public new ValueParameter<IFeatureSelectionEvaluator> EvaluatorParameter {
58      get { return (ValueParameter<IFeatureSelectionEvaluator>)Parameters["Evaluator"]; }
59    }
60    IParameter IProblem.EvaluatorParameter {
61      get { return EvaluatorParameter; }
62    }
63    #endregion
64
65    #region Properties
66    public new IBinaryVectorCreator SolutionCreator {
67      get { return SolutionCreatorParameter.Value; }
68      set { SolutionCreatorParameter.Value = value; }
69    }
70    ISolutionCreator IProblem.SolutionCreator {
71      get { return SolutionCreatorParameter.Value; }
72    }
73    public new IFeatureSelectionEvaluator Evaluator {
74      get { return EvaluatorParameter.Value; }
75      set { EvaluatorParameter.Value = value; }
76    }
77    IMultiObjectiveEvaluator IMultiObjectiveProblem.Evaluator {
78      get { return EvaluatorParameter.Value; }
79    }
80    IEvaluator IProblem.Evaluator {
81      get { return EvaluatorParameter.Value; }
82    }
83    public override IEnumerable<IOperator> Operators {
84      get { return operators; }
85    }
86    #endregion
87
88    [Storable]
89    private List<IOperator> operators;
90
91    [StorableConstructor]
92    protected FeatureSelectionProblem(bool deserializing) : base(deserializing) { }
93    public FeatureSelectionProblem()
94      : base() {
95      BinaryVectorCreator creator = new RandomBinaryVectorCreator();
96      var evaluator = new LinearRegressionFeatureSelectionEvaluator();
97      Parameters.Add(new ValueParameter<BoolArray>("Maximization", "Both objectives should be minimized.", new BoolArray(new bool[] {false, false})));
98      Parameters.Add(new ValueParameter<IBinaryVectorCreator>("SolutionCreator", "The operator which should be used to create new solutions.", creator));
99      Parameters.Add(new ValueParameter<IFeatureSelectionEvaluator>("Evaluator", "The evaluator that should be used for the feature selection problem.", evaluator));
100
101      creator.BinaryVectorParameter.ActualName = "FeatureArray";
102      evaluator.QualitiesParameter.ActualName = "SizeAndCVMeanSquaredError";
103
104      ParameterizeSolutionCreator();
105      ParameterizeEvaluator();
106
107      InitializeOperators();
108      RegisterParameterEvents();
109      RegisterParameterValueEvents();
110    }
111
112    [StorableHook(HookType.AfterDeserialization)]
113    private void AfterDeserializationHook() {
114      // BackwardsCompatibility3.3
115      #region Backwards compatible code (remove with 3.4)
116      if (operators == null) InitializeOperators();
117      #endregion
118      RegisterParameterEvents();
119      RegisterParameterValueEvents();
120    }
121
122    public override IDeepCloneable Clone(Cloner cloner) {
123      FeatureSelectionProblem clone = (FeatureSelectionProblem)base.Clone(cloner);
124      clone.operators = operators.Select(x => (IOperator)cloner.Clone(x)).ToList();
125      clone.RegisterParameterEvents();
126      clone.RegisterParameterValueEvents();
127      return clone;
128    }
129
130    private void RegisterParameterValueEvents() {
131      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
132      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
133    }
134
135    private void RegisterParameterEvents() {
136      SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_BinaryVectorParameter_ActualNameChanged);
137    }
138
139    #region event handling
140    protected override void OnDataAnalysisProblemChanged(EventArgs e) {
141      base.OnDataAnalysisProblemChanged(e);
142      ParameterizeSolutionCreator();
143    }
144    protected virtual void OnOperatorsChanged(EventArgs e) { RaiseOperatorsChanged(e); }
145    protected virtual void OnSolutionCreatorChanged(EventArgs e) {
146      SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_BinaryVectorParameter_ActualNameChanged);
147      ParameterizeSolutionCreator();
148      OnSolutionParameterNameChanged(e);
149      RaiseSolutionCreatorChanged(e);
150    }
151
152    protected virtual void OnSolutionParameterNameChanged(EventArgs e) {
153      ParameterizeEvaluator();
154      ParameterizeOperators();
155    }
156
157    protected virtual void OnEvaluatorChanged(EventArgs e) {
158      ParameterizeEvaluator();
159      RaiseEvaluatorChanged(e);
160    }
161    #endregion
162
163    #region event handlers
164    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
165      OnSolutionCreatorChanged(e);
166    }
167    private void SolutionCreator_BinaryVectorParameter_ActualNameChanged(object sender, EventArgs e) {
168      OnSolutionParameterNameChanged(e);
169    }
170    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
171      OnEvaluatorChanged(e);
172    }
173    #endregion
174
175    #region Helpers
176    private void InitializeOperators() {
177      operators = new List<IOperator>();
178      operators.AddRange(ApplicationManager.Manager.GetInstances<IBinaryVectorOperator>().OfType<IOperator>());
179      ParameterizeOperators();
180    }
181
182    private void ParameterizeSolutionCreator() {
183      SolutionCreator.LengthParameter.Value = new IntValue(DataAnalysisProblemData.InputVariables.CheckedItems.Count());
184    }
185
186    private void ParameterizeEvaluator() {
187      Evaluator.SolutionParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
188    }
189
190    private void ParameterizeOperators() {
191      foreach (IBinaryVectorCrossover op in Operators.OfType<IBinaryVectorCrossover>()) {
192        op.ParentsParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
193        op.ChildParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
194      }
195      foreach (IBinaryVectorManipulator op in Operators.OfType<IBinaryVectorManipulator>()) {
196        op.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
197      }
198    }
199    #endregion
200  }
201}
Note: See TracBrowser for help on using the repository browser.