Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4085 was 4082, checked in by gkronber, 14 years ago

Added plugin for evolutionary feature selection. #1097

File size: 7.8 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    private List<IOperator> operators;
84    public override IEnumerable<IOperator> Operators {
85      get { return operators; }
86    }
87    #endregion
88
89    public FeatureSelectionProblem()
90      : base() {
91      BinaryVectorCreator creator = new RandomBinaryVectorCreator();
92      var evaluator = new LinearRegressionFeatureSelectionEvaluator();
93      Parameters.Add(new ValueParameter<BoolArray>("Maximization", "Both objectives should be minimized.", new BoolArray(new bool[] {false, false})));
94      Parameters.Add(new ValueParameter<IBinaryVectorCreator>("SolutionCreator", "The operator which should be used to create new solutions.", creator));
95      Parameters.Add(new ValueParameter<IFeatureSelectionEvaluator>("Evaluator", "The evaluator that should be used for the feature selection problem.", evaluator));
96
97      creator.BinaryVectorParameter.ActualName = "FeatureArray";
98      evaluator.QualitiesParameter.ActualName = "SizeAndCVMeanSquaredError";
99
100      ParameterizeSolutionCreator();
101      ParameterizeEvaluator();
102
103      Initialize();
104    }
105
106    [StorableConstructor]
107    private FeatureSelectionProblem(bool deserializing) : base() { }
108
109    [StorableHook(HookType.AfterDeserialization)]
110    private void AfterDeserializationHook() {
111      RegisterParameterEvents();
112      RegisterParameterValueEvents();
113    }
114
115    public override IDeepCloneable Clone(Cloner cloner) {
116      FeatureSelectionProblem clone = (FeatureSelectionProblem)base.Clone(cloner);
117      clone.Initialize();
118      return clone;
119    }
120
121    private void RegisterParameterValueEvents() {
122      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
123      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
124    }
125
126    private void RegisterParameterEvents() {
127      SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_BinaryVectorParameter_ActualNameChanged);
128    }
129
130    #region event handling
131    protected override void OnDataAnalysisProblemChanged(EventArgs e) {
132      base.OnDataAnalysisProblemChanged(e);
133      ParameterizeSolutionCreator();
134    }
135    protected virtual void OnOperatorsChanged(EventArgs e) { RaiseOperatorsChanged(e); }
136    protected virtual void OnSolutionCreatorChanged(EventArgs e) {
137      SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_BinaryVectorParameter_ActualNameChanged);
138      ParameterizeSolutionCreator();
139      OnSolutionParameterNameChanged(e);
140      RaiseSolutionCreatorChanged(e);
141    }
142
143    protected virtual void OnSolutionParameterNameChanged(EventArgs e) {
144      ParameterizeEvaluator();
145      ParameterizeOperators();
146    }
147
148    protected virtual void OnEvaluatorChanged(EventArgs e) {
149      ParameterizeEvaluator();
150      RaiseEvaluatorChanged(e);
151    }
152    #endregion
153
154    #region event handlers
155    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
156      OnSolutionCreatorChanged(e);
157    }
158    private void SolutionCreator_BinaryVectorParameter_ActualNameChanged(object sender, EventArgs e) {
159      OnSolutionParameterNameChanged(e);
160    }
161    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
162      OnEvaluatorChanged(e);
163    }
164    #endregion
165
166    #region Helpers
167    private void Initialize() {
168      InitializeOperators();
169      RegisterParameterEvents();
170      RegisterParameterValueEvents();
171    }
172
173    private void InitializeOperators() {
174      operators = new List<IOperator>();
175      operators.AddRange(ApplicationManager.Manager.GetInstances<IBinaryVectorOperator>().OfType<IOperator>());
176      ParameterizeOperators();
177    }
178
179    private void ParameterizeSolutionCreator() {
180      SolutionCreator.LengthParameter.Value = new IntValue(DataAnalysisProblemData.InputVariables.CheckedItems.Count());
181    }
182
183    private void ParameterizeEvaluator() {
184      Evaluator.SolutionParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
185    }
186
187    private void ParameterizeOperators() {
188      foreach (IBinaryVectorCrossover op in Operators.OfType<IBinaryVectorCrossover>()) {
189        op.ParentsParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
190        op.ChildParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
191      }
192      foreach (IBinaryVectorManipulator op in Operators.OfType<IBinaryVectorManipulator>()) {
193        op.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
194      }
195    }
196    #endregion
197  }
198}
Note: See TracBrowser for help on using the repository browser.