Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11299 was 11151, checked in by mkommend, 10 years ago

#2171: Merged r10601:r10607, r10653, r11075, r11076, r11077, r11080, r11081 into stable.

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