Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Evaluators/MetaOptimizationEvaluator.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: 3.0 KB
RevLine 
[4516]1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Operators;
6using HeuristicLab.Data;
7using HeuristicLab.Parameters;
8using HeuristicLab.Core;
9using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
10using HeuristicLab.Optimization;
11using System.Threading;
12
13namespace HeuristicLab.Problems.MetaOptimization {
14  /// <summary>
15  /// A base class for operators which evaluate TSP solutions.
16  /// </summary>
17  [Item("MetaOptimizationEvaluator", "A base class for operators which evaluate Meta Optimization solutions.")]
18  [StorableClass]
19  public class MetaOptimizationEvaluator : SingleSuccessorOperator, IMetaOptimizationEvaluator {
20    private bool algorithmStopped;
21
22    public ILookupParameter<DoubleValue> QualityParameter {
23      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
24    }
25
26    public ILookupParameter<IAlgorithm> AlgorithmParameter {
27      get { return (ILookupParameter<IAlgorithm>)Parameters["Algorithm"]; }
28    }
29    public ILookupParameter<IParameterSet> ParameterSetParameter {
30      get { return (ILookupParameter<IParameterSet>)Parameters["ParameterSet"]; }
31    }
32
33    public MetaOptimizationEvaluator()
34      : base() {
35      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The evaluated quality of the TSP solution."));
36      Parameters.Add(new LookupParameter<IAlgorithm>("Algorithm", "Missing description."));
37      Parameters.Add(new LookupParameter<IParameterSet>("ParameterSet", "Missing description."));
38    }
39
40    public override IOperation Apply() {
41      AlgorithmParameter.ActualValue.Prepare();
42      ParametrizeAlgorithm();
43      algorithmStopped = false;
44      AlgorithmParameter.ActualValue.Stopped += new EventHandler(ActualValue_Stopped);
45      AlgorithmParameter.ActualValue.Start();
46      while (!algorithmStopped) {
47        Thread.Sleep(1000); // wait for algorithm to complete; do not rely on Algorithm.ExecutionState here, because change of ExecutionState happens before Run is added (which causes problems because Algorithm might get cloned when its started already)
48      }
49      AlgorithmParameter.ActualValue.Stopped -= new EventHandler(ActualValue_Stopped);
50      this.QualityParameter.ActualValue = (DoubleValue)AlgorithmParameter.ActualValue.Results["BestQuality"].Value;
51      return base.Apply();
52    }
53
54    void ActualValue_Stopped(object sender, EventArgs e) {
55      algorithmStopped = true;
56    }
57
58    private void ParametrizeAlgorithm() {
59      foreach (KeyValuePair<string, IParameter> parameter in ParameterSetParameter.ActualValue.Parameters) {
60        if (parameter.Key == "Algorithm") {
61          this.AlgorithmParameter.ActualValue.Parameters[parameter.Value.Name].ActualValue = parameter.Value.ActualValue;
62        } else if (parameter.Key == "Problem") {
63          this.AlgorithmParameter.ActualValue.Problem.Parameters[parameter.Value.Name].ActualValue = parameter.Value.ActualValue;
64        }
65      }
66    }
67  }
68}
Note: See TracBrowser for help on using the repository browser.