Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 14728 was 13390, checked in by abeham, 9 years ago

#2521: fixed template and bug in programmable problem with multi encoding

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