Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis/3.3/SupportVectorMachine/ParameterAdjustmentProblem/SupportVectorMachineParameterAdjustmentProblem.cs @ 5275

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

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

File size: 13.9 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.RealVectorEncoding;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.PluginInfrastructure;
33
34namespace HeuristicLab.Problems.DataAnalysis.SupportVectorMachine.ParameterAdjustmentProblem {
35  [Item("Support Vector Machine Parameter Adjustment Problem", "Represents the problem of finding good parameter settings for support vector machines.")]
36  [StorableClass]
37  [Creatable("Problems")]
38  public sealed class SupportVectorMachineParameterAdjustmentProblem : DataAnalysisProblem, ISingleObjectiveProblem {
39
40    #region Parameter Properties
41    public ValueParameter<BoolValue> MaximizationParameter {
42      get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }
43    }
44    IParameter ISingleObjectiveProblem.MaximizationParameter {
45      get { return MaximizationParameter; }
46    }
47    public ValueParameter<DoubleMatrix> BoundsParameter {
48      get { return (ValueParameter<DoubleMatrix>)Parameters["Bounds"]; }
49    }
50    public ValueParameter<IntValue> ProblemSizeParameter {
51      get { return (ValueParameter<IntValue>)Parameters["ProblemSize"]; }
52    }
53    public new ValueParameter<IRealVectorCreator> SolutionCreatorParameter {
54      get { return (ValueParameter<IRealVectorCreator>)Parameters["SolutionCreator"]; }
55    }
56    IParameter IProblem.SolutionCreatorParameter {
57      get { return SolutionCreatorParameter; }
58    }
59    public new ValueParameter<SupportVectorMachineParameterAdjustmentEvaluator> EvaluatorParameter {
60      get { return (ValueParameter<SupportVectorMachineParameterAdjustmentEvaluator>)Parameters["Evaluator"]; }
61    }
62    IParameter IProblem.EvaluatorParameter {
63      get { return EvaluatorParameter; }
64    }
65    public OptionalValueParameter<DoubleValue> BestKnownQualityParameter {
66      get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
67    }
68    IParameter ISingleObjectiveProblem.BestKnownQualityParameter {
69      get { return BestKnownQualityParameter; }
70    }
71    public OptionalValueParameter<RealVector> BestKnownSolutionParameter {
72      get { return (OptionalValueParameter<RealVector>)Parameters["BestKnownSolution"]; }
73    }
74    #endregion
75
76    #region Properties
77    public BoolValue Maximization {
78      get { return MaximizationParameter.Value; }
79      set { MaximizationParameter.Value = value; }
80    }
81    public DoubleMatrix Bounds {
82      get { return BoundsParameter.Value; }
83    }
84    public IntValue ProblemSize {
85      get { return ProblemSizeParameter.Value; }
86      set { ProblemSizeParameter.Value = value; }
87    }
88    public new IRealVectorCreator SolutionCreator {
89      get { return SolutionCreatorParameter.Value; }
90      set { SolutionCreatorParameter.Value = value; }
91    }
92    ISolutionCreator IProblem.SolutionCreator {
93      get { return SolutionCreatorParameter.Value; }
94    }
95    public new SupportVectorMachineParameterAdjustmentEvaluator Evaluator {
96      get { return EvaluatorParameter.Value; }
97      set { EvaluatorParameter.Value = value; }
98    }
99    ISingleObjectiveEvaluator ISingleObjectiveProblem.Evaluator {
100      get { return EvaluatorParameter.Value; }
101    }
102    IEvaluator IProblem.Evaluator {
103      get { return EvaluatorParameter.Value; }
104    }
105    public DoubleValue BestKnownQuality {
106      get { return BestKnownQualityParameter.Value; }
107      set { BestKnownQualityParameter.Value = value; }
108    }
109    public override IEnumerable<IOperator> Operators {
110      get { return operators; }
111    }
112    #endregion
113
114    public IntValue TrainingSamplesStart {
115      get { return new IntValue(DataAnalysisProblemData.TrainingSamplesStart.Value); }
116    }
117    public IntValue TrainingSamplesEnd {
118      get { return new IntValue(DataAnalysisProblemData.TrainingSamplesEnd.Value); }
119    }
120
121    [Storable]
122    private List<IOperator> operators;
123    [Storable]
124    private StdDevStrategyVectorCreator strategyVectorCreator;
125    [Storable]
126    private StdDevStrategyVectorCrossover strategyVectorCrossover;
127    [Storable]
128    private StdDevStrategyVectorManipulator strategyVectorManipulator;
129
130    [StorableConstructor]
131    private SupportVectorMachineParameterAdjustmentProblem(bool deserializing) : base(deserializing) { }
132    private SupportVectorMachineParameterAdjustmentProblem(SupportVectorMachineParameterAdjustmentProblem original, Cloner cloner)
133      : base(original, cloner) {
134      operators = original.operators.Where(x => IsNotFieldReferenced(x)).Select(x => (IOperator)cloner.Clone(x)).ToList();
135      strategyVectorCreator = (StdDevStrategyVectorCreator)cloner.Clone(original.strategyVectorCreator);
136      operators.Add(strategyVectorCreator);
137      strategyVectorCrossover = (StdDevStrategyVectorCrossover)cloner.Clone(original.strategyVectorCrossover);
138      operators.Add(strategyVectorCrossover);
139      strategyVectorManipulator = (StdDevStrategyVectorManipulator)cloner.Clone(original.strategyVectorManipulator);
140      operators.Add(strategyVectorManipulator);
141      AttachEventHandlers();
142    }
143    public SupportVectorMachineParameterAdjustmentProblem()
144      : base() {
145      UniformRandomRealVectorCreator creator = new UniformRandomRealVectorCreator();
146      SupportVectorMachineParameterAdjustmentEvaluator evaluator = new SupportVectorMachineParameterAdjustmentEvaluator();
147
148      var bounds = new DoubleMatrix(new double[,] {
149        { 0.01, 1.0 },
150        { -7, 9},
151        { -7, 9}
152      });
153
154      Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as we want to minimize the error.", new BoolValue(false)));
155      Parameters.Add(new ValueParameter<DoubleMatrix>("Bounds", "The lower and upper bounds in each dimension.", bounds));
156      Parameters.Add(new ValueParameter<IntValue>("ProblemSize", "The dimension of the problem.", new IntValue(3)));
157      Parameters.Add(new ValueParameter<IRealVectorCreator>("SolutionCreator", "The operator which should be used to create new test function solutions.", creator));
158      Parameters.Add(new ValueParameter<SupportVectorMachineParameterAdjustmentEvaluator>("Evaluator", "The operator which should be used to evaluate test function solutions.", evaluator));
159      Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this test function.", new DoubleValue(0)));
160      Parameters.Add(new OptionalValueParameter<RealVector>("BestKnownSolution", "The best known solution for this test function instance."));
161      Parameters.Add(new OptionalValueParameter<PercentValue>("ActualSamples", "The percentage of samples to use for cross validation."));
162
163      strategyVectorCreator = new StdDevStrategyVectorCreator();
164      strategyVectorCreator.LengthParameter.Value = ProblemSize;
165      strategyVectorCrossover = new StdDevStrategyVectorCrossover();
166      strategyVectorManipulator = new StdDevStrategyVectorManipulator();
167      strategyVectorManipulator.LearningRateParameter.Value = new DoubleValue(0.5);
168      strategyVectorManipulator.GeneralLearningRateParameter.Value = new DoubleValue(0.5);
169
170      creator.RealVectorParameter.ActualName = "ParameterVector";
171      ParameterizeSolutionCreator();
172      ParameterizeEvaluator();
173
174      InitializeOperators();
175      AttachEventHandlers();
176      UpdateStrategyVectorBounds();
177    }
178
179    public override IDeepCloneable Clone(Cloner cloner) {
180      return new SupportVectorMachineParameterAdjustmentProblem(this, cloner);
181    }
182    private bool IsNotFieldReferenced(IOperator x) {
183      return !(x == strategyVectorCreator
184        || x == strategyVectorCrossover
185        || x == strategyVectorManipulator);
186    }
187
188    protected override void OnDataAnalysisProblemChanged(EventArgs e) {
189      ParameterizeEvaluator();
190      base.OnDataAnalysisProblemChanged(e);
191    }
192
193    #region Events
194    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
195      ParameterizeSolutionCreator();
196      ParameterizeAnalyzers();
197      SolutionCreator_RealVectorParameter_ActualNameChanged(null, EventArgs.Empty);
198    }
199    private void SolutionCreator_RealVectorParameter_ActualNameChanged(object sender, EventArgs e) {
200      ParameterizeEvaluator();
201      ParameterizeOperators();
202      ParameterizeAnalyzers();
203    }
204    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
205      ParameterizeEvaluator();
206      ParameterizeAnalyzers();
207      RaiseReset(EventArgs.Empty);
208    }
209    private void strategyVectorCreator_StrategyParameterParameter_ActualNameChanged(object sender, EventArgs e) {
210      string name = strategyVectorCreator.StrategyParameterParameter.ActualName;
211      strategyVectorCrossover.ParentsParameter.ActualName = name;
212      strategyVectorCrossover.StrategyParameterParameter.ActualName = name;
213      strategyVectorManipulator.StrategyParameterParameter.ActualName = name;
214    }
215    #endregion
216
217    #region Helpers
218    [StorableHook(HookType.AfterDeserialization)]
219    private void AfterDeserializationHook() {
220      // BackwardsCompatibility3.3
221      #region Backwards compatible code (remove with 3.4)
222      if (operators == null) InitializeOperators();
223      #endregion
224      AttachEventHandlers();
225    }
226
227    private void AttachEventHandlers() {
228      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
229      SolutionCreator.RealVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_RealVectorParameter_ActualNameChanged);
230      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
231      strategyVectorCreator.StrategyParameterParameter.ActualNameChanged += new EventHandler(strategyVectorCreator_StrategyParameterParameter_ActualNameChanged);
232    }
233    private void InitializeOperators() {
234      operators = new List<IOperator>();
235      operators.AddRange(ApplicationManager.Manager.GetInstances<IRealVectorOperator>().Cast<IOperator>());
236      operators.RemoveAll(x => x is IMoveOperator);
237      operators.Add(new SupportVectorMachineParameterAdjustmentBestSolutionAnalyzer());
238      operators.Add(strategyVectorCreator);
239      operators.Add(strategyVectorCrossover);
240      operators.Add(strategyVectorManipulator);
241      ParameterizeOperators();
242    }
243    private void ParameterizeSolutionCreator() {
244      SolutionCreator.LengthParameter.Value = new IntValue(ProblemSize.Value);
245    }
246    private void ParameterizeEvaluator() {
247      Evaluator.ParameterVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
248      Evaluator.SamplesStartParameter.Value = TrainingSamplesStart;
249      Evaluator.SamplesEndParameter.Value = TrainingSamplesEnd;
250      Evaluator.NumberOfFoldsParameter.Value = new IntValue(5);
251    }
252    private void ParameterizeOperators() {
253      foreach (IRealVectorCrossover op in Operators.OfType<IRealVectorCrossover>()) {
254        op.ParentsParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
255        op.ChildParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
256        op.BoundsParameter.ActualName = BoundsParameter.Name;
257      }
258      foreach (IRealVectorManipulator op in Operators.OfType<IRealVectorManipulator>()) {
259        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
260        op.BoundsParameter.ActualName = BoundsParameter.Name;
261      }
262      foreach (IRealVectorMoveOperator op in Operators.OfType<IRealVectorMoveOperator>()) {
263        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
264      }
265      foreach (IRealVectorMoveGenerator op in Operators.OfType<IRealVectorMoveGenerator>()) {
266        op.BoundsParameter.ActualName = BoundsParameter.Name;
267      }
268      foreach (SupportVectorMachineParameterAdjustmentEvaluator op in Operators.OfType<SupportVectorMachineParameterAdjustmentEvaluator>()) {
269        op.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
270        op.ParameterVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
271      }
272    }
273    private void ParameterizeAnalyzers() {
274      foreach (SupportVectorMachineParameterAdjustmentBestSolutionAnalyzer analyzer in Operators.OfType<SupportVectorMachineParameterAdjustmentBestSolutionAnalyzer>()) {
275        analyzer.DataAnalysisProblemDataParameter.ActualName = DataAnalysisProblemDataParameter.Name;
276        analyzer.ParameterVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
277        analyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
278      }
279    }
280    private void UpdateStrategyVectorBounds() {
281      DoubleMatrix strategyBounds = (DoubleMatrix)Bounds.Clone();
282      for (int i = 0; i < strategyBounds.Rows; i++)
283        if (strategyBounds[i, 0] < 0) strategyBounds[i, 0] = 0;
284      strategyVectorCreator.BoundsParameter.Value = strategyBounds;
285    }
286    #endregion
287  }
288}
Note: See TracBrowser for help on using the repository browser.