Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.ParameterOptimization/3.3/ParameterOptimizationProblem.cs @ 10635

Last change on this file since 10635 was 10594, checked in by mkommend, 11 years ago

#2082: Updated copyright and used Environment.NewLine instead of \r\n.

File size: 7.9 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 fewer bounds are", 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      strategyVectorManipulator.GeneralLearningRateParameter.Value = new DoubleValue(1.0 / Math.Sqrt(2 * ProblemSize));
124      strategyVectorManipulator.LearningRateParameter.Value = new DoubleValue(1.0 / Math.Sqrt(2 * Math.Sqrt(ProblemSize)));
125      UpdateParameters();
126    }
127
128    private void RegisterEventHandlers() {
129      Bounds.ToStringChanged += Bounds_ToStringChanged;
130      ProblemSizeParameter.Value.ValueChanged += ProblemSize_Changed;
131      ParameterNames.Reset += ParameterNames_Reset;
132    }
133
134    private void UpdateParameters() {
135      Evaluator.ParameterVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
136      Evaluator.ParameterNamesParameter.ActualName = ParameterNamesParameter.Name;
137
138      var bestSolutionAnalyzer = Operators.OfType<BestSolutionAnalyzer>().First();
139      bestSolutionAnalyzer.ParameterVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
140      bestSolutionAnalyzer.ParameterNamesParameter.ActualName = ParameterNamesParameter.Name;
141
142      Bounds = new DoubleMatrix(ProblemSize, 2);
143      Bounds.RowNames = ParameterNames;
144      for (int i = 0; i < Bounds.Rows; i++) {
145        Bounds[i, 0] = 0.0;
146        Bounds[i, 1] = 100.0;
147      }
148
149      foreach (var op in Operators.OfType<IRealVectorManipulator>())
150        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
151    }
152
153    private void Bounds_ToStringChanged(object sender, EventArgs e) {
154      if (Bounds.Columns != 2 || Bounds.Rows < 1)
155        Bounds = new DoubleMatrix(1, 2);
156      UpdateStrategyVectorBounds();
157    }
158    protected virtual void UpdateStrategyVectorBounds() {
159      DoubleMatrix strategyBounds = (DoubleMatrix)Bounds.Clone();
160      for (int i = 0; i < strategyBounds.Rows; i++) {
161        if (strategyBounds[i, 0] < 0) strategyBounds[i, 0] = 0;
162        strategyBounds[i, 1] = 0.1 * (Bounds[i, 1] - Bounds[i, 0]);
163      }
164      strategyVectorCreator.BoundsParameter.Value = strategyBounds;
165    }
166
167    protected virtual void ProblemSize_Changed(object sender, EventArgs e) {
168      if (ParameterNames.Length != ProblemSize)
169        ((IStringConvertibleArray)ParameterNames).Length = ProblemSize;
170      for (int i = 0; i < ParameterNames.Length; i++) {
171        if (string.IsNullOrEmpty(ParameterNames[i])) ParameterNames[i] = "Parameter" + i;
172      }
173    }
174
175    protected virtual void ParameterNames_Reset(object sender, EventArgs e) {
176      ProblemSize = ParameterNames.Length;
177    }
178  }
179}
Note: See TracBrowser for help on using the repository browser.