[16565] | 1 | using System.Collections.Generic;
|
---|
[11753] | 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() {
|
---|
[11880] | 10 | // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
|
---|
[11753] | 11 | // Define the solution encoding which can also consist of multiple vectors, examples below
|
---|
[11900] | 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);
|
---|
[11753] | 15 | //Encoding = new PermutationEncoding("p", length: 5, type: PermutationTypes.Absolute);
|
---|
[12724] | 16 | //Encoding = new LinearLinkageEncoding("l", length: 5);
|
---|
| 17 | //Encoding = new SymbolicExpressionTreeEncoding("s", new SimpleSymbolicExpressionGrammar(), 50, 12);
|
---|
[11880] | 18 | // The encoding can also be a combination
|
---|
[11753] | 19 | //Encoding = new MultiEncoding()
|
---|
[11900] | 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))
|
---|
[11753] | 23 | //.Add(new PermutationEncoding("p", length: 5, type: PermutationTypes.Absolute))
|
---|
[12731] | 24 | //.Add(new LinearLinkageEncoding("l", length: 5))
|
---|
| 25 | //.Add(new SymbolicExpressionTreeEncoding("s", new SimpleSymbolicExpressionGrammar(), 50, 12))
|
---|
[11880] | 26 | ;
|
---|
| 27 | // Add additional initialization code e.g. private variables that you need for evaluating
|
---|
[11753] | 28 | }
|
---|
| 29 |
|
---|
| 30 | public double Evaluate(Individual individual, IRandom random) {
|
---|
[11880] | 31 | // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
|
---|
[11753] | 32 | var quality = 0.0;
|
---|
| 33 | //quality = individual.RealVector("r").Sum(x => x * x);
|
---|
| 34 | return quality;
|
---|
| 35 | }
|
---|
| 36 |
|
---|
[11880] | 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
|
---|
[12001] | 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();
|
---|
[11753] | 49 | }
|
---|
| 50 |
|
---|
| 51 | public IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random) {
|
---|
[11880] | 52 | // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
|
---|
[11753] | 53 | // Create new vectors, based on the given one that represent small changes
|
---|
[11880] | 54 | // This method is only called from move-based algorithms (Local Search, Simulated Annealing, etc.)
|
---|
[11753] | 55 | while (true) {
|
---|
[11880] | 56 | // Algorithm will draw only a finite amount of samples
|
---|
| 57 | // Change to a for-loop to return a concrete amount of neighbors
|
---|
[11753] | 58 | var neighbor = individual.Copy();
|
---|
[11880] | 59 | // For instance, perform a single bit-flip in a binary parameter
|
---|
[11753] | 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 |
|
---|
[11880] | 66 | // Implement further classes and methods
|
---|
[11753] | 67 | }
|
---|
| 68 | }
|
---|
| 69 |
|
---|