Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.ParameterOptimization/3.3/ParameterOptimizationProblem.cs

Last change on this file was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 8.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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.Linq;
24using HeuristicLab.Analysis;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.RealVectorEncoding;
29using HeuristicLab.Optimization;
30using HeuristicLab.Optimization.Operators;
31using HeuristicLab.Parameters;
32using HEAL.Attic;
33using HeuristicLab.PluginInfrastructure;
34
35namespace HeuristicLab.Problems.ParameterOptimization {
36  [Item("Parameter Optimization Problem", "A base class for other problems for the optimization of a parameter vector.")]
37  [StorableType("B1F529FE-483C-4EF2-9306-2F6A0833EEAC")]
38  public abstract class ParameterOptimizationProblem : SingleObjectiveHeuristicOptimizationProblem<IParameterVectorEvaluator, IRealVectorCreator>, IStorableContent {
39    public string Filename { get; set; }
40    private const string ProblemSizeParameterName = "ProblemSize";
41    private const string BoundsParameterName = "Bounds";
42    private const string ParameterNamesParameterName = "ParameterNames";
43
44
45    #region parameters
46    public IFixedValueParameter<IntValue> ProblemSizeParameter {
47      get { return (IFixedValueParameter<IntValue>)Parameters[ProblemSizeParameterName]; }
48    }
49    public IValueParameter<DoubleMatrix> BoundsParameter {
50      get { return (IValueParameter<DoubleMatrix>)Parameters[BoundsParameterName]; }
51    }
52    public IValueParameter<StringArray> ParameterNamesParameter {
53      get { return (IValueParameter<StringArray>)Parameters[ParameterNamesParameterName]; }
54    }
55    #endregion
56
57    #region properties
58    public int ProblemSize {
59      get { return ProblemSizeParameter.Value.Value; }
60      set { ProblemSizeParameter.Value.Value = value; }
61    }
62    public DoubleMatrix Bounds {
63      get { return BoundsParameter.Value; }
64      set { BoundsParameter.Value = value; }
65    }
66    public StringArray ParameterNames {
67      get { return ParameterNamesParameter.Value; }
68      set { ParameterNamesParameter.Value = value; }
69    }
70    #endregion
71
72
73    [Storable]
74    protected StdDevStrategyVectorCreator strategyVectorCreator;
75    [Storable]
76    protected StdDevStrategyVectorCrossover strategyVectorCrossover;
77    [Storable]
78    protected StdDevStrategyVectorManipulator strategyVectorManipulator;
79
80    [StorableConstructor]
81    protected ParameterOptimizationProblem(StorableConstructorFlag _) : base(_) { }
82    protected ParameterOptimizationProblem(ParameterOptimizationProblem original, Cloner cloner)
83      : base(original, cloner) {
84      strategyVectorCreator = cloner.Clone(original.strategyVectorCreator);
85      strategyVectorCrossover = cloner.Clone(original.strategyVectorCrossover);
86      strategyVectorManipulator = cloner.Clone(original.strategyVectorManipulator);
87      RegisterEventHandlers();
88    }
89
90    [StorableHook(HookType.AfterDeserialization)]
91    private void AfterDeserialization() {
92      RegisterEventHandlers();
93    }
94
95    protected ParameterOptimizationProblem(IParameterVectorEvaluator evaluator)
96      : base(evaluator, new UniformRandomRealVectorCreator()) {
97      Parameters.Add(new FixedValueParameter<IntValue>(ProblemSizeParameterName, "The dimension of the parameter vector that is to be optimized.", new IntValue(1)));
98      Parameters.Add(new ValueParameter<DoubleMatrix>(BoundsParameterName, "The bounds for each dimension of the parameter vector. If the number of bounds is smaller than the problem size then the bounds are reused in a cyclic manner.", new DoubleMatrix(new double[,] { { 0, 100 } }, new string[] { "LowerBound", "UpperBound" })));
99      Parameters.Add(new ValueParameter<StringArray>(ParameterNamesParameterName, "The element names which are used to calculate the quality of a parameter vector.", new StringArray(new string[] { "Parameter0" })));
100
101      SolutionCreator.LengthParameter.ActualName = "ProblemSize";
102
103      Operators.AddRange(ApplicationManager.Manager.GetInstances<IRealVectorOperator>());
104
105      strategyVectorCreator = new StdDevStrategyVectorCreator();
106      strategyVectorCreator.LengthParameter.ActualName = ProblemSizeParameter.Name;
107      strategyVectorCrossover = new StdDevStrategyVectorCrossover();
108      strategyVectorManipulator = new StdDevStrategyVectorManipulator();
109      strategyVectorManipulator.LearningRateParameter.Value = new DoubleValue(0.5);
110      strategyVectorManipulator.GeneralLearningRateParameter.Value = new DoubleValue(0.5);
111
112      Operators.Add(strategyVectorCreator);
113      Operators.Add(strategyVectorCrossover);
114      Operators.Add(strategyVectorManipulator);
115      Operators.Add(new BestSolutionAnalyzer());
116      Operators.Add(new BestSolutionsAnalyzer());
117
118      Operators.Add(new HammingSimilarityCalculator());
119      Operators.Add(new EuclideanSimilarityCalculator());
120      Operators.Add(new QualitySimilarityCalculator());
121      Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
122
123      UpdateParameters();
124      UpdateStrategyVectorBounds();
125
126      RegisterEventHandlers();
127    }
128
129    protected override void OnEvaluatorChanged() {
130      base.OnEvaluatorChanged();
131      UpdateParameters();
132    }
133
134    private void RegisterEventHandlers() {
135      Bounds.ToStringChanged += Bounds_ToStringChanged;
136      ProblemSizeParameter.Value.ValueChanged += ProblemSize_Changed;
137      ParameterNames.Reset += ParameterNames_Reset;
138    }
139
140    private void UpdateParameters() {
141      Evaluator.ParameterVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
142      Evaluator.ParameterNamesParameter.ActualName = ParameterNamesParameter.Name;
143
144      foreach (var bestSolutionAnalyzer in Operators.OfType<BestSolutionAnalyzer>()) {
145        bestSolutionAnalyzer.ParameterVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
146        bestSolutionAnalyzer.ParameterNamesParameter.ActualName = ParameterNamesParameter.Name;
147      }
148      Bounds = new DoubleMatrix(ProblemSize, 2);
149      Bounds.RowNames = ParameterNames;
150      for (int i = 0; i < Bounds.Rows; i++) {
151        Bounds[i, 0] = 0.0;
152        Bounds[i, 1] = 100.0;
153      }
154
155      foreach (var op in Operators.OfType<IRealVectorManipulator>())
156        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
157
158      foreach (var similarityCalculator in Operators.OfType<ISolutionSimilarityCalculator>()) {
159        similarityCalculator.SolutionVariableName = SolutionCreator.RealVectorParameter.ActualName;
160        similarityCalculator.QualityVariableName = Evaluator.QualityParameter.ActualName;
161      }
162    }
163
164    private void Bounds_ToStringChanged(object sender, EventArgs e) {
165      if (Bounds.Columns != 2 || Bounds.Rows < 1)
166        Bounds = new DoubleMatrix(1, 2);
167      UpdateStrategyVectorBounds();
168    }
169    protected virtual void UpdateStrategyVectorBounds() {
170      DoubleMatrix strategyBounds = (DoubleMatrix)Bounds.Clone();
171      for (int i = 0; i < strategyBounds.Rows; i++) {
172        if (strategyBounds[i, 0] < 0) strategyBounds[i, 0] = 0;
173        strategyBounds[i, 1] = 0.1 * (Bounds[i, 1] - Bounds[i, 0]);
174      }
175      strategyVectorCreator.BoundsParameter.Value = strategyBounds;
176    }
177
178    protected virtual void ProblemSize_Changed(object sender, EventArgs e) {
179      if (ParameterNames.Length != ProblemSize)
180        ((IStringConvertibleArray)ParameterNames).Length = ProblemSize;
181      for (int i = 0; i < ParameterNames.Length; i++) {
182        if (string.IsNullOrEmpty(ParameterNames[i])) ParameterNames[i] = "Parameter" + i;
183      }
184
185      strategyVectorManipulator.GeneralLearningRateParameter.Value = new DoubleValue(1.0 / Math.Sqrt(2 * ProblemSize));
186      strategyVectorManipulator.LearningRateParameter.Value = new DoubleValue(1.0 / Math.Sqrt(2 * Math.Sqrt(ProblemSize)));
187    }
188
189    protected virtual void ParameterNames_Reset(object sender, EventArgs e) {
190      ProblemSize = ParameterNames.Length;
191    }
192  }
193}
Note: See TracBrowser for help on using the repository browser.