1 | using System;
|
---|
2 | using System.Threading;
|
---|
3 | using HeuristicLab.Common; // required for parameters collection
|
---|
4 | using HeuristicLab.Core; // required for parameters collection
|
---|
5 | using HeuristicLab.Data; // IntValue, ...
|
---|
6 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
7 | using HeuristicLab.Optimization; // BasicAlgorithm
|
---|
8 | using HeuristicLab.Parameters;
|
---|
9 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
10 | using HeuristicLab.Problems.Binary;
|
---|
11 | using HeuristicLab.Random; // MersenneTwister
|
---|
12 |
|
---|
13 | namespace EmptyAlgorithm {
|
---|
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 = "MyAlgorithm", Description = "An demo 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 | [StorableClass] // for persistence (storing your algorithm to a files or transfer to HeuristicLab.Hive
|
---|
22 | public class MyAlgorithm : 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 MyAlgorithm() {
|
---|
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 MyAlgorithm(bool deserializing) : base(deserializing) { }
|
---|
52 |
|
---|
53 | // Each clonable item must have a cloning ctor (deep cloning, the cloner is used to handle cyclic object references)
|
---|
54 | public MyAlgorithm(MyAlgorithm 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 MyAlgorithm(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 | for (int i = 0; i < maxIters; i++) {
|
---|
78 | curItersItem.Value = i;
|
---|
79 |
|
---|
80 | // -----------------------------
|
---|
81 | // IMPLEMENT YOUR ALGORITHM HERE
|
---|
82 | // -----------------------------
|
---|
83 |
|
---|
84 |
|
---|
85 | // this is an example for random search
|
---|
86 | // for a more elaborate algorithm check the source code of "HeuristicLab.Algorithms.ParameterlessPopulationPyramid"
|
---|
87 | var cand = new BinaryVector(problem.Length, rand);
|
---|
88 | var quality = problem.Evaluate(cand, rand); // calling Evaluate like this is not possible for all problems...
|
---|
89 | if (problem.Maximization) bestQuality = Math.Max(bestQuality, quality);
|
---|
90 | else bestQuality = Math.Min(quality, bestQuality);
|
---|
91 | bestQualityItem.Value = bestQuality;
|
---|
92 |
|
---|
93 | // check the cancellation token to see if the used clicked "Stop"
|
---|
94 | if (cancellationToken.IsCancellationRequested) break;
|
---|
95 | }
|
---|
96 |
|
---|
97 | Results.Add(new Result("Execution time", new TimeSpanValue(this.ExecutionTime)));
|
---|
98 | }
|
---|
99 |
|
---|
100 | public override bool SupportsPause {
|
---|
101 | get { return false; }
|
---|
102 | }
|
---|
103 | }
|
---|
104 | }
|
---|