Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Templates/CompiledSingleObjectiveProblemDefinition.cs @ 11949

Last change on this file since 11949 was 11949, checked in by mkommend, 10 years ago

#2174: Distributed files in programmable problem branch to the correct plugins.

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.Encodings.BinaryVectorEncoding;
8using HeuristicLab.Encodings.IntegerVectorEncoding;
9using HeuristicLab.Encodings.PermutationEncoding;
10using HeuristicLab.Encodings.RealVectorEncoding;
11using HeuristicLab.Optimization;
12using HeuristicLab.Problems.Programmable;
13
14namespace HeuristicLab.Problems.Programmable {
15  public class CompiledSingleObjectiveProblemDefinition : CompiledProblemDefinition, ISingleObjectiveProblemDefinition {
16    public bool Maximization { get { return false; } }
17
18    public override void Initialize() {
19      // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
20      // Define the solution encoding which can also consist of multiple vectors, examples below
21      //Encoding = new BinaryVectorEncoding("b", length: 5);
22      //Encoding = new IntegerVectorEncoding("i", length: 5, min: 2, max: 14, step: 2);
23      //Encoding = new RealVectorEncoding("r", length: 5, min: -1.0, max: 1.0);
24      //Encoding = new PermutationEncoding("p", length: 5, type: PermutationTypes.Absolute);
25      // The encoding can also be a combination
26      //Encoding = new MultiEncoding()
27      //.Add(new BinaryVectorEncoding("b", length: 5))
28      //.Add(new IntegerVectorEncoding("i", length: 5, min: 2, max: 14, step: 4))
29      //.Add(new RealVectorEncoding("r", length: 5, min: -1.0, max: 1.0))
30      //.Add(new PermutationEncoding("p", length: 5, type: PermutationTypes.Absolute))
31      ;
32      // Add additional initialization code e.g. private variables that you need for evaluating
33    }
34
35    public double Evaluate(Individual individual, IRandom random) {
36      // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
37      var quality = 0.0;
38      //quality = individual.RealVector("r").Sum(x => x * x);
39      return quality;
40    }
41
42    public void Analyze(Individual[] individuals, 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 individual
46      //var bestIndex = Maximization ?
47      //         qualities.Select((v, i) => Tuple.Create(i, v)).OrderByDescending(x => x.Item2).First().Item1
48      //       : qualities.Select((v, i) => Tuple.Create(i, v)).OrderBy(x => x.Item2).First().Item1;
49      //var best = individuals[bestIndex];
50    }
51
52    public IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random) {
53      // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
54      // Create new vectors, based on the given one that represent small changes
55      // This method is only called from move-based algorithms (Local Search, Simulated Annealing, etc.)
56      while (true) {
57        // Algorithm will draw only a finite amount of samples
58        // Change to a for-loop to return a concrete amount of neighbors
59        var neighbor = individual.Copy();
60        // For instance, perform a single bit-flip in a binary parameter
61        //var bIndex = random.Next(neighbor.BinaryVector("b").Length);
62        //neighbor.BinaryVector("b")[bIndex] = !neighbor.BinaryVector("b")[bIndex];
63        yield return neighbor;
64      }
65    }
66
67    // Implement further classes and methods
68  }
69}
70
Note: See TracBrowser for help on using the repository browser.