Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization/3.3/Algorithms/EngineAlgorithm.cs @ 10187

Last change on this file since 10187 was 9456, checked in by swagner, 12 years ago

Updated copyright year and added some missing license headers (#1889)

File size: 8.8 KB
RevLine 
[2851]1#region License Information
2/* HeuristicLab
[9456]3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2851]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;
[2916]23using System.Linq;
[2851]24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[2916]27using HeuristicLab.PluginInfrastructure;
[2851]28
29namespace HeuristicLab.Optimization {
30  /// <summary>
31  /// A base class for algorithms which use an engine for execution.
32  /// </summary>
33  [Item("EngineAlgorithm", "A base class for algorithms which use an engine for execution.")]
[3017]34  [StorableClass]
[2851]35  public abstract class EngineAlgorithm : Algorithm {
[3280]36    [Storable]
[2851]37    private OperatorGraph operatorGraph;
[3361]38    public OperatorGraph OperatorGraph {
[2851]39      get { return operatorGraph; }
[3361]40      protected set {
[2851]41        if (value == null) throw new ArgumentNullException();
42        if (value != operatorGraph) {
[3280]43          operatorGraph.InitialOperatorChanged -= new EventHandler(OperatorGraph_InitialOperatorChanged);
[2851]44          operatorGraph = value;
[3280]45          operatorGraph.InitialOperatorChanged += new EventHandler(OperatorGraph_InitialOperatorChanged);
[2851]46          OnOperatorGraphChanged();
47          Prepare();
48        }
49      }
50    }
51
52    [Storable]
53    private IScope globalScope;
54    protected IScope GlobalScope {
55      get { return globalScope; }
56    }
57
[3280]58    [Storable]
[2851]59    private IEngine engine;
60    public IEngine Engine {
61      get { return engine; }
62      set {
63        if (engine != value) {
64          if (engine != null) DeregisterEngineEvents();
65          engine = value;
66          if (engine != null) RegisterEngineEvents();
67          OnEngineChanged();
68          Prepare();
69        }
70      }
71    }
72
[3226]73    public override ResultCollection Results {
[2882]74      get {
[3226]75        return (ResultCollection)globalScope.Variables["Results"].Value;
[2882]76      }
77    }
78
[2851]79    protected EngineAlgorithm()
80      : base() {
[2924]81      globalScope = new Scope("Global Scope");
[3226]82      globalScope.Variables.Add(new Variable("Results", new ResultCollection()));
[3280]83      operatorGraph = new OperatorGraph();
84      Initialize();
[2851]85    }
86    protected EngineAlgorithm(string name)
87      : base(name) {
[2924]88      globalScope = new Scope("Global Scope");
[3226]89      globalScope.Variables.Add(new Variable("Results", new ResultCollection()));
[3280]90      operatorGraph = new OperatorGraph();
91      Initialize();
[2851]92    }
93    protected EngineAlgorithm(string name, ParameterCollection parameters)
94      : base(name, parameters) {
[2924]95      globalScope = new Scope("Global Scope");
[3226]96      globalScope.Variables.Add(new Variable("Results", new ResultCollection()));
[3280]97      operatorGraph = new OperatorGraph();
98      Initialize();
[2851]99    }
100    protected EngineAlgorithm(string name, string description)
101      : base(name, description) {
[2924]102      globalScope = new Scope("Global Scope");
[3226]103      globalScope.Variables.Add(new Variable("Results", new ResultCollection()));
[3280]104      operatorGraph = new OperatorGraph();
105      Initialize();
[2851]106    }
107    protected EngineAlgorithm(string name, string description, ParameterCollection parameters)
108      : base(name, description, parameters) {
[2924]109      globalScope = new Scope("Global Scope");
[3226]110      globalScope.Variables.Add(new Variable("Results", new ResultCollection()));
[3280]111      operatorGraph = new OperatorGraph();
112      Initialize();
[2851]113    }
[3280]114    [StorableConstructor]
115    protected EngineAlgorithm(bool deserializing) : base(deserializing) { }
[4722]116    [StorableHook(HookType.AfterDeserialization)]
117    private void AfterDeserialization() {
118      Initialize();
[5195]119
120      // BackwardsCompatibility3.3
121      #region Backwards compatible code (remove with 3.4)
122      // clear global scope if it contains any sub-scopes or additional variables
123      if ((ExecutionState == Core.ExecutionState.Stopped) && ((globalScope.SubScopes.Count > 0) || (globalScope.Variables.Count > 1))) {
124        ResultCollection results = Results;
125        globalScope.Clear();
126        globalScope.Variables.Add(new Variable("Results", results));
127      }
128      #endregion
[4722]129    }
[2851]130
[4722]131    protected EngineAlgorithm(EngineAlgorithm original, Cloner cloner)
132      : base(original, cloner) {
133      globalScope = cloner.Clone(original.globalScope);
134      engine = cloner.Clone(original.engine);
135      operatorGraph = cloner.Clone(original.operatorGraph);
136      Initialize();
137    }
138
[3280]139    private void Initialize() {
140      operatorGraph.InitialOperatorChanged += new EventHandler(OperatorGraph_InitialOperatorChanged);
[3303]141      if (engine == null) {
[2917]142        var types = ApplicationManager.Manager.GetTypes(typeof(IEngine));
143        Type t = types.FirstOrDefault(x => x.Name.Equals("SequentialEngine"));
144        if (t == null) t = types.FirstOrDefault();
[3280]145        if (t != null) engine = (IEngine)Activator.CreateInstance(t);
[2917]146      }
[3280]147      if (engine != null) RegisterEngineEvents();
[2916]148    }
149
[3551]150    public virtual IAlgorithm CreateUserDefinedAlgorithm() {
[4722]151      return new UserDefinedAlgorithm(this, new Cloner());
[2864]152    }
153
[3275]154    public override void Prepare() {
155      base.Prepare();
[2851]156      globalScope.Clear();
[3275]157      globalScope.Variables.Add(new Variable("Results", new ResultCollection()));
[2882]158
[3770]159      if ((engine != null) && (operatorGraph.InitialOperator != null)) {
[2851]160        ExecutionContext context = null;
[3770]161        if (Problem != null) context = new ExecutionContext(context, Problem, globalScope);
162        context = new ExecutionContext(context, this, globalScope);
163        context = new ExecutionContext(context, operatorGraph.InitialOperator, globalScope);
[2851]164        engine.Prepare(context);
165      }
166    }
[3262]167    public override void Start() {
168      base.Start();
[2851]169      if (engine != null) engine.Start();
170    }
[3262]171    public override void Pause() {
172      base.Pause();
173      if (engine != null) engine.Pause();
174    }
175    public override void Stop() {
176      base.Stop();
177      if (engine != null) engine.Stop();
178    }
[2851]179
[3262]180    #region Events
[2851]181    public event EventHandler EngineChanged;
182    protected virtual void OnEngineChanged() {
[4722]183      EventHandler handler = EngineChanged;
184      if (handler != null) handler(this, EventArgs.Empty);
[2851]185    }
[3361]186    public event EventHandler OperatorGraphChanged;
187    protected virtual void OnOperatorGraphChanged() {
188      EventHandler handler = OperatorGraphChanged;
189      if (handler != null) handler(this, EventArgs.Empty);
190    }
[2851]191
192    private void RegisterEngineEvents() {
193      Engine.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Engine_ExceptionOccurred);
194      Engine.ExecutionTimeChanged += new EventHandler(Engine_ExecutionTimeChanged);
[3262]195      Engine.Paused += new EventHandler(Engine_Paused);
196      Engine.Prepared += new EventHandler(Engine_Prepared);
197      Engine.Started += new EventHandler(Engine_Started);
[3261]198      Engine.Stopped += new EventHandler(Engine_Stopped);
[2851]199    }
200    private void DeregisterEngineEvents() {
201      Engine.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Engine_ExceptionOccurred);
202      Engine.ExecutionTimeChanged -= new EventHandler(Engine_ExecutionTimeChanged);
[3262]203      Engine.Paused -= new EventHandler(Engine_Paused);
204      Engine.Prepared -= new EventHandler(Engine_Prepared);
205      Engine.Started -= new EventHandler(Engine_Started);
[3261]206      Engine.Stopped -= new EventHandler(Engine_Stopped);
[2851]207    }
208    private void Engine_ExceptionOccurred(object sender, EventArgs<Exception> e) {
209      OnExceptionOccurred(e.Value);
210    }
211    private void Engine_ExecutionTimeChanged(object sender, EventArgs e) {
[3262]212      ExecutionTime = Engine.ExecutionTime;
[2851]213    }
[3262]214    private void Engine_Paused(object sender, EventArgs e) {
215      OnPaused();
216    }
217    private void Engine_Prepared(object sender, EventArgs e) {
218      OnPrepared();
219    }
220    private void Engine_Started(object sender, EventArgs e) {
221      OnStarted();
222    }
[3261]223    private void Engine_Stopped(object sender, EventArgs e) {
[5195]224      ResultCollection results = Results;
225      globalScope.Clear();
226      globalScope.Variables.Add(new Variable("Results", results));
[3261]227      OnStopped();
[2851]228    }
[3262]229
230    private void OperatorGraph_InitialOperatorChanged(object sender, EventArgs e) {
231      Prepare();
232    }
233    #endregion
[2851]234  }
235}
Note: See TracBrowser for help on using the repository browser.