Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/SupportVectorMachine/ParameterAdjustmentProblem/SupportVectorMachineParameterAdjustmentProblem.cs @ 4068

Last change on this file since 4068 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 13.0 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    private List<IOperator> operators;
110    public override IEnumerable<IOperator> Operators {
111      get { return operators; }
112    }
113    #endregion
114
115    public IntValue TrainingSamplesStart {
116      get { return new IntValue(DataAnalysisProblemData.TrainingSamplesStart.Value); }
117    }
118    public IntValue TrainingSamplesEnd {
119      get { return new IntValue(DataAnalysisProblemData.TrainingSamplesEnd.Value); }
120    }
121
122    [Storable]
123    private StdDevStrategyVectorCreator strategyVectorCreator;
124    [Storable]
125    private StdDevStrategyVectorCrossover strategyVectorCrossover;
126    [Storable]
127    private StdDevStrategyVectorManipulator strategyVectorManipulator;
128
129    [StorableConstructor]
130    private SupportVectorMachineParameterAdjustmentProblem(bool deserializing) : base() { }
131    public SupportVectorMachineParameterAdjustmentProblem()
132      : base() {
133      UniformRandomRealVectorCreator creator = new UniformRandomRealVectorCreator();
134      SupportVectorMachineParameterAdjustmentEvaluator evaluator = new SupportVectorMachineParameterAdjustmentEvaluator();
135
136      var bounds = new DoubleMatrix(new double[,] {
137        { 0.01, 1.0 },
138        { -7, 9},
139        { -7, 9}
140      });
141
142      Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as we want to minimize the error.", new BoolValue(false)));
143      Parameters.Add(new ValueParameter<DoubleMatrix>("Bounds", "The lower and upper bounds in each dimension.", bounds));
144      Parameters.Add(new ValueParameter<IntValue>("ProblemSize", "The dimension of the problem.", new IntValue(3)));
145      Parameters.Add(new ValueParameter<IRealVectorCreator>("SolutionCreator", "The operator which should be used to create new test function solutions.", creator));
146      Parameters.Add(new ValueParameter<SupportVectorMachineParameterAdjustmentEvaluator>("Evaluator", "The operator which should be used to evaluate test function solutions.", evaluator));
147      Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this test function.", new DoubleValue(0)));
148      Parameters.Add(new OptionalValueParameter<RealVector>("BestKnownSolution", "The best known solution for this test function instance."));
149      Parameters.Add(new OptionalValueParameter<PercentValue>("ActualSamples", "The percentage of samples to use for cross validation."));
150
151      strategyVectorCreator = new StdDevStrategyVectorCreator();
152      strategyVectorCreator.LengthParameter.Value = ProblemSize;
153      strategyVectorCrossover = new StdDevStrategyVectorCrossover();
154      strategyVectorManipulator = new StdDevStrategyVectorManipulator();
155      strategyVectorManipulator.LearningRateParameter.Value = new DoubleValue(0.5);
156      strategyVectorManipulator.GeneralLearningRateParameter.Value = new DoubleValue(0.5);
157
158      creator.RealVectorParameter.ActualName = "ParameterVector";
159      ParameterizeSolutionCreator();
160      ParameterizeEvaluator();
161
162      Initialize();
163      UpdateStrategyVectorBounds();
164    }
165
166    public override IDeepCloneable Clone(Cloner cloner) {
167      SupportVectorMachineParameterAdjustmentProblem clone = (SupportVectorMachineParameterAdjustmentProblem)base.Clone(cloner);
168      clone.strategyVectorCreator = (StdDevStrategyVectorCreator)cloner.Clone(strategyVectorCreator);
169      clone.strategyVectorCrossover = (StdDevStrategyVectorCrossover)cloner.Clone(strategyVectorCrossover);
170      clone.strategyVectorManipulator = (StdDevStrategyVectorManipulator)cloner.Clone(strategyVectorManipulator);
171      clone.Initialize();
172      return clone;
173    }
174
175    protected override void OnDataAnalysisProblemChanged(EventArgs e) {
176      ParameterizeEvaluator();
177      base.OnDataAnalysisProblemChanged(e);
178    }
179
180    #region Events
181    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
182      ParameterizeSolutionCreator();
183      ParameterizeAnalyzers();
184      SolutionCreator_RealVectorParameter_ActualNameChanged(null, EventArgs.Empty);
185    }
186    private void SolutionCreator_RealVectorParameter_ActualNameChanged(object sender, EventArgs e) {
187      ParameterizeEvaluator();
188      ParameterizeOperators();
189      ParameterizeAnalyzers();
190    }
191    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
192      ParameterizeEvaluator();
193      ParameterizeAnalyzers();
194      RaiseReset(EventArgs.Empty);
195    }
196    private void strategyVectorCreator_StrategyParameterParameter_ActualNameChanged(object sender, EventArgs e) {
197      string name = strategyVectorCreator.StrategyParameterParameter.ActualName;
198      strategyVectorCrossover.ParentsParameter.ActualName = name;
199      strategyVectorCrossover.StrategyParameterParameter.ActualName = name;
200      strategyVectorManipulator.StrategyParameterParameter.ActualName = name;
201    }
202    #endregion
203
204    #region Helpers
205    [StorableHook(HookType.AfterDeserialization)]
206    private void Initialize() {
207      InitializeOperators();
208      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
209      SolutionCreator.RealVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_RealVectorParameter_ActualNameChanged);
210      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
211      strategyVectorCreator.StrategyParameterParameter.ActualNameChanged += new EventHandler(strategyVectorCreator_StrategyParameterParameter_ActualNameChanged);
212    }
213    private void InitializeOperators() {
214      operators = new List<IOperator>();
215      operators.AddRange(ApplicationManager.Manager.GetInstances<IRealVectorOperator>().Cast<IOperator>());
216      operators.Add(new SupportVectorMachineParameterAdjustmentBestSolutionAnalyzer());
217      operators.Add(strategyVectorCreator);
218      operators.Add(strategyVectorCrossover);
219      operators.Add(strategyVectorManipulator);
220      ParameterizeOperators();
221    }
222    private void ParameterizeSolutionCreator() {
223      SolutionCreator.LengthParameter.Value = new IntValue(ProblemSize.Value);
224    }
225    private void ParameterizeEvaluator() {
226      Evaluator.ParameterVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
227      Evaluator.SamplesStartParameter.Value = TrainingSamplesStart;
228      Evaluator.SamplesEndParameter.Value = TrainingSamplesEnd;
229      Evaluator.NumberOfFoldsParameter.Value = new IntValue(5);
230    }
231    private void ParameterizeOperators() {
232      foreach (IRealVectorCrossover op in Operators.OfType<IRealVectorCrossover>()) {
233        op.ParentsParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
234        op.ChildParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
235        op.BoundsParameter.ActualName = BoundsParameter.Name;
236      }
237      foreach (IRealVectorManipulator op in Operators.OfType<IRealVectorManipulator>()) {
238        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
239        op.BoundsParameter.ActualName = BoundsParameter.Name;
240      }
241      foreach (IRealVectorMoveOperator op in Operators.OfType<IRealVectorMoveOperator>()) {
242        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
243      }
244      foreach (IRealVectorMoveGenerator op in Operators.OfType<IRealVectorMoveGenerator>()) {
245        op.BoundsParameter.ActualName = BoundsParameter.Name;
246      }
247      foreach (SupportVectorMachineParameterAdjustmentEvaluator op in Operators.OfType<SupportVectorMachineParameterAdjustmentEvaluator>()) {
248        op.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
249        op.ParameterVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
250      }
251    }
252    private void ParameterizeAnalyzers() {
253      foreach (SupportVectorMachineParameterAdjustmentBestSolutionAnalyzer analyzer in Operators.OfType<SupportVectorMachineParameterAdjustmentBestSolutionAnalyzer>()) {
254        analyzer.DataAnalysisProblemDataParameter.ActualName = DataAnalysisProblemDataParameter.Name;
255        analyzer.ParameterVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
256        analyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
257      }
258    }
259    private void UpdateStrategyVectorBounds() {
260      DoubleMatrix strategyBounds = (DoubleMatrix)Bounds.Clone();
261      for (int i = 0; i < strategyBounds.Rows; i++)
262        if (strategyBounds[i, 0] < 0) strategyBounds[i, 0] = 0;
263      strategyVectorCreator.BoundsParameter.Value = strategyBounds;
264    }
265    #endregion
266  }
267}
Note: See TracBrowser for help on using the repository browser.