Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4516 was 4516, checked in by cneumuel, 14 years ago

initial prototype for Meta Optimization Problem (#1215)

File size: 11.7 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.Collections.Generic;
24using System.Linq;
25using System.Drawing;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using HeuristicLab.Core;
28using HeuristicLab.Optimization;
29using HeuristicLab.Common;
30using HeuristicLab.Parameters;
31using HeuristicLab.Data;
32using HeuristicLab.PluginInfrastructure;
33
34namespace HeuristicLab.Problems.MetaOptimization {
35  [Item("Meta Optimization Problem", "Represents a Meta Optimization Problem.")]
36  [Creatable("Problems")]
37  [StorableClass]
38  public sealed class MetaOptimizationProblem : ParameterizedNamedItem, ISingleObjectiveProblem, IStorableContent {
39    public string Filename { get; set; }
40
41    public override Image ItemImage {
42      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Type; }
43    }
44
45    #region Parameter Properties
46
47    public ValueParameter<IntValue> RepetitionsParameter {
48      get { return (ValueParameter<IntValue>)Parameters["Repetitions"]; }
49    }
50
51    public ValueParameter<IParameterSetCreator> SolutionCreatorParameter {
52      get { return (ValueParameter<IParameterSetCreator>)Parameters["SolutionCreator"]; }
53    }
54    IParameter IProblem.SolutionCreatorParameter {
55      get { return SolutionCreatorParameter; }
56    }
57
58    public ValueParameter<IMetaOptimizationEvaluator> EvaluatorParameter {
59      get { return (ValueParameter<IMetaOptimizationEvaluator>)Parameters["Evaluator"]; }
60    }
61    IParameter IProblem.EvaluatorParameter {
62      get { return EvaluatorParameter; }
63    }
64
65    public OptionalValueParameter<ParameterSet> BestKnownSolutionParameter {
66      get { return (OptionalValueParameter<ParameterSet>)Parameters["BestKnownSolution"]; }
67    }
68
69    public ValueParameter<BoolValue> MaximizationParameter {
70      get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }
71    }
72    IParameter ISingleObjectiveProblem.MaximizationParameter {
73      get { return MaximizationParameter; }
74    }
75
76    public OptionalValueParameter<DoubleValue> BestKnownQualityParameter {
77      get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
78    }
79    IParameter ISingleObjectiveProblem.BestKnownQualityParameter {
80      get { return BestKnownQualityParameter; }
81    }
82
83    public ValueParameter<IAlgorithm> AlgorithmParameter {
84      get { return (ValueParameter<IAlgorithm>)Parameters["Algorithm"]; }
85    }
86    public ValueParameter<ParameterConfigurationList> ParametersToOptimizeParameter {
87      get { return (ValueParameter<ParameterConfigurationList>)Parameters["ParametersToOptimize"]; }
88    }
89    #endregion
90
91    #region Properties
92
93    public IntValue Repetitions {
94      get { return RepetitionsParameter.Value; }
95      set { RepetitionsParameter.Value = value; }
96    }
97
98    public IEnumerable<IOperator> Operators {
99      get { return operators; }
100    }
101
102    IEvaluator IProblem.Evaluator {
103      get { return EvaluatorParameter.Value; }
104    }
105    public IMetaOptimizationEvaluator Evaluator {
106      get { return EvaluatorParameter.Value; }
107      set { EvaluatorParameter.Value = value; }
108    }
109
110    ISolutionCreator IProblem.SolutionCreator {
111      get { return SolutionCreatorParameter.Value; }
112    }
113    public IParameterSetCreator SolutionCreator {
114      get { return SolutionCreatorParameter.Value; }
115    }
116
117    ISingleObjectiveEvaluator ISingleObjectiveProblem.Evaluator {
118      get { return this.Evaluator; }
119    }
120
121    public DoubleValue BestKnownQuality {
122      get { return BestKnownQualityParameter.Value; }
123      set { BestKnownQualityParameter.Value = value; }
124    }
125    public ParameterSet BestKnownSolution {
126      get { return BestKnownSolutionParameter.Value; }
127      set { BestKnownSolutionParameter.Value = value; }
128    }
129
130    public IAlgorithm Algorithm {
131      get { return AlgorithmParameter.Value; }
132      set { AlgorithmParameter.Value = value; }
133    }
134
135    public ParameterConfigurationList ParametersToOptimize {
136      get { return ParametersToOptimizeParameter.Value; }
137      set { ParametersToOptimizeParameter.Value = value; }
138    }
139
140    private BestQualityAnalyzer BestQualityAnalyzer {
141      get { return operators.OfType<BestQualityAnalyzer>().FirstOrDefault(); }
142    }
143    #endregion
144
145    [Storable]
146    private List<IOperator> operators;
147
148    [StorableConstructor]
149    private MetaOptimizationProblem(bool deserializing) : base(deserializing) { }
150    public MetaOptimizationProblem()
151      : base() {
152
153      IParameterSetCreator creator = new RandomParameterSetCreator();
154      MetaOptimizationEvaluator evaluator = new MetaOptimizationEvaluator();
155      ParameterConfigurationList parametersToOptimize = new ParameterConfigurationList();
156
157      Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as the Traveling Salesman Problem is a minimization problem.", new BoolValue(false)));
158      Parameters.Add(new ValueParameter<IntValue>("Repetitions", "Number of evaluations for one individual.", new IntValue(3)));
159      Parameters.Add(new ValueParameter<IParameterSetCreator>("SolutionCreator", "The operator which should be used to create new TSP solutions.", creator));
160      Parameters.Add(new ValueParameter<IMetaOptimizationEvaluator>("Evaluator", "The operator which should be used to evaluate TSP solutions.", evaluator));
161      Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this TSP instance."));
162      Parameters.Add(new OptionalValueParameter<ParameterSet>("BestKnownSolution", "The best known solution of this TSP instance."));
163
164      Parameters.Add(new ValueParameter<IAlgorithm>("Algorithm", "The algorithm and problem which's parameters should be optimized."));
165      Parameters.Add(new ValueParameter<ParameterConfigurationList>("ParametersToOptimize", "List of parameters that should be optimized.", parametersToOptimize));
166
167      ParameterizeSolutionCreator();
168      ParameterizeEvaluator();
169
170      InitializeOperators();
171      AttachEventHandlers();
172    }
173
174    public override IDeepCloneable Clone(Cloner cloner) {
175      MetaOptimizationProblem clone = (MetaOptimizationProblem)base.Clone(cloner);
176      clone.operators = operators.Select(x => (IOperator)cloner.Clone(x)).ToList();
177      // todo
178      clone.AttachEventHandlers();
179      return clone;
180    }
181
182    #region Helpers
183    [StorableHook(HookType.AfterDeserialization)]
184    private void AfterDeserializationHook() {
185      AttachEventHandlers();
186    }
187    private void AttachEventHandlers() {
188      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
189      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
190      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
191      AlgorithmParameter.ValueChanged += new EventHandler(BaseLevelAlgorithmParameter_ValueChanged);
192    }
193    private void InitializeOperators() {
194      operators = new List<IOperator>();
195      operators.Add(new BestQualityAnalyzer());
196      ParameterizeAnalyzer();
197      ParameterizeOperators();
198    }
199    private void ParameterizeSolutionCreator() {
200      SolutionCreator.ParametersToOptimize = this.ParametersToOptimize;
201    }
202    private void ParameterizeEvaluator() {
203    }
204    private void ParameterizeAnalyzer() {
205      BestQualityAnalyzer.ResultsParameter.ActualName = "Results";
206    }
207    private void ParameterizeOperators() {
208    }
209
210    private void AddAlgorithmParameters() {
211      foreach (IParameter parameter in Algorithm.Parameters) {
212        this.ParametersToOptimize.Add(new NumericParameterConfiguration(parameter, "Algorithm"), false);
213      }
214    }
215    private void RemoveAlgorithmParameters() {
216      foreach (IParameter parameter in Algorithm.Parameters) {
217        IParameterConfiguration parameterConfiguration = this.ParametersToOptimize.Single(p => p.Parameter == parameter);
218        if (parameterConfiguration != null) {
219          this.ParametersToOptimize.Remove(parameterConfiguration);
220        }
221      }
222    }
223    private void ClearAlgorithmParameters() {
224      //this.ParametersToOptimize.Clear();
225    }
226
227    private void AddProblemParameters() {
228      foreach (IParameter parameter in Algorithm.Problem.Parameters) {
229        this.ParametersToOptimize.Add(new NumericParameterConfiguration(parameter, "Problem"), false);
230      }
231    }
232    private void RemoveProblemParameters() {
233      foreach (IParameter parameter in Algorithm.Problem.Parameters) {
234        IParameterConfiguration parameterConfiguration = this.ParametersToOptimize.Single(p => p.Parameter == parameter);
235        if (parameterConfiguration != null) {
236          this.ParametersToOptimize.Remove(parameterConfiguration);
237        }
238      }
239    }
240    private void ClearProblemParameters() {
241      //this.ParametersToOptimize.Clear();
242    }
243    #endregion
244
245    #region Events
246    public event EventHandler SolutionCreatorChanged;
247    private void OnSolutionCreatorChanged() {
248      EventHandler handler = SolutionCreatorChanged;
249      if (handler != null) handler(this, EventArgs.Empty);
250    }
251    public event EventHandler EvaluatorChanged;
252    private void OnEvaluatorChanged() {
253      EventHandler handler = EvaluatorChanged;
254      if (handler != null) handler(this, EventArgs.Empty);
255    }
256    public event EventHandler OperatorsChanged;
257    private void OnOperatorsChanged() {
258      EventHandler handler = OperatorsChanged;
259      if (handler != null) handler(this, EventArgs.Empty);
260    }
261    public event EventHandler Reset;
262    private void OnReset() {
263      EventHandler handler = Reset;
264      if (handler != null) handler(this, EventArgs.Empty);
265    }
266
267    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
268      ParameterizeSolutionCreator();
269      ParameterizeEvaluator();
270      ParameterizeAnalyzer();
271      ParameterizeOperators();
272      OnSolutionCreatorChanged();
273    }
274    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
275      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
276      ParameterizeEvaluator();
277      ParameterizeAnalyzer();
278      OnEvaluatorChanged();
279    }
280    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
281      ParameterizeAnalyzer();
282    }
283    void BaseLevelAlgorithmParameter_ValueChanged(object sender, EventArgs e) {
284      ClearAlgorithmParameters();
285      if (Algorithm != null) {
286        Algorithm.ProblemChanged += new EventHandler(BaseLevelAlgorithm_ProblemChanged);
287        AddAlgorithmParameters(); // TODO: When to Detach?
288      }
289      BaseLevelAlgorithm_ProblemChanged(sender, e);
290    }
291
292    void BaseLevelAlgorithm_ProblemChanged(object sender, EventArgs e) {
293      ClearProblemParameters();
294      if (Algorithm.Problem != null) {
295        AddProblemParameters();
296      }
297    }
298    #endregion
299  }
300}
Note: See TracBrowser for help on using the repository browser.