Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/tools/Templates/HeuristicLabAlgorithmTemplate/DefaultAlgorithm.cs @ 4184

Last change on this file since 4184 was 4176, checked in by abeham, 14 years ago

#567

  • Updated installer
  • Updated View template
  • Added Algorithm template (untested)
File size: 5.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-$year$ 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.Linq;
24using HeuristicLab.Analysis;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Optimization;
29using HeuristicLab.Optimization.Operators;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.PluginInfrastructure;
33using HeuristicLab.Random;
34
35namespace $rootnamespace$ {
36  /// <summary>
37  /// A genetic algorithm.
38  /// </summary>
39  [Item("$algorithmName$", "$algorithmDescription$")]
40  [Creatable("Algorithms")]
41  [StorableClass]
42  public sealed class $safeitemname$ : EngineAlgorithm {
43    #region Problem Properties
44    $problemType$
45    #endregion
46
47    #region Parameter Properties
48    private ValueParameter<IntValue> SeedParameter {
49      get { return (ValueParameter<IntValue>)Parameters["Seed"]; }
50    }
51    private ValueParameter<BoolValue> SetSeedRandomlyParameter {
52      get { return (ValueParameter<BoolValue>)Parameters["SetSeedRandomly"]; }
53    }
54    private ValueParameter<MultiAnalyzer> AnalyzerParameter {
55      get { return (ValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
56    }
57    #endregion
58
59    #region Properties
60    public IntValue Seed {
61      get { return SeedParameter.Value; }
62      set { SeedParameter.Value = value; }
63    }
64    public BoolValue SetSeedRandomly {
65      get { return SetSeedRandomlyParameter.Value; }
66      set { SetSeedRandomlyParameter.Value = value; }
67    }
68    public MultiAnalyzer Analyzer {
69      get { return AnalyzerParameter.Value; }
70      set { AnalyzerParameter.Value = value; }
71    }
72    #endregion
73
74    [StorableConstructor]
75    private $safeitemname$(bool deserializing) : base(deserializing) { }
76    public $safeitemname$()
77      : base() {
78      Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
79      Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
80      Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze each iteration.", new MultiAnalyzer()));
81     
82      // TODO: Create and assign OperatorGraph.InitialOperator
83
84      // TODO: Build operator graph
85     
86      AttachEventHandlers();
87    }
88
89    public override IDeepCloneable Clone(Cloner cloner) {
90      $safeitemname$ clone = ($safeitemname$)base.Clone(cloner);
91      // TODO: IMPORTANT! Clone necessary private fields here
92      clone.AttachEventHandlers();
93      return clone;
94    }
95
96    public override void Prepare() {
97      if (Problem != null) base.Prepare();
98    }
99
100    #region Events
101    protected override void OnProblemChanged() {
102      // TODO: Initialize and parameterize operators
103      base.OnProblemChanged();
104    }
105
106    protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
107      // TODO: Parameterize operators
108      base.Problem_SolutionCreatorChanged(sender, e);
109    }
110    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
111      // TODO: Parameterize operators
112      base.Problem_EvaluatorChanged(sender, e);
113    }
114    protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
115      // TODO: Parameterize operators
116      base.Problem_OperatorsChanged(sender, e);
117    }
118    #endregion
119
120    #region Helpers
121    [StorableHook(HookType.AfterDeserialization)]
122    private void AttachEventHandlers() {
123      // TODO: Attach event handlers to local parameters
124      if (Problem != null) {
125        // TODO: Attach event handlers to problem parameters
126      }
127    }
128    private void UpdateAnalyzers() {
129      Analyzer.Operators.Clear();
130      if (Problem != null) {
131        foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>()) {
132          foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
133            param.Depth = 1; // TODO: 0 when GlobalScope = Solution, 1 when GlobalScope = Population, 2 when GlobalScope = MetaPopulation (Islands)
134          Analyzer.Operators.Add(analyzer);
135        }
136      }
137      // TODO: Add your own algorithm specific analyzer here (problem analyzer should be added/executed first)
138    }
139    #endregion
140  }
141}
Note: See TracBrowser for help on using the repository browser.