source: branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/SingleObjectiveProblemScript.cs @ 11550

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

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

File size: 5.9 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("Single-objective Problem Definition Script", "Script that defines the parameter vector and evaluates the solution for a programmable problem.")]
31  [StorableClass]
32  public sealed class SingleObjectiveProblemScript : ProblemScript, ISingleObjectiveProblemDefinition, 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, 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      }
97    }
98
99    [StorableConstructor]
100    private SingleObjectiveProblemScript(bool deserializing) : base(deserializing) { }
101    private SingleObjectiveProblemScript(SingleObjectiveProblemScript original, Cloner cloner) : base(original, cloner) { }
102    public SingleObjectiveProblemScript() {
103      Code = CodeTemplate;
104    }
105
106    public override IDeepCloneable Clone(Cloner cloner) {
107      return new SingleObjectiveProblemScript(this, cloner);
108    }
109
110    public new ISingleObjectiveProblemDefinition Instance {
111      get { return (ISingleObjectiveProblemDefinition)base.Instance; }
112      private set { base.Instance = value; }
113    }
114
115    protected override void OnInstanceChanged() {
116      OnProblemDefinitionChanged();
117      base.OnInstanceChanged();
118    }
119
120    bool ISingleObjectiveProblemDefinition.IsMaximizationProblem {
121      get { return Instance != null && Instance.IsMaximizationProblem; }
122    }
123
124    IEncoding IProblemDefinition.Encoding {
125      get { return Instance != null ? Instance.Encoding : null; }
126    }
127
128    double ISingleObjectiveProblemDefinition.Evaluate(IRandom random, Individual individual) {
129      return Instance.Evaluate(random, individual);
130    }
131
132    void ISingleObjectiveProblemDefinition.Analyze(Individual[] individuals, double[] qualities, ResultCollection results) {
133      Instance.Analyze(individuals, qualities, results);
134    }
135
136    IEnumerable<Individual> IProblemDefinition.GetNeighbors(IRandom random, Individual individual) {
137      return Instance.GetNeighbors(random, individual);
138    }
139
140    public event EventHandler ProblemDefinitionChanged;
141    private void OnProblemDefinitionChanged() {
142      var handler = ProblemDefinitionChanged;
143      if (handler != null) handler(this, EventArgs.Empty);
144    }
145  }
146}
Note: See TracBrowser for help on using the repository browser.