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

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

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

File size: 5.4 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 SingleObjectiveProblemDefinitionScript : ProblemDefinitionScript, 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 : 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      }
97    }
98
99    [StorableConstructor]
100    private SingleObjectiveProblemDefinitionScript(bool deserializing) : base(deserializing) { }
101    private SingleObjectiveProblemDefinitionScript(SingleObjectiveProblemDefinitionScript original, Cloner cloner) : base(original, cloner) { }
102    public SingleObjectiveProblemDefinitionScript() :base(){
103      Code = CodeTemplate;
104    }
105
106    public override IDeepCloneable Clone(Cloner cloner) {
107      return new SingleObjectiveProblemDefinitionScript(this, cloner);
108    }
109
110    public new ISingleObjectiveProblemDefinition CompiledProblemDefinition {
111      get { return (ISingleObjectiveProblemDefinition)base.CompiledProblemDefinition; }
112    }
113
114    bool ISingleObjectiveProblemDefinition.Maximization {
115      get { return CompiledProblemDefinition != null && CompiledProblemDefinition.Maximization; }
116    }
117
118    double ISingleObjectiveProblemDefinition.Evaluate(Individual individual, IRandom random) {
119      return CompiledProblemDefinition.Evaluate(individual, random);
120    }
121
122    void ISingleObjectiveProblemDefinition.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.