Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.ParameterOptimization/3.3/ParameterOptimizationProblem.cs @ 17695

Last change on this file since 17695 was 17695, checked in by abeham, 4 years ago

#2521:

  • Moving solution creator parameter from problems to algorithms (breaking wiring in some HeuristicOptimizationProblems)
  • Disallowing evaluator or encoding changes in encoding-specific base problems (to avoid confusion in derived problems whether this needs to be handled or not)
  • Added private set to ReferenceParameter property (serialization)
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 HEAL.Attic;
25using HeuristicLab.Analysis;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Encodings.RealVectorEncoding;
30using HeuristicLab.Optimization;
31using HeuristicLab.Optimization.Operators;
32using HeuristicLab.Parameters;
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>, 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) {
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      // TODO
102      //SolutionCreator.LengthParameter.ActualName = "ProblemSize";
103
104      Operators.AddRange(ApplicationManager.Manager.GetInstances<IRealVectorOperator>());
105
106      strategyVectorCreator = new StdDevStrategyVectorCreator();
107      strategyVectorCreator.LengthParameter.ActualName = ProblemSizeParameter.Name;
108      strategyVectorCrossover = new StdDevStrategyVectorCrossover();
109      strategyVectorManipulator = new StdDevStrategyVectorManipulator();
110      strategyVectorManipulator.LearningRateParameter.Value = new DoubleValue(0.5);
111      strategyVectorManipulator.GeneralLearningRateParameter.Value = new DoubleValue(0.5);
112
113      Operators.Add(strategyVectorCreator);
114      Operators.Add(strategyVectorCrossover);
115      Operators.Add(strategyVectorManipulator);
116      Operators.Add(new BestSolutionAnalyzer());
117      Operators.Add(new BestSolutionsAnalyzer());
118
119      Operators.Add(new HammingSimilarityCalculator());
120      Operators.Add(new EuclideanSimilarityCalculator());
121      Operators.Add(new QualitySimilarityCalculator());
122      Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
123
124      UpdateParameters();
125      UpdateStrategyVectorBounds();
126
127      RegisterEventHandlers();
128    }
129
130    protected override void OnEvaluatorChanged() {
131      base.OnEvaluatorChanged();
132      UpdateParameters();
133    }
134
135    private void RegisterEventHandlers() {
136      Bounds.ToStringChanged += Bounds_ToStringChanged;
137      ProblemSizeParameter.Value.ValueChanged += ProblemSize_Changed;
138      ParameterNames.Reset += ParameterNames_Reset;
139    }
140
141    private void UpdateParameters() {
142      // TODO: This method wired solution creator -> should be done by encoding
143      //Evaluator.ParameterVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
144      Evaluator.ParameterNamesParameter.ActualName = ParameterNamesParameter.Name;
145
146      foreach (var bestSolutionAnalyzer in Operators.OfType<BestSolutionAnalyzer>()) {
147        //bestSolutionAnalyzer.ParameterVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
148        bestSolutionAnalyzer.ParameterNamesParameter.ActualName = ParameterNamesParameter.Name;
149      }
150      Bounds = new DoubleMatrix(ProblemSize, 2);
151      Bounds.RowNames = ParameterNames;
152      for (int i = 0; i < Bounds.Rows; i++) {
153        Bounds[i, 0] = 0.0;
154        Bounds[i, 1] = 100.0;
155      }
156      /* TODO
157      foreach (var op in Operators.OfType<IRealVectorManipulator>())
158        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
159
160      foreach (var similarityCalculator in Operators.OfType<ISolutionSimilarityCalculator>()) {
161        similarityCalculator.SolutionVariableName = SolutionCreator.RealVectorParameter.ActualName;
162        similarityCalculator.QualityVariableName = Evaluator.QualityParameter.ActualName;
163      }
164      */
165    }
166
167    private void Bounds_ToStringChanged(object sender, EventArgs e) {
168      if (Bounds.Columns != 2 || Bounds.Rows < 1)
169        Bounds = new DoubleMatrix(1, 2);
170      UpdateStrategyVectorBounds();
171    }
172    protected virtual void UpdateStrategyVectorBounds() {
173      DoubleMatrix strategyBounds = (DoubleMatrix)Bounds.Clone();
174      for (int i = 0; i < strategyBounds.Rows; i++) {
175        if (strategyBounds[i, 0] < 0) strategyBounds[i, 0] = 0;
176        strategyBounds[i, 1] = 0.1 * (Bounds[i, 1] - Bounds[i, 0]);
177      }
178      strategyVectorCreator.BoundsParameter.Value = strategyBounds;
179    }
180
181    protected virtual void ProblemSize_Changed(object sender, EventArgs e) {
182      if (ParameterNames.Length != ProblemSize)
183        ((IStringConvertibleArray)ParameterNames).Length = ProblemSize;
184      for (int i = 0; i < ParameterNames.Length; i++) {
185        if (string.IsNullOrEmpty(ParameterNames[i])) ParameterNames[i] = "Parameter" + i;
186      }
187
188      strategyVectorManipulator.GeneralLearningRateParameter.Value = new DoubleValue(1.0 / Math.Sqrt(2 * ProblemSize));
189      strategyVectorManipulator.LearningRateParameter.Value = new DoubleValue(1.0 / Math.Sqrt(2 * Math.Sqrt(ProblemSize)));
190    }
191
192    protected virtual void ParameterNames_Reset(object sender, EventArgs e) {
193      ProblemSize = ParameterNames.Length;
194    }
195  }
196}
Note: See TracBrowser for help on using the repository browser.