1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Operators;
|
---|
6 | using HeuristicLab.Data;
|
---|
7 | using HeuristicLab.Parameters;
|
---|
8 | using HeuristicLab.Core;
|
---|
9 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
10 | using HeuristicLab.Optimization;
|
---|
11 | using System.Threading;
|
---|
12 |
|
---|
13 | namespace 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 | }
|
---|