Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2521: Integrated changes of #2943 into problem refactoring branch.

File size: 3.5 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 CompiledMultiObjectiveProblemDefinition : CompiledMultiObjectiveProblemDefinition<CombinedEncoding, CombinedSolution> {
18    public override bool[] Maximization { get { return new[] { true, false }; } }
19    public override double[] ReferencePoint { get { return null; } }
20    public override IReadOnlyList<double[]> BestKnownFront { get { return null; } }
21
22    public override void Initialize() {
23      // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
24      // Define e.g. the length of the solution encoding or the solution creator by modifying the Encoding property
25      // Add additional initialization code e.g. private variables that you need for evaluating
26      Encoding.Add(new BinaryVectorEncoding("b") { Length = 10 });
27      //Encoding.Add(new IntegerVectorEncoding("i") { Length = 10, Bounds = new IntMatrix(new int[,] { { -100, 100 } }) });
28      Encoding.Add(new RealVectorEncoding("r") { Length = 10, Bounds = new DoubleMatrix(new double[,] { { -100, 100 } }) });
29      //Encoding.Add(new PermutationEncoding("p") { Length = 20, Type = PermutationTypes.Absolute });
30      //Encoding.Add(new LinearLinkageEncoding("lle") { Length = 30 });
31    }
32
33    public override double[] Evaluate(CombinedSolution solution, IRandom random) {
34      // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
35      var quality = new[] { 0.0, 0.0 };
36      var b = solution.GetEncodedSolution<BinaryVector>("b");
37      quality[0] = b.Count(x => x); // one max!
38      var r = solution.GetEncodedSolution<RealVector>("r");
39      quality[1] = r.Select((i, v) => new { Idx = i, Val = v }).Sum(x => b[x.Idx] ? x.Val * x.Val : 0.0); // sphere
40
41      // NOTE: Check the Maximization property above (true or false)!
42      return quality;
43    }
44
45    public override void Analyze(CombinedSolution[] solutions, double[][] qualities, ResultCollection results, IRandom random) {
46      // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
47      // Write or update results given the range of vectors and resulting qualities
48      // Uncomment the following lines if you want to retrieve the best solution
49    }
50
51    public override IEnumerable<CombinedSolution> GetNeighbors(CombinedSolution solution, IRandom random) {
52      // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
53      // Create new vectors, based on the given one that represent small changes
54      // This method is only called from move-based algorithms (Local Search, Simulated Annealing, etc.)
55      while (true) {
56        // Algorithm will draw only a finite amount of samples
57        // Change to a for-loop to return a concrete amount of neighbors
58        var neighbor = (CombinedSolution)solution.Clone();
59        // modify the solution specified as neighbor
60        yield return neighbor;
61      }
62    }
63
64    // Implement further classes and methods
65  }
66}
67
Note: See TracBrowser for help on using the repository browser.