Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/13/15 18:47:19 (9 years ago)
Author:
mkommend
Message:

#2174: First working version of refactored programmable problem.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/Scripts/SingleObjectiveProblemDefinitionScript.cs

    r11739 r11753  
    2020#endregion
    2121
    22 using System;
    2322using System.Collections.Generic;
    2423using HeuristicLab.Common;
     
    2625using HeuristicLab.Optimization;
    2726using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     27using HeuristicLab.Problems.Programmable.New.Scripts.Templates;
    2828
    2929namespace HeuristicLab.Problems.Programmable {
     
    3434
    3535    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    }
    4638
    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; }
    9741    }
    9842
     
    10044    private SingleObjectiveProblemDefinitionScript(bool deserializing) : base(deserializing) { }
    10145    private SingleObjectiveProblemDefinitionScript(SingleObjectiveProblemDefinitionScript original, Cloner cloner) : base(original, cloner) { }
    102     public SingleObjectiveProblemDefinitionScript() :base(){
     46    public SingleObjectiveProblemDefinitionScript()
     47      : base() {
    10348      Code = CodeTemplate;
    10449    }
     
    10853    }
    10954
    110     public new ISingleObjectiveProblemDefinition CompiledProblemDefinition {
    111       get { return (ISingleObjectiveProblemDefinition)base.CompiledProblemDefinition; }
    112     }
    113 
    11455    bool ISingleObjectiveProblemDefinition.Maximization {
    115       get { return CompiledProblemDefinition != null && CompiledProblemDefinition.Maximization; }
     56      get { return CompiledProblemDefinition.Maximization; }
    11657    }
    11758
     
    12364      CompiledProblemDefinition.Analyze(individuals, qualities, results);
    12465    }
     66    IEnumerable<Individual> ISingleObjectiveProblemDefinition.GetNeighbors(Individual individual, IRandom random) {
     67      return CompiledProblemDefinition.GetNeighbors(individual, random);
     68    }
    12569  }
    12670}
Note: See TracChangeset for help on using the changeset viewer.