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.Optimization;
|
---|
8 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
9 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
10 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
11 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
12 | using HeuristicLab.Encodings.LinearLinkageEncoding;
|
---|
13 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
14 |
|
---|
15 | namespace HeuristicLab.Problems.Programmable {
|
---|
16 | public class CompiledSingleObjectiveProblemDefinition : CompiledSingleObjectiveProblemDefinition<CombinedEncoding, CombinedSolution> {
|
---|
17 | public override bool Maximization { get { return true; } }
|
---|
18 |
|
---|
19 | public override void Initialize() {
|
---|
20 | // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
|
---|
21 | // Define e.g. the length of the solution encoding or the solution creator by modifying the Encoding property
|
---|
22 | // Add additional initialization code e.g. private variables that you need for evaluating
|
---|
23 | Encoding.Add(new BinaryVectorEncoding("b") { Length = 10 });
|
---|
24 | //Encoding.Add(new IntegerVectorEncoding("i") { Length = 10, Bounds = new IntMatrix(new int[,] { { -100, 100 } }) });
|
---|
25 | Encoding.Add(new RealVectorEncoding("r") { Length = 10, Bounds = new DoubleMatrix(new double[,] { { -100, 100 } }) });
|
---|
26 | //Encoding.Add(new PermutationEncoding("p") { Length = 20, Type = PermutationTypes.Absolute });
|
---|
27 | //Encoding.Add(new LinearLinkageEncoding("lle") { Length = 30 });
|
---|
28 | }
|
---|
29 |
|
---|
30 | public override double Evaluate(CombinedSolution solution, IRandom random) {
|
---|
31 | // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
|
---|
32 | var quality = 0.0;
|
---|
33 | var b = solution.GetEncodedSolution<BinaryVector>("b");
|
---|
34 | quality = b.Count(x => x); // one max!
|
---|
35 | var r = solution.GetEncodedSolution<RealVector>("r");
|
---|
36 | quality += r.Sum(x => -x * x); // sphere
|
---|
37 |
|
---|
38 | // NOTE: Check the Maximization property above (true or false)!
|
---|
39 | return quality;
|
---|
40 | }
|
---|
41 |
|
---|
42 | public override void Analyze(CombinedSolution[] solutions, 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 solution
|
---|
46 |
|
---|
47 | //var orderedSolutions = solutions.Zip(qualities, (i, q) => new { Solution = i, Quality = q }).OrderBy(z => z.Quality);
|
---|
48 | //var best = Maximization ? orderedSolutions.Last().Solution : orderedSolutions.First().Solution;
|
---|
49 |
|
---|
50 | //if (!results.ContainsKey("Best Solution")) {
|
---|
51 | // results.Add(new Result("Best Solution", typeof(CombinedSolution)));
|
---|
52 | //}
|
---|
53 | //results["Best Solution"].Value = (IItem)best.Clone();
|
---|
54 | }
|
---|
55 |
|
---|
56 | public override IEnumerable<CombinedSolution> GetNeighbors(CombinedSolution solution, IRandom random) {
|
---|
57 | // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
|
---|
58 | // Create new vectors, based on the given one that represent small changes
|
---|
59 | // This method is only called from move-based algorithms (Local Search, Simulated Annealing, etc.)
|
---|
60 | while (true) {
|
---|
61 | // Algorithm will draw only a finite amount of samples
|
---|
62 | // Change to a for-loop to return a concrete amount of neighbors
|
---|
63 | var neighbor = (CombinedSolution)solution.Clone();
|
---|
64 | // modify the solution specified as neighbor
|
---|
65 | yield return neighbor;
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | // Implement further classes and methods
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|