Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProblemRefactoring/HeuristicLab.Problems.Programmable/3.3/Templates/MultiObjectiveCombinedEncodingProblem_Template.cs @ 13373

Last change on this file since 13373 was 13373, checked in by abeham, 8 years ago

#2521: adapted templates, fixed missing reference in outdated plugin lawnmower

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