Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Evaluators/AlgorithmEvaluator.cs @ 6024

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

#1215

  • improved the way AlgorithmExecutor handles cancellation
File size: 4.2 KB
Line 
1using System;
2using System.Diagnostics;
3using System.Linq;
4using HeuristicLab.Common;
5using HeuristicLab.Core;
6using HeuristicLab.Data;
7using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
8using HeuristicLab.Operators;
9using HeuristicLab.Optimization;
10using HeuristicLab.Parameters;
11using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
12using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
13
14namespace HeuristicLab.Problems.MetaOptimization {
15  [Item("AlgorithmEvaluator", "")]
16  [StorableClass]
17  public class AlgorithmEvaluator : SingleSuccessorOperator {
18
19    #region Parameter properties
20    public ILookupParameter<IRandom> RandomParameter {
21      get { return (LookupParameter<IRandom>)Parameters["Random"]; }
22    }
23    public ILookupParameter<IAlgorithm> AlgorithmParameter {
24      get { return (LookupParameter<IAlgorithm>)Parameters["Algorithm"]; }
25    }
26    public ILookupParameter<IntValue> ProblemIndexParameter {
27      get { return (LookupParameter<IntValue>)Parameters["ProblemIndex"]; }
28    }
29    public ILookupParameter<IntValue> RepetitionIndexParameter {
30      get { return (LookupParameter<IntValue>)Parameters["RepetitionIndex"]; }
31    }
32    public ILookupParameter<ParameterConfigurationTree> ParameterConfigurationParameter {
33      get { return (ILookupParameter<ParameterConfigurationTree>)Parameters["ParameterConfigurationTree"]; }
34    }
35    public IValueParameter<IItemList<IRun>> RunsParameter {
36      get { return (IValueParameter<IItemList<IRun>>)Parameters["Runs"]; }
37    }
38    #endregion
39
40    [StorableConstructor]
41    protected AlgorithmEvaluator(bool deserializing) : base(deserializing) { }
42    public AlgorithmEvaluator()
43      : base() {
44      Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator which should be used to initialize the new random permutation."));
45      Parameters.Add(new LookupParameter<IAlgorithm>("Algorithm", ""));
46      Parameters.Add(new LookupParameter<IntValue>("ProblemIndex", ""));
47      Parameters.Add(new LookupParameter<IntValue>("RepetitionIndex", ""));
48      Parameters.Add(new LookupParameter<ParameterConfigurationTree>("ParameterConfigurationTree", ""));
49      Parameters.Add(new ValueParameter<IItemList<IRun>>("Runs", ""));
50    }
51    protected AlgorithmEvaluator(AlgorithmEvaluator original, Cloner cloner) : base(original, cloner) { }
52    public override IDeepCloneable Clone(Cloner cloner) {
53      return new AlgorithmEvaluator(this, cloner);
54    }
55
56    public override IOperation Apply() {
57      IAlgorithm algorithm = AlgorithmParameter.ActualValue;
58
59      #region Debug Code for SymbolicRegressionGrammar
60      var srp = (SymbolicRegressionSingleObjectiveProblem)algorithm.Problem;
61      //Console.WriteLine("Evaluating Grammar: " + PrintGrammar(srp.SymbolicExpressionTreeGrammar));
62      #endregion
63
64      var sw = new Stopwatch();
65      sw.Start();
66      //// prepare and clear is needed, if the algorithm has been started and stopped before (this happens when meta level algorithm is paused)
67      //algorithm.Prepare(); // <--- SHOULD NOT HAPPEN!
68      //algorithm.Runs.Clear();
69
70      algorithm.StartSync(CancellationToken);
71      sw.Stop();
72      Console.WriteLine("{0},{1}: {2} (Grammar: {3})", ProblemIndexParameter.ActualValue.Value, RepetitionIndexParameter.ActualValue.Value, sw.Elapsed, PrintGrammar(srp.SymbolicExpressionTreeGrammar));
73
74      RunsParameter.Value = new ItemList<IRun>(algorithm.Runs);
75
76      return base.Apply();
77    }
78
79    /// <summary>
80    /// This method should repair an invalid parameterConfiguration.
81    /// The strategy is to just randomize parameter settings. It's not guaranteed to be a vaild setting
82    /// </summary>
83    private void Repair(ParameterConfigurationTree parameterConfiguration, IRandom random) {
84      parameterConfiguration.Randomize(random);
85    }
86
87    // for debug purposes, remove later
88    private static string PrintGrammar(ISymbolicExpressionGrammar grammar) {
89      return string.Join(string.Empty, grammar.Symbols.Select(x => x.InitialFrequency).ToArray());
90      //foreach (var symbol in grammar.Symbols) {
91      //  Console.WriteLine("{0} ({1})", symbol.ToString(), symbol.InitialFrequency);
92      //}
93    }
94  }
95}
Note: See TracBrowser for help on using the repository browser.