Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProgrammableProblem/HeuristicLab.Problems.ExternalEvaluation/3.4/Programmable/CompiledSingleObjectiveOptimizationSupport.cs @ 11893

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

#2174:

  • Added possibility to define neighborhood and analyze function for external evaluation problems
File size: 2.0 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
10namespace HeuristicLab.Problems.ExternalEvaluation {
11  public class CompiledSingleObjectiveOptimizationSupport : CompiledOptimizationSupport, ISingleObjectiveOptimizationSupport {
12
13    public void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
14      // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
15      // Write or update results given the range of vectors and resulting qualities
16      // Uncomment the following lines if you want to retrieve the best individual
17      //var bestIndex = Maximization ?
18      //         qualities.Select((v, i) => Tuple.Create(i, v)).OrderByDescending(x => x.Item2).First().Item1
19      //       : qualities.Select((v, i) => Tuple.Create(i, v)).OrderBy(x => x.Item2).First().Item1;
20      //var best = individuals[bestIndex];
21    }
22
23    public IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random) {
24      // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
25      // Create new vectors, based on the given one that represent small changes
26      // This method is only called from move-based algorithms (Local Search, Simulated Annealing, etc.)
27      while (true) {
28        // Algorithm will draw only a finite amount of samples
29        // Change to a for-loop to return a concrete amount of neighbors
30        var neighbor = individual.Copy();
31        // For instance, perform a single bit-flip in a binary parameter
32        //var bIndex = random.Next(neighbor.BinaryVector("b").Length);
33        //neighbor.BinaryVector("b")[bIndex] = !neighbor.BinaryVector("b")[bIndex];
34        yield return neighbor;
35      }
36    }
37
38    // Implement further classes and methods
39  }
40}
41
Note: See TracBrowser for help on using the repository browser.