Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.FeatureSelection/3.3/FeatureSelectionProblem.cs @ 7160

Last change on this file since 7160 was 5275, checked in by gkronber, 14 years ago

Merged changes from trunk to data analysis exploration branch and added fractional distance metric evaluator. #1142

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