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.Problems.Programmable;
|
---|
9 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
10 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
11 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
12 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
13 | using HeuristicLab.Encodings.LinearLinkageEncoding;
|
---|
14 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
15 |
|
---|
16 | namespace HeuristicLab.Problems.Programmable {
|
---|
17 | public class CompiledMultiObjectiveProblemDefinition : CompiledMultiObjectiveProblemDefinition<CombinedEncoding, CombinedSolution> {
|
---|
18 | public override bool[] Maximization { get { return new[] { true, false }; } }
|
---|
19 | public override double[] ReferencePoint { get { return null; } }
|
---|
20 | public override IReadOnlyList<double[]> BestKnownFront { get { return null; } }
|
---|
21 |
|
---|
22 | public override void Initialize() {
|
---|
23 | // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
|
---|
24 | // Define e.g. the length of the solution encoding or the solution creator by modifying the Encoding property
|
---|
25 | // Add additional initialization code e.g. private variables that you need for evaluating
|
---|
26 | Encoding.Add(new BinaryVectorEncoding("b") { Length = 10 });
|
---|
27 | //Encoding.Add(new IntegerVectorEncoding("i") { Length = 10, Bounds = new IntMatrix(new int[,] { { -100, 100 } }) });
|
---|
28 | Encoding.Add(new RealVectorEncoding("r") { Length = 10, Bounds = new DoubleMatrix(new double[,] { { -100, 100 } }) });
|
---|
29 | //Encoding.Add(new PermutationEncoding("p") { Length = 20, Type = PermutationTypes.Absolute });
|
---|
30 | //Encoding.Add(new LinearLinkageEncoding("lle") { Length = 30 });
|
---|
31 | }
|
---|
32 |
|
---|
33 | public override double[] Evaluate(CombinedSolution solution, IRandom random) {
|
---|
34 | // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
|
---|
35 | var quality = new[] { 0.0, 0.0 };
|
---|
36 | var b = solution.GetEncodedSolution<BinaryVector>("b");
|
---|
37 | quality[0] = b.Count(x => x); // one max!
|
---|
38 | var r = solution.GetEncodedSolution<RealVector>("r");
|
---|
39 | quality[1] = r.Select((i, v) => new { Idx = i, Val = v }).Sum(x => b[x.Idx] ? x.Val * x.Val : 0.0); // sphere
|
---|
40 |
|
---|
41 | // NOTE: Check the Maximization property above (true or false)!
|
---|
42 | return quality;
|
---|
43 | }
|
---|
44 |
|
---|
45 | public override void Analyze(CombinedSolution[] solutions, double[][] qualities, ResultCollection results, IRandom random) {
|
---|
46 | // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
|
---|
47 | // Write or update results given the range of vectors and resulting qualities
|
---|
48 | // Uncomment the following lines if you want to retrieve the best solution
|
---|
49 | }
|
---|
50 |
|
---|
51 | public override IEnumerable<CombinedSolution> GetNeighbors(CombinedSolution solution, 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 = (CombinedSolution)solution.Clone();
|
---|
59 | // modify the solution specified as neighbor
|
---|
60 | yield return neighbor;
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | // Implement further classes and methods
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|