#region License Information /* HeuristicLab * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Optimization; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.PluginInfrastructure; using HeuristicLab.Algorithms.GeneticAlgorithm; using HeuristicLab.Problems.TestFunctions; namespace HeuristicLab.Problems.MetaOptimization { [Item("Meta Optimization Problem", "Represents a Meta Optimization Problem.")] [Creatable("Problems")] [StorableClass] public sealed class MetaOptimizationProblem : SingleObjectiveProblem { public const string AlgorithmTypeParameterName = "AlgorithmType"; public const string ProblemTypeParameterName = "ProblemType"; public const string ProblemsParameterName = "Problems"; public const string ParameterConfigurationParameterName = "InitialParameterConfigurationTree"; public const string RepetitionsParameterName = "Repetitions"; public const string IntValueManipulatorParameterName = "IntValueManipulator"; public const string DoubleValueManipulatorParameterName = "DoubleValueManipulator"; public const string IntValueCrossoverParameterName = "IntValueCrossover"; public const string DoubleValueCrossoverParameterName = "DoubleValueCrossover"; #region Parameter Properties public IValueParameter AlgorithmTypeParameter { get { return (ValueParameter)Parameters[AlgorithmTypeParameterName]; } } public IValueParameter ProblemTypeParameter { get { return (ValueParameter)Parameters[ProblemTypeParameterName]; } } public IValueParameter> ProblemsParameter { get { return (ValueParameter>)Parameters[ProblemsParameterName]; } } public IValueParameter ParameterConfigurationParameter { get { return (ValueParameter)Parameters[ParameterConfigurationParameterName]; } } public IValueParameter RepetitionsParameter { get { return (ValueParameter)Parameters[RepetitionsParameterName]; } } public IValueParameter IntValueManipulatorParameter { get { return (ValueParameter)Parameters[IntValueManipulatorParameterName]; } } public IValueParameter DoubleValueManipulatorParameter { get { return (ValueParameter)Parameters[DoubleValueManipulatorParameterName]; } } #endregion #region Properties public EngineAlgorithm Algorithm { get { return AlgorithmTypeParameter.Value; } set { AlgorithmTypeParameter.Value = value; } } public ISingleObjectiveProblem Problem { get { return ProblemTypeParameter.Value; } set { ProblemTypeParameter.Value = value; } } public ConstrainedItemList Problems { get { return ProblemsParameter.Value; } set { ProblemsParameter.Value = value; } } public ParameterConfigurationTree AlgorithmParameterConfiguration { get { return ParameterConfigurationParameter.Value; } set { ParameterConfigurationParameter.Value = value; } } public IntValue Repetitions { get { return RepetitionsParameter.Value; } set { RepetitionsParameter.Value = value; } } #endregion public MetaOptimizationProblem() : base() { Parameters.Add(new ValueParameter(AlgorithmTypeParameterName, "The algorithm which's parameters should be optimized.", new GeneticAlgorithm())); Parameters.Add(new ValueParameter(ProblemTypeParameterName, "The problem type.", new SingleObjectiveTestFunctionProblem())); Parameters.Add(new ValueParameter>(ProblemsParameterName, "The problems that should be evaluated.", new ConstrainedItemList())); Parameters.Add(new ValueParameter(ParameterConfigurationParameterName, "List of algorithm parameters that should be optimized.")); Parameters.Add(new ValueParameter(RepetitionsParameterName, "The number of evaluations for each problem.", new IntValue(3))); var validIntManipulators = new ItemSet( ApplicationManager.Manager.GetInstances()); var validDoubleManipulators = new ItemSet(ApplicationManager.Manager.GetInstances()); Parameters.Add(new ConstrainedValueParameter(IntValueManipulatorParameterName, "", validIntManipulators, new UniformIntValueManipulator())); Parameters.Add(new ConstrainedValueParameter(DoubleValueManipulatorParameterName, "", validDoubleManipulators, new NormalDoubleValueManipulator())); Maximization = new BoolValue(false); SolutionCreator = new RandomParameterConfigurationCreator(); Evaluator = new ParameterConfigurationEvaluator(); InitializeOperators(); RegisterParameterEvents(); ParameterizeSolutionCreator(); ParameterizeEvaluator(); ParameterizeOperators(); Problems.Type = Problem.GetType(); Algorithm.Problem = Problem; ParameterConfigurationParameter.ActualValue = new ParameterConfigurationTree(Algorithm); } [StorableConstructor] private MetaOptimizationProblem(bool deserializing) : base(deserializing) { } private MetaOptimizationProblem(MetaOptimizationProblem original, Cloner cloner) : base(original, cloner) { // todo this.RegisterParameterEvents(); } public override IDeepCloneable Clone(Cloner cloner) { return new MetaOptimizationProblem(this, cloner); } #region Helpers [StorableHook(HookType.AfterDeserialization)] private void AfterDeserializationHook() { RegisterParameterEvents(); } private void RegisterParameterEvents() { SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged); EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged); Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged); AlgorithmTypeParameter.ValueChanged += new EventHandler(AlgorithmTypeParameter_ValueChanged); ProblemTypeParameter.ValueChanged += new EventHandler(ProblemTypeParameter_ValueChanged); } private void InitializeOperators() { Operators.AddRange(ApplicationManager.Manager.GetInstances().Cast()); Operators.Add(new BestParameterConfigurationAnalyzer()); } private void ParameterizeSolutionCreator() { } private void ParameterizeEvaluator() { ((ParameterConfigurationEvaluator)Evaluator).ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName; } private void ParameterizeAnalyzer() { } private void ParameterizeOperators() { } #endregion #region Events private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) { ParameterizeSolutionCreator(); ParameterizeEvaluator(); ParameterizeAnalyzer(); ParameterizeOperators(); OnSolutionCreatorChanged(); } private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) { Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged); ParameterizeEvaluator(); ParameterizeAnalyzer(); OnEvaluatorChanged(); } private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) { ParameterizeAnalyzer(); } void AlgorithmTypeParameter_ValueChanged(object sender, EventArgs e) { Algorithm.Problem = Problem; ParameterConfigurationParameter.ActualValue = new ParameterConfigurationTree(Algorithm); } void ProblemTypeParameter_ValueChanged(object sender, EventArgs e) { Problems.Type = Problem.GetType(); Algorithm.Problem = Problem; ParameterConfigurationParameter.ActualValue = new ParameterConfigurationTree(Algorithm); } #endregion } }