[14364] | 1 | using System;
|
---|
| 2 | using System.Threading;
|
---|
[14362] | 3 | using HeuristicLab.Common; // required for parameters collection
|
---|
| 4 | using HeuristicLab.Core; // required for parameters collection
|
---|
| 5 | using HeuristicLab.Data; // IntValue, ...
|
---|
[14364] | 6 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
[14362] | 7 | using HeuristicLab.Optimization; // BasicAlgorithm
|
---|
| 8 | using HeuristicLab.Parameters;
|
---|
[14364] | 9 | using HeuristicLab.Problems.Binary;
|
---|
| 10 | using HeuristicLab.Random; // MersenneTwister
|
---|
[17199] | 11 | using HEAL.Attic;
|
---|
[14362] | 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 |
|
---|
[17199] | 21 | [StorableType("689280F7-E371-44A2-98A5-FCEDF22CA343")] // for persistence (storing your algorithm to a files or transfer to HeuristicLab.Hive
|
---|
[14362] | 22 | public class MyAlgorithm : BasicAlgorithm {
|
---|
[14364] | 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 |
|
---|
[14362] | 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]
|
---|
[17199] | 51 | public MyAlgorithm(StorableConstructorFlag _) : base(_) { }
|
---|
[14362] | 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;
|
---|
[14364] | 65 | var problem = Problem;
|
---|
| 66 | var rand = new MersenneTwister(1234);
|
---|
[14362] | 67 |
|
---|
[14364] | 68 | var bestQuality = problem.Maximization ? double.MinValue : double.MaxValue;
|
---|
[14362] | 69 |
|
---|
[14364] | 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);
|
---|
[14362] | 74 | Results.Add(curItersResult);
|
---|
[14364] | 75 | Results.Add(bestQualityResult);
|
---|
[14362] | 76 |
|
---|
| 77 | for (int i = 0; i < maxIters; i++) {
|
---|
[14364] | 78 | curItersItem.Value = i;
|
---|
[14362] | 79 |
|
---|
[14364] | 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 |
|
---|
[14362] | 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 | }
|
---|
[14816] | 99 |
|
---|
| 100 | public override bool SupportsPause {
|
---|
| 101 | get { return false; }
|
---|
| 102 | }
|
---|
[14362] | 103 | }
|
---|
| 104 | }
|
---|