Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.Programmable/3.3/Templates/CompiledSingleObjectiveProblemDefinition.cs @ 15377

Last change on this file since 15377 was 12746, checked in by ascheibe, 9 years ago

#2426 merged r12724 and r12731 into stable

File size: 4.1 KB
RevLine 
[11753]1using System;
2using System.Linq;
3using System.Collections.Generic;
4using HeuristicLab.Common;
5using HeuristicLab.Core;
6using HeuristicLab.Data;
[11949]7using HeuristicLab.Encodings.BinaryVectorEncoding;
8using HeuristicLab.Encodings.IntegerVectorEncoding;
[12746]9using HeuristicLab.Encodings.RealVectorEncoding;
[11753]10using HeuristicLab.Encodings.PermutationEncoding;
[12746]11using HeuristicLab.Encodings.LinearLinkageEncoding;
12using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
[11753]13using HeuristicLab.Optimization;
14using HeuristicLab.Problems.Programmable;
15
16namespace HeuristicLab.Problems.Programmable {
17  public class CompiledSingleObjectiveProblemDefinition : CompiledProblemDefinition, ISingleObjectiveProblemDefinition {
18    public bool Maximization { get { return false; } }
19
20    public override void Initialize() {
[11880]21      // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
[11753]22      // Define the solution encoding which can also consist of multiple vectors, examples below
[11900]23      //Encoding = new BinaryVectorEncoding("b", length: 5);
24      //Encoding = new IntegerVectorEncoding("i", length: 5, min: 2, max: 14, step: 2);
25      //Encoding = new RealVectorEncoding("r", length: 5, min: -1.0, max: 1.0);
[11753]26      //Encoding = new PermutationEncoding("p", length: 5, type: PermutationTypes.Absolute);
[12746]27      //Encoding = new LinearLinkageEncoding("l", length: 5);
28      //Encoding = new SymbolicExpressionTreeEncoding("s", new SimpleSymbolicExpressionGrammar(), 50, 12);
[11880]29      // The encoding can also be a combination
[11753]30      //Encoding = new MultiEncoding()
[11900]31      //.Add(new BinaryVectorEncoding("b", length: 5))
32      //.Add(new IntegerVectorEncoding("i", length: 5, min: 2, max: 14, step: 4))
33      //.Add(new RealVectorEncoding("r", length: 5, min: -1.0, max: 1.0))
[11753]34      //.Add(new PermutationEncoding("p", length: 5, type: PermutationTypes.Absolute))
[12746]35      //.Add(new LinearLinkageEncoding("l", length: 5))
36      //.Add(new SymbolicExpressionTreeEncoding("s", new SimpleSymbolicExpressionGrammar(), 50, 12))
[11880]37      ;
38      // Add additional initialization code e.g. private variables that you need for evaluating
[11753]39    }
40
41    public double Evaluate(Individual individual, IRandom random) {
[11880]42      // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
[11753]43      var quality = 0.0;
44      //quality = individual.RealVector("r").Sum(x => x * x);
45      return quality;
46    }
47
[11880]48    public void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
49      // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
50      // Write or update results given the range of vectors and resulting qualities
51      // Uncomment the following lines if you want to retrieve the best individual
[12005]52
53      //var orderedIndividuals = individuals.Zip(qualities, (i, q) => new { Individual = i, Quality = q }).OrderBy(z => z.Quality);
54      //var best = Maximization ? orderedIndividuals.Last().Individual : orderedIndividuals.First().Individual;
55
56      //if (!results.ContainsKey("Best Solution")) {
57      //  results.Add(new Result("Best Solution", typeof(RealVector)));
58      //}
59      //results["Best Solution"].Value = (IItem)best.RealVector("r").Clone();
[11753]60    }
61
62    public IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random) {
[11880]63      // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
[11753]64      // Create new vectors, based on the given one that represent small changes
[11880]65      // This method is only called from move-based algorithms (Local Search, Simulated Annealing, etc.)
[11753]66      while (true) {
[11880]67        // Algorithm will draw only a finite amount of samples
68        // Change to a for-loop to return a concrete amount of neighbors
[11753]69        var neighbor = individual.Copy();
[11880]70        // For instance, perform a single bit-flip in a binary parameter
[11753]71        //var bIndex = random.Next(neighbor.BinaryVector("b").Length);
72        //neighbor.BinaryVector("b")[bIndex] = !neighbor.BinaryVector("b")[bIndex];
73        yield return neighbor;
74      }
75    }
76
[11880]77    // Implement further classes and methods
[11753]78  }
79}
80
Note: See TracBrowser for help on using the repository browser.