[11484] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using HeuristicLab.Common;
|
---|
| 23 | using HeuristicLab.Core;
|
---|
| 24 | using HeuristicLab.Optimization;
|
---|
| 25 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 26 |
|
---|
| 27 | namespace HeuristicLab.Problems.Programmable {
|
---|
| 28 | [Item("Multi-objective Problem Definition Script", "Script that defines the parameter vector and evaluates the solution for a programmable problem.")]
|
---|
| 29 | [StorableClass]
|
---|
[11739] | 30 | public class MultiObjectiveProblemDefinitionScript : ProblemDefinitionScript, IMultiObjectiveProblemDefinition, IStorableContent {
|
---|
[11484] | 31 | public string Filename { get; set; }
|
---|
| 32 |
|
---|
| 33 | protected override string CodeTemplate {
|
---|
| 34 | get {
|
---|
| 35 | return @"using System;
|
---|
| 36 | using System.Linq;
|
---|
| 37 | using System.Collections.Generic;
|
---|
| 38 | using HeuristicLab.Common;
|
---|
| 39 | using HeuristicLab.Core;
|
---|
| 40 | using HeuristicLab.Data;
|
---|
| 41 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
| 42 | using HeuristicLab.Optimization;
|
---|
| 43 | using HeuristicLab.Problems.Programmable;
|
---|
| 44 |
|
---|
[11739] | 45 | public class CustomProblemDefinition : CompiledProblemDefinition, IMultiObjectiveProblemDefinition {
|
---|
[11484] | 46 | public bool[] Maximization { get { return new [] { false, false }; } }
|
---|
| 47 |
|
---|
| 48 | public CustomProblemDefinition() {
|
---|
| 49 | // Define the solution encoding which can also consist of multiple vectors, examples below
|
---|
| 50 | // Encoding = new BinaryEncoding(""b"", length: 5);
|
---|
| 51 | // Encoding = new IntegerEncoding(""i"", lenght: 5, min: 2, max: 14, step: 4);
|
---|
| 52 | // Encoding = new RealEncoding(""r"", length: 5, min: -1.0, max: 1.0);
|
---|
| 53 | // Encoding = new PermutationEncoding(""P"", length: 5, type: PermutationTypes.Absolute);
|
---|
| 54 | // Encoding = new MultiEncoding()
|
---|
| 55 | // .AddBinaryVector(""b"", length: 5)
|
---|
| 56 | // .AddIntegerVector(""i"", length: 5, min: 2, max: 14, step: 4)
|
---|
| 57 | // .AddRealVector(""r"", length: 5, min: -1.0, max: 1.0)
|
---|
| 58 | // .AddPermutation(""P"", length: 5, type: PermutationTypes.Absolute)
|
---|
| 59 | ;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | public override void Initialize() {
|
---|
| 63 | // when the definition is created here you can initialize variables in the variable store
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[11485] | 66 | public double[] Evaluate(IRandom random, Individual individual) {
|
---|
[11484] | 67 | var qualities = new [] { 0.0, 0.0 };
|
---|
| 68 | // use vars.yourVariable to access variables in the variable store i.e. yourVariable
|
---|
[11485] | 69 | // qualities = new [] { individual.RealVector(""r"").Sum(x => x * x), individual.RealVector(""r"").Sum(x => x * x * x) };
|
---|
[11484] | 70 | return qualities;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[11485] | 73 | public void Analyze(Individual[] individuals, double[][] qualities, ResultCollection results) {
|
---|
[11484] | 74 | // write or update results given the range of vectors and resulting qualities
|
---|
| 75 | // use e.g. vars.yourVariable to access variables in the variable store i.e. yourVariable
|
---|
| 76 | }
|
---|
| 77 |
|
---|
[11485] | 78 | public override IEnumerable<Individual> GetNeighbors(IRandom random, Individual individual) {
|
---|
[11484] | 79 | // Create new vectors, based on the given one that represent small changes
|
---|
| 80 | // This method is only called from move-based algorithms (LocalSearch, SimulatedAnnealing, etc.)
|
---|
| 81 | while (true) {
|
---|
| 82 | // this is not an infinite loop as only a finite amount of samples will be drawn
|
---|
| 83 | // it is possible to return a concrete amount of neighbors also
|
---|
[11485] | 84 | var neighbor = (Individual)individual.Clone();
|
---|
[11484] | 85 | //e.g. make a bit flip in a binary parameter
|
---|
| 86 | //var bIndex = random.Next(neighbor.BinaryVector(""b"").Length);
|
---|
| 87 | //neighbor.BinaryVector(""b"")[bIndex] = !neighbor.BinaryVector(""b"")[bIndex];
|
---|
| 88 | yield return neighbor;
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | // implement further classes and methods
|
---|
| 93 | }";
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | [StorableConstructor]
|
---|
[11739] | 98 | protected MultiObjectiveProblemDefinitionScript(bool deserializing) : base(deserializing) { }
|
---|
| 99 | protected MultiObjectiveProblemDefinitionScript(MultiObjectiveProblemDefinitionScript original, Cloner cloner)
|
---|
[11484] | 100 | : base(original, cloner) { }
|
---|
| 101 |
|
---|
[11739] | 102 | public MultiObjectiveProblemDefinitionScript() {
|
---|
[11484] | 103 | Code = CodeTemplate;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[11739] | 107 | return new MultiObjectiveProblemDefinitionScript(this, cloner);
|
---|
[11484] | 108 | }
|
---|
| 109 |
|
---|
[11739] | 110 | public new IMultiObjectiveProblemDefinition CompiledProblemDefinition {
|
---|
| 111 | get { return (IMultiObjectiveProblemDefinition)base.CompiledProblemDefinition; }
|
---|
[11484] | 112 | }
|
---|
| 113 |
|
---|
| 114 | bool[] IMultiObjectiveProblemDefinition.Maximization {
|
---|
[11739] | 115 | get { return CompiledProblemDefinition != null ? CompiledProblemDefinition.Maximization : new bool[0]; }
|
---|
[11484] | 116 | }
|
---|
| 117 |
|
---|
[11739] | 118 | double[] IMultiObjectiveProblemDefinition.Evaluate(Individual individual, IRandom random) {
|
---|
| 119 | return CompiledProblemDefinition.Evaluate(individual, random);
|
---|
[11484] | 120 | }
|
---|
| 121 |
|
---|
| 122 | void IMultiObjectiveProblemDefinition.Analyze(Individual[] individuals, double[][] qualities, ResultCollection results) {
|
---|
[11739] | 123 | CompiledProblemDefinition.Analyze(individuals, qualities, results);
|
---|
[11484] | 124 | }
|
---|
| 125 | }
|
---|
| 126 | }
|
---|