Free cookie consent management tool by TermsFeed Policy Generator

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

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

implemented basic crossover operator for ParameterSets. MetaOptimization is now functional on a basic level (Configuration and Crossing only works for IntValue Parameters) (#1215)

File size: 12.0 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    #region Cloning
175    public override IDeepCloneable Clone(Cloner cloner) {
176      MetaOptimizationProblem clone = (MetaOptimizationProblem)base.Clone(cloner);
177      clone.operators = operators.Select(x => (IOperator)cloner.Clone(x)).ToList();
178      // todo
179      clone.AttachEventHandlers();
180      return clone;
181    }
182    #endregion
183
184    #region Helpers
185    [StorableHook(HookType.AfterDeserialization)]
186    private void AfterDeserializationHook() {
187      AttachEventHandlers();
188    }
189    private void AttachEventHandlers() {
190      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
191      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
192      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
193      AlgorithmParameter.ValueChanged += new EventHandler(BaseLevelAlgorithmParameter_ValueChanged);
194    }
195    private void InitializeOperators() {
196      operators = new List<IOperator>();
197      operators.Add(new BestQualityAnalyzer());
198      ParameterizeAnalyzer();
199      operators.AddRange(ApplicationManager.Manager.GetInstances<IParameterSetOperator>().Cast<IOperator>());
200      ParameterizeOperators();
201
202      //UpdateMoveEvaluators();
203      //InitializeMoveGenerators();
204    }
205    private void ParameterizeSolutionCreator() {
206      SolutionCreator.ParametersToOptimize = this.ParametersToOptimize;
207    }
208    private void ParameterizeEvaluator() {
209    }
210    private void ParameterizeAnalyzer() {
211      BestQualityAnalyzer.ResultsParameter.ActualName = "Results";
212    }
213    private void ParameterizeOperators() {
214    }
215
216    private void AddAlgorithmParameters() {
217      foreach (IParameter parameter in Algorithm.Parameters) {
218        this.ParametersToOptimize.Add(new NumericParameterConfiguration(parameter, "Algorithm"), false);
219      }
220    }
221    private void RemoveAlgorithmParameters() {
222      foreach (IParameter parameter in Algorithm.Parameters) {
223        IParameterConfiguration parameterConfiguration = this.ParametersToOptimize.Single(p => p.Parameter == parameter);
224        if (parameterConfiguration != null) {
225          this.ParametersToOptimize.Remove(parameterConfiguration);
226        }
227      }
228    }
229    private void ClearAlgorithmParameters() {
230      //this.ParametersToOptimize.Clear();
231    }
232
233    private void AddProblemParameters() {
234      foreach (IParameter parameter in Algorithm.Problem.Parameters) {
235        this.ParametersToOptimize.Add(new NumericParameterConfiguration(parameter, "Problem"), false);
236      }
237    }
238    private void RemoveProblemParameters() {
239      foreach (IParameter parameter in Algorithm.Problem.Parameters) {
240        IParameterConfiguration parameterConfiguration = this.ParametersToOptimize.Single(p => p.Parameter == parameter);
241        if (parameterConfiguration != null) {
242          this.ParametersToOptimize.Remove(parameterConfiguration);
243        }
244      }
245    }
246    private void ClearProblemParameters() {
247      //this.ParametersToOptimize.Clear();
248    }
249    #endregion
250
251    #region Events
252    public event EventHandler SolutionCreatorChanged;
253    private void OnSolutionCreatorChanged() {
254      EventHandler handler = SolutionCreatorChanged;
255      if (handler != null) handler(this, EventArgs.Empty);
256    }
257    public event EventHandler EvaluatorChanged;
258    private void OnEvaluatorChanged() {
259      EventHandler handler = EvaluatorChanged;
260      if (handler != null) handler(this, EventArgs.Empty);
261    }
262    public event EventHandler OperatorsChanged;
263    private void OnOperatorsChanged() {
264      EventHandler handler = OperatorsChanged;
265      if (handler != null) handler(this, EventArgs.Empty);
266    }
267    public event EventHandler Reset;
268    private void OnReset() {
269      EventHandler handler = Reset;
270      if (handler != null) handler(this, EventArgs.Empty);
271    }
272
273    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
274      ParameterizeSolutionCreator();
275      ParameterizeEvaluator();
276      ParameterizeAnalyzer();
277      ParameterizeOperators();
278      OnSolutionCreatorChanged();
279    }
280    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
281      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
282      ParameterizeEvaluator();
283      ParameterizeAnalyzer();
284      OnEvaluatorChanged();
285    }
286    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
287      ParameterizeAnalyzer();
288    }
289    void BaseLevelAlgorithmParameter_ValueChanged(object sender, EventArgs e) {
290      ClearAlgorithmParameters();
291      if (Algorithm != null) {
292        Algorithm.ProblemChanged += new EventHandler(BaseLevelAlgorithm_ProblemChanged);
293        AddAlgorithmParameters(); // TODO: When to Detach?
294      }
295      BaseLevelAlgorithm_ProblemChanged(sender, e);
296    }
297
298    void BaseLevelAlgorithm_ProblemChanged(object sender, EventArgs e) {
299      ClearProblemParameters();
300      if (Algorithm.Problem != null) {
301        AddProblemParameters();
302      }
303    }
304    #endregion
305  }
306}
Note: See TracBrowser for help on using the repository browser.