[9682] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2013 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.Linq;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
| 28 | using HeuristicLab.Optimization;
|
---|
| 29 | using HeuristicLab.Parameters;
|
---|
| 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 31 | using HeuristicLab.PluginInfrastructure;
|
---|
| 32 |
|
---|
| 33 | namespace 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 names which are used within the SciLab script 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 | UpdateParameters();
|
---|
| 115 | UpdateStrategyVectorBounds();
|
---|
| 116 |
|
---|
| 117 | RegisterEventHandlers();
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | protected override void OnEvaluatorChanged() {
|
---|
| 121 | base.OnEvaluatorChanged();
|
---|
| 122 | strategyVectorManipulator.GeneralLearningRateParameter.Value = new DoubleValue(1.0 / Math.Sqrt(2 * ProblemSize));
|
---|
| 123 | strategyVectorManipulator.LearningRateParameter.Value = new DoubleValue(1.0 / Math.Sqrt(2 * Math.Sqrt(ProblemSize)));
|
---|
| 124 | UpdateParameters();
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | private void RegisterEventHandlers() {
|
---|
| 128 | Bounds.ToStringChanged += Bounds_ToStringChanged;
|
---|
| 129 | ProblemSizeParameter.Value.ValueChanged += ProblemSize_Changed;
|
---|
[9698] | 130 | ParameterNames.Reset += ParameterNames_Reset;
|
---|
[9682] | 131 | }
|
---|
| 132 |
|
---|
| 133 | private void UpdateParameters() {
|
---|
| 134 | Evaluator.ParameterVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
|
---|
| 135 | Evaluator.ParameterNamesParameter.ActualName = ParameterNamesParameter.Name;
|
---|
| 136 |
|
---|
| 137 | var bestSolutionAnalyzer = Operators.OfType<BestSolutionAnalyzer>().First();
|
---|
| 138 | bestSolutionAnalyzer.ParameterVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
|
---|
| 139 | bestSolutionAnalyzer.ParameterNamesParameter.ActualName = ParameterNamesParameter.Name;
|
---|
| 140 |
|
---|
| 141 | Bounds = new DoubleMatrix(ProblemSize, 2);
|
---|
| 142 | Bounds.RowNames = ParameterNames;
|
---|
| 143 | for (int i = 0; i < Bounds.Rows; i++) {
|
---|
| 144 | Bounds[i, 0] = 0.0;
|
---|
| 145 | Bounds[i, 1] = 100.0;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | foreach (var op in Operators.OfType<IRealVectorManipulator>())
|
---|
| 149 | op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | private void Bounds_ToStringChanged(object sender, EventArgs e) {
|
---|
| 153 | if (Bounds.Columns != 2 || Bounds.Rows < 1)
|
---|
| 154 | Bounds = new DoubleMatrix(1, 2);
|
---|
| 155 | UpdateStrategyVectorBounds();
|
---|
| 156 | }
|
---|
[9698] | 157 | protected virtual void UpdateStrategyVectorBounds() {
|
---|
[9682] | 158 | DoubleMatrix strategyBounds = (DoubleMatrix)Bounds.Clone();
|
---|
| 159 | for (int i = 0; i < strategyBounds.Rows; i++) {
|
---|
| 160 | if (strategyBounds[i, 0] < 0) strategyBounds[i, 0] = 0;
|
---|
| 161 | strategyBounds[i, 1] = 0.1 * (Bounds[i, 1] - Bounds[i, 0]);
|
---|
| 162 | }
|
---|
| 163 | strategyVectorCreator.BoundsParameter.Value = strategyBounds;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
[9698] | 166 | protected virtual void ProblemSize_Changed(object sender, EventArgs e) {
|
---|
[9682] | 167 | if (ParameterNames.Length != ProblemSize)
|
---|
| 168 | ((IStringConvertibleArray)ParameterNames).Length = ProblemSize;
|
---|
| 169 | for (int i = 0; i < ParameterNames.Length; i++) {
|
---|
| 170 | if (string.IsNullOrEmpty(ParameterNames[i])) ParameterNames[i] = "Parameter" + i;
|
---|
| 171 | }
|
---|
| 172 | }
|
---|
[9698] | 173 |
|
---|
| 174 | protected virtual void ParameterNames_Reset(object sender, EventArgs e) {
|
---|
| 175 | ProblemSize = ParameterNames.Length;
|
---|
| 176 | }
|
---|
[9682] | 177 | }
|
---|
| 178 | }
|
---|