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