Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3022-FastFunctionExtraction/FFX/FastFunctionExtraction.cs @ 17218

Last change on this file since 17218 was 17218, checked in by lleko, 5 years ago

#3022 renamed C#-Project, Solution, Plugin and Algorithm
created sample data
downloaded python repo
created script for testing core.py functionality of python repo

File size: 5.0 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.Encodings.BinaryVectorEncoding;
7using HeuristicLab.Optimization; // BasicAlgorithm
8using HeuristicLab.Parameters;
9using HeuristicLab.Problems.Binary;
10using HeuristicLab.Random; // MersenneTwister
11using HEAL.Attic;
12
13namespace HeuristicLab.Algorithms.DataAnalysis.FastFunctionExtraction {
14  // each HL item needs to have a name and a description (BasicAlgorithm is an Item)
15  // The name and description of items is shown in the GUI<<<<
16  [Item(Name = "FastFunctionExtraction", Description = "An FFX algorithm.")]
17
18  // 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
19  [Creatable(Category = CreatableAttribute.Categories.Algorithms, Priority = 999)]
20
21  [StorableType("689280F7-E371-44A2-98A5-FCEDF22CA343")] // for persistence (storing your algorithm to a files or transfer to HeuristicLab.Hive
22  public class FastFunctionExtraction : BasicAlgorithm {
23    // This algorithm only works for BinaryProblems.
24    // Overriding the ProblemType property has the effect that only BinaryProblems can be set as problem
25    // for the algorithm in the GUI
26    public override Type ProblemType { get { return typeof(BinaryProblem); } }
27    public new BinaryProblem Problem { get { return (BinaryProblem)base.Problem; } }
28   
29    #region parameters
30    // If an algorithm has parameters then we usually also add properties to access these parameters.
31    // This is not strictly required but considered good shape.
32    private IFixedValueParameter<IntValue> MaxIterationsParameter {
33      get { return (IFixedValueParameter<IntValue>)Parameters["MaxIterations"]; }
34    }
35    public int MaxIterations {
36      get { return MaxIterationsParameter.Value.Value; }
37      set { MaxIterationsParameter.Value.Value = value; }
38    }
39    #endregion
40
41    // createable items must have a default ctor
42    public FastFunctionExtraction() {
43      // algorithm parameters are shown in the GUI
44      Parameters.Add(new FixedValueParameter<IntValue>("MaxIterations", new IntValue(10000)));
45    }
46
47    // Persistence uses this ctor to improve deserialization efficiency.
48    // If we would use the default ctor instead this would completely initialize the object (e.g. creating parameters)
49    // even though the data is later overwritten by the stored data.
50    [StorableConstructor]
51    public FastFunctionExtraction(StorableConstructorFlag _) : base(_) { }
52
53    // Each clonable item must have a cloning ctor (deep cloning, the cloner is used to handle cyclic object references)
54    public FastFunctionExtraction(FastFunctionExtraction original, Cloner cloner) : base(original, cloner) {
55      // Don't forget to call the cloning ctor of the base class
56      // This class does not have fields, therefore we don't need to actually clone anything
57    }
58
59    public override IDeepCloneable Clone(Cloner cloner) {
60      return new FastFunctionExtraction(this, cloner);
61    }
62
63    protected override void Run(CancellationToken cancellationToken) {
64      int maxIters = MaxIterations;
65      var problem = Problem;
66      var rand = new MersenneTwister(1234);
67
68      var bestQuality = problem.Maximization ? double.MinValue : double.MaxValue;
69
70      var curItersItem = new IntValue();
71      var bestQualityItem = new DoubleValue(bestQuality);
72      var curItersResult = new Result("Iteration", curItersItem);
73      var bestQualityResult = new Result("Best quality", bestQualityItem);
74      Results.Add(curItersResult);
75      Results.Add(bestQualityResult);
76
77      var funcs = generateBasisFunctions();
78
79      for (int i = 0; i < maxIters; i++) {
80        curItersItem.Value = i;
81
82        // -----------------------------
83        // IMPLEMENT YOUR ALGORITHM HERE
84        // -----------------------------
85
86
87        // this is an example for random search
88        // for a more elaborate algorithm check the source code of "HeuristicLab.Algorithms.ParameterlessPopulationPyramid"
89        var cand = new BinaryVector(problem.Length, rand);
90        var quality = problem.Evaluate(cand, rand); // calling Evaluate like this is not possible for all problems...
91        if (problem.Maximization) bestQuality = Math.Max(bestQuality, quality);
92        else bestQuality = Math.Min(quality, bestQuality);
93        bestQualityItem.Value = bestQuality;
94
95        // check the cancellation token to see if the used clicked "Stop"
96        if (cancellationToken.IsCancellationRequested) break;
97      }
98
99      Results.Add(new Result("Execution time", new TimeSpanValue(this.ExecutionTime)));
100    }
101
102       
103        private object generateBasisFunctions()
104        {
105            throw new NotImplementedException();
106        }
107
108        public override bool SupportsPause {
109      get { return false; }
110    }
111  }
112}
Note: See TracBrowser for help on using the repository browser.