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/MultiObjectiveProblemDefinitionScript.cs

    r11739 r11753  
    2424using HeuristicLab.Optimization;
    2525using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     26using HeuristicLab.Problems.Programmable.New.Scripts.Templates;
    2627
    2728namespace HeuristicLab.Problems.Programmable {
     
    3233
    3334    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    }
    4437
    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; }
    9540    }
    9641
     
    10853    }
    10954
    110     public new IMultiObjectiveProblemDefinition CompiledProblemDefinition {
    111       get { return (IMultiObjectiveProblemDefinition)base.CompiledProblemDefinition; }
    112     }
    113 
    11455    bool[] IMultiObjectiveProblemDefinition.Maximization {
    115       get { return CompiledProblemDefinition != null ? CompiledProblemDefinition.Maximization : new bool[0]; }
     56      get { return CompiledProblemDefinition.Maximization; }
    11657    }
    11758
Note: See TracChangeset for help on using the changeset viewer.