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.BinaryVectorEncoding;
|
---|
8 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
9 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
10 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
11 | using HeuristicLab.Optimization;
|
---|
12 | using HeuristicLab.Problems.Programmable;
|
---|
13 |
|
---|
14 | namespace 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 | //var bestIndex = Maximization ?
|
---|
47 | // qualities.Select((v, i) => Tuple.Create(i, v)).OrderByDescending(x => x.Item2).First().Item1
|
---|
48 | // : qualities.Select((v, i) => Tuple.Create(i, v)).OrderBy(x => x.Item2).First().Item1;
|
---|
49 | //var best = individuals[bestIndex];
|
---|
50 | }
|
---|
51 |
|
---|
52 | public IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random) {
|
---|
53 | // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
|
---|
54 | // Create new vectors, based on the given one that represent small changes
|
---|
55 | // This method is only called from move-based algorithms (Local Search, Simulated Annealing, etc.)
|
---|
56 | while (true) {
|
---|
57 | // Algorithm will draw only a finite amount of samples
|
---|
58 | // Change to a for-loop to return a concrete amount of neighbors
|
---|
59 | var neighbor = individual.Copy();
|
---|
60 | // For instance, perform a single bit-flip in a binary parameter
|
---|
61 | //var bIndex = random.Next(neighbor.BinaryVector("b").Length);
|
---|
62 | //neighbor.BinaryVector("b")[bIndex] = !neighbor.BinaryVector("b")[bIndex];
|
---|
63 | yield return neighbor;
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | // Implement further classes and methods
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|