Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.Programmable/3.3/Templates/CompiledSingleObjectiveProblemDefinition.cs @ 12015

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

#2174: Adapted script template for programmable problem (single-objective).

File size: 3.7 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
47      //var orderedIndividuals = individuals.Zip(qualities, (i, q) => new { Individual = i, Quality = q }).OrderBy(z => z.Quality);
48      //var best = Maximization ? orderedIndividuals.Last().Individual : orderedIndividuals.First().Individual;
49
50      //if (!results.ContainsKey("Best Solution")) {
51      //  results.Add(new Result("Best Solution", typeof(RealVector)));
52      //}
53      //results["Best Solution"].Value = (IItem)best.RealVector("r").Clone();
54    }
55
56    public IEnumerable<Individual> GetNeighbors(Individual individual, 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 = individual.Copy();
64        // For instance, perform a single bit-flip in a binary parameter
65        //var bIndex = random.Next(neighbor.BinaryVector("b").Length);
66        //neighbor.BinaryVector("b")[bIndex] = !neighbor.BinaryVector("b")[bIndex];
67        yield return neighbor;
68      }
69    }
70
71    // Implement further classes and methods
72  }
73}
74
Note: See TracBrowser for help on using the repository browser.