[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";
|
---|
[5328] | 42 | public const string ParameterConfigurationTreeParameterName = "ParameterConfiguration";
|
---|
[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
|
---|
[5337] | 51 | public IValueParameter<ConstrainedTypeValue<IAlgorithm>> AlgorithmTypeParameter {
|
---|
| 52 | get { return (ValueParameter<ConstrainedTypeValue<IAlgorithm>>)Parameters[AlgorithmTypeParameterName]; }
|
---|
[4516] | 53 | }
|
---|
[5337] | 54 | public IValueParameter<ConstrainedTypeValue<IProblem>> ProblemTypeParameter {
|
---|
| 55 | get { return (ValueParameter<ConstrainedTypeValue<IProblem>>)Parameters[ProblemTypeParameterName]; }
|
---|
[4516] | 56 | }
|
---|
[5337] | 57 | public IValueParameter<ConstrainedItemList<IProblem>> ProblemsParameter {
|
---|
| 58 | get { return (ValueParameter<ConstrainedItemList<IProblem>>)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
|
---|
[5337] | 77 | public IAlgorithm Algorithm {
|
---|
[5313] | 78 | get { return CreateAlgorithm(AlgorithmType.Value, ProblemType.Value); }
|
---|
| 79 | }
|
---|
[5337] | 80 | public ConstrainedTypeValue<IAlgorithm> AlgorithmType {
|
---|
[5110] | 81 | get { return AlgorithmTypeParameter.Value; }
|
---|
| 82 | set { AlgorithmTypeParameter.Value = value; }
|
---|
[4516] | 83 | }
|
---|
[5337] | 84 | public ConstrainedTypeValue<IProblem> ProblemType {
|
---|
[5110] | 85 | get { return ProblemTypeParameter.Value; }
|
---|
| 86 | set { ProblemTypeParameter.Value = value; }
|
---|
| 87 | }
|
---|
[5337] | 88 | public ConstrainedItemList<IProblem> Problems {
|
---|
[4830] | 89 | get { return ProblemsParameter.Value; }
|
---|
| 90 | set { ProblemsParameter.Value = value; }
|
---|
[4516] | 91 | }
|
---|
[5144] | 92 | public ParameterConfigurationTree ParameterConfigurationTree {
|
---|
| 93 | get { return ParameterConfigurationTreeParameter.Value; }
|
---|
| 94 | set { ParameterConfigurationTreeParameter.Value = value; }
|
---|
[4516] | 95 | }
|
---|
[5110] | 96 | public IntValue Repetitions {
|
---|
| 97 | get { return RepetitionsParameter.Value; }
|
---|
| 98 | set { RepetitionsParameter.Value = value; }
|
---|
| 99 | }
|
---|
[5184] | 100 | private BestParameterConfigurationAnalyzer BestParameterConfigurationAnalyzer {
|
---|
| 101 | get { return Operators.OfType<BestParameterConfigurationAnalyzer>().FirstOrDefault(); }
|
---|
| 102 | }
|
---|
[5293] | 103 | private ReferenceQualityAnalyzer ReferenceQualityAnalyzer {
|
---|
| 104 | get { return Operators.OfType<ReferenceQualityAnalyzer>().FirstOrDefault(); }
|
---|
[5281] | 105 | }
|
---|
[5359] | 106 | private SolutionCacheAnalyzer RunsAnalyzer {
|
---|
| 107 | get { return Operators.OfType<SolutionCacheAnalyzer>().FirstOrDefault(); }
|
---|
[5303] | 108 | }
|
---|
[5522] | 109 | private PMOPopulationDiversityAnalyzer PMOPopulationDiversityAnalyzer {
|
---|
| 110 | get { return Operators.OfType<PMOPopulationDiversityAnalyzer>().FirstOrDefault(); }
|
---|
| 111 | }
|
---|
[4516] | 112 | #endregion
|
---|
| 113 |
|
---|
[5303] | 114 | public MetaOptimizationProblem()
|
---|
| 115 | : base() {
|
---|
[5337] | 116 | Parameters.Add(new ValueParameter<ConstrainedTypeValue<IAlgorithm>>(AlgorithmTypeParameterName, "The algorithm which's parameters should be optimized.", new ConstrainedTypeValue<IAlgorithm>(typeof(GeneticAlgorithm))));
|
---|
| 117 | Parameters.Add(new ValueParameter<ConstrainedTypeValue<IProblem>>(ProblemTypeParameterName, "The problem type.", new ConstrainedTypeValue<IProblem>()));
|
---|
| 118 | Parameters.Add(new ValueParameter<ConstrainedItemList<IProblem>>(ProblemsParameterName, "The problems that should be evaluated.", new ConstrainedItemList<IProblem>()));
|
---|
[5184] | 119 | Parameters.Add(new ValueParameter<ParameterConfigurationTree>(ParameterConfigurationTreeParameterName, "Tree of algorithm parameters that should be optimized."));
|
---|
[5110] | 120 | Parameters.Add(new ValueParameter<IntValue>(RepetitionsParameterName, "The number of evaluations for each problem.", new IntValue(3)));
|
---|
| 121 |
|
---|
[5303] | 122 | var validIntManipulators = new ItemSet<IIntValueManipulator>(ApplicationManager.Manager.GetInstances<IIntValueManipulator>());
|
---|
[5111] | 123 | var validDoubleManipulators = new ItemSet<IDoubleValueManipulator>(ApplicationManager.Manager.GetInstances<IDoubleValueManipulator>());
|
---|
[5277] | 124 | var validIntCrossovers = new ItemSet<IIntValueCrossover>(ApplicationManager.Manager.GetInstances<IIntValueCrossover>());
|
---|
| 125 | var validDoubleCrossovers = new ItemSet<IDoubleValueCrossover>(ApplicationManager.Manager.GetInstances<IDoubleValueCrossover>());
|
---|
[5293] | 126 | Parameters.Add(new ConstrainedValueParameter<IIntValueManipulator>(IntValueManipulatorParameterName, "", validIntManipulators, validIntManipulators.Where(x => x.GetType() == typeof(NormalIntValueManipulator)).SingleOrDefault()));
|
---|
| 127 | Parameters.Add(new ConstrainedValueParameter<IDoubleValueManipulator>(DoubleValueManipulatorParameterName, "", validDoubleManipulators, validDoubleManipulators.Where(x => x.GetType() == typeof(NormalDoubleValueManipulator)).SingleOrDefault()));
|
---|
| 128 | Parameters.Add(new ConstrainedValueParameter<IIntValueCrossover>(IntValueCrossoverParameterName, "", validIntCrossovers, validIntCrossovers.Where(x => x.GetType() == typeof(NormalIntValueCrossover)).SingleOrDefault()));
|
---|
| 129 | Parameters.Add(new ConstrainedValueParameter<IDoubleValueCrossover>(DoubleValueCrossoverParameterName, "", validDoubleCrossovers, validDoubleCrossovers.Where(x => x.GetType() == typeof(NormalDoubleValueCrossover)).SingleOrDefault()));
|
---|
[5111] | 130 |
|
---|
[4830] | 131 | Maximization = new BoolValue(false);
|
---|
| 132 | SolutionCreator = new RandomParameterConfigurationCreator();
|
---|
[5111] | 133 | Evaluator = new ParameterConfigurationEvaluator();
|
---|
[4516] | 134 |
|
---|
[4830] | 135 | InitializeOperators();
|
---|
| 136 | RegisterParameterEvents();
|
---|
[5184] | 137 | ParameterizeAnalyzer();
|
---|
[4516] | 138 | ParameterizeSolutionCreator();
|
---|
| 139 | ParameterizeEvaluator();
|
---|
[4830] | 140 | ParameterizeOperators();
|
---|
[5110] | 141 |
|
---|
[5337] | 142 | AlgorithmTypeParameter_ValueChanged(this, EventArgs.Empty);
|
---|
[4516] | 143 | }
|
---|
| 144 |
|
---|
[4830] | 145 | [StorableConstructor]
|
---|
| 146 | private MetaOptimizationProblem(bool deserializing) : base(deserializing) { }
|
---|
[5303] | 147 | private MetaOptimizationProblem(MetaOptimizationProblem original, Cloner cloner)
|
---|
| 148 | : base(original, cloner) {
|
---|
[4830] | 149 | this.RegisterParameterEvents();
|
---|
[4516] | 150 | }
|
---|
[4830] | 151 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 152 | return new MetaOptimizationProblem(this, cloner);
|
---|
| 153 | }
|
---|
[4516] | 154 |
|
---|
| 155 | #region Helpers
|
---|
| 156 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 157 | private void AfterDeserializationHook() {
|
---|
[4830] | 158 | RegisterParameterEvents();
|
---|
[4516] | 159 | }
|
---|
[4830] | 160 | private void RegisterParameterEvents() {
|
---|
[4516] | 161 | SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
|
---|
| 162 | EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
|
---|
| 163 | Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
[5110] | 164 | AlgorithmTypeParameter.ValueChanged += new EventHandler(AlgorithmTypeParameter_ValueChanged);
|
---|
[5313] | 165 | AlgorithmType.ValueChanged += new EventHandler(AlgorithmType_ValueChanged);
|
---|
[5110] | 166 | ProblemTypeParameter.ValueChanged += new EventHandler(ProblemTypeParameter_ValueChanged);
|
---|
[5313] | 167 | ProblemType.ValueChanged += new EventHandler(ProblemType_ValueChanged);
|
---|
[4516] | 168 | }
|
---|
[5303] | 169 |
|
---|
[4516] | 170 | private void InitializeOperators() {
|
---|
[4830] | 171 | Operators.AddRange(ApplicationManager.Manager.GetInstances<IParameterConfigurationOperator>().Cast<IOperator>());
|
---|
| 172 | Operators.Add(new BestParameterConfigurationAnalyzer());
|
---|
[5293] | 173 | Operators.Add(new ReferenceQualityAnalyzer());
|
---|
[5359] | 174 | Operators.Add(new SolutionCacheAnalyzer());
|
---|
[5522] | 175 | Operators.Add(new PMOPopulationDiversityAnalyzer());
|
---|
[4516] | 176 | }
|
---|
| 177 | private void ParameterizeSolutionCreator() {
|
---|
| 178 | }
|
---|
| 179 | private void ParameterizeEvaluator() {
|
---|
[5111] | 180 | ((ParameterConfigurationEvaluator)Evaluator).ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
|
---|
[4516] | 181 | }
|
---|
| 182 | private void ParameterizeAnalyzer() {
|
---|
[5184] | 183 | if (BestParameterConfigurationAnalyzer != null) {
|
---|
| 184 | BestParameterConfigurationAnalyzer.ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
|
---|
| 185 | }
|
---|
[5293] | 186 | if (ReferenceQualityAnalyzer != null) {
|
---|
| 187 | ReferenceQualityAnalyzer.ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
|
---|
[5281] | 188 | }
|
---|
[5303] | 189 | if (RunsAnalyzer != null) {
|
---|
| 190 | RunsAnalyzer.ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
|
---|
[5522] | 191 | }
|
---|
| 192 | if (PMOPopulationDiversityAnalyzer != null) {
|
---|
| 193 | PMOPopulationDiversityAnalyzer.SolutionParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
|
---|
| 194 | PMOPopulationDiversityAnalyzer.StoreHistoryParameter.Value.Value = true;
|
---|
[5303] | 195 | }
|
---|
[4516] | 196 | }
|
---|
| 197 | private void ParameterizeOperators() {
|
---|
[5184] | 198 | foreach (IParameterConfigurationCrossover op in Operators.OfType<IParameterConfigurationCrossover>()) {
|
---|
| 199 | op.ParentsParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
|
---|
| 200 | op.ChildParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
|
---|
| 201 | }
|
---|
| 202 | foreach (IParameterConfigurationManipulator op in Operators.OfType<IParameterConfigurationManipulator>()) {
|
---|
| 203 | op.ParameterConfigurationTreeParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
|
---|
| 204 | }
|
---|
[4516] | 205 | }
|
---|
| 206 |
|
---|
| 207 | #endregion
|
---|
| 208 |
|
---|
| 209 | #region Events
|
---|
| 210 |
|
---|
| 211 | private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 212 | ParameterizeSolutionCreator();
|
---|
| 213 | ParameterizeEvaluator();
|
---|
| 214 | ParameterizeAnalyzer();
|
---|
| 215 | ParameterizeOperators();
|
---|
| 216 | OnSolutionCreatorChanged();
|
---|
| 217 | }
|
---|
| 218 | private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 219 | Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
| 220 | ParameterizeEvaluator();
|
---|
| 221 | ParameterizeAnalyzer();
|
---|
| 222 | OnEvaluatorChanged();
|
---|
| 223 | }
|
---|
| 224 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
| 225 | ParameterizeAnalyzer();
|
---|
| 226 | }
|
---|
[5313] | 227 | private void AlgorithmTypeParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 228 | AlgorithmType_ValueChanged(sender, e);
|
---|
[4516] | 229 | }
|
---|
[5313] | 230 |
|
---|
| 231 | private void AlgorithmType_ValueChanged(object sender, EventArgs e) {
|
---|
[5337] | 232 | IAlgorithm instance = (IAlgorithm)Activator.CreateInstance(AlgorithmType.Value);
|
---|
| 233 | this.ProblemType.ValidTypes = ApplicationManager.Manager.GetTypes(instance.ProblemType, true).ToList();
|
---|
[5340] | 234 | this.ProblemType.Value = this.ProblemType.ValidTypes.Where(t => t != typeof(MetaOptimizationProblem)).FirstOrDefault();
|
---|
| 235 | if (ProblemType.Value != null) {
|
---|
| 236 | ParameterConfigurationTreeParameter.ActualValue = new ParameterConfigurationTree(CreateAlgorithm(AlgorithmType.Value, ProblemType.Value));
|
---|
| 237 | } else {
|
---|
| 238 | ParameterConfigurationTreeParameter.ActualValue = null;
|
---|
| 239 | }
|
---|
[4516] | 240 | }
|
---|
[5313] | 241 |
|
---|
| 242 | private void ProblemTypeParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 243 | ProblemType_ValueChanged(sender, e);
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | private void ProblemType_ValueChanged(object sender, EventArgs e) {
|
---|
| 247 | Problems.Clear();
|
---|
| 248 | Problems.Type = ProblemType.Value;
|
---|
| 249 | ParameterConfigurationTreeParameter.ActualValue = new ParameterConfigurationTree(CreateAlgorithm(AlgorithmType.Value, ProblemType.Value));
|
---|
| 250 | }
|
---|
[4516] | 251 | #endregion
|
---|
[5313] | 252 |
|
---|
[5337] | 253 | private IAlgorithm CreateAlgorithm(Type algorithmType, Type problemType) {
|
---|
| 254 | IAlgorithm algorithm = (IAlgorithm)Activator.CreateInstance(algorithmType);
|
---|
[5313] | 255 | algorithm.Problem = (IProblem)Activator.CreateInstance(problemType);
|
---|
| 256 | return algorithm;
|
---|
| 257 | }
|
---|
| 258 |
|
---|
[5337] | 259 | public void ImportAlgorithm(IAlgorithm algorithm) {
|
---|
[5313] | 260 | AlgorithmType.Value = algorithm.GetType();
|
---|
[5337] | 261 | if (algorithm.Problem != null) ProblemType.Value = algorithm.Problem.GetType();
|
---|
[5313] | 262 | ParameterConfigurationTreeParameter.ActualValue = new ParameterConfigurationTree(algorithm);
|
---|
[5337] | 263 | if (algorithm.Problem != null) Problems.Add((IProblem)algorithm.Problem);
|
---|
[5313] | 264 | }
|
---|
[4516] | 265 | }
|
---|
| 266 | }
|
---|