1 | using System;
|
---|
2 | using System.Diagnostics;
|
---|
3 | using System.Linq;
|
---|
4 | using HeuristicLab.Common;
|
---|
5 | using HeuristicLab.Core;
|
---|
6 | using HeuristicLab.Data;
|
---|
7 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
8 | using HeuristicLab.Operators;
|
---|
9 | using HeuristicLab.Optimization;
|
---|
10 | using HeuristicLab.Parameters;
|
---|
11 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
12 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
|
---|
13 |
|
---|
14 | namespace 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 | }
|
---|