Changeset 11753 for branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/Scripts/MultiObjectiveProblemDefinitionScript.cs
- Timestamp:
- 01/13/15 18:47:19 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/Scripts/MultiObjectiveProblemDefinitionScript.cs
r11739 r11753 24 24 using HeuristicLab.Optimization; 25 25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 26 using HeuristicLab.Problems.Programmable.New.Scripts.Templates; 26 27 27 28 namespace HeuristicLab.Problems.Programmable { … … 32 33 33 34 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; 35 get { return ScriptTemplates.CompiledMultiObjectiveProblemDefinition; } 36 } 44 37 45 public class CustomProblemDefinition : CompiledProblemDefinition, IMultiObjectiveProblemDefinition { 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 66 public double[] Evaluate(IRandom random, Individual individual) { 67 var qualities = new [] { 0.0, 0.0 }; 68 // use vars.yourVariable to access variables in the variable store i.e. yourVariable 69 // qualities = new [] { individual.RealVector(""r"").Sum(x => x * x), individual.RealVector(""r"").Sum(x => x * x * x) }; 70 return qualities; 71 } 72 73 public void Analyze(Individual[] individuals, double[][] qualities, ResultCollection results) { 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 78 public override IEnumerable<Individual> GetNeighbors(IRandom random, Individual individual) { 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 84 var neighbor = (Individual)individual.Clone(); 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 } 38 private new IMultiObjectiveProblemDefinition CompiledProblemDefinition { 39 get { return (IMultiObjectiveProblemDefinition)base.CompiledProblemDefinition; } 95 40 } 96 41 … … 108 53 } 109 54 110 public new IMultiObjectiveProblemDefinition CompiledProblemDefinition {111 get { return (IMultiObjectiveProblemDefinition)base.CompiledProblemDefinition; }112 }113 114 55 bool[] IMultiObjectiveProblemDefinition.Maximization { 115 get { return CompiledProblemDefinition != null ? CompiledProblemDefinition.Maximization : new bool[0]; }56 get { return CompiledProblemDefinition.Maximization; } 116 57 } 117 58
Note: See TracChangeset
for help on using the changeset viewer.