Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2708_ScopedAlgorithms/HeuristicLab.Problems.Programmable/3.3/Templates/SingleObjectiveCombinedEncodingProblem_Template.cs

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

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

File size: 3.6 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
15namespace HeuristicLab.Problems.Programmable {
16  public class CompiledSingleObjectiveProblemDefinition : CompiledSingleObjectiveProblemDefinition<CombinedEncoding, CombinedSolution> {
17    public override bool Maximization { get { return true; } }
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
23      //Encoding.Add(new BinaryVectorEncoding("b") { Length = 10 });
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 });
27      //Encoding.Add(new LinearLinkageEncoding("lle") { Length = 30 });
28    }
29
30    public override double Evaluate(CombinedSolution solution, IRandom random) {
31      // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
32      var quality = 0.0;
33      //var b = solution.GetSolution<BinaryVector>("b");
34      //quality = b.Count(x => x); // one max!
35      //var r = solution.GetSolution<RealVector>("r");
36      //quality += r.Sum(x => -x * x); // sphere
37
38      // NOTE: Check the Maximization property above (true or false)!
39      return quality;
40    }
41
42    public override void Analyze(CombinedSolution[] solutions, double[] qualities, ResultCollection results, IRandom random) {
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
45      // Uncomment the following lines if you want to retrieve the best solution
46
47      //var orderedSolutions = solutions.Zip(qualities, (i, q) => new { Solution = i, Quality = q }).OrderBy(z => z.Quality);
48      //var best = Maximization ? orderedSolutions.Last().Solution : orderedSolutions.First().Solution;
49
50      //if (!results.ContainsKey("Best Solution")) {
51      //  results.Add(new Result("Best Solution", typeof(SOLUTION_CLASS)));
52      //}
53      //results["Best Solution"].Value = (IItem)best.Clone();
54    }
55
56    public override IEnumerable<CombinedSolution> GetNeighbors(CombinedSolution solution, IRandom random) {
57      // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
58      // Create new vectors, based on the given one that represent small changes
59      // This method is only called from move-based algorithms (Local Search, Simulated Annealing, etc.)
60      while (true) {
61        // Algorithm will draw only a finite amount of samples
62        // Change to a for-loop to return a concrete amount of neighbors
63        var neighbor = (CombinedSolution)solution.Clone();
64        // modify the solution specified as neighbor
65        yield return neighbor;
66      }
67    }
68
69    // Implement further classes and methods
70  }
71}
72
Note: See TracBrowser for help on using the repository browser.