Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/MetaOptimizationProblem.cs @ 4982

Last change on this file since 4982 was 4982, checked in by cneumuel, 13 years ago

#1215 worked on metaoptimization

File size: 7.5 KB
Line 
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
22using System;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.PluginInfrastructure;
31
32namespace HeuristicLab.Problems.MetaOptimization {
33  [Item("Meta Optimization Problem", "Represents a Meta Optimization Problem.")]
34  [Creatable("Problems")]
35  [StorableClass]
36  public sealed class MetaOptimizationProblem : SingleObjectiveProblem<IMetaOptimizationEvaluator, IParameterConfigurationCreator> {
37    private const string AlgorithmParameterName = "Algorithm";
38    private const string ProblemsParameterName = "Problems";
39    private const string AlgorithmParameterConfigurationParameterName = "AlgorithmParameterConfiguration";
40    private const string ProblemParametersConfigurationParameterName = "ProblemParametersConfiguration";
41
42    #region Parameter Properties
43    public ValueParameter<IAlgorithm> AlgorithmParameter {
44      get { return (ValueParameter<IAlgorithm>)Parameters[AlgorithmParameterName]; }
45    }
46    public ValueParameter<IItemList<ISingleObjectiveProblem>> ProblemsParameter {
47      get { return (ValueParameter<IItemList<ISingleObjectiveProblem>>)Parameters[ProblemsParameterName]; }
48    }
49    public ValueParameter<IValueConfiguration> AlgorithmParameterConfigurationParameter {
50      get { return (ValueParameter<IValueConfiguration>)Parameters[AlgorithmParameterConfigurationParameterName]; }
51    }
52    //public ValueParameter<IItemList<IParameterConfiguration>> ProblemParametersConfigurationParameter {
53    //  get { return (ValueParameter<IItemList<IParameterConfiguration>>)Parameters[ProblemParametersConfigurationParameterName]; }
54    //}
55    #endregion
56
57    #region Properties
58    public IAlgorithm Algorithm {
59      get { return AlgorithmParameter.Value; }
60      set { AlgorithmParameter.Value = value; }
61    }
62    public IItemList<ISingleObjectiveProblem> Problems {
63      get { return ProblemsParameter.Value; }
64      set { ProblemsParameter.Value = value; }
65    }
66    public IValueConfiguration AlgorithmParameterConfiguration {
67      get { return AlgorithmParameterConfigurationParameter.Value; }
68      set { AlgorithmParameterConfigurationParameter.Value = value; }
69    }
70    //public IItemList<IParameterConfiguration> ProblemParametersConfiguration {
71    //  get { return ProblemParametersConfigurationParameter.Value; }
72    //  set { ProblemParametersConfigurationParameter.Value = value; }
73    //}
74    #endregion
75
76    public MetaOptimizationProblem()
77      : base() {
78      Parameters.Add(new ValueParameter<IAlgorithm>(AlgorithmParameterName, "The algorithm which's parameters should be optimized."));
79      Parameters.Add(new ValueParameter<IItemList<IProblem>>(ProblemsParameterName, "The problems that should be evaluated.", new ItemList<IProblem>()));
80      Parameters.Add(new ValueParameter<IValueConfiguration>(AlgorithmParameterConfigurationParameterName, "List of algorithm parameters that should be optimized."));
81      //Parameters.Add(new ValueParameter<IItemList<IParameterConfiguration>>(ProblemParametersConfigurationParameterName, "List of problem parameters that should be optimized.", new ItemList<IParameterConfiguration>()));
82     
83      Maximization = new BoolValue(false);
84      SolutionCreator = new RandomParameterConfigurationCreator();
85      Evaluator = new MetaOptimizationEvaluator();
86
87      InitializeOperators();
88      RegisterParameterEvents();
89      ParameterizeSolutionCreator();
90      ParameterizeEvaluator();
91      ParameterizeOperators();
92    }
93
94    [StorableConstructor]
95    private MetaOptimizationProblem(bool deserializing) : base(deserializing) { }
96    private MetaOptimizationProblem(MetaOptimizationProblem original, Cloner cloner) : base(original, cloner) {
97      // todo
98      this.RegisterParameterEvents();
99    }
100    public override IDeepCloneable Clone(Cloner cloner) {
101      return new MetaOptimizationProblem(this, cloner);
102    }
103
104    #region Helpers
105    [StorableHook(HookType.AfterDeserialization)]
106    private void AfterDeserializationHook() {
107      RegisterParameterEvents();
108    }
109    private void RegisterParameterEvents() {
110      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
111      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
112      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
113      AlgorithmParameter.ValueChanged += new EventHandler(BaseLevelAlgorithmParameter_ValueChanged);
114    }
115    private void InitializeOperators() {
116      Operators.AddRange(ApplicationManager.Manager.GetInstances<IParameterConfigurationOperator>().Cast<IOperator>());
117      Operators.Add(new BestParameterConfigurationAnalyzer());
118    }
119    private void ParameterizeSolutionCreator() {
120      //SolutionCreator.ParametersToOptimize = this.ParametersToOptimize;
121    }
122    private void ParameterizeEvaluator() {
123      ((MetaOptimizationEvaluator)Evaluator).ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
124    }
125    private void ParameterizeAnalyzer() {
126      //BestQualityAnalyzer.ResultsParameter.ActualName = "Results";
127    }
128    private void ParameterizeOperators() {
129     
130    }
131
132    #endregion
133
134    #region Events
135
136    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
137      ParameterizeSolutionCreator();
138      ParameterizeEvaluator();
139      ParameterizeAnalyzer();
140      ParameterizeOperators();
141      OnSolutionCreatorChanged();
142    }
143    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
144      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
145      ParameterizeEvaluator();
146      ParameterizeAnalyzer();
147      OnEvaluatorChanged();
148    }
149    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
150      ParameterizeAnalyzer();
151    }
152    void BaseLevelAlgorithmParameter_ValueChanged(object sender, EventArgs e) {
153      if (Algorithm != null) {
154        Algorithm.ProblemChanged += new EventHandler(BaseLevelAlgorithm_ProblemChanged);
155        AlgorithmParameterConfiguration = new RootValueConfiguration(Algorithm, Algorithm.GetType());
156      }
157      BaseLevelAlgorithm_ProblemChanged(sender, e);
158    }
159
160    void BaseLevelAlgorithm_ProblemChanged(object sender, EventArgs e) {
161      //ClearProblemParameters();
162      //if (Algorithm.Problem != null) {
163      //  AddProblemParameters();
164      //}
165    }
166    #endregion
167  }
168}
Note: See TracBrowser for help on using the repository browser.