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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
29 | using HeuristicLab.Optimization;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 | using HeuristicLab.PluginInfrastructure;
|
---|
33 |
|
---|
34 | namespace 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 | public SupportVectorMachineParameterAdjustmentProblem()
|
---|
133 | : base() {
|
---|
134 | UniformRandomRealVectorCreator creator = new UniformRandomRealVectorCreator();
|
---|
135 | SupportVectorMachineParameterAdjustmentEvaluator evaluator = new SupportVectorMachineParameterAdjustmentEvaluator();
|
---|
136 |
|
---|
137 | var bounds = new DoubleMatrix(new double[,] {
|
---|
138 | { 0.01, 1.0 },
|
---|
139 | { -7, 9},
|
---|
140 | { -7, 9}
|
---|
141 | });
|
---|
142 |
|
---|
143 | Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as we want to minimize the error.", new BoolValue(false)));
|
---|
144 | Parameters.Add(new ValueParameter<DoubleMatrix>("Bounds", "The lower and upper bounds in each dimension.", bounds));
|
---|
145 | Parameters.Add(new ValueParameter<IntValue>("ProblemSize", "The dimension of the problem.", new IntValue(3)));
|
---|
146 | Parameters.Add(new ValueParameter<IRealVectorCreator>("SolutionCreator", "The operator which should be used to create new test function solutions.", creator));
|
---|
147 | Parameters.Add(new ValueParameter<SupportVectorMachineParameterAdjustmentEvaluator>("Evaluator", "The operator which should be used to evaluate test function solutions.", evaluator));
|
---|
148 | Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this test function.", new DoubleValue(0)));
|
---|
149 | Parameters.Add(new OptionalValueParameter<RealVector>("BestKnownSolution", "The best known solution for this test function instance."));
|
---|
150 | Parameters.Add(new OptionalValueParameter<PercentValue>("ActualSamples", "The percentage of samples to use for cross validation."));
|
---|
151 |
|
---|
152 | strategyVectorCreator = new StdDevStrategyVectorCreator();
|
---|
153 | strategyVectorCreator.LengthParameter.Value = ProblemSize;
|
---|
154 | strategyVectorCrossover = new StdDevStrategyVectorCrossover();
|
---|
155 | strategyVectorManipulator = new StdDevStrategyVectorManipulator();
|
---|
156 | strategyVectorManipulator.LearningRateParameter.Value = new DoubleValue(0.5);
|
---|
157 | strategyVectorManipulator.GeneralLearningRateParameter.Value = new DoubleValue(0.5);
|
---|
158 |
|
---|
159 | creator.RealVectorParameter.ActualName = "ParameterVector";
|
---|
160 | ParameterizeSolutionCreator();
|
---|
161 | ParameterizeEvaluator();
|
---|
162 |
|
---|
163 | InitializeOperators();
|
---|
164 | AttachEventHandlers();
|
---|
165 | UpdateStrategyVectorBounds();
|
---|
166 | }
|
---|
167 |
|
---|
168 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
169 | SupportVectorMachineParameterAdjustmentProblem clone = (SupportVectorMachineParameterAdjustmentProblem)base.Clone(cloner);
|
---|
170 | clone.operators = operators.Where(x => IsNotFieldReferenced(x)).Select(x => (IOperator)cloner.Clone(x)).ToList();
|
---|
171 | clone.strategyVectorCreator = (StdDevStrategyVectorCreator)cloner.Clone(strategyVectorCreator);
|
---|
172 | clone.operators.Add(clone.strategyVectorCreator);
|
---|
173 | clone.strategyVectorCrossover = (StdDevStrategyVectorCrossover)cloner.Clone(strategyVectorCrossover);
|
---|
174 | clone.operators.Add(strategyVectorCrossover);
|
---|
175 | clone.strategyVectorManipulator = (StdDevStrategyVectorManipulator)cloner.Clone(strategyVectorManipulator);
|
---|
176 | clone.operators.Add(strategyVectorManipulator);
|
---|
177 | clone.AttachEventHandlers();
|
---|
178 | return clone;
|
---|
179 | }
|
---|
180 |
|
---|
181 | private bool IsNotFieldReferenced(IOperator x) {
|
---|
182 | return !(x == strategyVectorCreator
|
---|
183 | || x == strategyVectorCrossover
|
---|
184 | || x == strategyVectorManipulator);
|
---|
185 | }
|
---|
186 |
|
---|
187 | protected override void OnDataAnalysisProblemChanged(EventArgs e) {
|
---|
188 | ParameterizeEvaluator();
|
---|
189 | base.OnDataAnalysisProblemChanged(e);
|
---|
190 | }
|
---|
191 |
|
---|
192 | #region Events
|
---|
193 | private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
194 | ParameterizeSolutionCreator();
|
---|
195 | ParameterizeAnalyzers();
|
---|
196 | SolutionCreator_RealVectorParameter_ActualNameChanged(null, EventArgs.Empty);
|
---|
197 | }
|
---|
198 | private void SolutionCreator_RealVectorParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
199 | ParameterizeEvaluator();
|
---|
200 | ParameterizeOperators();
|
---|
201 | ParameterizeAnalyzers();
|
---|
202 | }
|
---|
203 | private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
204 | ParameterizeEvaluator();
|
---|
205 | ParameterizeAnalyzers();
|
---|
206 | RaiseReset(EventArgs.Empty);
|
---|
207 | }
|
---|
208 | private void strategyVectorCreator_StrategyParameterParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
209 | string name = strategyVectorCreator.StrategyParameterParameter.ActualName;
|
---|
210 | strategyVectorCrossover.ParentsParameter.ActualName = name;
|
---|
211 | strategyVectorCrossover.StrategyParameterParameter.ActualName = name;
|
---|
212 | strategyVectorManipulator.StrategyParameterParameter.ActualName = name;
|
---|
213 | }
|
---|
214 | #endregion
|
---|
215 |
|
---|
216 | #region Helpers
|
---|
217 | [StorableHook(HookType.AfterDeserialization)]
|
---|
218 | private void AfterDeserializationHook() {
|
---|
219 | // BackwardsCompatibility3.3
|
---|
220 | #region Backwards compatible code (remove with 3.4)
|
---|
221 | if (operators == null) InitializeOperators();
|
---|
222 | #endregion
|
---|
223 | AttachEventHandlers();
|
---|
224 | }
|
---|
225 |
|
---|
226 | private void AttachEventHandlers() {
|
---|
227 | SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
|
---|
228 | SolutionCreator.RealVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_RealVectorParameter_ActualNameChanged);
|
---|
229 | EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
|
---|
230 | strategyVectorCreator.StrategyParameterParameter.ActualNameChanged += new EventHandler(strategyVectorCreator_StrategyParameterParameter_ActualNameChanged);
|
---|
231 | }
|
---|
232 | private void InitializeOperators() {
|
---|
233 | operators = new List<IOperator>();
|
---|
234 | operators.AddRange(ApplicationManager.Manager.GetInstances<IRealVectorOperator>().Cast<IOperator>());
|
---|
235 | operators.RemoveAll(x => x is IMoveOperator);
|
---|
236 | operators.Add(new SupportVectorMachineParameterAdjustmentBestSolutionAnalyzer());
|
---|
237 | operators.Add(strategyVectorCreator);
|
---|
238 | operators.Add(strategyVectorCrossover);
|
---|
239 | operators.Add(strategyVectorManipulator);
|
---|
240 | ParameterizeOperators();
|
---|
241 | }
|
---|
242 | private void ParameterizeSolutionCreator() {
|
---|
243 | SolutionCreator.LengthParameter.Value = new IntValue(ProblemSize.Value);
|
---|
244 | }
|
---|
245 | private void ParameterizeEvaluator() {
|
---|
246 | Evaluator.ParameterVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
|
---|
247 | Evaluator.SamplesStartParameter.Value = TrainingSamplesStart;
|
---|
248 | Evaluator.SamplesEndParameter.Value = TrainingSamplesEnd;
|
---|
249 | Evaluator.NumberOfFoldsParameter.Value = new IntValue(5);
|
---|
250 | }
|
---|
251 | private void ParameterizeOperators() {
|
---|
252 | foreach (IRealVectorCrossover op in Operators.OfType<IRealVectorCrossover>()) {
|
---|
253 | op.ParentsParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
|
---|
254 | op.ChildParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
|
---|
255 | op.BoundsParameter.ActualName = BoundsParameter.Name;
|
---|
256 | }
|
---|
257 | foreach (IRealVectorManipulator op in Operators.OfType<IRealVectorManipulator>()) {
|
---|
258 | op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
|
---|
259 | op.BoundsParameter.ActualName = BoundsParameter.Name;
|
---|
260 | }
|
---|
261 | foreach (IRealVectorMoveOperator op in Operators.OfType<IRealVectorMoveOperator>()) {
|
---|
262 | op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
|
---|
263 | }
|
---|
264 | foreach (IRealVectorMoveGenerator op in Operators.OfType<IRealVectorMoveGenerator>()) {
|
---|
265 | op.BoundsParameter.ActualName = BoundsParameter.Name;
|
---|
266 | }
|
---|
267 | foreach (SupportVectorMachineParameterAdjustmentEvaluator op in Operators.OfType<SupportVectorMachineParameterAdjustmentEvaluator>()) {
|
---|
268 | op.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
269 | op.ParameterVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
|
---|
270 | }
|
---|
271 | }
|
---|
272 | private void ParameterizeAnalyzers() {
|
---|
273 | foreach (SupportVectorMachineParameterAdjustmentBestSolutionAnalyzer analyzer in Operators.OfType<SupportVectorMachineParameterAdjustmentBestSolutionAnalyzer>()) {
|
---|
274 | analyzer.DataAnalysisProblemDataParameter.ActualName = DataAnalysisProblemDataParameter.Name;
|
---|
275 | analyzer.ParameterVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
|
---|
276 | analyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
277 | }
|
---|
278 | }
|
---|
279 | private void UpdateStrategyVectorBounds() {
|
---|
280 | DoubleMatrix strategyBounds = (DoubleMatrix)Bounds.Clone();
|
---|
281 | for (int i = 0; i < strategyBounds.Rows; i++)
|
---|
282 | if (strategyBounds[i, 0] < 0) strategyBounds[i, 0] = 0;
|
---|
283 | strategyVectorCreator.BoundsParameter.Value = strategyBounds;
|
---|
284 | }
|
---|
285 | #endregion
|
---|
286 | }
|
---|
287 | }
|
---|