[4516] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2010 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;
|
---|
[4839] | 24 | using HeuristicLab.Common;
|
---|
[4516] | 25 | using HeuristicLab.Core;
|
---|
[4839] | 26 | using HeuristicLab.Data;
|
---|
[4516] | 27 | using HeuristicLab.Optimization;
|
---|
| 28 | using HeuristicLab.Parameters;
|
---|
[4839] | 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[4516] | 30 | using HeuristicLab.PluginInfrastructure;
|
---|
[5110] | 31 | using HeuristicLab.Algorithms.GeneticAlgorithm;
|
---|
| 32 | using HeuristicLab.Problems.TestFunctions;
|
---|
[4516] | 33 |
|
---|
| 34 | namespace HeuristicLab.Problems.MetaOptimization {
|
---|
| 35 | [Item("Meta Optimization Problem", "Represents a Meta Optimization Problem.")]
|
---|
| 36 | [Creatable("Problems")]
|
---|
| 37 | [StorableClass]
|
---|
[5111] | 38 | public sealed class MetaOptimizationProblem : SingleObjectiveProblem<IParameterConfigurationEvaluator, IParameterConfigurationCreator> {
|
---|
[5110] | 39 | public const string AlgorithmTypeParameterName = "AlgorithmType";
|
---|
| 40 | public const string ProblemTypeParameterName = "ProblemType";
|
---|
| 41 | public const string ProblemsParameterName = "Problems";
|
---|
[5184] | 42 | public const string ParameterConfigurationTreeParameterName = "InitialParameterConfigurationTree";
|
---|
[5110] | 43 | public const string RepetitionsParameterName = "Repetitions";
|
---|
[4516] | 44 |
|
---|
[5111] | 45 | public const string IntValueManipulatorParameterName = "IntValueManipulator";
|
---|
| 46 | public const string DoubleValueManipulatorParameterName = "DoubleValueManipulator";
|
---|
| 47 | public const string IntValueCrossoverParameterName = "IntValueCrossover";
|
---|
| 48 | public const string DoubleValueCrossoverParameterName = "DoubleValueCrossover";
|
---|
| 49 |
|
---|
[4516] | 50 | #region Parameter Properties
|
---|
[5110] | 51 | public IValueParameter<EngineAlgorithm> AlgorithmTypeParameter {
|
---|
| 52 | get { return (ValueParameter<EngineAlgorithm>)Parameters[AlgorithmTypeParameterName]; }
|
---|
[4516] | 53 | }
|
---|
[5110] | 54 | public IValueParameter<ISingleObjectiveProblem> ProblemTypeParameter {
|
---|
| 55 | get { return (ValueParameter<ISingleObjectiveProblem>)Parameters[ProblemTypeParameterName]; }
|
---|
[4516] | 56 | }
|
---|
[5110] | 57 | public IValueParameter<ConstrainedItemList<ISingleObjectiveProblem>> ProblemsParameter {
|
---|
| 58 | get { return (ValueParameter<ConstrainedItemList<ISingleObjectiveProblem>>)Parameters[ProblemsParameterName]; }
|
---|
[4516] | 59 | }
|
---|
[5144] | 60 | public IValueParameter<ParameterConfigurationTree> ParameterConfigurationTreeParameter {
|
---|
| 61 | get { return (ValueParameter<ParameterConfigurationTree>)Parameters[ParameterConfigurationTreeParameterName]; }
|
---|
[5110] | 62 | }
|
---|
| 63 | public IValueParameter<IntValue> RepetitionsParameter {
|
---|
| 64 | get { return (ValueParameter<IntValue>)Parameters[RepetitionsParameterName]; }
|
---|
| 65 | }
|
---|
[5111] | 66 |
|
---|
| 67 | public IValueParameter<IIntValueManipulator> IntValueManipulatorParameter {
|
---|
| 68 | get { return (ValueParameter<IIntValueManipulator>)Parameters[IntValueManipulatorParameterName]; }
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | public IValueParameter<IDoubleValueManipulator> DoubleValueManipulatorParameter {
|
---|
| 72 | get { return (ValueParameter<IDoubleValueManipulator>)Parameters[DoubleValueManipulatorParameterName]; }
|
---|
| 73 | }
|
---|
[4516] | 74 | #endregion
|
---|
| 75 |
|
---|
| 76 | #region Properties
|
---|
[5009] | 77 | public EngineAlgorithm Algorithm {
|
---|
[5110] | 78 | get { return AlgorithmTypeParameter.Value; }
|
---|
| 79 | set { AlgorithmTypeParameter.Value = value; }
|
---|
[4516] | 80 | }
|
---|
[5110] | 81 | public ISingleObjectiveProblem Problem {
|
---|
| 82 | get { return ProblemTypeParameter.Value; }
|
---|
| 83 | set { ProblemTypeParameter.Value = value; }
|
---|
| 84 | }
|
---|
| 85 | public ConstrainedItemList<ISingleObjectiveProblem> Problems {
|
---|
[4830] | 86 | get { return ProblemsParameter.Value; }
|
---|
| 87 | set { ProblemsParameter.Value = value; }
|
---|
[4516] | 88 | }
|
---|
[5144] | 89 | public ParameterConfigurationTree ParameterConfigurationTree {
|
---|
| 90 | get { return ParameterConfigurationTreeParameter.Value; }
|
---|
| 91 | set { ParameterConfigurationTreeParameter.Value = value; }
|
---|
[4516] | 92 | }
|
---|
[5110] | 93 | public IntValue Repetitions {
|
---|
| 94 | get { return RepetitionsParameter.Value; }
|
---|
| 95 | set { RepetitionsParameter.Value = value; }
|
---|
| 96 | }
|
---|
[5184] | 97 | private BestParameterConfigurationAnalyzer BestParameterConfigurationAnalyzer {
|
---|
| 98 | get { return Operators.OfType<BestParameterConfigurationAnalyzer>().FirstOrDefault(); }
|
---|
| 99 | }
|
---|
[4516] | 100 | #endregion
|
---|
| 101 |
|
---|
[5009] | 102 | public MetaOptimizationProblem() : base() {
|
---|
[5110] | 103 | Parameters.Add(new ValueParameter<EngineAlgorithm>(AlgorithmTypeParameterName, "The algorithm which's parameters should be optimized.", new GeneticAlgorithm()));
|
---|
| 104 | Parameters.Add(new ValueParameter<ISingleObjectiveProblem>(ProblemTypeParameterName, "The problem type.", new SingleObjectiveTestFunctionProblem()));
|
---|
| 105 | Parameters.Add(new ValueParameter<ConstrainedItemList<ISingleObjectiveProblem>>(ProblemsParameterName, "The problems that should be evaluated.", new ConstrainedItemList<ISingleObjectiveProblem>()));
|
---|
[5184] | 106 | Parameters.Add(new ValueParameter<ParameterConfigurationTree>(ParameterConfigurationTreeParameterName, "Tree of algorithm parameters that should be optimized."));
|
---|
[5110] | 107 | Parameters.Add(new ValueParameter<IntValue>(RepetitionsParameterName, "The number of evaluations for each problem.", new IntValue(3)));
|
---|
| 108 |
|
---|
[5111] | 109 | var validIntManipulators = new ItemSet<IIntValueManipulator>( ApplicationManager.Manager.GetInstances<IIntValueManipulator>());
|
---|
| 110 | var validDoubleManipulators = new ItemSet<IDoubleValueManipulator>(ApplicationManager.Manager.GetInstances<IDoubleValueManipulator>());
|
---|
[5212] | 111 | Parameters.Add(new ConstrainedValueParameter<IIntValueManipulator>(IntValueManipulatorParameterName, "", validIntManipulators, validIntManipulators.First()));
|
---|
| 112 | Parameters.Add(new ConstrainedValueParameter<IDoubleValueManipulator>(DoubleValueManipulatorParameterName, "", validDoubleManipulators, validDoubleManipulators.First()));
|
---|
[5111] | 113 |
|
---|
[4830] | 114 | Maximization = new BoolValue(false);
|
---|
| 115 | SolutionCreator = new RandomParameterConfigurationCreator();
|
---|
[5111] | 116 | Evaluator = new ParameterConfigurationEvaluator();
|
---|
[4516] | 117 |
|
---|
[4830] | 118 | InitializeOperators();
|
---|
| 119 | RegisterParameterEvents();
|
---|
[5184] | 120 | ParameterizeAnalyzer();
|
---|
[4516] | 121 | ParameterizeSolutionCreator();
|
---|
| 122 | ParameterizeEvaluator();
|
---|
[4830] | 123 | ParameterizeOperators();
|
---|
[5110] | 124 |
|
---|
| 125 | Problems.Type = Problem.GetType();
|
---|
| 126 | Algorithm.Problem = Problem;
|
---|
[5144] | 127 | ParameterConfigurationTreeParameter.ActualValue = new ParameterConfigurationTree(Algorithm);
|
---|
[4516] | 128 | }
|
---|
| 129 |
|
---|
[4830] | 130 | [StorableConstructor]
|
---|
| 131 | private MetaOptimizationProblem(bool deserializing) : base(deserializing) { }
|
---|
| 132 | private MetaOptimizationProblem(MetaOptimizationProblem original, Cloner cloner) : base(original, cloner) {
|
---|
[4516] | 133 | // todo
|
---|
[4830] | 134 | this.RegisterParameterEvents();
|
---|
[4516] | 135 | }
|
---|
[4830] | 136 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 137 | return new MetaOptimizationProblem(this, cloner);
|
---|
| 138 | }
|
---|
[4516] | 139 |
|
---|
| 140 | #region Helpers
|
---|
| 141 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 142 | private void AfterDeserializationHook() {
|
---|
[4830] | 143 | RegisterParameterEvents();
|
---|
[4516] | 144 | }
|
---|
[4830] | 145 | private void RegisterParameterEvents() {
|
---|
[4516] | 146 | SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
|
---|
| 147 | EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
|
---|
| 148 | Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
[5110] | 149 | AlgorithmTypeParameter.ValueChanged += new EventHandler(AlgorithmTypeParameter_ValueChanged);
|
---|
| 150 | ProblemTypeParameter.ValueChanged += new EventHandler(ProblemTypeParameter_ValueChanged);
|
---|
[4516] | 151 | }
|
---|
[5110] | 152 |
|
---|
[4516] | 153 | private void InitializeOperators() {
|
---|
[4830] | 154 | Operators.AddRange(ApplicationManager.Manager.GetInstances<IParameterConfigurationOperator>().Cast<IOperator>());
|
---|
| 155 | Operators.Add(new BestParameterConfigurationAnalyzer());
|
---|
[4516] | 156 | }
|
---|
| 157 | private void ParameterizeSolutionCreator() {
|
---|
| 158 | }
|
---|
| 159 | private void ParameterizeEvaluator() {
|
---|
[5111] | 160 | ((ParameterConfigurationEvaluator)Evaluator).ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
|
---|
[4516] | 161 | }
|
---|
| 162 | private void ParameterizeAnalyzer() {
|
---|
[5184] | 163 | if (BestParameterConfigurationAnalyzer != null) {
|
---|
| 164 | BestParameterConfigurationAnalyzer.ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
|
---|
| 165 | }
|
---|
[4516] | 166 | }
|
---|
| 167 | private void ParameterizeOperators() {
|
---|
[5184] | 168 | foreach (IParameterConfigurationCrossover op in Operators.OfType<IParameterConfigurationCrossover>()) {
|
---|
| 169 | op.ParentsParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
|
---|
| 170 | op.ChildParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
|
---|
| 171 | }
|
---|
| 172 | foreach (IParameterConfigurationManipulator op in Operators.OfType<IParameterConfigurationManipulator>()) {
|
---|
| 173 | op.ParameterConfigurationTreeParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
|
---|
| 174 | }
|
---|
[4516] | 175 | }
|
---|
| 176 |
|
---|
| 177 | #endregion
|
---|
| 178 |
|
---|
| 179 | #region Events
|
---|
| 180 |
|
---|
| 181 | private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 182 | ParameterizeSolutionCreator();
|
---|
| 183 | ParameterizeEvaluator();
|
---|
| 184 | ParameterizeAnalyzer();
|
---|
| 185 | ParameterizeOperators();
|
---|
| 186 | OnSolutionCreatorChanged();
|
---|
| 187 | }
|
---|
| 188 | private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 189 | Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
| 190 | ParameterizeEvaluator();
|
---|
| 191 | ParameterizeAnalyzer();
|
---|
| 192 | OnEvaluatorChanged();
|
---|
| 193 | }
|
---|
| 194 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
| 195 | ParameterizeAnalyzer();
|
---|
| 196 | }
|
---|
[5110] | 197 | void AlgorithmTypeParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 198 | Algorithm.Problem = Problem;
|
---|
[5144] | 199 | ParameterConfigurationTreeParameter.ActualValue = new ParameterConfigurationTree(Algorithm);
|
---|
[4516] | 200 | }
|
---|
[5110] | 201 | void ProblemTypeParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 202 | Problems.Type = Problem.GetType();
|
---|
| 203 | Algorithm.Problem = Problem;
|
---|
[5144] | 204 | ParameterConfigurationTreeParameter.ActualValue = new ParameterConfigurationTree(Algorithm);
|
---|
[4516] | 205 | }
|
---|
| 206 | #endregion
|
---|
| 207 | }
|
---|
| 208 | }
|
---|