Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Templates/EmptyAlgorithm/EmptyAlgorithm/MyAlgorithm.cs @ 14362

Last change on this file since 14362 was 14362, checked in by gkronber, 7 years ago

added algorithm template

File size: 3.1 KB
Line 
1using System.Threading;
2using HeuristicLab.Common; // required for parameters collection
3using HeuristicLab.Core; // required for parameters collection
4using HeuristicLab.Data; // IntValue, ...
5using HeuristicLab.Optimization; // BasicAlgorithm
6using HeuristicLab.Parameters;
7using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
8
9namespace EmptyAlgorithm {
10  // each HL item needs to have a name and a description (BasicAlgorithm is an Item)
11  // The name and description of items is shown in the GUI
12  [Item(Name = "MyAlgorithm", Description = "An demo algorithm.")]
13
14  // 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
15  [Creatable(Category = CreatableAttribute.Categories.Algorithms, Priority = 999)]
16
17  [StorableClass] // for persistence (storing your algorithm to a files or transfer to HeuristicLab.Hive
18  public class MyAlgorithm : BasicAlgorithm {
19    #region parameters
20    // If an algorithm has parameters then we usually also add properties to access these parameters.
21    // This is not strictly required but considered good shape.
22    private IFixedValueParameter<IntValue> MaxIterationsParameter {
23      get { return (IFixedValueParameter<IntValue>)Parameters["MaxIterations"]; }
24    }
25    public int MaxIterations {
26      get { return MaxIterationsParameter.Value.Value; }
27      set { MaxIterationsParameter.Value.Value = value; }
28    }
29    #endregion
30
31    // createable items must have a default ctor
32    public MyAlgorithm() {
33      // algorithm parameters are shown in the GUI
34      Parameters.Add(new FixedValueParameter<IntValue>("MaxIterations", new IntValue(10000)));
35    }
36
37    // Persistence uses this ctor to improve deserialization efficiency.
38    // If we would use the default ctor instead this would completely initialize the object (e.g. creating parameters)
39    // even though the data is later overwritten by the stored data.
40    [StorableConstructor]
41    public MyAlgorithm(bool deserializing) : base(deserializing) { }
42
43    // Each clonable item must have a cloning ctor (deep cloning, the cloner is used to handle cyclic object references)
44    public MyAlgorithm(MyAlgorithm original, Cloner cloner) : base(original, cloner) {
45      // Don't forget to call the cloning ctor of the base class
46      // This class does not have fields, therefore we don't need to actually clone anything
47    }
48
49    public override IDeepCloneable Clone(Cloner cloner) {
50      return new MyAlgorithm(this, cloner);
51    }
52
53    protected override void Run(CancellationToken cancellationToken) {
54      int maxIters = MaxIterations;
55
56
57      var curIters = new IntValue();
58      var curItersResult = new Result("Iteration", curIters);
59      Results.Add(curItersResult);
60
61      for (int i = 0; i < maxIters; i++) {
62        curIters.Value = i;
63
64        // check the cancellation token to see if the used clicked "Stop"
65        if (cancellationToken.IsCancellationRequested) break;
66      }
67
68      Results.Add(new Result("Execution time", new TimeSpanValue(this.ExecutionTime)));
69    }
70  }
71}
Note: See TracBrowser for help on using the repository browser.