Changeset 11753 for branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/Scripts/SingleObjectiveProblemDefinitionScript.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/SingleObjectiveProblemDefinitionScript.cs
r11739 r11753 20 20 #endregion 21 21 22 using System;23 22 using System.Collections.Generic; 24 23 using HeuristicLab.Common; … … 26 25 using HeuristicLab.Optimization; 27 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 using HeuristicLab.Problems.Programmable.New.Scripts.Templates; 28 28 29 29 namespace HeuristicLab.Problems.Programmable { … … 34 34 35 35 protected override string CodeTemplate { 36 get { 37 return @"using System; 38 using System.Linq; 39 using System.Collections.Generic; 40 using HeuristicLab.Common; 41 using HeuristicLab.Core; 42 using HeuristicLab.Data; 43 using HeuristicLab.Encodings.PermutationEncoding; 44 using HeuristicLab.Optimization; 45 using HeuristicLab.Problems.Programmable; 36 get { return ScriptTemplates.CompiledSingleObjectiveProblemDefinition; } 37 } 46 38 47 public class CustomProblemDefinition : CompiledProblemDefinition, ISingleObjectiveProblemDefinition { 48 public bool IsMaximizationProblem { get { return false; } } 49 50 public CustomProblemDefinition() { 51 // Define the solution encoding which can also consist of multiple vectors, examples below 52 // Encoding = new BinaryEncoding(""b"", length: 5); 53 // Encoding = new IntegerEncoding(""i"", lenght: 5, min: 2, max: 14, step: 4); 54 // Encoding = new RealEncoding(""r"", length: 5, min: -1.0, max: 1.0); 55 // Encoding = new PermutationEncoding(""P"", length: 5, type: PermutationTypes.Absolute); 56 // Encoding = new MultiEncoding() 57 // .AddBinaryVector(""b"", length: 5) 58 // .AddIntegerVector(""i"", length: 5, min: 2, max: 14, step: 4) 59 // .AddRealVector(""r"", length: 5, min: -1.0, max: 1.0) 60 // .AddPermutation(""P"", length: 5, type: PermutationTypes.Absolute) 61 ; 62 } 63 64 public override void Initialize() { 65 // when the definition is created here you can initialize variables in the variable store 66 } 67 68 public double Evaluate(IRandom random, Individual individual) { 69 var quality = 0.0; 70 // use vars.yourVariable to access variables in the variable store i.e. yourVariable 71 // quality = individual.RealVector(""r"").Sum(x => x * x); 72 return quality; 73 } 74 75 public void Analyze(Individual[] individuals, double[] qualities, ResultCollection results) { 76 // write or update results given the range of vectors and resulting qualities 77 // use e.g. vars.yourVariable to access variables in the variable store i.e. yourVariable 78 } 79 80 public override IEnumerable<Individual> GetNeighbors(IRandom random, Individual individual) { 81 // Create new vectors, based on the given one that represent small changes 82 // This method is only called from move-based algorithms (LocalSearch, SimulatedAnnealing, etc.) 83 while (true) { 84 // this is not an infinite loop as only a finite amount of samples will be drawn 85 // it is possible to return a concrete amount of neighbors also 86 var neighbor = (Individual)individual.Clone(); 87 //e.g. make a bit flip in a binary parameter 88 //var bIndex = random.Next(neighbor.BinaryVector(""b"").Length); 89 //neighbor.BinaryVector(""b"")[bIndex] = !neighbor.BinaryVector(""b"")[bIndex]; 90 yield return neighbor; 91 } 92 } 93 94 // implement further classes and methods 95 }"; 96 } 39 private new ISingleObjectiveProblemDefinition CompiledProblemDefinition { 40 get { return (ISingleObjectiveProblemDefinition)base.CompiledProblemDefinition; } 97 41 } 98 42 … … 100 44 private SingleObjectiveProblemDefinitionScript(bool deserializing) : base(deserializing) { } 101 45 private SingleObjectiveProblemDefinitionScript(SingleObjectiveProblemDefinitionScript original, Cloner cloner) : base(original, cloner) { } 102 public SingleObjectiveProblemDefinitionScript() :base(){ 46 public SingleObjectiveProblemDefinitionScript() 47 : base() { 103 48 Code = CodeTemplate; 104 49 } … … 108 53 } 109 54 110 public new ISingleObjectiveProblemDefinition CompiledProblemDefinition {111 get { return (ISingleObjectiveProblemDefinition)base.CompiledProblemDefinition; }112 }113 114 55 bool ISingleObjectiveProblemDefinition.Maximization { 115 get { return CompiledProblemDefinition != null && CompiledProblemDefinition.Maximization; }56 get { return CompiledProblemDefinition.Maximization; } 116 57 } 117 58 … … 123 64 CompiledProblemDefinition.Analyze(individuals, qualities, results); 124 65 } 66 IEnumerable<Individual> ISingleObjectiveProblemDefinition.GetNeighbors(Individual individual, IRandom random) { 67 return CompiledProblemDefinition.GetNeighbors(individual, random); 68 } 125 69 } 126 70 }
Note: See TracChangeset
for help on using the changeset viewer.