Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5849 was 4187, checked in by abeham, 14 years ago

#567

  • Worked on Algorithm Template
File size: 4.6 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    $parameterProperties$
49    #endregion
50
51    #region Properties
52    $properties$
53    private RandomCreator RandomCreator {
54      get { return (RandomCreator)OperatorGraph.InitialOperator; }
55    }
56    #endregion
57
58    [StorableConstructor]
59    private $safeitemname$(bool deserializing) : base(deserializing) { }
60    public $safeitemname$()
61      : base() {
62      $parameterInitializers$
63     
64      RandomCreator randomCreator = new RandomCreator();
65      OperatorGraph.InitialOperator = randomCreator;
66
67      randomCreator.RandomParameter.ActualName = "Random";
68      randomCreator.SeedParameter.ActualName = SeedParameter.Name;
69      randomCreator.SeedParameter.Value = null;
70      randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
71      randomCreator.SetSeedRandomlyParameter.Value = null;
72      randomCreator.Successor = null; // TODO:
73
74      // TODO: Create further operators and build operator graph
75     
76      UpdateAnalyzers();
77      AttachEventHandlers();
78    }
79
80    public override IDeepCloneable Clone(Cloner cloner) {
81      $safeitemname$ clone = ($safeitemname$)base.Clone(cloner);
82      // TODO: IMPORTANT! Clone necessary private fields here
83      clone.AttachEventHandlers();
84      return clone;
85    }
86
87    public override void Prepare() {
88      if (Problem != null) base.Prepare();
89    }
90
91    #region Events
92    protected override void OnProblemChanged() {
93      // TODO: Initialize and parameterize operators
94      UpdateAnalyzers();
95      base.OnProblemChanged();
96    }
97
98    protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
99      // TODO: Parameterize operators
100      base.Problem_SolutionCreatorChanged(sender, e);
101    }
102    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
103      // TODO: Parameterize operators
104      base.Problem_EvaluatorChanged(sender, e);
105    }
106    protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
107      // TODO: Parameterize operators
108      UpdateAnalyzers();
109      base.Problem_OperatorsChanged(sender, e);
110    }
111    #endregion
112
113    #region Helpers
114    [StorableHook(HookType.AfterDeserialization)]
115    private void AttachEventHandlers() {
116      // TODO: Attach event handlers to local parameters
117      if (Problem != null) {
118        // TODO: Attach event handlers to problem parameters
119      }
120    }
121    private void UpdateAnalyzers() {
122      Analyzer.Operators.Clear();
123      if (Problem != null) {
124        foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>()) {
125          foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
126            param.Depth = 1; // TODO: 0 when GlobalScope = Solution, 1 when GlobalScope = Population, 2 when GlobalScope = MetaPopulation (Islands)
127          Analyzer.Operators.Add(analyzer);
128        }
129      }
130      // TODO: Add your own algorithm specific analyzer here (problem analyzer should be added/executed first)
131    }
132    #endregion
133  }
134}
Note: See TracBrowser for help on using the repository browser.