Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/MultiObjectiveProblemScript.cs @ 11571

Last change on this file since 11571 was 11550, checked in by mkommend, 10 years ago

#2174: Updated IEncoding interface, adapted problems and refactored operator discovery in realencoding

File size: 6.0 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 System;
23using System.Collections.Generic;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Optimization;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Problems.Programmable {
30  [Item("Multi-objective Problem Definition Script", "Script that defines the parameter vector and evaluates the solution for a programmable problem.")]
31  [StorableClass]
32  public class MultiObjectiveProblemScript : ProblemScript, IMultiObjectiveProblemDefinition, IStorableContent {
33    public string Filename { get; set; }
34
35    protected override string CodeTemplate {
36      get {
37        return @"using System;
38using System.Linq;
39using System.Collections.Generic;
40using HeuristicLab.Common;
41using HeuristicLab.Core;
42using HeuristicLab.Data;
43using HeuristicLab.Encodings.PermutationEncoding;
44using HeuristicLab.Optimization;
45using HeuristicLab.Problems.Programmable;
46
47public class CustomProblemDefinition : ProblemScriptBase, IMultiObjectiveProblemDefinition {
48  public bool[] Maximization { get { return new [] { false, 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 qualities = new [] { 0.0, 0.0 };
70    // use vars.yourVariable to access variables in the variable store i.e. yourVariable
71    // qualities = new [] { individual.RealVector(""r"").Sum(x => x * x), individual.RealVector(""r"").Sum(x => x * x * x) };
72    return qualities;
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      }
97    }
98
99    [StorableConstructor]
100    protected MultiObjectiveProblemScript(bool deserializing) : base(deserializing) { }
101    protected MultiObjectiveProblemScript(MultiObjectiveProblemScript original, Cloner cloner)
102      : base(original, cloner) { }
103
104    public MultiObjectiveProblemScript() {
105      Code = CodeTemplate;
106    }
107
108    public override IDeepCloneable Clone(Cloner cloner) {
109      return new MultiObjectiveProblemScript(this, cloner);
110    }
111
112    public new IMultiObjectiveProblemDefinition Instance {
113      get { return (IMultiObjectiveProblemDefinition)base.Instance; }
114      protected set { base.Instance = value; }
115    }
116
117    protected override void OnInstanceChanged() {
118      OnProblemDefinitionChanged();
119      base.OnInstanceChanged();
120    }
121
122    bool[] IMultiObjectiveProblemDefinition.Maximization {
123      get { return Instance != null ? Instance.Maximization : new bool[0]; }
124    }
125
126    IEncoding IProblemDefinition.Encoding {
127      get { return Instance != null ? Instance.Encoding : null; }
128    }
129
130    double[] IMultiObjectiveProblemDefinition.Evaluate(IRandom random, Individual individual) {
131      return Instance.Evaluate(random, individual);
132    }
133
134    void IMultiObjectiveProblemDefinition.Analyze(Individual[] individuals, double[][] qualities, ResultCollection results) {
135      Instance.Analyze(individuals, qualities, results);
136    }
137
138    IEnumerable<Individual> IProblemDefinition.GetNeighbors(IRandom random, Individual individual) {
139      return Instance.GetNeighbors(random, individual);
140    }
141
142    public event EventHandler ProblemDefinitionChanged;
143    private void OnProblemDefinitionChanged() {
144      var handler = ProblemDefinitionChanged;
145      if (handler != null) handler(this, EventArgs.Empty);
146    }
147  }
148}
Note: See TracBrowser for help on using the repository browser.