Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2929_PrioritizedGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.PGE/3.3/PGE.cs @ 16003

Last change on this file since 16003 was 15991, checked in by gkronber, 6 years ago

#2929 prepared plugin and empty algorithm class for PGE implementation

File size: 4.5 KB
Line 
1using System;
2using System.Threading;
3using HeuristicLab.Common; // required for parameters collection
4using HeuristicLab.Core; // required for parameters collection
5using HeuristicLab.Data; // IntValue, ...
6using HeuristicLab.Optimization; // BasicAlgorithm
7using HeuristicLab.Parameters;
8using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
9using HeuristicLab.Problems.DataAnalysis;
10using HeuristicLab.Random; // MersenneTwister
11
12namespace EmptyAlgorithm {
13  // each HL item needs to have a name and a description (BasicAlgorithm is an Item)
14  // The name and description of items is shown in the GUI
15  [Item(Name = "Priorizied Grammar Enumeration (PGE)", Description = "Priorizied grammar enumeration algorithm. Worm, T. and Chiu K., 'Prioritized Grammar Enumeration: Symbolic Regression by Dynamic Programming'. GECCO 2013")]
16
17  // If the algorithm should be shown in the "New..." dialog it must be creatable. Entries in the new dialog are grouped to categories and ordered by priorities
18  [Creatable(Category = CreatableAttribute.Categories.Algorithms, Priority = 999)]
19
20  [StorableClass] // for persistence (storing your algorithm to a files or transfer to HeuristicLab.Hive)
21  public class PGE : BasicAlgorithm {
22    // This algorithm only works for BinaryProblems.
23    // Overriding the ProblemType property has the effect that only RegressionProblems can be set as problem
24    // for the algorithm in the GUI
25    public override Type ProblemType { get { return typeof(RegressionProblem); } }
26    public new RegressionProblem Problem { get { return (RegressionProblem)base.Problem; } }
27
28    #region parameters
29    // If an algorithm has parameters then we usually also add properties to access these parameters.
30    // This is not strictly required but considered good shape.
31    private IFixedValueParameter<IntValue> MaxIterationsParameter {
32      get { return (IFixedValueParameter<IntValue>)Parameters["MaxIterations"]; }
33    }
34    public int MaxIterations {
35      get { return MaxIterationsParameter.Value.Value; }
36      set { MaxIterationsParameter.Value.Value = value; }
37    }
38    #endregion
39
40    // createable items must have a default ctor
41    public PGE() {
42      // algorithm parameters are shown in the GUI
43      Parameters.Add(new FixedValueParameter<IntValue>("MaxIterations", new IntValue(10000)));
44    }
45
46    // Persistence uses this ctor to improve deserialization efficiency.
47    // If we would use the default ctor instead this would completely initialize the object (e.g. creating parameters)
48    // even though the data is later overwritten by the stored data.
49    [StorableConstructor]
50    public PGE(bool deserializing) : base(deserializing) { }
51
52    // Each clonable item must have a cloning ctor (deep cloning, the cloner is used to handle cyclic object references)
53    public PGE(PGE original, Cloner cloner) : base(original, cloner) {
54      // Don't forget to call the cloning ctor of the base class
55      // TODO: if this class has fields then they need to be cloned here
56    }
57
58    public override IDeepCloneable Clone(Cloner cloner) {
59      return new PGE(this, cloner);
60    }
61
62    protected override void Run(CancellationToken cancellationToken) {
63      int maxIters = MaxIterations;
64      var problem = Problem;
65      var rand = new MersenneTwister(1234);
66
67      var bestQuality = 0.0;
68
69      var curItersItem = new IntValue();
70      var bestQualityItem = new DoubleValue(bestQuality);
71      var curItersResult = new Result("Iteration", curItersItem);
72      var bestQualityResult = new Result("Best quality", bestQualityItem);
73      Results.Add(curItersResult);
74      Results.Add(bestQualityResult);
75
76      for (int i = 0; i < maxIters; i++) {
77        curItersItem.Value = i;
78
79        // -----------------------------
80        // IMPLEMENT YOUR ALGORITHM HERE
81        // -----------------------------
82
83        // for a more elaborate algorithm check the source code of "HeuristicLab.Algorithms.ParameterlessPopulationPyramid"
84
85        var quality = rand.NextDouble();
86        if (quality > bestQuality) {
87          bestQuality = quality;
88          bestQualityItem.Value = quality;
89        }
90
91        // check the cancellation token to see if the used clicked "Stop"
92        if (cancellationToken.IsCancellationRequested) break;
93      }
94
95      Results.Add(new Result("Execution time", new TimeSpanValue(this.ExecutionTime)));
96    }
97
98    public override bool SupportsPause {
99      get { return false; }
100    }
101  }
102}
Note: See TracBrowser for help on using the repository browser.