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