Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.Programmable/3.3/Templates/MultiObjectiveCombinedEncodingProblem_Template.cs @ 16801

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

#2521: Merged trunk changes and adapted programmable problem templates for combined solutions.

File size: 3.4 KB
Line 
1using System;
2using System.Linq;
3using System.Collections.Generic;
4using HeuristicLab.Common;
5using HeuristicLab.Core;
6using HeuristicLab.Data;
7using HeuristicLab.Optimization;
8using HeuristicLab.Problems.Programmable;
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 : CompiledMultiObjectiveProblemDefinition<CombinedEncoding, CombinedSolution> {
18    public override bool[] Maximization { get { return new[] { true, false }; } }
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) {
32      // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
33      var quality = new[] { 0.0, 0.0 };
34      var b = solution.GetEncodedSolution<BinaryVector>("b");
35      quality[0] = b.Count(x => x); // one max!
36      var r = solution.GetEncodedSolution<RealVector>("r");
37      quality[1] = r.Select((i, v) => new { Idx = i, Val = v }).Sum(x => b[x.Idx] ? x.Val * x.Val : 0.0); // 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
49    public override IEnumerable<CombinedSolution> GetNeighbors(CombinedSolution solution, IRandom random) {
50      // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
51      // Create new vectors, based on the given one that represent small changes
52      // This method is only called from move-based algorithms (Local Search, Simulated Annealing, etc.)
53      while (true) {
54        // Algorithm will draw only a finite amount of samples
55        // Change to a for-loop to return a concrete amount of neighbors
56        var neighbor = (CombinedSolution)solution.Clone();
57        // modify the solution specified as neighbor
58        yield return neighbor;
59      }
60    }
61
62    // Implement further classes and methods
63  }
64}
65
Note: See TracBrowser for help on using the repository browser.