- Timestamp:
- 10/31/16 15:11:46 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Templates/EmptyAlgorithm/EmptyAlgorithm/MyAlgorithm.cs
r14362 r14364 1 using System.Threading; 1 using System; 2 using System.Threading; 2 3 using HeuristicLab.Common; // required for parameters collection 3 4 using HeuristicLab.Core; // required for parameters collection 4 5 using HeuristicLab.Data; // IntValue, ... 6 using HeuristicLab.Encodings.BinaryVectorEncoding; 5 7 using HeuristicLab.Optimization; // BasicAlgorithm 6 8 using HeuristicLab.Parameters; 7 9 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 10 using HeuristicLab.Problems.Binary; 11 using HeuristicLab.Random; // MersenneTwister 8 12 9 13 namespace EmptyAlgorithm { … … 17 21 [StorableClass] // for persistence (storing your algorithm to a files or transfer to HeuristicLab.Hive 18 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 19 29 #region parameters 20 30 // If an algorithm has parameters then we usually also add properties to access these parameters. … … 53 63 protected override void Run(CancellationToken cancellationToken) { 54 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 // ----------------------------- 55 83 56 84 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; 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; 63 92 64 93 // check the cancellation token to see if the used clicked "Stop"
Note: See TracChangeset
for help on using the changeset viewer.