Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.Programmable/3.3/Templates/SingleObjectiveProblem_Template.cs

Last change on this file was 17383, checked in by mkommend, 5 years ago

#2521: Adapted analyze methods of single-obj problems.

File size: 2.7 KB
Line 
1using System;
2using System.Linq;
3using System.Collections.Generic;
4using System.Threading;
5using HeuristicLab.Common;
6using HeuristicLab.Core;
7using HeuristicLab.Data;
8using ENCODING_NAMESPACE;
9using HeuristicLab.Optimization;
10using HeuristicLab.Problems.Programmable;
11
12namespace HeuristicLab.Problems.Programmable {
13  public class CompiledSingleObjectiveProblemDefinition : CompiledSingleObjectiveProblemDefinition<ENCODING_CLASS, SOLUTION_CLASS> {
14    public override bool Maximization { get { return false; } }
15
16    public override void Initialize() {
17      // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
18      // Define e.g. the length of the solution encoding or the solution creator by modifying the Encoding property
19      // Encoding.Length = 100;
20      // Add additional initialization code e.g. private variables that you need for evaluating
21    }
22
23    //TODO add other methods
24
25    public override ISingleObjectiveEvaluationResult Evaluate(SOLUTION_CLASS solution, IRandom random, CancellationToken cancellationToken) {
26      // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
27      var quality = 0.0;
28      var evaluationResult = new SingleObjectiveEvaluationResult(quality);
29      return evaluationResult;
30    }
31
32    public override void Analyze(ISingleObjectiveSolutionContext<SOLUTION_CLASS>[] solutionContexts, ResultCollection results, IRandom random) {
33      // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
34      // Write or update results given the range of vectors and resulting qualities
35      // Uncomment the following lines if you want to retrieve the best solution
36
37      //var orderedSolutions = solutionContexts.OrderBy(s => s.EvaluationResult.Quality);
38      //var best = Maximization ? orderedSolutions.Last() : orderedSolutions.First();
39      //results.AddOrUpdateResult("Best Solution",(IItem) best.EncodedSolution.Clone());
40    }
41
42
43
44    public override IEnumerable<SOLUTION_CLASS> GetNeighbors(SOLUTION_CLASS solution, IRandom random) {
45      // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
46      // Create new vectors, based on the given one that represent small changes
47      // This method is only called from move-based algorithms (Local Search, Simulated Annealing, etc.)
48      while (true) {
49        // Algorithm will draw only a finite amount of samples
50        // Change to a for-loop to return a concrete amount of neighbors
51        var neighbor = (SOLUTION_CLASS)solution.Clone();
52        // modify the solution specified as neighbor
53        yield return neighbor;
54      }
55    }
56
57    // Implement further classes and methods
58  }
59}
60
Note: See TracBrowser for help on using the repository browser.