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