Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.Programmable/3.3/Templates/SingleObjectiveCombinedEncodingProblem_Template.cs @ 17381

Last change on this file since 17381 was 17366, checked in by mkommend, 5 years ago

#2521: Fixed bugs in solution context and adapted programmable problem template to include a cancellation token in the evaluate function.

File size: 3.7 KB
Line 
1using System;
2using System.Linq;
3using System.Collections.Generic;
4using System.Threading;
5using HeuristicLab.Common;
6using HeuristicLab.Core;
7using HeuristicLab.Data;
8using HeuristicLab.Optimization;
9using HeuristicLab.Encodings.BinaryVectorEncoding;
10using HeuristicLab.Encodings.IntegerVectorEncoding;
11using HeuristicLab.Encodings.RealVectorEncoding;
12using HeuristicLab.Encodings.PermutationEncoding;
13using HeuristicLab.Encodings.LinearLinkageEncoding;
14using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
15
16namespace HeuristicLab.Problems.Programmable {
17  public class CompiledSingleObjectiveProblemDefinition : CompiledSingleObjectiveProblemDefinition<CombinedEncoding, CombinedSolution> {
18    public override bool Maximization { get { return true; } }
19
20    public override void Initialize() {
21      // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
22      // Define e.g. the length of the solution encoding or the solution creator by modifying the Encoding property
23      // Add additional initialization code e.g. private variables that you need for evaluating
24      Encoding.Add(new BinaryVectorEncoding("b") { Length = 10 });
25      //Encoding.Add(new IntegerVectorEncoding("i") { Length = 10, Bounds = new IntMatrix(new int[,] { { -100, 100 } }) });
26      Encoding.Add(new RealVectorEncoding("r") { Length = 10, Bounds = new DoubleMatrix(new double[,] { { -100, 100 } }) });
27      //Encoding.Add(new PermutationEncoding("p") { Length = 20, Type = PermutationTypes.Absolute });
28      //Encoding.Add(new LinearLinkageEncoding("lle") { Length = 30 });
29    }
30
31    public override double Evaluate(CombinedSolution solution, IRandom random, CancellationToken cancellationToken) {
32      // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
33      var quality = 0.0;
34      var b = solution.GetEncodedSolution<BinaryVector>("b");
35      quality = b.Count(x => x); // one max!
36      var r = solution.GetEncodedSolution<RealVector>("r");
37      quality += r.Sum(x => -x * x); // sphere
38
39      // NOTE: Check the Maximization property above (true or false)!
40      return quality;
41    }
42
43    public override void Analyze(CombinedSolution[] solutions, double[] qualities, ResultCollection results, IRandom random) {
44      // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
45      // Write or update results given the range of vectors and resulting qualities
46      // Uncomment the following lines if you want to retrieve the best solution
47
48      //var orderedSolutions = solutions.Zip(qualities, (i, q) => new { Solution = i, Quality = q }).OrderBy(z => z.Quality);
49      //var best = Maximization ? orderedSolutions.Last().Solution : orderedSolutions.First().Solution;
50
51      //if (!results.ContainsKey("Best Solution")) {
52      //  results.Add(new Result("Best Solution", typeof(CombinedSolution)));
53      //}
54      //results["Best Solution"].Value = (IItem)best.Clone();
55    }
56
57    public override IEnumerable<CombinedSolution> GetNeighbors(CombinedSolution solution, IRandom random) {
58      // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
59      // Create new vectors, based on the given one that represent small changes
60      // This method is only called from move-based algorithms (Local Search, Simulated Annealing, etc.)
61      while (true) {
62        // Algorithm will draw only a finite amount of samples
63        // Change to a for-loop to return a concrete amount of neighbors
64        var neighbor = (CombinedSolution)solution.Clone();
65        // modify the solution specified as neighbor
66        yield return neighbor;
67      }
68    }
69
70    // Implement further classes and methods
71  }
72}
73
Note: See TracBrowser for help on using the repository browser.