source: branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/Scripts/MultiObjectiveProblemDefinitionScript.cs @ 11739

Last change on this file since 11739 was 11739, checked in by mkommend, 8 years ago

#2174: Worked on operators and programmable problem base classes and scripts.

File size: 5.5 KB
Line 
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
22using HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Optimization;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26
27namespace 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]
30  public class MultiObjectiveProblemDefinitionScript : ProblemDefinitionScript, IMultiObjectiveProblemDefinition, IStorableContent {
31    public string Filename { get; set; }
32
33    protected override string CodeTemplate {
34      get {
35        return @"using System;
36using System.Linq;
37using System.Collections.Generic;
38using HeuristicLab.Common;
39using HeuristicLab.Core;
40using HeuristicLab.Data;
41using HeuristicLab.Encodings.PermutationEncoding;
42using HeuristicLab.Optimization;
43using HeuristicLab.Problems.Programmable;
44
45public 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      }
95    }
96
97    [StorableConstructor]
98    protected MultiObjectiveProblemDefinitionScript(bool deserializing) : base(deserializing) { }
99    protected MultiObjectiveProblemDefinitionScript(MultiObjectiveProblemDefinitionScript original, Cloner cloner)
100      : base(original, cloner) { }
101
102    public MultiObjectiveProblemDefinitionScript() {
103      Code = CodeTemplate;
104    }
105
106    public override IDeepCloneable Clone(Cloner cloner) {
107      return new MultiObjectiveProblemDefinitionScript(this, cloner);
108    }
109
110    public new IMultiObjectiveProblemDefinition CompiledProblemDefinition {
111      get { return (IMultiObjectiveProblemDefinition)base.CompiledProblemDefinition; }
112    }
113
114    bool[] IMultiObjectiveProblemDefinition.Maximization {
115      get { return CompiledProblemDefinition != null ? CompiledProblemDefinition.Maximization : new bool[0]; }
116    }
117
118    double[] IMultiObjectiveProblemDefinition.Evaluate(Individual individual, IRandom random) {
119      return CompiledProblemDefinition.Evaluate(individual, random);
120    }
121
122    void IMultiObjectiveProblemDefinition.Analyze(Individual[] individuals, double[][] qualities, ResultCollection results) {
123      CompiledProblemDefinition.Analyze(individuals, qualities, results);
124    }
125  }
126}
Note: See TracBrowser for help on using the repository browser.