Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1215

  • support for maximization problems
  • made base level algorithms stoppable
  • optimization for multiple goals possible (AverageQuality, AverageDeviation, AverageEvaluatedSolutions)
  • lots of fixes
File size: 4.0 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      algorithm.StartSync(CancellationToken);
67      sw.Stop();
68      Console.WriteLine("{0},{1}: {2} (Grammar: {3})", ProblemIndexParameter.ActualValue.Value, RepetitionIndexParameter.ActualValue.Value, sw.Elapsed, PrintGrammar(srp.SymbolicExpressionTreeGrammar));
69
70      RunsParameter.Value = new ItemList<IRun>(algorithm.Runs);
71
72      return base.Apply();
73    }
74
75    /// <summary>
76    /// This method should repair an invalid parameterConfiguration.
77    /// The strategy is to just randomize parameter settings. It's not guaranteed to be a vaild setting
78    /// </summary>
79    private void Repair(ParameterConfigurationTree parameterConfiguration, IRandom random) {
80      parameterConfiguration.Randomize(random);
81    }
82
83    // for debug purposes, remove later
84    private static string PrintGrammar(ISymbolicExpressionGrammar grammar) {
85      return string.Join(string.Empty, grammar.Symbols.Select(x => x.InitialFrequency).ToArray());
86      //foreach (var symbol in grammar.Symbols) {
87      //  Console.WriteLine("{0} ({1})", symbol.ToString(), symbol.InitialFrequency);
88      //}
89    }
90  }
91}
Note: See TracBrowser for help on using the repository browser.